Merge pull request #162 from Rotzbua/example-codestyle

Upd some examples codestyle
Everything looks good, thank you @Rotzbua
This commit is contained in:
Miki Balboa
2016-01-03 23:40:07 -08:00
5 changed files with 123 additions and 157 deletions

View File

@@ -1,28 +1,15 @@
/* /*
* MFRC522 - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT. * --------------------------------------------------------------------------------------------------------------------
* The library file MFRC522.h has a wealth of useful info. Please read it. * Example to change UID of changeable MIFARE card.
* The functions are documented in MFRC522.cpp. * --------------------------------------------------------------------------------------------------------------------
* * This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
* Based on code Dr.Leong ( WWW.B2CQSHOP.COM )
* Created by Miguel Balboa (circuitito.com), Jan, 2012.
* Rewritten by Søren Thing Andersen (access.thing.dk), fall of 2013 (Translation to English, refactored, comments, anti collision, cascade levels.)
* Extended by Tom Clement with functionality to write to sector 0 of UID changeable Mifare cards.
* *
* Released into the public domain.
*
* This sample shows how to set the UID on a UID changeable MIFARE card. * This sample shows how to set the UID on a UID changeable MIFARE card.
* NOTE: for more informations read the README.rst
* *
----------------------------------------------------------------------------- empty_skull * @author Tom Clement
* @license Released into the public domain.
- Aggiunti pin per arduino Mega *
- Scritto semplice codice per la scrittura e lettura
- add pin configuration for arduino mega
- write simple read/write Code for new entry user
http://mac86project.altervista.org/
----------------------------------------------------------------------------- Nicola Coppola
* Typical pin layout used: * Typical pin layout used:
* ----------------------------------------------------------------------------------------- * -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino * MFRC522 Arduino Arduino Arduino Arduino Arduino
@@ -34,91 +21,91 @@
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* The reader can be found on eBay for around 5 dollars. Search for "mf-rc522" on ebay.com.
*/ */
#include <SPI.h> #include <SPI.h>
#include <MFRC522.h> #include <MFRC522.h>
#define RST_PIN 9 // Configurable, see typical pin layout above
#define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
/* Set your new UID here! */ /* Set your new UID here! */
#define NEW_UID {0xDE, 0xAD, 0xBE, 0xEF} #define NEW_UID {0xDE, 0xAD, 0xBE, 0xEF}
#define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key; MFRC522::MIFARE_Key key;
void setup() { void setup() {
Serial.begin(9600); // Initialize serial communications with the PC Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Warning: this example overwrites the UID of your UID changeable card, use with care!")); Serial.println(F("Warning: this example overwrites the UID of your UID changeable card, use with care!"));
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory. // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
for (byte i = 0; i < 6; i++) { for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF; key.keyByte[i] = 0xFF;
} }
} }
// Setting the UID can be as simple as this: // Setting the UID can be as simple as this:
//void loop() { //void loop() {
// byte newUid[] = NEW_UID; // byte newUid[] = NEW_UID;
// if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) { // if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
// Serial.println("Wrote new UID to card."); // Serial.println("Wrote new UID to card.");
// } // }
// delay(1000); // delay(1000);
//} //}
// But of course this is a more proper approach // But of course this is a more proper approach
void loop() { void loop() {
// Look for new cards, and select one if present // Look for new cards, and select one if present
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) { if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
delay(50); delay(50);
return; return;
} }
// Now a card is selected. The UID and SAK is in mfrc522.uid. // Now a card is selected. The UID and SAK is in mfrc522.uid.
// Dump UID // Dump UID
Serial.print(F("Card UID:")); Serial.print(F("Card UID:"));
for (byte i = 0; i < mfrc522.uid.size; i++) { for (byte i = 0; i < mfrc522.uid.size; i++) {
Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(mfrc522.uid.uidByte[i], HEX); Serial.print(mfrc522.uid.uidByte[i], HEX);
} }
Serial.println(); Serial.println();
// Dump PICC type // Dump PICC type
// MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); // MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak);
// Serial.print(F("PICC type: ")); // Serial.print(F("PICC type: "));
// Serial.print(mfrc522.PICC_GetTypeName(piccType)); // Serial.print(mfrc522.PICC_GetTypeName(piccType));
// Serial.print(F(" (SAK ")); // Serial.print(F(" (SAK "));
// Serial.print(mfrc522.uid.sak); // Serial.print(mfrc522.uid.sak);
// Serial.print(")\r\n"); // Serial.print(")\r\n");
// if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI // if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI
// && piccType != MFRC522::PICC_TYPE_MIFARE_1K // && piccType != MFRC522::PICC_TYPE_MIFARE_1K
// && piccType != MFRC522::PICC_TYPE_MIFARE_4K) { // && piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
// Serial.println(F("This sample only works with MIFARE Classic cards.")); // Serial.println(F("This sample only works with MIFARE Classic cards."));
// return; // return;
// } // }
// Set new UID // Set new UID
byte newUid[] = NEW_UID; byte newUid[] = NEW_UID;
if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) { if ( mfrc522.MIFARE_SetUid(newUid, (byte)4, true) ) {
Serial.println(F("Wrote new UID to card.")); Serial.println(F("Wrote new UID to card."));
} }
// Halt PICC and re-select it so DumpToSerial doesn't get confused // Halt PICC and re-select it so DumpToSerial doesn't get confused
mfrc522.PICC_HaltA(); mfrc522.PICC_HaltA();
if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) { if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial() ) {
return; return;
} }
// Dump the new memory contents // Dump the new memory contents
Serial.println(F("New UID and contents:")); Serial.println(F("New UID and contents:"));
mfrc522.PICC_DumpToSerial(&(mfrc522.uid)); mfrc522.PICC_DumpToSerial(&(mfrc522.uid));
delay(2000); delay(2000);
} }

