diff --git a/NfcAdapter.cpp b/NfcAdapter.cpp index 9099bde..d279b25 100644 --- a/NfcAdapter.cpp +++ b/NfcAdapter.cpp @@ -1,8 +1,17 @@ #include -NfcAdapter::NfcAdapter(PN532Interface &interface) +NfcAdapter::NfcAdapter(PN532Interface &interface, uint8_t irqPin) { shield = new PN532(interface); + + //set the IRQ pin + _irqPin = irqPin; + + // if defined, set the pin to input mode + if (irqPin > -1) + { + pinMode(irqPin, INPUT); + } } NfcAdapter::~NfcAdapter(void) @@ -194,6 +203,20 @@ boolean NfcAdapter::write(NdefMessage& ndefMessage) return success; } + +boolean NfcAdapter::startPassive() +{ + if (_irqPin > -1) + { + #ifdef NDEF_DEBUG + Serial.println(F("IRQ pin not specified")); + #endif + shield->startPassiveTargetIDDetection(PN532_MIFARE_ISO14443A); + return 1; + } + return 0; +} + // TODO this should return a Driver MifareClassic, MifareUltralight, Type 4, Unknown // Guess Tag Type by looking at the ATQA and SAK values // Need to follow spec for Card Identification. Maybe AN1303, AN1305 and ??? diff --git a/NfcAdapter.h b/NfcAdapter.h index 1ef9e59..f55e6cc 100644 --- a/NfcAdapter.h +++ b/NfcAdapter.h @@ -22,7 +22,7 @@ class NfcAdapter { public: - NfcAdapter(PN532Interface &interface); + NfcAdapter(PN532Interface &interface, uint8_t irqPin = -1); ~NfcAdapter(void); void begin(boolean verbose=true); @@ -35,9 +35,12 @@ class NfcAdapter { boolean format(); // reset tag back to factory state boolean clean(); + // enter passive detection mode (IRQ). For use with I2C ONLY. + boolean startPassive(); private: PN532* shield; byte uid[7]; // Buffer to store the returned UID + uint8_t _irqPin; // stores the IRQ pin for I2C mode unsigned int uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) unsigned int guessTagType(); }; diff --git a/examples/ReadTagIRQ/ReadTagIQR.ino b/examples/ReadTagIRQ/ReadTagIQR.ino new file mode 100644 index 0000000..422f456 --- /dev/null +++ b/examples/ReadTagIRQ/ReadTagIQR.ino @@ -0,0 +1,60 @@ +/* -------------------------------------------------------------- */ +/* This example uses the IRQ port on the PN532 which is only */ +/* available when the PN532 is in IRQ mode. It will not work in */ +/* other modes. You will need to connect the IRQ pin to an IO */ +/* -------------------------------------------------------------- */ + +#include +#include "PN532_I2C.h" +#include "PN532.h" +#include "NfcAdapter.h" + +//how long to wait between card reads +#define CARD_DELAY 1000 // wait 1s before reading another card +#define IRQ_PIN 14 // pin the IRQ on the PN532 is connected to + +PN532_I2C pn532_i2c(Wire); +NfcAdapter nfc = NfcAdapter(pn532_i2c, IRQ_PIN); + +long lastRead = 0; +bool enabled = true; +int irqCurr; +int irqPrev; + +void irqListen() { + irqPrev = irqCurr = HIGH; + nfc.startPassive(); + Serial.println("Scan a NFC tag"); +} + +void readCard() { + if (nfc.tagPresent()) { + NfcTag tag = nfc.read(); + tag.print(); + lastRead = millis(); + } + enabled = false; +} + +void setup () { + Serial.begin(9600); + nfc.begin(); + irqListen(); +} + +void loop () { + if (!enabled) { + if (millis() - lastRead > CARD_DELAY) { + enabled = true; + irqListen(); + } + } else { + irqCurr = digitalRead(IRQ_PIN); + + if (irqCurr == LOW && irqPrev == HIGH) { + readCard(); + } + + irqPrev = irqCurr; + } +}