M2M im2ag M2PGI
Dependencies: mbed X_NUCLEO_NFC01A1 NDefLib
Diff: main.cpp
- Revision:
- 2:7183a6189b65
- Parent:
- 1:20d7b4998087
- Child:
- 4:47b9a09a74bf
diff -r 20d7b4998087 -r 7183a6189b65 main.cpp --- a/main.cpp Tue Mar 08 15:14:42 2016 +0000 +++ b/main.cpp Tue Mar 08 15:32:58 2016 +0000 @@ -1,12 +1,52 @@ + #include "mbed.h" -DigitalOut myled(LED1); +#include "X_NUCLEO_NFC01A1.h" +#include "NDefLib/NDefNfcTag.h" +#include "NDefLib/RecordType/RecordURI.h" -int main() { - while(1) { - myled = 1; // LED is ON - wait(0.1); // 200 ms - myled = 0; // LED is OFF - wait(0.1); // 1 sec - } -} +/** + * Write a Ndef URI message linking to st.com site. + */ +int main(void){ + + //use default board pinout + I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN); + X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel,NULL, + X_NUCLEO_NFC01A1::DEFAULT_GPO_PIN,X_NUCLEO_NFC01A1::DEFAULT_RF_DISABLE_PIN, + X_NUCLEO_NFC01A1::DEFAULT_LED1_PIN,X_NUCLEO_NFC01A1::DEFAULT_LED2_PIN, + X_NUCLEO_NFC01A1::DEFAULT_LED3_PIN); + + //retrieve the NdefLib interface + NDefLib::NDefNfcTag& tag =nfcNucleo->getM24SR().getNDefTag(); + printf("System Init done: !\n\r"); + + //open the i2c session with the nfc chip + if(tag.openSession()){ + printf("Session opened\n\r"); + nfcNucleo->getLed1()=1; + + //create the NDef message and record + NDefLib::Message msg; + NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"st.com"); + msg.addRecord(&rUri); + + //write the tag + if(tag.write(msg)){ + printf("Tag written\n\r"); + nfcNucleo->getLed2()=1; + }else{ + printf("Error writing \n\r"); + }//if-else + + //close the i2c session + if(tag.closeSession()){ + printf("Session closed\n\r"); + nfcNucleo->getLed3()=1; + }else{ + printf("Error closing the session\n\r"); + }//if-else + + }else + printf("Error opening the session\n\r"); +} \ No newline at end of file