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/SampleAsync_countClick.cpp
- Revision:
- 17:d8d3d2088cac
- Parent:
- 15:94c98d2aa235
- Child:
- 18:d596eb3f575f
diff -r 02611ca49f48 -r d8d3d2088cac Samples/SampleAsync_countClick.cpp --- a/Samples/SampleAsync_countClick.cpp Wed Aug 31 15:14:08 2016 +0000 +++ b/Samples/SampleAsync_countClick.cpp Thu Jul 13 09:41:01 2017 +0000 @@ -36,7 +36,7 @@ #include "mbed.h" -#include "X_NUCLEO_NFC01A1.h" +#include "XNucleoNFC01A1.h" #include "NDefLib/NDefNfcTag.h" #include "MyRecord.h" @@ -49,7 +49,7 @@ /** * board where change the led status */ - X_NUCLEO_NFC01A1 *mNfcNucleo; + XNucleoNFC01A1 *mNfcNucleo; /** * message to write @@ -74,23 +74,23 @@ * @param nfcNucleo board with the leds and nfc tag * @param msg message to write */ - WriteMyRecordCallback(X_NUCLEO_NFC01A1 *nfcNucleo,NDefLib::Message &msg): + WriteMyRecordCallback(XNucleoNFC01A1 *nfcNucleo,NDefLib::Message &msg): mNfcNucleo(nfcNucleo),mMsg(msg),mCommandFinish(true), mRequestRefresh(false){} /** * start the write process */ - void updateMessage(){ + void update_message(){ if(mCommandFinish){ - mNfcNucleo->getM24SR().getNDefTag().openSession(); + mNfcNucleo->get_M24SR().get_NDef_tag().open_session(); }else// if it is doing something remember this request mRequestRefresh=true; }//updateMessage private: - void onError(){ + void on_error(){ printf("Error updating the tag\r\n"); mCommandFinish=false; } @@ -98,10 +98,10 @@ /** * when the session is open change the led status and ask to write the message */ - virtual void onSessionOpen(NDefLib::NDefNfcTag *tag,bool success){ + virtual void on_session_open(NDefLib::NDefNfcTag *tag,bool success){ if(!success) - return onError(); - mNfcNucleo->getLed1()=!mNfcNucleo->getLed1(); + return on_error(); + mNfcNucleo->get_led1()=!mNfcNucleo->get_led1(); printf("Session opened\r\n"); tag->write(mMsg); } @@ -109,17 +109,17 @@ /** * if the user ask to update the value it write again the message, otherwise close the session */ - virtual void onMessageWrite(NDefLib::NDefNfcTag *tag,bool success, + virtual void on_message_write(NDefLib::NDefNfcTag *tag,bool success, const NDefLib::Message&){ if(!success) - return onError(); - mNfcNucleo->getLed2()=!mNfcNucleo->getLed2(); + return on_error(); + mNfcNucleo->get_led2()=!mNfcNucleo->get_led2(); printf("Tag wrote\r\n"); if(mRequestRefresh){ mRequestRefresh=false; tag->write(mMsg); }else - tag->closeSession(); + tag->close_session(); //if-else }; @@ -127,14 +127,14 @@ /** * if the user ask to update the value it open a new session */ - virtual void onSessionClose(NDefLib::NDefNfcTag *tag,bool success){ + virtual void on_session_close(NDefLib::NDefNfcTag *tag,bool success){ if(!success) - return onError(); - mNfcNucleo->getLed3()=!mNfcNucleo->getLed3(); + return on_error(); + mNfcNucleo->get_led3()=!mNfcNucleo->get_led3(); printf("Session Close\r\n"); if(mRequestRefresh){ mRequestRefresh=false; - tag->openSession(); + tag->open_session(); }else mCommandFinish=true; //if-else @@ -149,11 +149,11 @@ /** * Call back called when the user press the button. */ -static void setButtonPress(){ +static void set_button_press(){ buttonPress=true; }//if buttonPress -static void setNfcEventCallback(){ +static void set_nfc_event_callback(){ nfcEvent=true; }//if buttonPress @@ -163,39 +163,44 @@ void sampleAsync_countClick() { //instance the board with the default parameters - I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN); - X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel,&setNfcEventCallback); + I2C i2cChannel(XNucleoNFC01A1::DEFAULT_SDA_PIN,XNucleoNFC01A1::DEFAULT_SDL_PIN); + XNucleoNFC01A1 *nfcNucleo = XNucleoNFC01A1::instance(i2cChannel,&set_nfc_event_callback); //set the button interrupt - InterruptIn userButton(USER_BUTTON); - //InterruptIn userButton(SW1); - userButton.fall(setButtonPress); + #if defined(TARGET_STM) + InterruptIn userButton(USER_BUTTON); + #else + InterruptIn userButton(SW2); + #endif + userButton.fall(set_button_press); - //No call back needed since default behavior is sync - //nfcNucleo->getM24SR().GetSession(); - //nfcNucleo->getM24SR().ManageI2CGPO(I2C_ANSWER_READY); //switch to async mode + M24SR &nfcTag =nfcNucleo->get_M24SR(); + + //No call back needed since default behavior is sync + if(nfcTag.get_session()==M24SR::M24SR_SUCCESS) + nfcTag.manage_I2C_GPO(M24SR::I2C_ANSWER_READY);//Set async mode //create the NDef message and record NDefLib::Message msg; MyRecord rClickCount; - msg.addRecord(&rClickCount); + msg.add_record(&rClickCount); WriteMyRecordCallback writeMyRecordCallback(nfcNucleo,msg); - nfcNucleo->getM24SR().getNDefTag().setCallback(&writeMyRecordCallback); - writeMyRecordCallback.updateMessage(); + nfcNucleo->get_M24SR().get_NDef_tag().set_callback(&writeMyRecordCallback); + writeMyRecordCallback.update_message(); while(true){ if(buttonPress){ buttonPress=false; //change the record content - rClickCount.incrementClick(); + rClickCount.increment_click(); printf("upgade Ndef message\r\n"); - writeMyRecordCallback.updateMessage(); + writeMyRecordCallback.update_message(); }//if if(nfcEvent){ nfcEvent=false; - nfcNucleo->getM24SR().ManageEvent(); + nfcNucleo->get_M24SR().manage_event(); }//if //wait next event __WFE();