From 0f818f53075c8e3f09721b5c352e4b792654e172 Mon Sep 17 00:00:00 2001 From: gilphilbert Date: Sun, 4 Jul 2021 00:21:56 -0700 Subject: [PATCH 1/2] passive read - non blocking --- NfcAdapter.cpp | 25 +++++++++++++- NfcAdapter.h | 5 ++- examples/ReadTagIRQ/ReadTagIQR.ino | 55 ++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 examples/ReadTagIRQ/ReadTagIQR.ino diff --git a/NfcAdapter.cpp b/NfcAdapter.cpp index 9099bde..15da978 100644 --- a/NfcAdapter.cpp +++ b/NfcAdapter.cpp @@ -1,8 +1,17 @@ #include -NfcAdapter::NfcAdapter(PN532Interface &interface) +NfcAdapter::NfcAdapter(PN532Interface &interface, uint8_t irqPin = -1) { 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..6c12d3e --- /dev/null +++ b/examples/ReadTagIRQ/ReadTagIQR.ino @@ -0,0 +1,55 @@ +#include +#include "PN532_I2C.h" +#include "PN532.h" +#include "NfcAdapter.h" +#include "PN532Interface.h" + +#define CARD_DELAY 1000 +#define IRQ_PIN 14 + +// IRQ only works in I2C mode for the PN532 +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("\nScan a NFC tag\n"); +} + +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; + } +} \ No newline at end of file From 201dac1f9e755e8e67b3b48090cd6d6856fde5de Mon Sep 17 00:00:00 2001 From: gilphilbert Date: Mon, 5 Jul 2021 10:28:41 -0700 Subject: [PATCH 2/2] simplify i2c irq example --- NfcAdapter.cpp | 2 +- examples/ReadTagIRQ/ReadTagIQR.ino | 17 +++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/NfcAdapter.cpp b/NfcAdapter.cpp index 15da978..d279b25 100644 --- a/NfcAdapter.cpp +++ b/NfcAdapter.cpp @@ -1,6 +1,6 @@ #include -NfcAdapter::NfcAdapter(PN532Interface &interface, uint8_t irqPin = -1) +NfcAdapter::NfcAdapter(PN532Interface &interface, uint8_t irqPin) { shield = new PN532(interface); diff --git a/examples/ReadTagIRQ/ReadTagIQR.ino b/examples/ReadTagIRQ/ReadTagIQR.ino index 6c12d3e..422f456 100644 --- a/examples/ReadTagIRQ/ReadTagIQR.ino +++ b/examples/ReadTagIRQ/ReadTagIQR.ino @@ -1,13 +1,18 @@ +/* -------------------------------------------------------------- */ +/* 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" -#include "PN532Interface.h" -#define CARD_DELAY 1000 -#define IRQ_PIN 14 +//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 -// IRQ only works in I2C mode for the PN532 PN532_I2C pn532_i2c(Wire); NfcAdapter nfc = NfcAdapter(pn532_i2c, IRQ_PIN); @@ -19,7 +24,7 @@ int irqPrev; void irqListen() { irqPrev = irqCurr = HIGH; nfc.startPassive(); - Serial.println("\nScan a NFC tag\n"); + Serial.println("Scan a NFC tag"); } void readCard() { @@ -52,4 +57,4 @@ void loop () { irqPrev = irqCurr; } -} \ No newline at end of file +}