View File

@@ -1,29 +1,24 @@
/* /*
* ---------------------------------------------------------------------------- * --------------------------------------------------------------------------------------------------------------------
* This is a MFRC522 library example; see https://github.com/miguelbalboa/rfid * Example sketch/program showing how to read data from a PICC to serial.
* for further details and other examples. * --------------------------------------------------------------------------------------------------------------------
* This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
* *
* NOTE: The library file MFRC522.h has a lot of useful info. Please read it. * Example sketch/program showing how to read data from a PICC (that is: a RFID Tag or Card) using a MFRC522 based RFID
* Reader on the Arduino SPI interface.
* *
* Released into the public domain. * When the Arduino and the MFRC522 module are connected (see the pin layout below), load this sketch into Arduino IDE
* ---------------------------------------------------------------------------- * then verify/compile and upload it. To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). When
* Example sketch/program showing how to read data from a PICC (that is: a RFID * you present a PICC (that is: a RFID Tag or Card) at reading distance of the MFRC522 Reader/PCD, the serial output
* Tag or Card) using a MFRC522 based RFID Reader on the Arduino SPI interface. * will show the ID/UID, type and any data blocks it can read. Note: you may see "Timeout in communication" messages
* when removing the PICC from reading distance too early.
* *
* When the Arduino and the MFRC522 module are connected (see the pin layout * If your reader supports it, this sketch/program will read all the PICCs presented (that is: multiple tag reading).
* below), load this sketch into Arduino IDE then verify/compile and upload it. * So if you stack two or more PICCs on top of each other and present them to the reader, it will first output all
* To see the output: use Tools, Serial Monitor of the IDE (hit Ctrl+Shft+M). * details of the first and then the next PICC. Note that this may take some time as all data blocks are dumped, so
* When you present a PICC (that is: a RFID Tag or Card) at reading distance * keep the PICCs at reading distance until complete.
* of the MFRC522 Reader/PCD, the serial output will show the ID/UID, type and
* any data blocks it can read. Note: you may see "Timeout in communication"
* messages when removing the PICC from reading distance too early.
* *
* If your reader supports it, this sketch/program will read all the PICCs * @license Released into the public domain.
* presented (that is: multiple tag reading). So if you stack two or more
* PICCs on top of each other and present them to the reader, it will first
* output all details of the first and then the next PICC. Note that this
* may take some time as all data blocks are dumped, so keep the PICCs at
* reading distance until complete.
* *
* Typical pin layout used: * Typical pin layout used:
* ----------------------------------------------------------------------------------------- * -----------------------------------------------------------------------------------------

View File

@@ -1,28 +1,14 @@
/* /*
* MFRC522 - Library to use ARDUINO RFID MODULE KIT 13.56 MHZ WITH TAGS SPI W AND R BY COOQROBOT. * --------------------------------------------------------------------------------------------------------------------
* The library file MFRC522.h has a wealth of useful info. Please read it. * Example sketch/program to fix a broken UID changeable MIFARE cards.
* The functions are documented in MFRC522.cpp. * --------------------------------------------------------------------------------------------------------------------
* * This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
* Based on code Dr.Leong ( WWW.B2CQSHOP.COM )
* Created by Miguel Balboa (circuitito.com), Jan, 2012.
* Rewritten by Søren Thing Andersen (access.thing.dk), fall of 2013 (Translation to English, refactored, comments, anti collision, cascade levels.)
* Extended by Tom Clement with functionality to write to sector 0 of UID changeable Mifare cards.
* *
* Released into the public domain. * This sample shows how to fix a broken UID changeable MIFARE cards that have a corrupted sector 0.
* *
* This sample shows how to fix a UID changeable MIFARE cards that have a corrupted sector 0. * @author Tom Clement
* @license Released into the public domain.
* *
----------------------------------------------------------------------------- empty_skull
- Aggiunti pin per arduino Mega
- Scritto semplice codice per la scrittura e lettura
- add pin configuration for arduino mega
- write simple read/write Code for new entry user
http://mac86project.altervista.org/
----------------------------------------------------------------------------- Nicola Coppola
* Typical pin layout used: * Typical pin layout used:
* ----------------------------------------------------------------------------------------- * -----------------------------------------------------------------------------------------
* MFRC522 Arduino Arduino Arduino Arduino Arduino * MFRC522 Arduino Arduino Arduino Arduino Arduino
@@ -34,35 +20,34 @@
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* The reader can be found on eBay for around 5 dollars. Search for "mf-rc522" on ebay.com.
*/ */
#include <SPI.h> #include <SPI.h>
#include <MFRC522.h> #include <MFRC522.h>
#define SS_PIN 10 #define RST_PIN 9 // Configurable, see typical pin layout above
#define RST_PIN 9 #define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522::MIFARE_Key key; MFRC522::MIFARE_Key key;
void setup() { void setup() {
Serial.begin(9600); // Initialize serial communications with the PC Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4) while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
SPI.begin(); // Init SPI bus SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Warning: this example clears your mifare UID, use with care!")); Serial.println(F("Warning: this example clears your mifare UID, use with care!"));
// Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory. // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory.
for (byte i = 0; i < 6; i++) { for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF; key.keyByte[i] = 0xFF;
} }
} }
void loop() { void loop() {
if ( mfrc522.MIFARE_UnbrickUidSector(false) ) { if ( mfrc522.MIFARE_UnbrickUidSector(false) ) {
Serial.println(F("Cleared sector 0, set UID to 1234. Card should be responsive again now.")); Serial.println(F("Cleared sector 0, set UID to 1234. Card should be responsive again now."));
} }
delay(1000); delay(1000);
} }

