diff --git a/firmware/core/core.ino b/firmware/core/core.ino index 85b74c9..7421a20 100644 --- a/firmware/core/core.ino +++ b/firmware/core/core.ino @@ -1,12 +1,10 @@ #include #include -#include -#include -#include "PN532.h" #include "protocol.h" #include "common.h" #include "peripheral.h" +#include "rfid.h" const int led = 13; //LED pin config @@ -20,9 +18,7 @@ Door door; Alarm alarm; Button button; - -static PN532_I2C pn532_i2c(Wire); -static PN532 pn532(pn532_i2c); +RFID rfid; void software_Reset() // Restarts program from beginning but does not reset the peripherals and registers { @@ -49,30 +45,6 @@ } } -void initRfid() -{ - pn532.begin(); - - uint32_t versiondata = pn532.getFirmwareVersion(); - if (! versiondata) { - Serial.println("Didn't find PN53x board"); - return; - } - - // Got ok data, print it out! - Serial.print("Found chip PN5"); Serial.print((versiondata>>24) & 0xFF, HEX); - Serial.print(" with firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); - Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC); - - // 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. - pn532.setPassiveActivationRetries(0xFF); - - // configure board to read RFID tags - pn532.SAMConfig(); -} - void setup() { pinMode(led, OUTPUT); door.Init(); @@ -87,7 +59,7 @@ } Serial.println("Card9 controller started"); - initRfid(); + rfid.Init(); // start the Ethernet connection: Serial.println("Trying to get an IP address using DHCP"); @@ -174,45 +146,6 @@ } } -void findRfidCard() -{ - boolean success; - static uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID - static uint8_t uidLength = 0; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) - static unsigned long previousMillis = 0; - - - unsigned long currentMillis = millis(); - if(currentMillis - previousMillis > 300){ - previousMillis = currentMillis; - }else{ - return; - } - - // Wait for an ISO14443A type cards (Mifare, etc.). When one is found - // 'uid' will be populated with the UID, and uidLength will indicate - // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) - success = pn532.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength, 10); - - if (success) { - Serial.println("Found a card!"); - 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(" 0x");Serial.print(uid[i], HEX); - } - Serial.println(""); - // Wait 1 second before continuing - // delay(1000); - } - else - { - // PN532 probably timed out waiting for a card - Serial.println("Timed out waiting for a card"); - } -} - void loop() { static uint8_t connect_retries = 0; @@ -229,7 +162,15 @@ alarm.Off(); } - findRfidCard(); + rfid.Poll(); + if(rfid.Found()){ + sendEventPacket(CardDidScan); + if(rfid.SkeletonKey()){ + door.Open(); + } + delay(500); + rfid.Next(); + } if (!client.connected()) { digitalWrite(led, LOW); diff --git a/firmware/core/rfid.h b/firmware/core/rfid.h new file mode 100644 index 0000000..2a442a7 --- /dev/null +++ b/firmware/core/rfid.h @@ -0,0 +1,34 @@ +#ifndef RFID_H__ +#define RFID_H__ + +#include "Arduino.h" + + +class RFID +{ + static uint8_t skeletonKey[8]; + static uint8_t skeletonKeyLength; + uint8_t uid[8]; // Buffer to store the returned UID + uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) + unsigned long previousPollMillis; +public: + RFID():uidLength(0),previousPollMillis(0){} + void Init(); + void Poll(); + bool Found() + { + return uidLength != 0; + } + uint8_t* GetUid(uint8_t &len) + { + len = uidLength; + return uid; + } + bool SkeletonKey(); + void Next() + { + uidLength = 0; + } +}; + +#endif diff --git a/firmware/core/rfid.ino b/firmware/core/rfid.ino new file mode 100644 index 0000000..8d609a0 --- /dev/null +++ b/firmware/core/rfid.ino @@ -0,0 +1,90 @@ +#include "rfid.h" +#include +#include +#include "PN532.h" + + +static PN532_I2C pn532_i2c(Wire); +static PN532 pn532(pn532_i2c); + +uint8_t RFID::skeletonKey[8] = {0x64,0x1C,0x6D,0xDB}; +uint8_t RFID::skeletonKeyLength = 4; + + +void RFID::Poll() +{ + boolean success; + + if(uidLength > 0){ //A card had been found previously + return; + } + + unsigned long currentMillis = millis(); + if(currentMillis - previousPollMillis > 300){ + previousPollMillis = currentMillis; + }else{ + return; + } + + // Wait for an ISO14443A type cards (Mifare, etc.). When one is found + // 'uid' will be populated with the UID, and uidLength will indicate + // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) + success = pn532.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength, 10); + + if (success) { + Serial.println("Found a card!"); + 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(" 0x");Serial.print(uid[i], HEX); + } + Serial.println(""); + // Wait 1 second before continuing + // delay(1000); + } + else + { + // PN532 probably timed out waiting for a card + Serial.println("Timed out waiting for a card"); + uidLength = 0; + } +} + +bool RFID::SkeletonKey() +{ + if(uidLength > 0){ + if(uidLength != skeletonKeyLength) + return false; + for(int i=0; i>24) & 0xFF, HEX); + Serial.print(" with firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); + Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC); + + // 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. + pn532.setPassiveActivationRetries(0xFF); + + // configure board to read RFID tags + pn532.SAMConfig(); +}