This application provides a set of demos with X-NUCLEO-NFC01A1 expansion board.

Dependencies:   NDefLib X_NUCLEO_NFC01A1 mbed

Fork of X-MBED-NFC1 by Giovanni Visentini

This application provides a set of demos with X-NUCLEO-NFC01A1 expansion board.

The available demos are:

  • SAMPLE_WRITE_URL: write a tag with the ST home page URL
  • SAMPLE_COUNT_CLICK: create a custom tag to count and report the user button clicks.
  • SAMPLE_WRITE_AND_CHANGE_ALL: write a tag with all the supported records and update the tag contents when the user button is pressed.
  • SAMPLE_LOCK_TAG_CONTENT: use the M24SR component API to set the NFC tag as read-only.

To enable the different demos comment/uncomment the SAMPLE_* macros provided in main.cpp .

Files at this revision

API Documentation at this revision

Comitter:
giovannivisentini
Date:
Mon Jan 11 16:10:12 2016 +0000
Parent:
10:ca6f3293ecfb
Child:
12:c4a778f52d8a
Commit message:
update comments 2

Changed in this revision

Samples/Sample_countClick.cpp Show annotated file Show diff for this revision Revisions of this file
Samples/Sample_writeAndChangeAll.cpp Show annotated file Show diff for this revision Revisions of this file
Samples/Samples.h Show annotated file Show diff for this revision Revisions of this file
Samples/sample_lockWriteTag.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Samples/Sample_countClick.cpp	Mon Jan 11 13:53:53 2016 +0000
+++ b/Samples/Sample_countClick.cpp	Mon Jan 11 16:10:12 2016 +0000
@@ -40,13 +40,18 @@
 #include "NDefLib/NDefNfcTag.h"
 #include "NDefLib/RecordType/RecordText.h"
 
-
+/**
+ * castom record that map an integer in a text record
+ */
 class MyRecord : public NDefLib::RecordText{
     
 private:
-    char nClickStringBuffer[12];
-    uint32_t nClick;
+    char nClickStringBuffer[12]; ///buffer used for print the number
+    uint32_t nClick; /// number to show
     
+    /**
+     * convert the number in char and update the tag content
+     */
     void syncTextValue(){
         sprintf(nClickStringBuffer,"%d",nClick);
         setText(nClickStringBuffer);
@@ -58,6 +63,9 @@
         syncTextValue();
     }
     
+    /**
+     * increment the stored number and update the racord content
+     */
     void incrementClick(){
         nClick++;
         syncTextValue();
@@ -65,6 +73,10 @@
 
 };
 
+/* write a nfc message into a tag
+ * @param nfcNucleo board where write the data
+ * @param msg message to write
+ */
 static void writeMessage(X_NUCLEO_NFC01A1 *nfcNucleo,NDefLib::Message &msg){
     NDefLib::NDefNfcTag& tag = nfcNucleo->getM24SR().getNDefTag();
     //open the i2c session with the nfc chip
@@ -83,19 +95,28 @@
 }//writeMessage
 
 
-static volatile bool buttonPress=false;
+static volatile bool buttonPress=false; /// true when the user press the message
 
+/**
+ * call back called when the user press the button
+ */
 static void setButtonPress(){
     buttonPress=true;
 }//if buttonPress
 
+/**
+ * Write a castum record that count how many times the user press the button
+ */
 void sample_countClick() {
+    
+    //instance the board with the default paramiters
     I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN);
-
     X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel);
+    
     //get the wrapper for use the NdefLib
     NDefLib::NDefNfcTag& tag = nfcNucleo->getM24SR().getNDefTag();
     
+    //set the button interrupt
     InterruptIn userButton(USER_BUTTON);    
     userButton.fall(setButtonPress);
     
