![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
M2M im2ag M2PGI
Dependencies: mbed X_NUCLEO_NFC01A1 NDefLib
main.cpp@2:7183a6189b65, 2016-03-08 (annotated)
- Committer:
- ThibaudB
- Date:
- Tue Mar 08 15:32:58 2016 +0000
- Revision:
- 2:7183a6189b65
- Parent:
- 1:20d7b4998087
- Child:
- 4:47b9a09a74bf
Hello world avec NFC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ThibaudB | 2:7183a6189b65 | 1 | |
hellmak | 0:4d794bbc5de1 | 2 | #include "mbed.h" |
hellmak | 0:4d794bbc5de1 | 3 | |
ThibaudB | 2:7183a6189b65 | 4 | #include "X_NUCLEO_NFC01A1.h" |
ThibaudB | 2:7183a6189b65 | 5 | #include "NDefLib/NDefNfcTag.h" |
ThibaudB | 2:7183a6189b65 | 6 | #include "NDefLib/RecordType/RecordURI.h" |
hellmak | 0:4d794bbc5de1 | 7 | |
ThibaudB | 2:7183a6189b65 | 8 | /** |
ThibaudB | 2:7183a6189b65 | 9 | * Write a Ndef URI message linking to st.com site. |
ThibaudB | 2:7183a6189b65 | 10 | */ |
ThibaudB | 2:7183a6189b65 | 11 | int main(void){ |
ThibaudB | 2:7183a6189b65 | 12 | |
ThibaudB | 2:7183a6189b65 | 13 | //use default board pinout |
ThibaudB | 2:7183a6189b65 | 14 | I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN); |
ThibaudB | 2:7183a6189b65 | 15 | X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel,NULL, |
ThibaudB | 2:7183a6189b65 | 16 | X_NUCLEO_NFC01A1::DEFAULT_GPO_PIN,X_NUCLEO_NFC01A1::DEFAULT_RF_DISABLE_PIN, |
ThibaudB | 2:7183a6189b65 | 17 | X_NUCLEO_NFC01A1::DEFAULT_LED1_PIN,X_NUCLEO_NFC01A1::DEFAULT_LED2_PIN, |
ThibaudB | 2:7183a6189b65 | 18 | X_NUCLEO_NFC01A1::DEFAULT_LED3_PIN); |
ThibaudB | 2:7183a6189b65 | 19 | |
ThibaudB | 2:7183a6189b65 | 20 | //retrieve the NdefLib interface |
ThibaudB | 2:7183a6189b65 | 21 | NDefLib::NDefNfcTag& tag =nfcNucleo->getM24SR().getNDefTag(); |
ThibaudB | 2:7183a6189b65 | 22 | printf("System Init done: !\n\r"); |
ThibaudB | 2:7183a6189b65 | 23 | |
ThibaudB | 2:7183a6189b65 | 24 | //open the i2c session with the nfc chip |
ThibaudB | 2:7183a6189b65 | 25 | if(tag.openSession()){ |
ThibaudB | 2:7183a6189b65 | 26 | printf("Session opened\n\r"); |
ThibaudB | 2:7183a6189b65 | 27 | nfcNucleo->getLed1()=1; |
ThibaudB | 2:7183a6189b65 | 28 | |
ThibaudB | 2:7183a6189b65 | 29 | //create the NDef message and record |
ThibaudB | 2:7183a6189b65 | 30 | NDefLib::Message msg; |
ThibaudB | 2:7183a6189b65 | 31 | NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"st.com"); |
ThibaudB | 2:7183a6189b65 | 32 | msg.addRecord(&rUri); |
ThibaudB | 2:7183a6189b65 | 33 | |
ThibaudB | 2:7183a6189b65 | 34 | //write the tag |
ThibaudB | 2:7183a6189b65 | 35 | if(tag.write(msg)){ |
ThibaudB | 2:7183a6189b65 | 36 | printf("Tag written\n\r"); |
ThibaudB | 2:7183a6189b65 | 37 | nfcNucleo->getLed2()=1; |
ThibaudB | 2:7183a6189b65 | 38 | }else{ |
ThibaudB | 2:7183a6189b65 | 39 | printf("Error writing \n\r"); |
ThibaudB | 2:7183a6189b65 | 40 | }//if-else |
ThibaudB | 2:7183a6189b65 | 41 | |
ThibaudB | 2:7183a6189b65 | 42 | //close the i2c session |
ThibaudB | 2:7183a6189b65 | 43 | if(tag.closeSession()){ |
ThibaudB | 2:7183a6189b65 | 44 | printf("Session closed\n\r"); |
ThibaudB | 2:7183a6189b65 | 45 | nfcNucleo->getLed3()=1; |
ThibaudB | 2:7183a6189b65 | 46 | }else{ |
ThibaudB | 2:7183a6189b65 | 47 | printf("Error closing the session\n\r"); |
ThibaudB | 2:7183a6189b65 | 48 | }//if-else |
ThibaudB | 2:7183a6189b65 | 49 | |
ThibaudB | 2:7183a6189b65 | 50 | }else |
ThibaudB | 2:7183a6189b65 | 51 | printf("Error opening the session\n\r"); |
ThibaudB | 2:7183a6189b65 | 52 | } |