mbed OS 5 example application using X-NUCLEO-NFC02A1
Dependencies: NDefLib X_NUCLEO_NFC02A1
Fork of HelloWorld_NFC02A1 by
Diff: main.cpp
- Revision:
- 13:17208d6d9971
- Parent:
- 12:85bf9d143843
- Child:
- 14:bc4a5dbef1e7
--- a/main.cpp Tue Jul 11 15:26:04 2017 +0000 +++ b/main.cpp Wed Jul 12 12:35:47 2017 +0000 @@ -36,133 +36,122 @@ */ #include "mbed.h" - #include "XNucleoNFC02A1.h" #include "DevI2C.h" #include "NDefLib/NDefNfcTag.h" #include "NDefLib/RecordType/RecordURI.h" -static volatile bool buttonPress=false; // true when the user press the message +static volatile bool buttonPress = false; // true when the user press the message + +/** + * Callback for button press. + */ +static void set_button_press() { + buttonPress = true; +} //if buttonPress -static void set_button_press(){ - buttonPress=true; -}//if buttonPress - - static void read_and_print_nfc_tag(NDefLib::NDefNfcTag &tag){ - using namespace NDefLib; +/** + * Reading and printing NFC tag. + */ +static void read_and_print_nfc_tag(NDefLib::NDefNfcTag &tag) { + using namespace NDefLib; - if(tag.open_session() == true){ - printf("Open Session\r\n"); - NDefLib::Message readMsg; - - tag.read(&readMsg); - printf("Message Read\r\n"); + if (tag.open_session() == true) { + printf("Open Session\r\n"); + NDefLib::Message readMsg; - if(readMsg.get_N_records()==0){ - printf("Error Read\r\n"); - }else{ - for(uint32_t i=0;i<readMsg.get_N_records();i++){ - Record *r = readMsg[i]; - // printRecord(r); - //----- - RecordURI *const temp = (RecordURI*)r; - printf("Read uriId: %d\r\n",temp->get_uri_id()); - printf("Read uriType: %s\r\n",temp->get_uri_type().c_str()); - printf("Read uriContent: %s\r\n",temp->get_content().c_str()); - //------ - delete r; - }//for - }//if-else + tag.read(&readMsg); + printf("Message Read\r\n"); - tag.close_session(); - printf("Close session\r\n"); - }else{ - printf("Error open read Session\n\r"); - } + if (readMsg.get_N_records()==0) { + printf("Error Read\r\n"); + } else { + for (uint32_t i=0;i<readMsg.get_N_records();i++) { + Record *r = readMsg[i]; + //printRecord(r); + RecordURI *const temp = (RecordURI*)r; + printf("Read uriId: %d\r\n",temp->get_uri_id()); + printf("Read uriType: %s\r\n",temp->get_uri_type().c_str()); + printf("Read uriContent: %s\r\n",temp->get_content().c_str()); + delete r; + } //for + } //if-else + + tag.close_session(); + printf("Close session\r\n"); + } else { + printf("Error open read Session\n\r"); + } } /** - * Write a Ndef URI message linking to st.com site. + * Writing a Ndef URI message linking to the "st.com" website. */ - int main(void) { - - /*use default board pinout*/ - DevI2C *i2cChannel = new DevI2C(XNucleoNFC02A1::DEFAULT_SDA_PIN,XNucleoNFC02A1::DEFAULT_SDL_PIN); + /* Using default board pinout. */ + DevI2C i2cChannel(XNucleoNFC02A1::DEFAULT_SDA_PIN,XNucleoNFC02A1::DEFAULT_SDL_PIN); XNucleoNFC02A1 *nfcNucleo = XNucleoNFC02A1::instance(i2cChannel, - XNucleoNFC02A1::DEFAULT_GPO_PIN,XNucleoNFC02A1::DEFAULT_RF_DISABLE_PIN, - XNucleoNFC02A1::DEFAULT_LED1_PIN,XNucleoNFC02A1::DEFAULT_LED2_PIN, - XNucleoNFC02A1::DEFAULT_LED3_PIN); + XNucleoNFC02A1::DEFAULT_GPO_PIN,XNucleoNFC02A1::DEFAULT_RF_DISABLE_PIN, + XNucleoNFC02A1::DEFAULT_LED1_PIN,XNucleoNFC02A1::DEFAULT_LED2_PIN, + XNucleoNFC02A1::DEFAULT_LED3_PIN); NDefLib::NDefNfcTag& tag =nfcNucleo->get_M24LR().get_NDef_tag(); M24LR & mM24LRp = nfcNucleo->get_M24LR(); - /* Enable Energy Harvesting */ + /* Enabling Energy Harvesting. */ mM24LRp.enable_energy_harvesting(); printf("System Initialization done: !\n\r"); - /*open the i2c session with the nfc chip*/ - if(tag.open_session() == true) - { + /* Opening the i2c session with the nfc chip. */ + if (tag.open_session() == true) { printf("Session opened\n\r"); - nfcNucleo->get_led1()=1; + nfcNucleo->get_led1() = 1; - /*create the NDef message and record*/ + /* Creating the NDef message and record. */ NDefLib::Message msg; NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"st.com/st25"); msg.add_record(&rUri); - /*write the tag*/ - if(tag.write(msg) == true) - { + /* Writing the tag. */ + if (tag.write(msg) == true) { printf("Tag written\n\r"); - nfcNucleo->get_led2()=1; - } - else - { + nfcNucleo->get_led2() = 1; + } else { printf("Error writing \n\r"); - nfcNucleo->get_led1()=0; - }//if-else - - /*close the i2c session*/ - if(tag.close_session() == true) - { - printf("Session closed\n\r"); - nfcNucleo->get_led3()=1; - } - else - { - printf("Error closing the session\n\r"); - }//if-else + nfcNucleo->get_led1() = 0; + } //if-else + /* Closing the i2c session. */ + if (tag.close_session() == true) { + printf("Session closed\n\r"); + nfcNucleo->get_led3() = 1; + } else { + printf("Error closing the session\n\r"); + } //if-else + } else { + printf("Error opening the session\n\r"); } - else - printf("Error opening the session\n\r"); - + #if defined(TARGET_STM) - /* enable the button */ - InterruptIn mybutton(USER_BUTTON); - - mybutton.fall(set_button_press); + /* enable the button */ + InterruptIn mybutton(USER_BUTTON); + + mybutton.fall(set_button_press); - /*each second change the led status and see if the user press the button*/ - while(1) - { - if(buttonPress) - { - /*write the read message on console*/ - read_and_print_nfc_tag(tag); - buttonPress=false; - } + /* Each second change the led status and see if the user press the button. */ + while(1) { + if (buttonPress) { + /* Writing the read message on console. */ + read_and_print_nfc_tag(tag); + buttonPress=false; } + } #else - read_and_print_nfc_tag(tag); + read_and_print_nfc_tag(tag); #endif } - - /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/