@@ -109,8 +130,11 @@
     while(true){
         
         if(buttonPress){
+            //change the record content
             rClickCount.incrementClick();
-            writeMessage(nfcNucleo,msg);            
+            //write the new record content
+            writeMessage(nfcNucleo,msg);  
+            //wait a new button press          
             buttonPress=false;
         }//if
         //wait next event
--- a/Samples/Sample_writeAndChangeAll.cpp	Mon Jan 11 13:53:53 2016 +0000
+++ b/Samples/Sample_writeAndChangeAll.cpp	Mon Jan 11 16:10:12 2016 +0000
@@ -49,8 +49,12 @@
 
 #include "X_NUCLEO_NFC01A1.h"
 
-static Serial pc(SERIAL_TX, SERIAL_RX);
+static Serial pc(SERIAL_TX, SERIAL_RX); ///serial console
+
 
+/**
+ * shift the led status between the 3 leds
+ */
 static void shiftLed(DigitalOut &led1,DigitalOut &led2,DigitalOut &led3){
     const uint8_t prevLed1=led1;
     const uint8_t prevLed2=led2;
@@ -60,6 +64,10 @@
     led3=prevLed2;
 }
 
+/**
+ * Create a message that contains all the possible records, and write it in the tag
+ * @param tag nfc tag where write the message
+ */
 static void setNFCTag(NDefLib::NDefNfcTag &tag){
 
     bool writeStatus,closeStatus;
@@ -126,48 +134,52 @@
         pc.printf("CloseFail\n\r");
 }
 
-static void printRecord(NDefLib::Record *r){
+/**
+ * Print the record content
+ * @param r record to print
+ */
+static void printRecord(NDefLib::Record *const r){
     using namespace NDefLib;
     switch(r->getType()){
         case Record::TYPE_TEXT: {
-            RecordText *temp = (RecordText*)r;
+            const RecordText *const temp = ( RecordText* )r;
             pc.printf("Read Text: %s\r\n",temp->getText().c_str());
             break; }
         case Record::TYPE_AAR:{
-            RecordAAR *temp = (RecordAAR*)r;
+            const RecordAAR *const temp = ( RecordAAR* )r;
             pc.printf("Read ARR: %s\r\n",temp->getPackage().c_str());
             break; }
         case Record::TYPE_MIME:{
-            RecordMimeType *temp = (RecordMimeType*)r;
+            const RecordMimeType *const temp = ( RecordMimeType* )r;
             pc.printf("Read mimeType: %s\r\n",temp->getMimeType().c_str());
             pc.printf("Read mimeData: %s\r\n",
                     std::string((const char*)temp->getMimeData(),
                             temp->getMimeDataLenght()).c_str());
             break;}
         case Record::TYPE_URI:{
-            RecordURI *temp = (RecordURI*)r;
+            RecordURI *const temp = (RecordURI*)r;
             pc.printf("Read uriId: %d\r\n",temp->getUriId());
             pc.printf("Read uriType: %s\r\n",temp->getUriType().c_str());
             pc.printf("Read uriContent: %s\r\n",temp->getContent().c_str());
             break;}
         case Record::TYPE_URI_MAIL:{
-            RecordMail *temp = (RecordMail*)r;
+            const RecordMail*const temp = (RecordMail*)r;
             pc.printf("Read Dest: %s\r\n",temp->getToAddress().c_str());
             pc.printf("Read Subject: %s\r\n",temp->getSubject().c_str());
             pc.printf("Read Body: %s\r\n",temp->getBody().c_str());
             break;}
         case Record::TYPE_URI_SMS:{
-            RecordSMS *temp = (RecordSMS*)r;
+            const RecordSMS*const temp = (RecordSMS*)r;
             pc.printf("Read number: %s\r\n",temp->getNumber().c_str());
             pc.printf("Read message: %s\r\n",temp->getMessagge().c_str());
             break;}
         case Record::TYPE_URI_GEOLOCATION:{
-            RecordGeo *temp = (RecordGeo*)r;
+            const RecordGeo*const temp = (RecordGeo*)r;
             pc.printf("Read lat: %f\r\n",temp->getLatitude());
             pc.printf("Read long: %f\r\n",temp->getLongitude());
             break;}
         case Record::TYPE_MIME_VCARD:{
-            const RecordVCard *temp = (RecordVCard*)r;
+            const RecordVCard *const temp = (RecordVCard*)r;
             pc.printf("Read Name: %s\r\n",(*temp)[RecordVCard::NAME].c_str());
             pc.printf("Read Mail: %s\r\n",(*temp)[RecordVCard::EMAIL_WORK].c_str());
             pc.printf("Read ORG: %s\r\n",(*temp)[RecordVCard::ORGANIZATION].c_str());
@@ -178,7 +190,11 @@
     }//switch
 }
 
-static void changeRecord(NDefLib::Record *r){
+/**
+ * change the record content
+ * @param r record to change
+ */
+static void changeRecord(NDefLib::Record const* r){
     using namespace NDefLib;
     switch(r->getType()){
         case Record::TYPE_TEXT: {
@@ -224,6 +240,10 @@
     }//switch
 }
 
+/**
+ * read the nfc message and print the content on the serial console
+ * @param tag nfc tag where read the content
+ */
 static void readNfcTag(NDefLib::NDefNfcTag &tag){
     using namespace NDefLib;
 
@@ -248,7 +268,10 @@
     }
 }
 
-
+/**
+ * read a nfc message, change the content of each record and write the new message
+ * @param tag tag where read and write the nfc message
+ */
 static void changeNfcTag(NDefLib::NDefNfcTag &tag){
     using NDefLib::Record;
     using NDefLib::Message;
@@ -274,36 +297,53 @@
     }
 }
 
-static volatile bool buttonPress=false;
+static volatile bool buttonPress=false; /// true when the user press the message
 
+/**
+ * call back called when the user press the button
+ */
 static void setButtonPress(){
     buttonPress=true;
-}
+}//if buttonPress
 
+/**
+ * write a message and when the user press the button it read the message, change it and update it
+ */
 void sample_writeAndChangeAll() {
-    I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN);
-
     DigitalOut nucleoLed(LED1);
-
+    
+    //instance the board with the default paramiters
+    I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN);
     X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel);
