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 .
Samples/Sample_countClick.cpp@4:f40b3505070c, 2015-12-03 (annotated)
- Committer:
- giovannivisentini
- Date:
- Thu Dec 03 13:44:40 2015 +0000
- Revision:
- 4:f40b3505070c
- Child:
- 5:3b300c8235b7
move the driver file in a separate library + split the sample in different files
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| giovannivisentini | 4:f40b3505070c | 1 | |
| giovannivisentini | 4:f40b3505070c | 2 | #include "mbed.h" |
| giovannivisentini | 4:f40b3505070c | 3 | |
| giovannivisentini | 4:f40b3505070c | 4 | #include "Type4NfcTagSTM24SR.h" |
| giovannivisentini | 4:f40b3505070c | 5 | #include "NDefLib/RecordType/RecordText.h" |
| giovannivisentini | 4:f40b3505070c | 6 | |
| giovannivisentini | 4:f40b3505070c | 7 | #include "X_NUCLEO_NFC01A1.h" |
| giovannivisentini | 4:f40b3505070c | 8 | |
| giovannivisentini | 4:f40b3505070c | 9 | class MyRecord : public NDefLib::RecordText{ |
| giovannivisentini | 4:f40b3505070c | 10 | |
| giovannivisentini | 4:f40b3505070c | 11 | private: |
| giovannivisentini | 4:f40b3505070c | 12 | char nClickStringBuffer[12]; |
| giovannivisentini | 4:f40b3505070c | 13 | uint32_t nClick; |
| giovannivisentini | 4:f40b3505070c | 14 | |
| giovannivisentini | 4:f40b3505070c | 15 | void syncTextValue(){ |
| giovannivisentini | 4:f40b3505070c | 16 | sprintf(nClickStringBuffer,"%d",nClick); |
| giovannivisentini | 4:f40b3505070c | 17 | setText(nClickStringBuffer); |
| giovannivisentini | 4:f40b3505070c | 18 | } |
| giovannivisentini | 4:f40b3505070c | 19 | |
| giovannivisentini | 4:f40b3505070c | 20 | public: |
| giovannivisentini | 4:f40b3505070c | 21 | |
| giovannivisentini | 4:f40b3505070c | 22 | MyRecord():nClick(0){ |
| giovannivisentini | 4:f40b3505070c | 23 | syncTextValue(); |
| giovannivisentini | 4:f40b3505070c | 24 | } |
| giovannivisentini | 4:f40b3505070c | 25 | |
| giovannivisentini | 4:f40b3505070c | 26 | void incrementClick(){ |
| giovannivisentini | 4:f40b3505070c | 27 | nClick++; |
| giovannivisentini | 4:f40b3505070c | 28 | syncTextValue(); |
| giovannivisentini | 4:f40b3505070c | 29 | } |
| giovannivisentini | 4:f40b3505070c | 30 | |
| giovannivisentini | 4:f40b3505070c | 31 | }; |
| giovannivisentini | 4:f40b3505070c | 32 | |
| giovannivisentini | 4:f40b3505070c | 33 | static void writeMessage(X_NUCLEO_NFC01A1 *nfcNucleo,NDefLib::Message &msg){ |
| giovannivisentini | 4:f40b3505070c | 34 | Type4NfcTagSTM24SR tag(nfcNucleo->getM24SR()); |
| giovannivisentini | 4:f40b3505070c | 35 | //open the i2c session with the nfc chip |
| giovannivisentini | 4:f40b3505070c | 36 | if(tag.openSession()){ |
| giovannivisentini | 4:f40b3505070c | 37 | nfcNucleo->getLed1()=! nfcNucleo->getLed1(); |
| giovannivisentini | 4:f40b3505070c | 38 | |
| giovannivisentini | 4:f40b3505070c | 39 | //write the tag |
| giovannivisentini | 4:f40b3505070c | 40 | if(tag.write(msg)){ |
| giovannivisentini | 4:f40b3505070c | 41 | nfcNucleo->getLed2()=!nfcNucleo->getLed2(); |
| giovannivisentini | 4:f40b3505070c | 42 | }//if |
| giovannivisentini | 4:f40b3505070c | 43 | |
| giovannivisentini | 4:f40b3505070c | 44 | //close the i2c session |
| giovannivisentini | 4:f40b3505070c | 45 | if(tag.closeSession()) |
| giovannivisentini | 4:f40b3505070c | 46 | nfcNucleo->getLed3()=!nfcNucleo->getLed3(); |
| giovannivisentini | 4:f40b3505070c | 47 | }//if open session |
| giovannivisentini | 4:f40b3505070c | 48 | }//writeMessage |
| giovannivisentini | 4:f40b3505070c | 49 | |
| giovannivisentini | 4:f40b3505070c | 50 | |
| giovannivisentini | 4:f40b3505070c | 51 | static volatile bool buttonPress=false; |
| giovannivisentini | 4:f40b3505070c | 52 | |
| giovannivisentini | 4:f40b3505070c | 53 | static void setButtonPress(){ |
| giovannivisentini | 4:f40b3505070c | 54 | buttonPress=true; |
| giovannivisentini | 4:f40b3505070c | 55 | }//if buttonPress |
| giovannivisentini | 4:f40b3505070c | 56 | |
| giovannivisentini | 4:f40b3505070c | 57 | void sample_countClick() { |
| giovannivisentini | 4:f40b3505070c | 58 | I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN); |
| giovannivisentini | 4:f40b3505070c | 59 | i2cChannel.frequency(400000); |
| giovannivisentini | 4:f40b3505070c | 60 | |
| giovannivisentini | 4:f40b3505070c | 61 | X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel); |
| giovannivisentini | 4:f40b3505070c | 62 | //create the wrapper for use the NdefLib |
| giovannivisentini | 4:f40b3505070c | 63 | Type4NfcTagSTM24SR tag(nfcNucleo->getM24SR()); |
| giovannivisentini | 4:f40b3505070c | 64 | |
| giovannivisentini | 4:f40b3505070c | 65 | InterruptIn userButton(USER_BUTTON); |
| giovannivisentini | 4:f40b3505070c | 66 | userButton.fall(setButtonPress); |
| giovannivisentini | 4:f40b3505070c | 67 | |
| giovannivisentini | 4:f40b3505070c | 68 | //create the NDef message and record |
| giovannivisentini | 4:f40b3505070c | 69 | NDefLib::Message msg; |
| giovannivisentini | 4:f40b3505070c | 70 | MyRecord rClickCount; |
| giovannivisentini | 4:f40b3505070c | 71 | msg.addRecord(&rClickCount); |
| giovannivisentini | 4:f40b3505070c | 72 | |
| giovannivisentini | 4:f40b3505070c | 73 | writeMessage(nfcNucleo,msg); |
| giovannivisentini | 4:f40b3505070c | 74 | |
| giovannivisentini | 4:f40b3505070c | 75 | while(true){ |
| giovannivisentini | 4:f40b3505070c | 76 | |
| giovannivisentini | 4:f40b3505070c | 77 | if(buttonPress){ |
| giovannivisentini | 4:f40b3505070c | 78 | rClickCount.incrementClick(); |
| giovannivisentini | 4:f40b3505070c | 79 | writeMessage(nfcNucleo,msg); |
| giovannivisentini | 4:f40b3505070c | 80 | buttonPress=false; |
| giovannivisentini | 4:f40b3505070c | 81 | }//if |
| giovannivisentini | 4:f40b3505070c | 82 | //wait next event |
| giovannivisentini | 4:f40b3505070c | 83 | __WFE(); |
| giovannivisentini | 4:f40b3505070c | 84 | }//while |
| giovannivisentini | 4:f40b3505070c | 85 | |
| giovannivisentini | 4:f40b3505070c | 86 | }//sample_countClick |
