[U] Refactor

This commit is contained in:
2023-11-25 02:54:35 -05:00
parent 121c3cca2c
commit e7d4e6d966
+75 -71
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()
{ {
delay(1000); // Add initial delay to allow the serial monitor to catch up
Serial.begin(115200); delay(1000);
Serial.println("Hello!");
Wire.setPins(4, 5);
nfc.begin(); // Initialize serial port
USBSerial.begin(115200);
USBSerial.println("Hello!");
uint32_t versiondata = nfc.getFirmwareVersion(); // Initialize I2C communication
if (!versiondata) // Wire.setPins(GPIO_NUM_4, GPIO_NUM_5);
{
Serial.print("Didn't find PN53x board");
while (1) {delay(10);}; // halt
}
// Got ok data, print it out! // Find the PN532 NFC module
Serial.print("Found chip PN5"); Serial.println((versiondata >> 24) & 0xFF, HEX); nfc.begin();
Serial.print("Firmware ver. "); Serial.print((versiondata >> 16) & 0xFF, DEC); u32 versiondata;
Serial.print('.'); Serial.println((versiondata >> 8) & 0xFF, DEC); 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 // Got ok data, print it out!
// This prevents us from waiting forever for a card, which is USBSerial.print("Found chip PN5");
// the default behaviour of the PN532. USBSerial.println(versiondata >> 24 & 0xFF, HEX);
nfc.setPassiveActivationRetries(0xFF); USBSerial.print("Firmware ver. ");
nfc.SAMConfig(); 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) { 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 (memcmp(idm, prevIDm, 8) == 0 && millis() - prevTime < 3000)
{
delay(5);
return;
}
if (ret == 1) { USBSerial.println("\nFound a Felica card!");
if ( memcmp(idm, _prevIDm, 8) == 0 ) { printUid(idm, 8);
if ( (millis() - _prevTime) < 3000 ) {
delay(5); memcpy(prevIDm, idm, 8);
prevTime = millis();
return; return;
}
} }
Serial.println("\nFound a Felica card!"); u8 uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
printUid(idm, 8);
memcpy(_prevIDm, idm, 8); USBSerial.print("M");
_prevTime = millis(); if (nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, idm, &uidLength, 5))
return; {
} // Check if the same card is present
if (memcmp(idm, prevIDm, uidLength) == 0 && millis() - prevTime < 3000)
{
delay(5);
return;
}
Serial.print("M"); USBSerial.println("\nFound a MIFARE card!");
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID printUid(idm, uidLength);
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)) { memcpy(prevIDm, idm, uidLength);
// Check if the same card is present prevTime = millis();
if (memcmp(uid, _prevIDm, uidLength) == 0 && (millis() - _prevTime) < 3000) {
delay(5);
return;
} }
Serial.println("\nFound a MIFARE card!");
printUid(uid, uidLength);
memcpy(_prevIDm, uid, uidLength);
_prevTime = millis();
return;
}
} }