+    
+    //get the wrapper for use the NdefLib
     NDefLib::NDefNfcTag& tag = nfcNucleo->getM24SR().getNDefTag();
-    InterruptIn mybutton(USER_BUTTON);
     
+    //switch on the first led    
     nfcNucleo->getLed1()=1;
     nfcNucleo->getLed2()=0;
     nfcNucleo->getLed3()=0;
 
+    //write the message
     setNFCTag(tag);
+    //read the message and write it on console
     readNfcTag(tag);
 
+    //enable the button
+    InterruptIn mybutton(USER_BUTTON);
     mybutton.fall(setButtonPress);
 
+    //each second change the led status and see if the user press the button
     while(true) {
         wait(1);
+        //update the status
         nucleoLed = !nucleoLed;
         shiftLed(nfcNucleo->getLed1(),nfcNucleo->getLed2(),nfcNucleo->getLed3());
+        
         if(buttonPress){
+            //update the message content
             changeNfcTag(tag);
+            //write the new message on console
             readNfcTag(tag);
             buttonPress=false;
         }
--- a/Samples/Samples.h	Mon Jan 11 13:53:53 2016 +0000
+++ b/Samples/Samples.h	Mon Jan 11 16:10:12 2016 +0000
@@ -35,8 +35,24 @@
 #ifndef __SAMPLES_H__
 #define __SAMPLES_H__
 
+/**
+ * Wirte a tag with a web site link
+ */
 void sample_writeUrl();
+
+/**
+ * Write a castom tag that count how many times the user press the button
+ */
 void sample_countClick();
+
+/**
+ * Write a message with all the tag, when the user press the button read the message and change the
+ * tag content
+ */
 void sample_writeAndChangeAll();
+
+/**
+ * Set the tag as read only when the user pres the button
+ */
 void sample_lockTagContent();
 #endif /* __SAMPLES_H__ */
\ No newline at end of file
--- a/Samples/sample_lockWriteTag.cpp	Mon Jan 11 13:53:53 2016 +0000
+++ b/Samples/sample_lockWriteTag.cpp	Mon Jan 11 16:10:12 2016 +0000
@@ -38,8 +38,11 @@
 
 #include "X_NUCLEO_NFC01A1.h"
 
-static volatile bool buttonPress=false;
+static volatile bool buttonPress=false; /// true when the user press the message
 
+/**
+ * call back called when the user press the button
+ */
 static void setButtonPress(){
     buttonPress=true;
 }//if buttonPress
@@ -92,34 +95,39 @@
 		return false;
 }
 
+/**
+ * When the user press the button enable/disable the tag write protetion
+ */
 void sample_lockTagContent() {
 	Serial console(SERIAL_TX, SERIAL_RX);
-
+	
+    //instance the board with the default paramiters
     I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN);
-
     X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel);
 	
+	//get the wrapper for use the NdefLib
 	NDefLib::NDefNfcTag& tag = nfcNucleo->getM24SR().getNDefTag();
 
     //when the user press the button set buttonPress to true
     InterruptIn userButton(USER_BUTTON);
     userButton.fall(setButtonPress);
     
-    //restore the board as read/write
+    //restore the board as read/write, set the led1 if it is ok
     tag.openSession(true);
-    nfcNucleo->getLed1()=nfcNucleo->getM24SR().disableAllPassword()==NFC_SUCCESS;
+    nfcNucleo->getLed1()= (nfcNucleo->getM24SR().disableAllPassword()==NFC_SUCCESS);
     tag.closeSession();
+    
+	// now we are in read/write -> switch on the writable led    
     getReadOnlyLed(nfcNucleo)=false;
     getWriteableLed(nfcNucleo)=true;
     bool tagIsLock=false;
 
-
     while(true){
 
         if(buttonPress){
         	if(tag.openSession()){
         		bool changeStatusResult;
-
+				//switch the status
 				if(tagIsLock)
 					changeStatusResult=enableReadOnly(nfcNucleo);
 				else