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
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 .
Diff: Samples/Sample_writeAndChangeAll.cpp
- Revision:
- 11:283df715f241
- Parent:
- 9:9742acd0c015
- Child:
- 12:c4a778f52d8a
--- 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;
}
