Add deposit functionality

This commit is contained in:
Laura Hausmann 2023-05-18 23:04:50 +02:00
parent 49c26b7ed4
commit 15215f02d0
Signed by: zotan
GPG key ID: D044E84C5BE01605

View file

@ -36,10 +36,13 @@ Reader* scannedCardReader;
String scannedCardId = "";
String lastStatusText = "";
int transactionAmount = 150;
String transactionAmountDecimal = "1.50";
bool deposit = false;
int transactionAmountIndex = 0;
int transactionAmountsDebit[] = {-150, -200};
int transactionAmountsCredit[] = {+100, +200, +500, +1000};
volatile bool toggleTransactionAmount = false;
volatile bool toggleTransactionAmountDebit = false;
volatile bool toggleTransactionAmountCredit = false;
volatile bool interruptFired = false;
unsigned long timer = 0;
@ -81,6 +84,9 @@ void IRAM_ATTR LinkInterruptHandler() {
else if (state == STATE_LINK_CARD_SCAN) {
PN532Reader::toggleMode = true;
}
else if (state == STATE_TRANSACT_CARDSCAN) {
toggleTransactionAmountCredit = true;
}
else {
return;
}
@ -98,7 +104,7 @@ void IRAM_ATTR BalanceInterruptHandler() {
PN532Reader::toggleMode = true;
}
else if (state == STATE_TRANSACT_CARDSCAN) {
toggleTransactionAmount = true;
toggleTransactionAmountDebit = true;
}
else {
return;
@ -111,6 +117,42 @@ void IRAM_ATTR CancelInterruptHandler() {
state = STATE_IDLE;
}
int GetTransactionAmount() {
if (deposit)
return transactionAmountsCredit[transactionAmountIndex];
else
return transactionAmountsDebit[transactionAmountIndex];
}
String GetDisplayTransactionAmount() {
String result = "";
int total = abs(GetTransactionAmount());
int cents = total % 100;
if (PN532Reader::mode == PN532_MIFARE_ISO14443A) {
if (deposit)
result += "Deposit: ";
result += total / 100;
result += ".";
result += cents;
if (cents < 10)
result += "0";
result += "";
}
else { // TOKYO MODE
if (deposit)
result += "Deposit: ";
result += "¥";
result += total;
}
return result;
}
bool cooldownCheck(long timeout) {
if (millis() - timer > timeout) {
state = STATE_IDLE;
@ -215,20 +257,37 @@ void loop() {
if (PN532Reader::mode != PN532_MIFARE_ISO14443A)
PN532Reader::mode = PN532_MIFARE_ISO14443A;
if (deposit == true)
deposit = false;
if (transactionAmountIndex != 0)
transactionAmountIndex = 0;
oled.updateOLED(state);
break;
case STATE_TRANSACT_CARDSCAN:
if (toggleTransactionAmount && millis() - timer > 250) {
if (transactionAmount == 150) {
transactionAmount = 200;
transactionAmountDecimal = "2.00";
if (toggleTransactionAmountDebit) {
if (millis() - timer > 250) {
if (deposit) {
deposit = false;
transactionAmountIndex = -1;
}
transactionAmountIndex = ++transactionAmountIndex % 2;
timer = millis();
}
else {
transactionAmount = 150;
transactionAmountDecimal = "1.50";
toggleTransactionAmountDebit = false;
}
if (toggleTransactionAmountCredit) {
if (millis() - timer > 250) {
if (!deposit) {
deposit = true;
transactionAmountIndex = -1;
}
transactionAmountIndex = ++transactionAmountIndex % 4;
timer = millis();
}
toggleTransactionAmount = false;
timer = millis();
toggleTransactionAmountCredit = false;
}
for (Reader* reader : readers) {
@ -241,12 +300,9 @@ void loop() {
}
}
if (!cooldownCheck(scanTimeout)) {
if (PN532Reader::mode == PN532_MIFARE_ISO14443A)
oled.updateOLED(state, transactionAmountDecimal + String(""), String(cooldownSecondsRemaining(scanTimeout, timer)));
else
oled.updateOLED(state, String("¥") + transactionAmount, String(cooldownSecondsRemaining(scanTimeout, timer)));
}
if (!cooldownCheck(scanTimeout))
oled.updateOLED(state, GetDisplayTransactionAmount(), String(cooldownSecondsRemaining(scanTimeout, timer)));
break;
case STATE_TRANSACT_VERIFY:
oled.updateOLED(state, lastStatusText);
@ -259,7 +315,7 @@ void loop() {
tone(PIN_BUZZER, NOTE_NONE, 150);
}
lastStatusText = cardTransaction(wifi, http, apiUrl, scannedCardId, String("-") + transactionAmount);
lastStatusText = cardTransaction(wifi, http, apiUrl, scannedCardId, String(GetTransactionAmount()));
if (lastStatusText.startsWith("S:")) {
tone(PIN_BUZZER, NOTE_C7, 650);
state = STATE_RESULT_SUCCESS;