From e7d4e6d9668d2712e5cc61514c65d792d7cc6f5d Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Sat, 25 Nov 2023 02:54:35 -0500 Subject: [PATCH] [U] Refactor --- src/main.cpp | 146 ++++++++++++++++++++++++++------------------------- 1 file changed, 75 insertions(+), 71 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f58dbd8..e2d661f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,101 +4,105 @@ #include #include +#define u8 uint8_t +#define u16 uint16_t +#define u32 uint32_t + PN532_I2C pn532i2c(Wire); PN532 nfc(pn532i2c); -#define Serial USBSerial - -#include - -uint8_t _prevIDm[8]; -unsigned long _prevTime; +uint8_t prevIDm[8]; +unsigned long prevTime; void setup() { - delay(1000); - Serial.begin(115200); - Serial.println("Hello!"); - Wire.setPins(4, 5); + // Add initial delay to allow the serial monitor to catch up + delay(1000); - nfc.begin(); + // Initialize serial port + USBSerial.begin(115200); + USBSerial.println("Hello!"); - uint32_t versiondata = nfc.getFirmwareVersion(); - if (!versiondata) - { - Serial.print("Didn't find PN53x board"); - while (1) {delay(10);}; // halt - } + // Initialize I2C communication + // Wire.setPins(GPIO_NUM_4, GPIO_NUM_5); - // Got ok data, print it out! - Serial.print("Found chip PN5"); Serial.println((versiondata >> 24) & 0xFF, HEX); - Serial.print("Firmware ver. "); Serial.print((versiondata >> 16) & 0xFF, DEC); - Serial.print('.'); Serial.println((versiondata >> 8) & 0xFF, DEC); + // Find the PN532 NFC module + nfc.begin(); + u32 versiondata; + while ((versiondata = nfc.getFirmwareVersion()) == 0) + { + USBSerial.println("Didn't find PN53x board"); + delay(100); + } - // Set the max number of retry attempts to read from a card - // This prevents us from waiting forever for a card, which is - // the default behaviour of the PN532. - nfc.setPassiveActivationRetries(0xFF); - nfc.SAMConfig(); + // Got ok data, print it out! + USBSerial.print("Found chip PN5"); + USBSerial.println(versiondata >> 24 & 0xFF, HEX); + USBSerial.print("Firmware ver. "); + USBSerial.print(versiondata >> 16 & 0xFF, DEC); + USBSerial.print('.'); + USBSerial.println(versiondata >> 8 & 0xFF, DEC); - memset(_prevIDm, 0, 8); + // Set the max number of retry attempts to read from a card + // This prevents us from waiting forever for a card, which is the default behaviour of the PN532. + nfc.setPassiveActivationRetries(0xFF); + nfc.SAMConfig(); + + // Clear the IDm buffer + memset(prevIDm, 0, 8); } -void printUid(uint8_t* uid, uint8_t uidLength) { - Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); - Serial.print("UID Value: "); - for (uint8_t i = 0; i < uidLength; i++) { - Serial.print(uid[i], HEX); - } - Serial.println(""); +void printUid(const uint8_t* uid, const uint8_t uidLength) +{ + USBSerial.print("UID Length: "); + USBSerial.print(uidLength, DEC); + USBSerial.println(" bytes"); + USBSerial.print("UID Value: "); + for (u8 i = 0; i < uidLength; i++) + USBSerial.print(uid[i], HEX); + USBSerial.println(""); } void loop() { - uint8_t ret; - uint16_t systemCode = 0xFFFF; - uint8_t requestCode = 0x00; // System Code request - uint8_t idm[8]; - uint8_t pmm[8]; - uint16_t systemCodeResponse; + u8 idm[8]; + u8 pmm[8]; - // Wait for an FeliCa type cards. - // When one is found, some basic information such as IDm, PMm, and System Code are retrieved. - Serial.print("F"); - ret = nfc.felica_Polling(systemCode, requestCode, idm, pmm, &systemCodeResponse, 5); + // Wait for an FeliCa type cards. + // When one is found, some basic information such as IDm, PMm, and System Code are retrieved. + USBSerial.print("F"); + if (nfc.felica_Polling(0xFFFF, 0x01, idm, pmm, nullptr, 5) == 1) + { + if (memcmp(idm, prevIDm, 8) == 0 && millis() - prevTime < 3000) + { + delay(5); + return; + } - if (ret == 1) { - if ( memcmp(idm, _prevIDm, 8) == 0 ) { - if ( (millis() - _prevTime) < 3000 ) { - delay(5); + USBSerial.println("\nFound a Felica card!"); + printUid(idm, 8); + + memcpy(prevIDm, idm, 8); + prevTime = millis(); return; - } } - Serial.println("\nFound a Felica card!"); - printUid(idm, 8); + u8 uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) - memcpy(_prevIDm, idm, 8); - _prevTime = millis(); - return; - } + USBSerial.print("M"); + if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, idm, &uidLength, 5)) + { + // Check if the same card is present + if (memcmp(idm, prevIDm, uidLength) == 0 && millis() - prevTime < 3000) + { + delay(5); + return; + } - Serial.print("M"); - uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID - uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) + USBSerial.println("\nFound a MIFARE card!"); + printUid(idm, uidLength); - if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 5)) { - // Check if the same card is present - if (memcmp(uid, _prevIDm, uidLength) == 0 && (millis() - _prevTime) < 3000) { - delay(5); - return; + memcpy(prevIDm, idm, uidLength); + prevTime = millis(); } - - Serial.println("\nFound a MIFARE card!"); - printUid(uid, uidLength); - - memcpy(_prevIDm, uid, uidLength); - _prevTime = millis(); - return; - } }