Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support IRQ mode #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion NfcAdapter.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#include <NfcAdapter.h>

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)
Expand Down Expand Up @@ -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 ???
Expand Down
5 changes: 4 additions & 1 deletion NfcAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class NfcAdapter {
public:
NfcAdapter(PN532Interface &interface);
NfcAdapter(PN532Interface &interface, uint8_t irqPin = -1);

~NfcAdapter(void);
void begin(boolean verbose=true);
Expand All @@ -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();
};
Expand Down
60 changes: 60 additions & 0 deletions examples/ReadTagIRQ/ReadTagIQR.ino
Original file line number Diff line number Diff line change
@@ -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 <Wire.h>
#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;
}
}