[U] Refactor

This commit is contained in:
2023-11-25 02:54:35 -05:00
parent 121c3cca2c
commit e7d4e6d966
+57 -53
View File
@@ -4,101 +4,105 @@
#include <PN532_I2C.h> #include <PN532_I2C.h>
#include <PN532.h> #include <PN532.h>
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
PN532_I2C pn532i2c(Wire); PN532_I2C pn532i2c(Wire);
PN532 nfc(pn532i2c); PN532 nfc(pn532i2c);
#define Serial USBSerial uint8_t prevIDm[8];
unsigned long prevTime;
#include <PN532_debug.h>
uint8_t _prevIDm[8];
unsigned long _prevTime;
void setup() void setup()
{ {
// Add initial delay to allow the serial monitor to catch up
delay(1000); delay(1000);
Serial.begin(115200);
Serial.println("Hello!");
Wire.setPins(4, 5);
// Initialize serial port
USBSerial.begin(115200);
USBSerial.println("Hello!");
// Initialize I2C communication
// Wire.setPins(GPIO_NUM_4, GPIO_NUM_5);
// Find the PN532 NFC module
nfc.begin(); nfc.begin();
u32 versiondata;
uint32_t versiondata = nfc.getFirmwareVersion(); while ((versiondata = nfc.getFirmwareVersion()) == 0)
if (!versiondata)
{ {
Serial.print("Didn't find PN53x board"); USBSerial.println("Didn't find PN53x board");
while (1) {delay(10);}; // halt delay(100);
} }
// Got ok data, print it out! // Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata >> 24) & 0xFF, HEX); USBSerial.print("Found chip PN5");
Serial.print("Firmware ver. "); Serial.print((versiondata >> 16) & 0xFF, DEC); USBSerial.println(versiondata >> 24 & 0xFF, HEX);
Serial.print('.'); Serial.println((versiondata >> 8) & 0xFF, DEC); USBSerial.print("Firmware ver. ");
USBSerial.print(versiondata >> 16 & 0xFF, DEC);
USBSerial.print('.');
USBSerial.println(versiondata >> 8 & 0xFF, DEC);
// Set the max number of retry attempts to read from a card // Set the max number of retry attempts to read from a card
// This prevents us from waiting forever for a card, which is // This prevents us from waiting forever for a card, which is the default behaviour of the PN532.
// the default behaviour of the PN532.
nfc.setPassiveActivationRetries(0xFF); nfc.setPassiveActivationRetries(0xFF);
nfc.SAMConfig(); nfc.SAMConfig();
memset(_prevIDm, 0, 8); // Clear the IDm buffer
memset(prevIDm, 0, 8);
} }
void printUid(uint8_t* uid, uint8_t uidLength) { void printUid(const uint8_t* uid, const uint8_t uidLength)
Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes"); {
Serial.print("UID Value: "); USBSerial.print("UID Length: ");
for (uint8_t i = 0; i < uidLength; i++) { USBSerial.print(uidLength, DEC);
Serial.print(uid[i], HEX); USBSerial.println(" bytes");
} USBSerial.print("UID Value: ");
Serial.println(""); for (u8 i = 0; i < uidLength; i++)
USBSerial.print(uid[i], HEX);
USBSerial.println("");
} }
void loop() void loop()
{ {
uint8_t ret; u8 idm[8];
uint16_t systemCode = 0xFFFF; u8 pmm[8];
uint8_t requestCode = 0x00; // System Code request
uint8_t idm[8];
uint8_t pmm[8];
uint16_t systemCodeResponse;
// Wait for an FeliCa type cards. // Wait for an FeliCa type cards.
// When one is found, some basic information such as IDm, PMm, and System Code are retrieved. // When one is found, some basic information such as IDm, PMm, and System Code are retrieved.
Serial.print("F"); USBSerial.print("F");
ret = nfc.felica_Polling(systemCode, requestCode, idm, pmm, &systemCodeResponse, 5); if (nfc.felica_Polling(0xFFFF, 0x01, idm, pmm, nullptr, 5) == 1)
{
if (ret == 1) { if (memcmp(idm, prevIDm, 8) == 0 && millis() - prevTime < 3000)
if ( memcmp(idm, _prevIDm, 8) == 0 ) { {
if ( (millis() - _prevTime) < 3000 ) {
delay(5); delay(5);
return; return;
} }
}
Serial.println("\nFound a Felica card!"); USBSerial.println("\nFound a Felica card!");
printUid(idm, 8); printUid(idm, 8);
memcpy(_prevIDm, idm, 8); memcpy(prevIDm, idm, 8);
_prevTime = millis(); prevTime = millis();
return; return;
} }
Serial.print("M"); u8 uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
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)
if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength, 5)) { USBSerial.print("M");
if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, idm, &uidLength, 5))
{
// Check if the same card is present // Check if the same card is present
if (memcmp(uid, _prevIDm, uidLength) == 0 && (millis() - _prevTime) < 3000) { if (memcmp(idm, prevIDm, uidLength) == 0 && millis() - prevTime < 3000)
{
delay(5); delay(5);
return; return;
} }
Serial.println("\nFound a MIFARE card!"); USBSerial.println("\nFound a MIFARE card!");
printUid(uid, uidLength); printUid(idm, uidLength);
memcpy(_prevIDm, uid, uidLength); memcpy(prevIDm, idm, uidLength);
_prevTime = millis(); prevTime = millis();
return;
} }
} }