From f526a42efc861a3e85262c862e83126c261457b4 Mon Sep 17 00:00:00 2001 From: Rotzbua Date: Fri, 7 Apr 2017 10:24:03 +0200 Subject: [PATCH] upd code style of examples --- .../rfid_read_personal_data.ino | 52 ++-- .../rfid_write_personal_data.ino | 232 +++++++++--------- 2 files changed, 142 insertions(+), 142 deletions(-) diff --git a/examples/rfid_read_personal_data/rfid_read_personal_data.ino b/examples/rfid_read_personal_data/rfid_read_personal_data.ino index 48fafb2..5271cd1 100644 --- a/examples/rfid_read_personal_data/rfid_read_personal_data.ino +++ b/examples/rfid_read_personal_data/rfid_read_personal_data.ino @@ -1,32 +1,31 @@ /* - Initial Author: ryand1011 (https://github.com/ryand1011) - - Reads data written by a program such as "rfid_write_personal_data.ino" - - See: https://github.com/miguelbalboa/rfid/tree/master/examples/rfid_write_personal_data - - Uses MIFARE RFID card using RFID-RC522 reader - Uses MFRC522 - Library - ----------------------------------------------------------------------------------------- - MFRC522 Arduino Arduino Arduino Arduino Arduino - Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro - Signal Pin Pin Pin Pin Pin Pin - ----------------------------------------------------------------------------------------- - RST/Reset RST 9 5 D9 RESET/ICSP-5 RST - SPI SS SDA(SS) 10 53 D10 10 10 - SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 - SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 - SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 - + * Initial Author: ryand1011 (https://github.com/ryand1011) + * + * Reads data written by a program such as "rfid_write_personal_data.ino" + * + * See: https://github.com/miguelbalboa/rfid/tree/master/examples/rfid_write_personal_data + * + * Uses MIFARE RFID card using RFID-RC522 reader + * Uses MFRC522 - Library + * ----------------------------------------------------------------------------------------- + * MFRC522 Arduino Arduino Arduino Arduino Arduino + * Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro + * Signal Pin Pin Pin Pin Pin Pin + * ----------------------------------------------------------------------------------------- + * RST/Reset RST 9 5 D9 RESET/ICSP-5 RST + * SPI SS SDA(SS) 10 53 D10 10 10 + * SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16 + * SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14 + * SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15 */ #include #include -#define RST_PIN 9 // Configurable, see typical pin layout above -#define SS_PIN 10 // Configurable, see typical pin layout above +#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 object 'card' +MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance //*****************************************************************************************// void setup() { @@ -50,12 +49,12 @@ void loop() { //------------------------------------------- - //look for card + // Look for new cards if ( ! mfrc522.PICC_IsNewCardPresent()) { return; } - //select a card + // Select one of the cards if ( ! mfrc522.PICC_ReadCardSerial()) { return; } @@ -70,7 +69,7 @@ void loop() { //------------------------------------------- - Serial.print("Name: "); + Serial.print(F("Name: ")); byte buffer1[18]; @@ -122,8 +121,7 @@ void loop() { } //PRINT LAST NAME - for (uint8_t i = 0; i < 16; i++) - { + for (uint8_t i = 0; i < 16; i++) { Serial.write(buffer2[i] ); } diff --git a/examples/rfid_write_personal_data/rfid_write_personal_data.ino b/examples/rfid_write_personal_data/rfid_write_personal_data.ino index 30972d8..75bb2fb 100644 --- a/examples/rfid_write_personal_data/rfid_write_personal_data.ino +++ b/examples/rfid_write_personal_data/rfid_write_personal_data.ino @@ -28,127 +28,129 @@ MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance void setup() { - Serial.begin(9600); // Initialize serial communications with the PC - SPI.begin(); // Init SPI bus - mfrc522.PCD_Init(); // Init MFRC522 card - Serial.println(F("Write personal data on a MIFARE PICC ")); + Serial.begin(9600); // Initialize serial communications with the PC + SPI.begin(); // Init SPI bus + mfrc522.PCD_Init(); // Init MFRC522 card + Serial.println(F("Write personal data on a MIFARE PICC ")); } void loop() { - - // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory. - MFRC522::MIFARE_Key key; - for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF; - - // Look for new cards - if ( ! mfrc522.PICC_IsNewCardPresent()) { - return; - } - // Select one of the cards - if ( ! mfrc522.PICC_ReadCardSerial()) return; - - Serial.print(F("Card UID:")); //Dump UID - for (byte i = 0; i < mfrc522.uid.size; i++) { - Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); - Serial.print(mfrc522.uid.uidByte[i], HEX); - } - Serial.print(F(" PICC type: ")); // Dump PICC type - MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); - Serial.println(mfrc522.PICC_GetTypeName(piccType)); - - byte buffer[34]; - byte block; - MFRC522::StatusCode status; - byte len; - - Serial.setTimeout(20000L) ; // wait until 20 seconds for input from serial - // Ask personal data: Family name - Serial.println(F("Type Family name, ending with #")); - len=Serial.readBytesUntil('#', (char *) buffer, 30) ; // read family name from serial - for (byte i = len; i < 30; i++) buffer[i] = ' '; // pad with spaces - - block = 1; - //Serial.println(F("Authenticating using key A...")); - status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); - if (status != MFRC522::STATUS_OK) { - Serial.print(F("PCD_Authenticate() failed: ")); - Serial.println(mfrc522.GetStatusCodeName(status)); - return; - } - else Serial.println(F("PCD_Authenticate() success: ")); - - // Write block - status = mfrc522.MIFARE_Write(block, buffer, 16); - if (status != MFRC522::STATUS_OK) { - Serial.print(F("MIFARE_Write() failed: ")); - Serial.println(mfrc522.GetStatusCodeName(status)); - return; - } - else Serial.println(F("MIFARE_Write() success: ")); + // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory. + MFRC522::MIFARE_Key key; + for (byte i = 0; i < 6; i++) key.keyByte[i] = 0xFF; - block = 2; - //Serial.println(F("Authenticating using key A...")); - status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); - if (status != MFRC522::STATUS_OK) { - Serial.print(F("PCD_Authenticate() failed: ")); - Serial.println(mfrc522.GetStatusCodeName(status)); - return; - } - - // Write block - status = mfrc522.MIFARE_Write(block, &buffer[16], 16); - if (status != MFRC522::STATUS_OK) { - Serial.print(F("MIFARE_Write() failed: ")); - Serial.println(mfrc522.GetStatusCodeName(status)); - return; - } - else Serial.println(F("MIFARE_Write() success: ")); + // Look for new cards + if ( ! mfrc522.PICC_IsNewCardPresent()) { + return; + } - // Ask personal data: First name - Serial.println(F("Type First name, ending with #")); - len=Serial.readBytesUntil('#', (char *) buffer, 20) ; // read first name from serial - for (byte i = len; i < 20; i++) buffer[i] = ' '; // pad with spaces - - block = 4; - //Serial.println(F("Authenticating using key A...")); - status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); - if (status != MFRC522::STATUS_OK) { - Serial.print(F("PCD_Authenticate() failed: ")); - Serial.println(mfrc522.GetStatusCodeName(status)); - return; - } - - // Write block - status = mfrc522.MIFARE_Write(block, buffer, 16); - if (status != MFRC522::STATUS_OK) { - Serial.print(F("MIFARE_Write() failed: ")); - Serial.println(mfrc522.GetStatusCodeName(status)); - return; - } - else Serial.println(F("MIFARE_Write() success: ")); + // Select one of the cards + if ( ! mfrc522.PICC_ReadCardSerial()) { + return; + } - block = 5; - //Serial.println(F("Authenticating using key A...")); - status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); - if (status != MFRC522::STATUS_OK) { - Serial.print(F("PCD_Authenticate() failed: ")); - Serial.println(mfrc522.GetStatusCodeName(status)); - return; - } - - // Write block - status = mfrc522.MIFARE_Write(block, &buffer[16], 16); - if (status != MFRC522::STATUS_OK) { - Serial.print(F("MIFARE_Write() failed: ")); - Serial.println(mfrc522.GetStatusCodeName(status)); - return; - } - else Serial.println(F("MIFARE_Write() success: ")); + Serial.print(F("Card UID:")); //Dump UID + for (byte i = 0; i < mfrc522.uid.size; i++) { + Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); + Serial.print(mfrc522.uid.uidByte[i], HEX); + } + Serial.print(F(" PICC type: ")); // Dump PICC type + MFRC522::PICC_Type piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); + Serial.println(mfrc522.PICC_GetTypeName(piccType)); + + byte buffer[34]; + byte block; + MFRC522::StatusCode status; + byte len; + + Serial.setTimeout(20000L) ; // wait until 20 seconds for input from serial + // Ask personal data: Family name + Serial.println(F("Type Family name, ending with #")); + len = Serial.readBytesUntil('#', (char *) buffer, 30) ; // read family name from serial + for (byte i = len; i < 30; i++) buffer[i] = ' '; // pad with spaces + + block = 1; + //Serial.println(F("Authenticating using key A...")); + status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); + if (status != MFRC522::STATUS_OK) { + Serial.print(F("PCD_Authenticate() failed: ")); + Serial.println(mfrc522.GetStatusCodeName(status)); + return; + } + else Serial.println(F("PCD_Authenticate() success: ")); + + // Write block + status = mfrc522.MIFARE_Write(block, buffer, 16); + if (status != MFRC522::STATUS_OK) { + Serial.print(F("MIFARE_Write() failed: ")); + Serial.println(mfrc522.GetStatusCodeName(status)); + return; + } + else Serial.println(F("MIFARE_Write() success: ")); + + block = 2; + //Serial.println(F("Authenticating using key A...")); + status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); + if (status != MFRC522::STATUS_OK) { + Serial.print(F("PCD_Authenticate() failed: ")); + Serial.println(mfrc522.GetStatusCodeName(status)); + return; + } + + // Write block + status = mfrc522.MIFARE_Write(block, &buffer[16], 16); + if (status != MFRC522::STATUS_OK) { + Serial.print(F("MIFARE_Write() failed: ")); + Serial.println(mfrc522.GetStatusCodeName(status)); + return; + } + else Serial.println(F("MIFARE_Write() success: ")); + + // Ask personal data: First name + Serial.println(F("Type First name, ending with #")); + len = Serial.readBytesUntil('#', (char *) buffer, 20) ; // read first name from serial + for (byte i = len; i < 20; i++) buffer[i] = ' '; // pad with spaces + + block = 4; + //Serial.println(F("Authenticating using key A...")); + status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); + if (status != MFRC522::STATUS_OK) { + Serial.print(F("PCD_Authenticate() failed: ")); + Serial.println(mfrc522.GetStatusCodeName(status)); + return; + } + + // Write block + status = mfrc522.MIFARE_Write(block, buffer, 16); + if (status != MFRC522::STATUS_OK) { + Serial.print(F("MIFARE_Write() failed: ")); + Serial.println(mfrc522.GetStatusCodeName(status)); + return; + } + else Serial.println(F("MIFARE_Write() success: ")); + + block = 5; + //Serial.println(F("Authenticating using key A...")); + status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(mfrc522.uid)); + if (status != MFRC522::STATUS_OK) { + Serial.print(F("PCD_Authenticate() failed: ")); + Serial.println(mfrc522.GetStatusCodeName(status)); + return; + } + + // Write block + status = mfrc522.MIFARE_Write(block, &buffer[16], 16); + if (status != MFRC522::STATUS_OK) { + Serial.print(F("MIFARE_Write() failed: ")); + Serial.println(mfrc522.GetStatusCodeName(status)); + return; + } + else Serial.println(F("MIFARE_Write() success: ")); - Serial.println(" "); - mfrc522.PICC_HaltA(); // Halt PICC - mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD - -} + Serial.println(" "); + mfrc522.PICC_HaltA(); // Halt PICC + mfrc522.PCD_StopCrypto1(); // Stop encryption on PCD + +} \ No newline at end of file