309 lines
7.9 KiB
C++
309 lines
7.9 KiB
C++
#include <Arduino.h>
|
|
#include <U8g2lib.h>
|
|
#include <Wire.h>
|
|
#include <SPIFFS.h>
|
|
#include <WiFiSettings.h>
|
|
#include <WiFi.h>
|
|
#include <HTTPClient.h>
|
|
#include <SPI.h>
|
|
#include <ESPDateTime.h>
|
|
#include <DateTimeTZ.h>
|
|
|
|
#include "wifiFix.h"
|
|
#include "pitches.h"
|
|
#include "utils.h"
|
|
#include "oled.h"
|
|
#include "readers.h"
|
|
#include "vars.h"
|
|
|
|
OLED oled(U8G2_SH1106_128X64_NONAME_F_HW_I2C(U8G2_R0, U8X8_PIN_NONE));
|
|
WiFiClient* wifi = new WiFiClientFixed();
|
|
HTTPClient* http = new HTTPClient();
|
|
|
|
Reader* readers[] = {
|
|
//new MFRC522CardReader(PIN_HSPI_SS, PIN_HSPI_RST),
|
|
new PN532Reader(PIN_HSPI_SCLK, PIN_HSPI_MISO, PIN_HSPI_MOSI, PIN_HSPI_CS0),
|
|
//new RDM6300Reader(PIN_HWSERIAL_RX)
|
|
};
|
|
|
|
e_state state = STATE_IDLE;
|
|
e_scanstate scanstate = SCANSTATE_IDLE;
|
|
String apiUrl = "";
|
|
const int gTimeout = 5000;
|
|
const int scanTimeout = 15000;
|
|
|
|
Reader* scannedCardReader;
|
|
String scannedCardId = "";
|
|
String lastStatusText = "";
|
|
|
|
|
|
volatile bool interruptFired = false;
|
|
unsigned long timer = 0;
|
|
|
|
void IRAM_ATTR PN532_IRQ() {
|
|
PN532Reader::irq++;
|
|
}
|
|
|
|
void IRAM_ATTR TransactInterruptHandler() {
|
|
if (interruptFired || state != STATE_IDLE || digitalRead(PIN_INTERRUPT_TRANSACT)) {
|
|
return;
|
|
}
|
|
interruptFired = true;
|
|
state = STATE_TRANSACT_CARDSCAN;
|
|
}
|
|
|
|
void IRAM_ATTR LinkInterruptHandler() {
|
|
if (interruptFired || state != STATE_IDLE || digitalRead(PIN_INTERRUPT_LINK)) {
|
|
return;
|
|
}
|
|
interruptFired = true;
|
|
state = STATE_LINK_CARD_SCAN;
|
|
}
|
|
|
|
void IRAM_ATTR BalanceInterruptHandler() {
|
|
if (interruptFired || state != STATE_IDLE || digitalRead(PIN_INTERRUPT_BALANCE)) {
|
|
return;
|
|
}
|
|
interruptFired = true;
|
|
state = STATE_BALANCE_CARDSCAN;
|
|
}
|
|
|
|
void IRAM_ATTR CancelInterruptHandler() {
|
|
if (digitalRead(PIN_INTERRUPT_CANCEL))
|
|
return;
|
|
state = STATE_IDLE;
|
|
}
|
|
|
|
bool cooldownCheck(long timeout) {
|
|
if (millis() - timer > timeout) {
|
|
state = STATE_IDLE;
|
|
oled.updateOLED(state);
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void updateCardscan() {
|
|
switch (state) {
|
|
case STATE_TRANSACT_CARDSCAN:
|
|
case STATE_LINK_CARD_SCAN:
|
|
case STATE_LINK_CARD_RESCAN:
|
|
case STATE_BALANCE_CARDSCAN:
|
|
if (scanstate == SCANSTATE_ACTIVE)
|
|
return;
|
|
|
|
for (Reader* reader : readers) {
|
|
reader->begin();
|
|
}
|
|
scanstate = SCANSTATE_ACTIVE;
|
|
break;
|
|
case STATE_IDLE:
|
|
case STATE_TRANSACT_VERIFY:
|
|
case STATE_LINK_VERIFY:
|
|
case STATE_BALANCE_VERIFY:
|
|
case STATE_RESULT_SUCCESS:
|
|
case STATE_RESULT_FAILURE:
|
|
case STATE_RESULT_DISPLAY:
|
|
if (scanstate == SCANSTATE_IDLE)
|
|
return;
|
|
|
|
for (Reader* reader : readers) {
|
|
reader->end();
|
|
}
|
|
scanstate = SCANSTATE_IDLE;
|
|
break;
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
oled.begin();
|
|
Serial2.begin(115200, SERIAL_8N1, PIN_HWSERIAL_RX, PIN_HWSERIAL_TX);
|
|
SPI.begin(PIN_HSPI_SCLK, PIN_HSPI_MISO, PIN_HSPI_MOSI);
|
|
|
|
ledcSetup(0, 5000, 12);
|
|
pinMode(PIN_INTERRUPT_IRQ_PN532, INPUT_PULLUP);
|
|
pinMode(PIN_INTERRUPT_TRANSACT, INPUT_PULLUP);
|
|
pinMode(PIN_INTERRUPT_BALANCE, INPUT_PULLUP);
|
|
pinMode(PIN_INTERRUPT_CANCEL, INPUT_PULLUP);
|
|
pinMode(PIN_INTERRUPT_LINK, INPUT_PULLUP);
|
|
|
|
SPIFFS.begin(true);
|
|
WiFiSettings.hostname = "afrapay-";
|
|
apiUrl = WiFiSettings.string("AfRApay.Web API", "http://192.168.50.170:5296");
|
|
if (!digitalRead(PIN_INTERRUPT_CANCEL))
|
|
WiFiSettings.portal();
|
|
else
|
|
WiFiSettings.connect();
|
|
|
|
for (Reader* reader : readers) {
|
|
reader->init();
|
|
}
|
|
|
|
DateTime.setTimeZone(TZ_Europe_Berlin);
|
|
DateTime.begin();
|
|
oled.updateOLED(state);
|
|
|
|
attachInterrupt(PIN_INTERRUPT_IRQ_PN532, PN532_IRQ, FALLING);
|
|
attachInterrupt(PIN_INTERRUPT_TRANSACT, TransactInterruptHandler, FALLING);
|
|
attachInterrupt(PIN_INTERRUPT_BALANCE, BalanceInterruptHandler, FALLING);
|
|
attachInterrupt(PIN_INTERRUPT_CANCEL, CancelInterruptHandler, FALLING);
|
|
attachInterrupt(PIN_INTERRUPT_LINK, LinkInterruptHandler, FALLING);
|
|
}
|
|
|
|
void loop() {
|
|
if (WiFiClass::status() != WL_CONNECTED) {
|
|
oled.updateOLED(state, "WiFi disconnected :(");
|
|
state = STATE_IDLE;
|
|
WiFi.reconnect();
|
|
delay(50);
|
|
return;
|
|
}
|
|
|
|
updateCardscan();
|
|
|
|
if (interruptFired) {
|
|
timer = millis();
|
|
interruptFired = false;
|
|
}
|
|
|
|
switch (state) {
|
|
case STATE_IDLE:
|
|
oled.updateOLED(state);
|
|
break;
|
|
case STATE_TRANSACT_CARDSCAN:
|
|
for (Reader* reader : readers) {
|
|
if (reader->isNewCardPresent()) {
|
|
scannedCardId = reader->getCardUid();
|
|
lastStatusText = "Card #" + scannedCardId;
|
|
scannedCardReader = reader;
|
|
state = STATE_TRANSACT_VERIFY;
|
|
return;
|
|
}
|
|
}
|
|
if (!cooldownCheck(scanTimeout)) {
|
|
oled.updateOLED(state, String("1.50€"), String(cooldownSecondsRemaining(scanTimeout, timer)));
|
|
}
|
|
break;
|
|
case STATE_TRANSACT_VERIFY:
|
|
oled.updateOLED(state, lastStatusText);
|
|
tone(PIN_BUZZER, NOTE_A5, 25);
|
|
tone(PIN_BUZZER, NOTE_NONE, 150);
|
|
lastStatusText = cardTransaction(wifi, http, apiUrl, scannedCardId, "-150");
|
|
if (lastStatusText.startsWith("S:")) {
|
|
tone(PIN_BUZZER, NOTE_C7, 650);
|
|
state = STATE_RESULT_SUCCESS;
|
|
} else {
|
|
tone(PIN_BUZZER, NOTE_CS5, 100);
|
|
tone(PIN_BUZZER, NOTE_NONE, 25);
|
|
tone(PIN_BUZZER, NOTE_CS5, 400);
|
|
state = STATE_RESULT_FAILURE;
|
|
}
|
|
lastStatusText = lastStatusText.substring(2);
|
|
timer = millis();
|
|
return;
|
|
case STATE_LINK_CARD_SCAN:
|
|
for (Reader* reader : readers) {
|
|
if (reader->isNewCardPresent()) {
|
|
scannedCardReader = reader;
|
|
scannedCardId = reader->getCardUid();
|
|
if (reader->canHaveUnstableIdentifier()) {
|
|
reader->reset();
|
|
timer = millis();
|
|
state = STATE_LINK_CARD_RESCAN;
|
|
return;
|
|
}
|
|
else {
|
|
lastStatusText = "Card #" + scannedCardId;
|
|
state = STATE_LINK_VERIFY;
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
if (!cooldownCheck(scanTimeout)) {
|
|
oled.updateOLED(state, "Link", String(cooldownSecondsRemaining(scanTimeout, timer)));
|
|
}
|
|
break;
|
|
case STATE_LINK_CARD_RESCAN:
|
|
if (scannedCardReader->isNewCardPresent()) {
|
|
if (scannedCardId == scannedCardReader->getCardUid()) {
|
|
lastStatusText = "Card #" + scannedCardId;
|
|
state = STATE_LINK_VERIFY;
|
|
return;
|
|
}
|
|
else {
|
|
lastStatusText = "Unstable identifier";
|
|
state = STATE_RESULT_FAILURE;
|
|
return;
|
|
}
|
|
}
|
|
if (!cooldownCheck(scanTimeout)) {
|
|
oled.updateOLED(state, "Link - rescan ", String(cooldownSecondsRemaining(scanTimeout, timer)));
|
|
}
|
|
break;
|
|
case STATE_LINK_VERIFY:
|
|
oled.updateOLED(state, lastStatusText);
|
|
tone(PIN_BUZZER, NOTE_A5, 25);
|
|
tone(PIN_BUZZER, NOTE_NONE, 150);
|
|
lastStatusText = cardLink(wifi, http, apiUrl, scannedCardId);
|
|
if (lastStatusText.startsWith("S:")) {
|
|
tone(PIN_BUZZER, NOTE_C6, 100);
|
|
tone(PIN_BUZZER, NOTE_NONE, 10);
|
|
tone(PIN_BUZZER, NOTE_C6, 100);
|
|
state = STATE_RESULT_SUCCESS;
|
|
} else {
|
|
tone(PIN_BUZZER, NOTE_CS5, 100);
|
|
tone(PIN_BUZZER, NOTE_NONE, 25);
|
|
tone(PIN_BUZZER, NOTE_CS5, 400);
|
|
state = STATE_RESULT_FAILURE;
|
|
}
|
|
lastStatusText = lastStatusText.substring(2);
|
|
timer = millis();
|
|
return;
|
|
case STATE_BALANCE_CARDSCAN:
|
|
for (Reader* reader : readers) {
|
|
if (reader->isNewCardPresent()) {
|
|
scannedCardId = reader->getCardUid();
|
|
lastStatusText = "Card #" + scannedCardId;
|
|
state = STATE_BALANCE_VERIFY;
|
|
return;
|
|
}
|
|
}
|
|
if (!cooldownCheck(scanTimeout)) {
|
|
oled.updateOLED(state, "Balance", String(cooldownSecondsRemaining(scanTimeout, timer)));
|
|
}
|
|
break;
|
|
case STATE_BALANCE_VERIFY:
|
|
oled.updateOLED(state, lastStatusText);
|
|
tone(PIN_BUZZER, NOTE_A5, 25);
|
|
tone(PIN_BUZZER, NOTE_NONE, 150);
|
|
lastStatusText = cardBalance(wifi, http, apiUrl, scannedCardId);
|
|
if (lastStatusText.startsWith("S:")) {
|
|
lastStatusText = lastStatusText + ":Card #" + scannedCardId;
|
|
state = STATE_RESULT_DISPLAY;
|
|
} else {
|
|
tone(PIN_BUZZER, NOTE_CS5, 100);
|
|
tone(PIN_BUZZER, NOTE_NONE, 25);
|
|
tone(PIN_BUZZER, NOTE_CS5, 400);
|
|
state = STATE_RESULT_FAILURE;
|
|
}
|
|
lastStatusText = lastStatusText.substring(2);
|
|
timer = millis();
|
|
case STATE_RESULT_SUCCESS:
|
|
if (cooldownSecondsRemaining(gTimeout, timer) <= 4) {
|
|
lastStatusText = lastStatusText + ":Card #" + scannedCardId;
|
|
state = STATE_RESULT_DISPLAY;
|
|
}
|
|
case STATE_RESULT_FAILURE:
|
|
case STATE_RESULT_DISPLAY:
|
|
if (!cooldownCheck(gTimeout)) {
|
|
oled.updateOLED(state, lastStatusText, String(cooldownSecondsRemaining(gTimeout, timer)));
|
|
}
|
|
break;
|
|
}
|
|
|
|
delay(50);
|
|
}
|
|
#pragma clang diagnostic pop
|