View File

@@ -6,10 +6,10 @@
* *
* This example test the firmware of your MFRC522 reader module, only known version can be checked. If the test passed * This example test the firmware of your MFRC522 reader module, only known version can be checked. If the test passed
* it do not mean that your module is faultless! Some modules have bad or broken antennas or the PICC is broken. * it do not mean that your module is faultless! Some modules have bad or broken antennas or the PICC is broken.
*
* NOTE: for more informations read the README.rst * NOTE: for more informations read the README.rst
* *
* Released into the public domain. * @author Rotzbua
* @license Released into the public domain.
* *
* Typical pin layout used: * Typical pin layout used:
* ----------------------------------------------------------------------------------------- * -----------------------------------------------------------------------------------------
@@ -22,8 +22,6 @@
* SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
* SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
* SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*
* @author Rotzbua
*/ */
#include <SPI.h> #include <SPI.h>

View File

@@ -22,13 +22,14 @@
#include <SPI.h> #include <SPI.h>
#include <MFRC522.h> #include <MFRC522.h>
#define SS_PIN 10 //Arduino Uno #define RST_PIN 9 // Configurable, see typical pin layout above
#define RST_PIN 9 #define SS_PIN 10 // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
void setup() { void setup() {
Serial.begin(9600); // Initialize serial communications with the PC Serial.begin(9600); // Initialize serial communications with the PC
SPI.begin(); // Init SPI bus SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card mfrc522.PCD_Init(); // Init MFRC522 card
Serial.println(F("Write personal data on a MIFARE PICC ")); Serial.println(F("Write personal data on a MIFARE PICC "));
} }