Skip to content

Commit

Permalink
Add the PN532 library
Browse files Browse the repository at this point in the history
  • Loading branch information
ShallowGreen123 committed Jan 21, 2025
1 parent fad22f2 commit 2fd84cf
Show file tree
Hide file tree
Showing 79 changed files with 10,000 additions and 68 deletions.
117 changes: 117 additions & 0 deletions examples/pn532_emulatetag/pn532_emulatetag.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**************************************************************************/
/*!
This example will attempt to connect to an ISO14443A
card or tag and retrieve some basic information about it
that can be used to determine what type of card it is.
Note that you need the baud rate to be 115200 because we need to print
out the data and read from the card at the same time!
To enable debug message, define DEBUG in PN532/PN532_debug.h
*/
/**************************************************************************/


#define NFC_INTERFACE_I2C
#define BOARD_PN532_RF_REST 45
#define BOARD_PN532_IRQ 17
#define BOARD_I2C_SDA 8
#define BOARD_I2C_SCL 18

#include <Wire.h>
#include <PN532_I2C.h>
#include <PN532_I2C.cpp>
#include <PN532.h>

#include "NdefMessage.h"
#include "emulatetag.h"
uint8_t ndefBuf[120];
NdefMessage message;
int messageSize;

uint8_t uid[3] = {0x12, 0x34, 0x56};

PN532_I2C pn532i2c(Wire);
EmulateTag nfc(pn532i2c);


void setup(void)
{
Serial.begin(115200);

int start_delay = 3;
while (start_delay)
{
Serial.print(start_delay);
delay(1000);
start_delay--;
}
Serial.println("Hello!");

// pinMode(_irq, INPUT);
pinMode(BOARD_PN532_RF_REST, OUTPUT);

digitalWrite(BOARD_PN532_RF_REST, LOW);
delay(1); // min 20ns
digitalWrite(BOARD_PN532_RF_REST, HIGH);
delay(2); // max 2ms

// iic scan
Wire.setPins(BOARD_I2C_SDA, BOARD_I2C_SCL);
Wire.begin();

message = NdefMessage();
message.addUriRecord("http://www.seeedstudio.com");
messageSize = message.getEncodedSize();
if (messageSize > sizeof(ndefBuf))
{
Serial.println("ndefBuf is too small");
while (1)
{
}
}

Serial.print("Ndef encoded message size: ");
Serial.println(messageSize);

message.encode(ndefBuf);

// comment out this command for no ndef message
nfc.setNdefFile(ndefBuf, messageSize);

// uid must be 3 bytes!
nfc.setUid(uid);

nfc.init();
}

void loop(void)
{
// uncomment for overriding ndef in case a write to this tag occured
// nfc.setNdefFile(ndefBuf, messageSize);

// start emulation (blocks)
// nfc.emulate();

// or start emulation with timeout
if(!nfc.emulate(1000)){ // timeout 1 second
Serial.println("timed out");
}

// deny writing to the tag
// nfc.setTagWriteable(false);

if (nfc.writeOccured())
{
Serial.println("\nWrite occured !");
uint8_t *tag_buf;
uint16_t length;

nfc.getContent(&tag_buf, &length);
NdefMessage msg = NdefMessage(tag_buf, length);
// msg.print();
}

delay(20);
}
25 changes: 25 additions & 0 deletions lib/NDEF/Due.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// redefine some stuff so code works on Due
// http://arduino.cc/forum/index.php?&topic=153761.0

#ifndef Due_h
#define Due_h

#if defined(__SAM3X8E__)
#define PROGMEM
#define pgm_read_byte(x) (*((char *)x))
// #define pgm_read_word(x) (*((short *)(x & 0xfffffffe)))
#define pgm_read_word(x) ( ((*((unsigned char *)x + 1)) << 8) + (*((unsigned char *)x)))
#define pgm_read_byte_near(x) (*((char *)x))
#define pgm_read_byte_far(x) (*((char *)x))
// #define pgm_read_word_near(x) (*((short *)(x & 0xfffffffe))
// #define pgm_read_word_far(x) (*((short *)(x & 0xfffffffe)))
#define pgm_read_word_near(x) ( ((*((unsigned char *)x + 1)) << 8) + (*((unsigned char *)x)))
#define pgm_read_word_far(x) ( ((*((unsigned char *)x + 1)) << 8) + (*((unsigned char *)x))))
#define PSTR(x) x
#if defined F
#undef F
#endif
#define F(X) (X)
#endif

#endif
29 changes: 29 additions & 0 deletions lib/NDEF/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Software License Agreement (BSD License)

Copyright (c) 2013-2014, Don Coleman
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Loading

0 comments on commit 2fd84cf

Please sign in to comment.