diff --git a/src/main.cpp b/src/main.cpp index 8412b30..17ec49c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,65 +2,73 @@ #include #include -#define RST_PIN 9 // Configurable, see typical pin layout above -#define SS_1_PIN 10 // Configurable, take a unused pin, only HIGH/LOW required, must be different to SS 2 +#define RST_PIN 9 // Configurable, see typical pin layout above +#define SS_1_PIN 10 // Configurable, take a unused pin, only HIGH/LOW required, must be different to SS 2 -MFRC522 mfrc522; // Create MFRC522 instance. +MFRC522 mfrc522; // Create MFRC522 instance. String SerialCMD; /** * Helper routine to dump a byte array as hex values to Serial. */ -void dump_byte_array(byte *buffer, byte bufferSize) { - for (byte i = 0; i < bufferSize; i++) { +void dump_byte_array(byte *buffer, byte bufferSize) +{ + for (byte i = 0; i < bufferSize; i++) + { Serial.print(buffer[i] < 0x10 ? " 0" : " "); Serial.print(buffer[i], HEX); } } +void serialprint_rfid_uid() +{ + Serial.print(F("R#")); + // Show some details of the PICC (that is: the tag/card) + dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); + Serial.print(F("#")); + MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); + // Serial.println(mfrc522.PICC_GetTypeName(piccType)); + Serial.print(piccType, HEX); + Serial.print(F("#")); + Serial.println(mfrc522.PICC_GetTypeName(piccType)); +} + /** * Initialize. */ -void setup() { +void setup() +{ Serial.begin(9600); // Initialize serial communications with the PC - while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) + while (!Serial) + ; // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) - SPI.begin(); // Init SPI bus + SPI.begin(); // Init SPI bus mfrc522.PCD_Init(SS_1_PIN, RST_PIN); // Init each MFRC522 card - Serial.print(F("Reader ")); - Serial.print(F(": ")); + Serial.print(F("AT168RC522#Reader#")); mfrc522.PCD_DumpVersionToSerial(); } /** * Main loop. */ -void loop() { +void loop() +{ - if (Serial.available() > 0) { - SerialCMD = Serial.readString(); - SerialCMD.trim(); - } - - if ((mfrc522.PICC_IsNewCardPresent() || SerialCMD.startsWith("READ") ) && mfrc522.PICC_ReadCardSerial()) { - Serial.print(F("R#")); - // Show some details of the PICC (that is: the tag/card) - dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size); - Serial.print(F("#")); - MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); - //Serial.println(mfrc522.PICC_GetTypeName(piccType)); - Serial.print(piccType,HEX); - Serial.print(F("#")); - Serial.println(mfrc522.PICC_GetTypeName(piccType)); + if (Serial.available() > 0) + { + SerialCMD = Serial.readString(); + SerialCMD.trim(); + } - // Halt PICC - mfrc522.PICC_HaltA(); - // Stop encryption on PCD - mfrc522.PCD_StopCrypto1(); + if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) + { + serialprint_rfid_uid(); + // Halt PICC + mfrc522.PICC_HaltA(); + // Stop encryption on PCD + mfrc522.PCD_StopCrypto1(); + } - //Reset SerialCMD - SerialCMD = ""; - } } \ No newline at end of file