summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergiusz 'q3k' Bazański <q3k@q3k.org>2015-04-02 20:46:26 +0200
committerSergiusz 'q3k' Bazański <q3k@q3k.org>2015-04-02 20:46:26 +0200
commit3cbd7d377accc9b62a59b7d44082993326d988af (patch)
tree0b2e04c0932fdceaf9c35e40ed3d53b217b9e1d0
parent1079e497e4561bb6be02cc83c7c8f1ac147b0ed7 (diff)
downloaddoorman-3cbd7d377accc9b62a59b7d44082993326d988af.tar.gz
doorman-3cbd7d377accc9b62a59b7d44082993326d988af.tar.bz2
doorman-3cbd7d377accc9b62a59b7d44082993326d988af.zip
Switch to PN532 NFC reader
m---------arduino/lib/Adafruit-PN5320
-rw-r--r--arduino/lib/SM130/README7
-rw-r--r--arduino/lib/SM130/SM130.cpp541
-rw-r--r--arduino/lib/SM130/SM130.h147
-rw-r--r--arduino/lib/SM130/examples/sm130demo/sm130demo.ino297
-rw-r--r--arduino/lib/SM130/examples/sm130seek/sm130seek.ino51
-rw-r--r--arduino/lib/SM130/keywords.txt61
-rw-r--r--arduino/src/doorman.ino1
-rw-r--r--arduino/src/rf.cpp65
9 files changed, 18 insertions, 1152 deletions
diff --git a/arduino/lib/Adafruit-PN532 b/arduino/lib/Adafruit-PN532
new file mode 160000
+Subproject 437426fe069efc159c43635573eefe7b169207d
diff --git a/arduino/lib/SM130/README b/arduino/lib/SM130/README
deleted file mode 100644
index b69de3b..0000000
--- a/arduino/lib/SM130/README
+++ /dev/null
@@ -1,7 +0,0 @@
-Arduino library for SonMicro SM130 RFID reader
-
-see:
-http://www.sonmicro.com/en/index.php?option=com_content&view=article&id=57&Itemid=70
-
-June 2011
-marc@marcboon.com \ No newline at end of file
diff --git a/arduino/lib/SM130/SM130.cpp b/arduino/lib/SM130/SM130.cpp
deleted file mode 100644
index 5ac31db..0000000
--- a/arduino/lib/SM130/SM130.cpp
+++ /dev/null
@@ -1,541 +0,0 @@
-/**
- * @file SM130.cpp
- * @brief SM130 library for Arduino
- * @author Marc Boon <http://www.marcboon.com>
- * @date February 2012
- *
- * <p>
- * Controls a SonMicro SM130/mini RFID reader or RFIDuino by I2C
- * </p>
- * <p>
- * Arduino analog input 4 is I2C SDA (SM130/mini pin 10/6)<br>
- * Arduino analog input 5 is I2C SCL (SM130/mini pin 9/5)<br>
- * Arduino digital input 4 is DREADY (SM130/mini pin 21/18)<br>
- * Arduino digital output 3 is RESET (SM130/mini pin 18/14)
- * </p>
- *
- * @see http://www.arduino.cc
- * @see http://www.sonmicro.com/1356/sm130.php
- * @see http://rfid.marcboon.com
- */
-
-#include <Wire.h>
-#include <string.h>
-
-#include "SM130.h"
-
-// local functions
-void arrayToHex(char *s, byte array[], byte len);
-char toHex(byte b);
-
-/** Constructor.
- *
- * An instance of SM130 should be created as a global variable, outside of
- * any function.
- * The constructor sets data fields to default values for use with RFIDuino.
- * These may be changed in setup() before SM130::reset() is called.
- */
-SM130::SM130()
-{
- address = 0x42;
- pinRESET = 3;
- pinDREADY = 4;
- debug = false;
- t = millis() + 10;
-}
-
-/* Public member functions ****************************************************/
-
-
-/** Reset the SM130 module
- *
- * This function should be called in setup(). It initializes the IO pins and
- * issues a hardware or software reset, depending on the definition of pinRESET.
- * After reset, a HALT_TAG command is issued to terminate the automatic SEEK mode.
- *
- * Wire.begin() should also be called in setup(), and Wire.h should be included.
- *
- * If pinRESET has the value 0xff (-1), software reset over I2C will be used.
- * If pinDREADY has the value 0xff (-1), the SM130 will be polled over I2C while
- * in SEEK mode, otherwise the DREADY pin will be polled in SEEK mode.
- * For other commands, response polling is always over I2C.
- */
-void SM130::reset()
-{
- // Init DREADY pin
- if (pinDREADY != 0xff)
- {
- pinMode(pinDREADY, INPUT);
- }
-
- // Init RESET pin
- if (pinRESET != 0xff) // hardware reset
- {
- pinMode(pinRESET, OUTPUT);
- digitalWrite(pinRESET, HIGH);
- delay(10);
- digitalWrite(pinRESET, LOW);
- }
- else // software reset
- {
- sendCommand(CMD_RESET);
- }
-
- // Allow enough time for reset
- delay(200);
-
- // Set antenna power
- setAntennaPower(1);
-
- // To cancel automatic seek mode after reset, we send a HALT_TAG command
- haltTag();
-}
-
-/** Get the firmware version string.
- */
-const char* SM130::getFirmwareVersion()
-{
- // return immediately if version string already retrieved
- if (*versionString != 0)
- return versionString;
-
- // else send VERSION command and retry a few times if no response
- for (byte n = 0; n < 10; n++)
- {
- sendCommand(CMD_VERSION);
- if (available() && getCommand() == CMD_VERSION)
- return versionString;
- delay(100);
- }
- // time-out after 1s
- return 0;
-}
-
-/** Checks for availability of a valid response packet.
- *
- * This function should always be called and return true prior to using results
- * of a command.
- *
- * @returns true if a valid response packet is available
- */
-boolean SM130::available()
-{
- // If in SEEK mode and using DREADY pin, check the status
- if (cmd == CMD_SEEK_TAG && pinDREADY != 0xff)
- {
- if (!digitalRead(pinDREADY))
- return false;
- }
-
- // Set the maximum length of the expected response packet
- byte len;
- switch(cmd)
- {
- case CMD_ANTENNA_POWER:
- case CMD_AUTHENTICATE:
- case CMD_DEC_VALUE:
- case CMD_INC_VALUE:
- case CMD_WRITE_KEY:
- case CMD_HALT_TAG:
- case CMD_SLEEP:
- len = 4;
- break;
- case CMD_WRITE4:
- case CMD_WRITE_VALUE:
- case CMD_READ_VALUE:
- len = 8;
- case CMD_SEEK_TAG:
- case CMD_SELECT_TAG:
- len = 11;
- break;
- default:
- len = SIZE_PACKET;
- }
-
- // If valid data received, process the response packet
- if (receiveData(len) > 0)
- {
- // Init response variables
- tagType = tagLength = *tagString = 0;
-
- // If packet length is 2, the command failed. Set error code.
- errorCode = getPacketLength() < 3 ? data[2] : 0;
-
- // Process command response
- switch (getCommand())
- {
- case CMD_RESET:
- case CMD_VERSION:
- // RESET and VERSION commands produce the firmware version
- len = min(getPacketLength(), sizeof(versionString)) - 1;
- memcpy(versionString, data + 2, len);
- versionString[len] = 0;
- break;
-
- case CMD_SEEK_TAG:
- case CMD_SELECT_TAG:
- // If no error, get tag number
- if(errorCode == 0 && getPacketLength() >= 6)
- {
- tagLength = getPacketLength() - 2;
- tagType = data[2];
- memcpy(tagNumber, data + 3, tagLength);
- arrayToHex(tagString, tagNumber, tagLength);
- }
- break;
-
- case CMD_AUTHENTICATE:
- break;
-
- case CMD_READ16:
- break;
-
- case CMD_WRITE16:
- case CMD_WRITE4:
- break;
-
- case CMD_ANTENNA_POWER:
- errorCode = 0;
- antennaPower = data[2];
- break;
-
- case CMD_SLEEP:
- // If in SLEEP mode, no data is available
- return false;
- }
-
- // Data available
- return true;
- }
- // No data available
- return false;
-}
-
-/** Get error message for last command.
- *
- * @return Human-readable error message as a null-terminated string
- */
-const char* SM130::getErrorMessage()
-{
- switch(errorCode)
- {
- case 'L':
- if(getCommand() == CMD_SEEK_TAG) return "Seek in progress";
- case 0:
- return "OK";
- case 'N':
- if(getCommand() == CMD_WRITE_KEY) return "Write master key failed";
- if(getCommand() == CMD_SET_BAUD) return "Set baud rate failed";
- if(getCommand() == CMD_AUTHENTICATE) return "No tag present or login failed";
- return "No tag present";
- case 'U':
- if(getCommand() == CMD_AUTHENTICATE) return "Authentication failed";
- if(getCommand() == CMD_WRITE16 || getCommand() == CMD_WRITE4) return "Verification failed";
- return "Antenna off";
- case 'F':
- if(getCommand() == CMD_READ16) return "Read failed";
- return "Write failed";
- case 'I':
- return "Invalid value block";
- case 'X':
- return "Block is read-protected";
- case 'E':
- return "Invalid key format in EEPROM";
- default:
- return "Unknown error";
- }
-}
-
-/** Turns on/off the RF field.
- *
- * @param level 0 is off, anything else is on
- */
-void SM130::setAntennaPower(byte level)
-{
- antennaPower = level;
- data[0] = 2;
- data[1] = CMD_ANTENNA_POWER;
- data[2] = antennaPower;
- transmitData();
-}
-
-/** Authenticate with transport key (0xFFFFFFFFFFFF).
- *
- * @param block Block number
- */
-void SM130::authenticate(byte block)
-{
- data[0] = 3;
- data[1] = CMD_AUTHENTICATE;
- data[2] = block;
- data[3] = 0xff;
- transmitData();
-}
-
-/** Authenticate with specified key A or key B.
- *
- * @param block Block number
- * @param keyType Which key to use: 0xAA for key A or 0xBB for key B
- * @param key Key value (6 bytes)
- */
-void SM130::authenticate(byte block, byte keyType, byte key[6])
-{
- data[0] = 9;
- data[1] = CMD_AUTHENTICATE;
- data[2] = block;
- data[3] = keyType;
- memcpy(data + 4, key, 6);
- transmitData();
-}
-
-/** Read 16-byte block.
- *
- * @param block Block number
- */
-void SM130::readBlock(byte block)
-{
- data[0] = 2;
- data[1] = CMD_READ16;
- data[2] = block;
- transmitData();
-}
-
-/** Write 16-byte block.
- *
- * The block will be padded with zeroes if the message is shorter
- * than 15 characters.
- *
- * @param block Block number
- * @param message Null-terminated string of up to 15 characters
- */
-void SM130::writeBlock(byte block, const char* message)
-{
- data[0] = 18;
- data[1] = CMD_WRITE16;
- data[2] = block;
- strncpy((char*)data + 3, message, 15);
- data[18] = 0;
- transmitData();
-}
-
-/** Write 4-byte block.
- *
- * This command is used for Mifare Ultralight tags which have 4 byte blocks.
- *
- * @param block Block number
- * @param message Null-terminated string of up to 3 characters
- */
-void SM130::writeFourByteBlock(byte block, const char* message)
-{
- data[0] = 6;
- data[1] = CMD_WRITE4;
- data[2] = block;
- strncpy((char*)data + 3, message, 3);
- data[6] = 0;
- transmitData();
-}
-
-/** Send 1-byte command.
- *
- * @param cmd Command
- */
-void SM130::sendCommand(byte cmd)
-{
- data[0] = 1;
- data[1] = cmd;
- transmitData();
-}
-
-/* Private member functions ****************************************************/
-
-
-/** Transmit a packet with checksum to the SM130.
- */
-void SM130::transmitData()
-{
- // wait until at least 20ms passed since last I2C transmission
- while(t > millis());
- t = millis() + 20;
-
- // init checksum and packet length
- byte sum = 0;
- byte len = data[0] + 1;
-
- // remember which command was sent
- cmd = data[1];
-
- // transmit packet with checksum
- Wire.beginTransmission(address);
- for (int i = 0; i < len; i++)
- {
-#if defined(ARDUINO) && ARDUINO >= 100
- Wire.write(data[i]);
-#else
- Wire.send(data[i]);
-#endif
- sum += data[i];
- }
-#if defined(ARDUINO) && ARDUINO >= 100
- Wire.write(sum);
-#else
- Wire.send(sum);
-#endif
- Wire.endTransmission();
-
- // show transmitted packet for debugging
- if (debug)
- {
- Serial.print("> ");
- printArrayHex(data, len);
- Serial.print(' ');
- printHex(sum);
- Serial.println();
- }
-}
-
-/** Receives a packet from the SM130 and verifies the checksum.
- *
- * @param length the number of bytes to receive
- * @return the number of bytes in the payload, or -1 if bad checksum
- */
-byte SM130::receiveData(byte length)
-{
- // wait until at least 20ms passed since last I2C transmission
- while(t > millis());
- t = millis() + 20;
-
- // read response
- Wire.requestFrom(address, length);
- byte n = Wire.available();
-
- // get data if available
- if(n > 0)
- {
- for (byte i = 0; i < n;)
- {
-#if defined(ARDUINO) && ARDUINO >= 100
- data[i++] = Wire.read();
-#else
- data[i++] = Wire.receive();
-#endif
- }
-
- // show received packet for debugging
- if (debug && data[0] > 0 )
- {
- Serial.print("< ");
- printArrayHex(data, n);
- Serial.println();
- }
-
- // verify checksum if length > 0 and <= SIZE_PAYLOAD
- if (data[0] > 0 && data[0] <= SIZE_PAYLOAD)
- {
- byte i, sum;
- for (i = 0, sum = 0; i <= data[0]; i++)
- {
- sum += data[i];
- }
- // return with length of response, or -1 if invalid checksum
- return sum == data[i] ? data[0] : -1;
- }
- }
- return 0;
-}
-
-/** Maps tag types to names.
- *
- * @param type numeric tag type
- * @return Human-readable tag name as null-terminated string
- */
-const char* SM130::tagName(byte type)
-{
- switch(type)
- {
- case 1: return "Mifare UL";
- case 2: return "Mifare 1K";
- case 3: return "Mifare 4K";
- default: return "Unknown Tag";
- }
-}
-
-// Global helper functions
-
-/** Convert byte array to null-terminated hexadecimal string.
- *
- * @param s pointer to destination string
- * @param array byte array to convert
- * @param len length of byte array to convert
- */
-void arrayToHex(char *s, byte array[], byte len)
-{
- for (byte i = 0; i < len; i++)
- {
- *s++ = toHex(array[i] >> 4);
- *s++ = toHex(array[i]);
- }
- *s = 0;
-}
-
-/** Convert low-nibble of byte to ASCII hex.
- *
- * @param b byte to convert
- * $return uppercase hexadecimal character [0-9A-F]
- */
-char toHex(byte b)
-{
- b = b & 0x0f;
- return b < 10 ? b + '0' : b + 'A' - 10;
-}
-
-/** Print byte array as ASCII string.
- *
- * Non-printable characters (<0x20 or >0x7E) are printed as dot.
- *
- * @param array byte array
- * @param len length of byte array
- */
-void printArrayAscii(byte array[], byte len)
-{
- for (byte i = 0; i < len;)
- {
- char c = array[i++];
- if (c < 0x20 || c > 0x7e)
- {
- Serial.print('.');
- }
- else
- {
- Serial.print(char(c));
- }
- }
-}
-
-/** Print byte array as hexadecimal character pairs.
- *
- * @param array byte array
- * @param len length of byte array
- */
-void printArrayHex(byte array[], byte len)
-{
- for (byte i = 0; i < len;)
- {
- printHex(array[i++]);
- if (i < len)
- {
- Serial.print(' ');
- }
- }
-}
-/** Print byte as two hexadecimal characters.
- *
- * @param val byte value
- */
-void printHex(byte val)
-{
- if (val < 0x10)
- {
- Serial.print('0');
- }
- Serial.print(val, HEX);
-}
diff --git a/arduino/lib/SM130/SM130.h b/arduino/lib/SM130/SM130.h
deleted file mode 100644
index fee602d..0000000
--- a/arduino/lib/SM130/SM130.h
+++ /dev/null
@@ -1,147 +0,0 @@
-/**
- * @file SM130.h
- * @brief Header file for SM130 library
- * @author Marc Boon <http://www.marcboon.com>
- * @date February 2012
- */
-
-#ifndef SM130_h
-#define SM130_h
-
-#if defined(ARDUINO) && ARDUINO >= 100
-#include "Arduino.h"
-#else
-#include "WProgram.h"
-#endif
-
-#define SIZE_PAYLOAD 18 // maximum payload size of I2C packet
-#define SIZE_PACKET (SIZE_PAYLOAD + 2) // total I2C packet size, including length byte and checksum
-
-#define halt haltTag // deprecated function halt() renamed to haltTag()
-
-// Global functions
-void printArrayAscii(byte array[], byte len);
-void printArrayHex(byte array[], byte len);
-void printHex(byte val);
-
-/** Class representing a <a href="http://www.sonmicro.com/en/index.php?option=com_content&view=article&id=57&Itemid=70">SonMicro SM130 RFID module</a>.
- *
- * Nearly complete implementation of the <a href="http://www.sonmicro.com/en/downloads/Mifare/ds_SM130.pdf">SM130 datasheet</a>.<br>
- * Functions dealing with value blocks and stored keys are not implemented.
- */
-class SM130
-{
- byte data[SIZE_PACKET]; //!< packet data
- char versionString[8]; //!< version string
- byte tagNumber[7]; //!< tag number as byte array
- byte tagLength; //!< length of tag number in bytes (4 or 7)
- char tagString[15]; //!< tag number as hex string
- byte tagType; //!< type of tag
- char errorCode; //!< error code from some commands
- byte antennaPower; //!< antenna power level
- byte cmd; //!< last sent command
- unsigned long t; //!< timer for sending I2C commands
-
-public:
- static const int VERSION = 1; //!< version of this library
-
- static const byte MIFARE_ULTRALIGHT = 1;
- static const byte MIFARE_1K = 2;
- static const byte MIFARE_4K = 3;
-
- static const byte CMD_RESET = 0x80;
- static const byte CMD_VERSION = 0x81;
- static const byte CMD_SEEK_TAG = 0x82;
- static const byte CMD_SELECT_TAG = 0x83;
- static const byte CMD_AUTHENTICATE = 0x85;
- static const byte CMD_READ16 = 0x86;
- static const byte CMD_READ_VALUE = 0x87;
- static const byte CMD_WRITE16 = 0x89;
- static const byte CMD_WRITE_VALUE = 0x8a;
- static const byte CMD_WRITE4 = 0x8b;
- static const byte CMD_WRITE_KEY = 0x8c;
- static const byte CMD_INC_VALUE = 0x8d;
- static const byte CMD_DEC_VALUE = 0x8e;
- static const byte CMD_ANTENNA_POWER = 0x90;
- static const byte CMD_READ_PORT = 0x91;
- static const byte CMD_WRITE_PORT = 0x92;
- static const byte CMD_HALT_TAG = 0x93;
- static const byte CMD_SET_BAUD = 0x94;
- static const byte CMD_SLEEP = 0x96;
-
- boolean debug; //!< debug mode, prints all I2C communication to Serial port
- byte address; //!< I2C address (default 0x42)
- byte pinRESET; //!< RESET pin (default 3)
- byte pinDREADY; //!< DREADY pin (default 4)
-
- //! Constructor
- SM130();
- //! Hardware or software reset of the SM130 module
- void reset();
- //! Returns a null-terminated string with the firmware version of the SM130 module
- const char* getFirmwareVersion();
- //! Returns true if a response packet is available
- boolean available();
- //! Returns a pointer to the response packet
- byte* getRawData() { return data; };
- //! Returns the last executed command
- byte getCommand() { return data[1]; };
- //! Returns the packet length, excluding checksum
- byte getPacketLength() { return data[0]; };
- //! Returns the checksum
- byte getCheckSum() { return data[data[0]+1]; };
- //! Returns a pointer to the packet payload
- byte* getPayload() { return data+2; };
- //! Returns the block number for read/write commands
- byte getBlockNumber() { return data[2]; };
- //! Returns a pointer to the read block (with a length of 16 bytes)
- byte* getBlock() { return data+3; };
- //! Returns the tag's serial number as a byte array
- byte* getTagNumber() { return tagNumber; };
- //! Returns the length of the tag's serial number obtained by getTagNumer()
- byte getTagLength() { return tagLength; };
- //! Returns the tag's serial number as a hexadecimal null-terminated string
- const char* getTagString() { return tagString; };
- //! Returns the tag type (SM130::MIFARE_XX)
- byte getTagType() { return tagType; };
- //! Returns the tag type as a null-terminated string
- const char* getTagName() { return tagName(tagType); };
- //! Returns the error code of the last executed command
- char getErrorCode() { return errorCode; };
- //! Returns a human-readable error message corresponding to the error code
- const char* getErrorMessage();
- //! Returns the antenna power level (0 or 1)
- byte getAntennaPower() { return antennaPower; };
- //! Sends a SEEK_TAG command
- void seekTag() { sendCommand(CMD_SEEK_TAG); };
- //! Sends a SELECT_TAG command
- void selectTag() { sendCommand(CMD_SELECT_TAG); };
- //! Sends a HALT_TAG command
- void haltTag() { sendCommand(CMD_HALT_TAG); };
- //! Set antenna power (on/off)
- void setAntennaPower(byte level);
- //! Sends a SLEEP command (can only wake-up with hardware reset!)
- void sleep() { sendCommand(CMD_SLEEP); };
- //! Writes a null-terminated string of maximum 15 characters
- void writeBlock(byte block, const char* message);
- //! Writes a null-terminated string of maximum 3 characters to a Mifare Ultralight
- void writeFourByteBlock(byte block, const char* message);
- //! Sends a AUTHENTICATE command using the transport key
- void authenticate(byte block);
- //! Sends a AUTHENTICATE command using the specified key
- void authenticate(byte block, byte keyType, byte key[6]);
- //! Reads a 16-byte block
- void readBlock(byte block);
-
-private:
- //! Send single-byte command
- void sendCommand(byte cmd);
- //! Transmit command packet over I2C
- void transmitData();
- //! Receive response packet over I2C
- byte receiveData(byte length);
- //! Returns human-readable tag name corresponding to tag type
- const char* tagName(byte type);
-};
-
-#endif // SM130_h
diff --git a/arduino/lib/SM130/examples/sm130demo/sm130demo.ino b/arduino/lib/SM130/examples/sm130demo/sm130demo.ino
deleted file mode 100644
index 3690d66..0000000
--- a/arduino/lib/SM130/examples/sm130demo/sm130demo.ino
+++ /dev/null
@@ -1,297 +0,0 @@
-// SM130 demo application
-// Marc Boon <http://www.marcboon.com>
-// June 2009
-
-// Controls a SonMicro SM130/mini RFID reader or RFIDuino by I2C
-// Arduino analog input 4 is I2C SDA (SM130/mini pin 10/6)
-// Arduino analog input 5 is I2C SCL (SM130/mini pin 9/5)
-// Arduino digital input 4 is DREADY (SM130/mini pin 21/18)
-// Arduino digital output 3 is RESET (SM130/mini pin 18/14)
-
-#include <Wire.h>
-#include <SM130.h> // v1
-
-// Actions
-#define NONE 0
-#define SEEK 1
-#define READ 2
-#define WRITE 3
-
-// Create SM130 instance for RFIDuino
-SM130 RFIDuino;
-
-// Global vars
-byte action = NONE;
-boolean authenticated;
-byte block;
-byte numBlocks;
-byte tagType;
-char msg[16];
-
-void setup()
-{
- Wire.begin();
- Serial.begin(115200);
- Serial.println("RFIDuino");
-
- // reset RFIDuino
- RFIDuino.reset();
-
- // read firmware version
- Serial.print("Version ");
- Serial.println(RFIDuino.getFirmwareVersion());
-
- // help
- Serial.println("Type ? for help");
-}
-
-void loop()
-{
- // check for command from serial port
- if(Serial.available() > 0)
- {
- switch(Serial.read())
- {
- case '?':
- Serial.println("Commands:");
- Serial.println("D - Debug on/off");
- Serial.println("A - Antenna on/off");
- Serial.println("H - Halt tag");
- Serial.println("S - Seek tag");
- Serial.println("R - Read sector");
- Serial.println("W - Write string");
- Serial.println("V - Version");
- Serial.println("Q - Sleep");
- Serial.println("X - Reset");
- break;
- case 'd':
- case 'D':
- // debug on/off
- RFIDuino.debug = !RFIDuino.debug;
- Serial.print("Debug ");
- Serial.println(RFIDuino.debug ? "on" : "off");
- break;
- case 'a':
- case 'A':
- // antenna on/off
- RFIDuino.setAntennaPower(!RFIDuino.getAntennaPower());
- Serial.print("Antenna ");
- Serial.println(RFIDuino.getAntennaPower() ? "on" : "off");
- break;
- case 'h':
- case 'H':
- // halt
- Serial.println("Halt");
- RFIDuino.halt();
- action = NONE;
- break;
- case 's':
- case 'S':
- // enter seek mode
- Serial.println("Seek");
- RFIDuino.seekTag();
- action = SEEK;
- break;
- case 'r':
- case 'R':
- // read tag
- Serial.println("Read");
- action = READ;
- // specify what to read
- block = 0;
- numBlocks = 4;
- // tag has to be selected first
- RFIDuino.selectTag();
- break;
- case 'w':
- case 'W':
- // collect up to 15 characters from input, and terminate with zero
- if(readQuotedString(msg, sizeof(msg)) > 0)
- {
- // write string to tag
- Serial.print("Write '");
- Serial.print(msg);
- Serial.println("'");
- action = WRITE;
- block = 1;
- // tag has to be selected first
- RFIDuino.selectTag();
- }
- break;
- case 'v':
- case 'V':
- // read firmware version
- Serial.print("Version ");
- Serial.println(RFIDuino.getFirmwareVersion());
- break;
- case 'q':
- case 'Q':
- // enter sleep mode
- Serial.println("Sleep");
- RFIDuino.sleep();
- break;
- case 'x':
- case 'X':
- // reset
- Serial.println("Reset");
- RFIDuino.reset();
- action = NONE;
- break;
- }
- }
-
- // check for response from RFIDuino
- if(RFIDuino.available())
- {
- // check for errors, 0 means no error, L means logged in
- if(RFIDuino.getErrorCode() != 0 && RFIDuino.getErrorCode() != 'L')
- {
- Serial.println(RFIDuino.getErrorMessage());
- action = NONE;
- }
- else // deal with response
- {
- switch(RFIDuino.getCommand())
- {
- case SM130::CMD_SEEK_TAG:
- if(RFIDuino.getErrorCode() == 'L' || action == NONE)
- {
- // seek in progress, or auto-seek after reset, which we ignore
- break;
- }
- case SM130::CMD_SELECT_TAG:
- // store tag type
- tagType = RFIDuino.getTagType();
- // show tag name and serial number
- Serial.print(RFIDuino.getTagName());
- Serial.print(": ");
- Serial.println(RFIDuino.getTagString());
- // in case of read or write action, authenticate first
- if(action == READ || action == WRITE)
- {
- // mifare ultralight does not need authentication, and has 4-byte blocks
- if(tagType == SM130::MIFARE_ULTRALIGHT)
- {
- authenticated = true;
- if(action == READ)
- {
- RFIDuino.readBlock(block);
- }
- else
- {
- // write to last 4-byte block (because all others are write-protected on tikitags)
- RFIDuino.writeFourByteBlock(15, msg);
- }
- }
- else
- {
- authenticated = false;
- RFIDuino.authenticate(block);
- }
- }
- else if(action == SEEK)
- {
- // keep seeking
- RFIDuino.seekTag();
- }
- else
- {
- // terminate seek
- action = NONE;
- }
- break;
- case SM130::CMD_AUTHENTICATE:
- authenticated = true;
- if(action == READ)
- {
- RFIDuino.readBlock(block);
- }
- else if(action == WRITE)
- {
- RFIDuino.writeBlock(block, msg);
- }
- break;
- case SM130::CMD_READ16:
- // print 16-byte block in hex and ascii
- Serial.print("Block ");
- printHex(RFIDuino.getBlockNumber());
- Serial.print(": ");
- printArrayHex(RFIDuino.getBlock(), 16);
- Serial.print(" ");
- printArrayAscii(RFIDuino.getBlock(), 16);
- Serial.println();
- // get next block
- if(++block < numBlocks)
- {
- // mifare ultralight does not need authentication, and has 4-byte blocks
- if(tagType == SM130::MIFARE_ULTRALIGHT)
- {
- RFIDuino.readBlock(block * 4);
- }
- else if(authenticated && (block & 0x03) != 0)
- {
- // blocks from same sector don't need further authentication
- RFIDuino.readBlock(block);
- }
- else
- {
- // authenticate next sector
- authenticated = false;
- RFIDuino.authenticate(block);
- }
- }
- else // read completed
- {
- RFIDuino.haltTag();
- action = NONE;
- }
- break;
- case SM130::CMD_WRITE4:
- case SM130::CMD_WRITE16:
- // write completed
- Serial.println("OK");
- RFIDuino.haltTag();
- action = NONE;
- break;
- }
- }
- }
-}
-
-int readQuotedString(char *s, int len)
-{
- int i = 0;
- char quote = 0;
- while(i < len)
- {
- delay(5);
- if(Serial.available() == 0)
- {
- break;
- }
- *s = Serial.read();
- if(quote == 0)
- {
- if(*s == '"' || *s == '\'')
- {
- quote = *s;
- }
- else if(*s != ' ')
- {
- ++s;
- ++i;
- }
- }
- else if(*s == quote)
- {
- break;
- }
- else
- {
- ++s;
- ++i;
- }
- }
- *s = 0;
- return i;
-}
diff --git a/arduino/lib/SM130/examples/sm130seek/sm130seek.ino b/arduino/lib/SM130/examples/sm130seek/sm130seek.ino
deleted file mode 100644
index ae36596..0000000
--- a/arduino/lib/SM130/examples/sm130seek/sm130seek.ino
+++ /dev/null
@@ -1,51 +0,0 @@
-// SM130 - seek continuosly for tags and print type and id to serial port
-// Marc Boon <http://www.marcboon.com>
-// April 2009
-
-// Controls a SonMicro SM130/mini RFID reader or RFIDuino by I2C
-// Arduino analog input 4 is I2C SDA (SM130/mini pin 10/6)
-// Arduino analog input 5 is I2C SCL (SM130/mini pin 9/5)
-// Arduino digital input 4 is DREADY (SM130/mini pin 21/18)
-// Arduino digital output 3 is RESET (SM130/mini pin 18/14)
-
-// Following two includes are required for SM130
-#include <Wire.h>
-#include <SM130.h>
-
-// Create SM130 instance for RFIDuino
-SM130 RFIDuino;
-
-void setup()
-{
- // Start I2C bus master (required)
- Wire.begin();
-
- // Using the serial port is optional
- Serial.begin(115200);
- Serial.println("RFIDuino");
-
- // Reset RFIDuino, this will also configure IO pins DREADY and RESET
- RFIDuino.reset();
-
- // Read firmware version (optional)
- Serial.print("Version ");
- Serial.println(RFIDuino.getFirmwareVersion());
-
- // Start SEEK mode
- RFIDuino.seekTag();
-}
-
-void loop()
-{
- // Tag detected?
- if(RFIDuino.available())
- {
- // Print the tag's type and serial number
- Serial.print(RFIDuino.getTagName());
- Serial.print(": ");
- Serial.println(RFIDuino.getTagString());
-
- // Start new SEEK
- RFIDuino.seekTag();
- }
-}
diff --git a/arduino/lib/SM130/keywords.txt b/arduino/lib/SM130/keywords.txt
deleted file mode 100644
index 0e6d3a3..0000000
--- a/arduino/lib/SM130/keywords.txt
+++ /dev/null
@@ -1,61 +0,0 @@
-#### Class name ####
-SM130 KEYWORD1
-#### Constants ####
-VERSION LITERAL1
-MIFARE_ULTRALIGHT LITERAL1
-MIFARE_1K LITERAL1
-MIFARE_4K LITERAL1
-CMD_RESET LITERAL1
-CMD_VERSION LITERAL1
-CMD_SEEK_TAG LITERAL1
-CMD_SELECT_TAG LITERAL1
-CMD_AUTHENTICATE LITERAL1
-CMD_READ16 LITERAL1
-CMD_READ_VALUE LITERAL1
-CMD_WRITE16 LITERAL1
-CMD_WRITE_VALUE LITERAL1
-CMD_WRITE4 LITERAL1
-CMD_WRITE_KEY LITERAL1
-CMD_INC_VALUE LITERAL1
-CMD_DEC_VALUE LITERAL1
-CMD_RF_POWER LITERAL1
-CMD_READ_PORT LITERAL1
-CMD_WRITE_PORT LITERAL1
-CMD_HALT_TAG LITERAL1
-CMD_SET_BAUD LITERAL1
-CMD_SLEEP LITERAL1
-#### Member functions ####
-debug KEYWORD2
-address KEYWORD2
-pinRESET KEYWORD2
-pinDREADY KEYWORD2
-reset KEYWORD2
-getFirmwareVersion KEYWORD2
-available KEYWORD2
-getRawData KEYWORD2
-getCommand KEYWORD2
-getPacketLength KEYWORD2
-getCheckSum KEYWORD2
-getPayload KEYWORD2
-getTagNumber KEYWORD2
-getTagLength KEYWORD2
-getTagString KEYWORD2
-getTagType KEYWORD2
-getTagName KEYWORD2
-getErrorCode KEYWORD2
-getErrorMessage KEYWORD2
-getAntennaPower KEYWORD2
-getBlock KEYWORD2
-getBlockNumber KEYWORD2
-seekTag KEYWORD2
-selectTag KEYWORD2
-haltTag KEYWORD2
-setAntennaPower KEYWORD2
-sleep KEYWORD2
-writeBlock KEYWORD2
-writeFourByteBlock KEYWORD2
-authenticate KEYWORD2
-readBlock KEYWORD2
-printArrayAscii KEYWORD2
-printArrayHex KEYWORD2
-printHex KEYWORD2
diff --git a/arduino/src/doorman.ino b/arduino/src/doorman.ino
index e176bd1..b240970 100644
--- a/arduino/src/doorman.ino
+++ b/arduino/src/doorman.ino
@@ -160,7 +160,6 @@ void loop(){
pin=0;
rfid=0;
state=STATE_MIFARE;
- rf_reset();
break;
}
//Communication with PC (optional).
diff --git a/arduino/src/rf.cpp b/arduino/src/rf.cpp
index 181b784..2f12c01 100644
--- a/arduino/src/rf.cpp
+++ b/arduino/src/rf.cpp
@@ -1,77 +1,48 @@
#include <Arduino.h>
-#include <SM130.h>
-#include <Wire.h>
+#include <Adafruit_PN532.h>
#include "config.h"
/** Global variable that holds RFID reader object. */
-SM130 rfid;
+Adafruit_PN532 rfid(3, 4);
/**
* Init NFC subsystem
*/
void rf_init(void) {
- Wire.begin();
- rfid.address = 0x30;
- rfid.reset();
+ rfid.begin();
#ifdef DEBUG
- Serial.print("SM130 firmware version: ");
- Serial.println(rfid.getFirmwareVersion());
+ uint32_t versiondata = rfid.getFirmwareVersion();
+ Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
+ Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
+ Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
#endif //DEBUG
+ rfid.setPassiveActivationRetries(0xFF);
+ rfid.SAMConfig();
}
-/**
- * Resets the NFC controller
- */
-
-void rf_reset() {
- rfid.reset();
-}
-
-/**
- * Sends seek command to rfid reader.
- */
-void rf_seek() {
- rfid.seekTag();
-}
-
+uint8_t mifareBytes[7];
/**
* Handles communication with rfid reader.
* @param p_rfid Pointer to space where mifare id will be written.
* @return Returns true when authorized user rfid was rd.
*/
boolean rf_comm(unsigned long * p_rfid) {
- static unsigned long last_millis_seek = 0;
- static unsigned long last_millis_reset = 0;
-
- unsigned long new_millis = millis();
-
- (*p_rfid) = 0;
- if (new_millis < last_millis_seek) {
- last_millis_seek = new_millis;
- } else if ((new_millis - last_millis_seek) > RF_PERIOD_SEEK_MS) {
- rf_seek();
- last_millis_seek = new_millis;
- }
- if (new_millis < last_millis_reset) {
- last_millis_reset = new_millis;
- } else if ((new_millis - last_millis_reset) > RF_PERIOD_RESET_MS) {
- rf_reset();
- last_millis_reset = new_millis;
- }
+ bool success;
+ uint8_t mifareLength;
- if (rfid.available()){
- //Message received
- uint8_t *rf_bytes = rfid.getTagNumber();
+ success = rfid.readPassiveTargetID(PN532_MIFARE_ISO14443A,
+ &mifareBytes[0], &mifareLength);
+ if (success){
#ifdef DEBUG
Serial.print("RFID Tag: ");
- for (int i = 0; i < 8; i++) {
- Serial.print(rf_bytes[i], HEX);
+ for (int i = 0; i < mifareLength; i++) {
+ Serial.print(mifareBytes[i], HEX);
Serial.print(",");
}
Serial.println();
#endif //DEBUG
- unsigned long data=(long(rf_bytes[3])<<24)|(long(rf_bytes[2])<<16)|(long(rf_bytes[1])<<8)|(long(rf_bytes[0]));
+ unsigned long data=(long(mifareBytes[3])<<24)|(long(mifareBytes[2])<<16)|(long(mifareBytes[1])<<8)|(long(mifareBytes[0]));
(*p_rfid) = data;
return true;
}