![](/media/cache/group/ST_logo_2020_blue_V_cmyk.jpg.50x50_q85.jpg)
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/SampleAsync_countClick.cpp@20:ba95e0dc5975, 2017-08-21 (annotated)
- Committer:
- giovannivisentini
- Date:
- Mon Aug 21 12:12:30 2017 +0000
- Revision:
- 20:ba95e0dc5975
- Parent:
- 18:d596eb3f575f
update NDefLib and mbed libs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
giovannivisentini | 13:685d95525ec8 | 1 | /** |
giovannivisentini | 13:685d95525ec8 | 2 | ****************************************************************************** |
Davidroid | 18:d596eb3f575f | 3 | * @file SampleAsync_countClick.cpp |
giovannivisentini | 13:685d95525ec8 | 4 | * @author ST / Central Labs |
giovannivisentini | 13:685d95525ec8 | 5 | * @date 03 Dic 2015 |
giovannivisentini | 13:685d95525ec8 | 6 | * @brief This demo define a custom record that contains the number of time the user |
giovannivisentini | 13:685d95525ec8 | 7 | * press the user button |
giovannivisentini | 13:685d95525ec8 | 8 | ****************************************************************************** |
giovannivisentini | 13:685d95525ec8 | 9 | * |
giovannivisentini | 13:685d95525ec8 | 10 | * COPYRIGHT(c) 2015 STMicroelectronics |
giovannivisentini | 13:685d95525ec8 | 11 | * |
giovannivisentini | 13:685d95525ec8 | 12 | * Redistribution and use in source and binary forms, with or without modification, |
giovannivisentini | 13:685d95525ec8 | 13 | * are permitted provided that the following conditions are met: |
giovannivisentini | 13:685d95525ec8 | 14 | * 1. Redistributions of source code must retain the above copyright notice, |
giovannivisentini | 13:685d95525ec8 | 15 | * this list of conditions and the following disclaimer. |
giovannivisentini | 13:685d95525ec8 | 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
giovannivisentini | 13:685d95525ec8 | 17 | * this list of conditions and the following disclaimer in the documentation |
giovannivisentini | 13:685d95525ec8 | 18 | * and/or other materials provided with the distribution. |
giovannivisentini | 13:685d95525ec8 | 19 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
giovannivisentini | 13:685d95525ec8 | 20 | * may be used to endorse or promote products derived from this software |
giovannivisentini | 13:685d95525ec8 | 21 | * without specific prior written permission. |
giovannivisentini | 13:685d95525ec8 | 22 | * |
giovannivisentini | 13:685d95525ec8 | 23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
giovannivisentini | 13:685d95525ec8 | 24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
giovannivisentini | 13:685d95525ec8 | 25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
giovannivisentini | 13:685d95525ec8 | 26 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
giovannivisentini | 13:685d95525ec8 | 27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
giovannivisentini | 13:685d95525ec8 | 28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
giovannivisentini | 13:685d95525ec8 | 29 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
giovannivisentini | 13:685d95525ec8 | 30 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
giovannivisentini | 13:685d95525ec8 | 31 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
giovannivisentini | 13:685d95525ec8 | 32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
giovannivisentini | 13:685d95525ec8 | 33 | * |
giovannivisentini | 13:685d95525ec8 | 34 | ****************************************************************************** |
giovannivisentini | 13:685d95525ec8 | 35 | */ |
giovannivisentini | 13:685d95525ec8 | 36 | |
giovannivisentini | 13:685d95525ec8 | 37 | #include "mbed.h" |
giovannivisentini | 13:685d95525ec8 | 38 | |
giovannivisentini | 17:d8d3d2088cac | 39 | #include "XNucleoNFC01A1.h" |
giovannivisentini | 13:685d95525ec8 | 40 | #include "NDefLib/NDefNfcTag.h" |
giovannivisentini | 13:685d95525ec8 | 41 | |
giovannivisentini | 13:685d95525ec8 | 42 | #include "MyRecord.h" |
giovannivisentini | 13:685d95525ec8 | 43 | |
giovannivisentini | 13:685d95525ec8 | 44 | /** |
giovannivisentini | 13:685d95525ec8 | 45 | * class with the callback needed to update the record in the tag |
giovannivisentini | 13:685d95525ec8 | 46 | */ |
giovannivisentini | 14:3b604972b89f | 47 | class WriteMyRecordCallback : public NDefLib::NDefNfcTag::Callbacks { |
giovannivisentini | 13:685d95525ec8 | 48 | |
giovannivisentini | 13:685d95525ec8 | 49 | /** |
giovannivisentini | 13:685d95525ec8 | 50 | * board where change the led status |
giovannivisentini | 13:685d95525ec8 | 51 | */ |
giovannivisentini | 17:d8d3d2088cac | 52 | XNucleoNFC01A1 *mNfcNucleo; |
giovannivisentini | 13:685d95525ec8 | 53 | |
giovannivisentini | 13:685d95525ec8 | 54 | /** |
giovannivisentini | 13:685d95525ec8 | 55 | * message to write |
giovannivisentini | 13:685d95525ec8 | 56 | */ |
giovannivisentini | 13:685d95525ec8 | 57 | NDefLib::Message &mMsg; |
giovannivisentini | 13:685d95525ec8 | 58 | |
giovannivisentini | 13:685d95525ec8 | 59 | /** |
giovannivisentini | 13:685d95525ec8 | 60 | * true if the message is wrote in the tag |
giovannivisentini | 13:685d95525ec8 | 61 | */ |
giovannivisentini | 13:685d95525ec8 | 62 | bool mCommandFinish; |
giovannivisentini | 13:685d95525ec8 | 63 | |
giovannivisentini | 13:685d95525ec8 | 64 | /** |
giovannivisentini | 13:685d95525ec8 | 65 | * true if an update request is done while writing a tag |
giovannivisentini | 13:685d95525ec8 | 66 | */ |
giovannivisentini | 13:685d95525ec8 | 67 | bool mRequestRefresh; |
giovannivisentini | 13:685d95525ec8 | 68 | |
giovannivisentini | 13:685d95525ec8 | 69 | |
giovannivisentini | 13:685d95525ec8 | 70 | public: |
giovannivisentini | 13:685d95525ec8 | 71 | |
giovannivisentini | 13:685d95525ec8 | 72 | /** |
giovannivisentini | 13:685d95525ec8 | 73 | * build an object write an message into a nfc tag |
giovannivisentini | 13:685d95525ec8 | 74 | * @param nfcNucleo board with the leds and nfc tag |
giovannivisentini | 13:685d95525ec8 | 75 | * @param msg message to write |
giovannivisentini | 13:685d95525ec8 | 76 | */ |
giovannivisentini | 17:d8d3d2088cac | 77 | WriteMyRecordCallback(XNucleoNFC01A1 *nfcNucleo,NDefLib::Message &msg): |
giovannivisentini | 13:685d95525ec8 | 78 | mNfcNucleo(nfcNucleo),mMsg(msg),mCommandFinish(true), |
giovannivisentini | 13:685d95525ec8 | 79 | mRequestRefresh(false){} |
giovannivisentini | 13:685d95525ec8 | 80 | |
giovannivisentini | 13:685d95525ec8 | 81 | /** |
giovannivisentini | 13:685d95525ec8 | 82 | * start the write process |
giovannivisentini | 13:685d95525ec8 | 83 | */ |
giovannivisentini | 17:d8d3d2088cac | 84 | void update_message(){ |
giovannivisentini | 13:685d95525ec8 | 85 | if(mCommandFinish){ |
giovannivisentini | 17:d8d3d2088cac | 86 | mNfcNucleo->get_M24SR().get_NDef_tag().open_session(); |
Davidroid | 18:d596eb3f575f | 87 | }else{// if it is doing something remember this request |
giovannivisentini | 13:685d95525ec8 | 88 | mRequestRefresh=true; |
Davidroid | 18:d596eb3f575f | 89 | } |
giovannivisentini | 13:685d95525ec8 | 90 | }//updateMessage |
giovannivisentini | 13:685d95525ec8 | 91 | |
giovannivisentini | 13:685d95525ec8 | 92 | private: |
giovannivisentini | 13:685d95525ec8 | 93 | |
giovannivisentini | 17:d8d3d2088cac | 94 | void on_error(){ |
giovannivisentini | 15:94c98d2aa235 | 95 | printf("Error updating the tag\r\n"); |
giovannivisentini | 13:685d95525ec8 | 96 | mCommandFinish=false; |
giovannivisentini | 13:685d95525ec8 | 97 | } |
giovannivisentini | 13:685d95525ec8 | 98 | |
giovannivisentini | 13:685d95525ec8 | 99 | /** |
giovannivisentini | 13:685d95525ec8 | 100 | * when the session is open change the led status and ask to write the message |
giovannivisentini | 13:685d95525ec8 | 101 | */ |
giovannivisentini | 17:d8d3d2088cac | 102 | virtual void on_session_open(NDefLib::NDefNfcTag *tag,bool success){ |
giovannivisentini | 13:685d95525ec8 | 103 | if(!success) |
giovannivisentini | 17:d8d3d2088cac | 104 | return on_error(); |
giovannivisentini | 17:d8d3d2088cac | 105 | mNfcNucleo->get_led1()=!mNfcNucleo->get_led1(); |
giovannivisentini | 15:94c98d2aa235 | 106 | printf("Session opened\r\n"); |
giovannivisentini | 13:685d95525ec8 | 107 | tag->write(mMsg); |
giovannivisentini | 13:685d95525ec8 | 108 | } |
giovannivisentini | 13:685d95525ec8 | 109 | |
giovannivisentini | 13:685d95525ec8 | 110 | /** |
giovannivisentini | 14:3b604972b89f | 111 | * if the user ask to update the value it write again the message, otherwise close the session |
giovannivisentini | 13:685d95525ec8 | 112 | */ |
giovannivisentini | 20:ba95e0dc5975 | 113 | virtual void on_message_write(NDefLib::NDefNfcTag *tag,bool success){ |
Davidroid | 18:d596eb3f575f | 114 | if(!success) { |
giovannivisentini | 17:d8d3d2088cac | 115 | return on_error(); |
Davidroid | 18:d596eb3f575f | 116 | } |
giovannivisentini | 17:d8d3d2088cac | 117 | mNfcNucleo->get_led2()=!mNfcNucleo->get_led2(); |
giovannivisentini | 15:94c98d2aa235 | 118 | printf("Tag wrote\r\n"); |
giovannivisentini | 13:685d95525ec8 | 119 | if(mRequestRefresh){ |
giovannivisentini | 13:685d95525ec8 | 120 | mRequestRefresh=false; |
giovannivisentini | 13:685d95525ec8 | 121 | tag->write(mMsg); |
Davidroid | 18:d596eb3f575f | 122 | }else { |
giovannivisentini | 17:d8d3d2088cac | 123 | tag->close_session(); |
Davidroid | 18:d596eb3f575f | 124 | } |
giovannivisentini | 13:685d95525ec8 | 125 | //if-else |
giovannivisentini | 13:685d95525ec8 | 126 | |
giovannivisentini | 13:685d95525ec8 | 127 | }; |
giovannivisentini | 13:685d95525ec8 | 128 | |
giovannivisentini | 13:685d95525ec8 | 129 | /** |
giovannivisentini | 14:3b604972b89f | 130 | * if the user ask to update the value it open a new session |
giovannivisentini | 13:685d95525ec8 | 131 | */ |
giovannivisentini | 17:d8d3d2088cac | 132 | virtual void on_session_close(NDefLib::NDefNfcTag *tag,bool success){ |
giovannivisentini | 13:685d95525ec8 | 133 | if(!success) |
giovannivisentini | 17:d8d3d2088cac | 134 | return on_error(); |
giovannivisentini | 17:d8d3d2088cac | 135 | mNfcNucleo->get_led3()=!mNfcNucleo->get_led3(); |
giovannivisentini | 15:94c98d2aa235 | 136 | printf("Session Close\r\n"); |
giovannivisentini | 13:685d95525ec8 | 137 | if(mRequestRefresh){ |
giovannivisentini | 13:685d95525ec8 | 138 | mRequestRefresh=false; |
giovannivisentini | 17:d8d3d2088cac | 139 | tag->open_session(); |
Davidroid | 18:d596eb3f575f | 140 | }else { |
giovannivisentini | 13:685d95525ec8 | 141 | mCommandFinish=true; |
Davidroid | 18:d596eb3f575f | 142 | } |
giovannivisentini | 13:685d95525ec8 | 143 | //if-else |
giovannivisentini | 13:685d95525ec8 | 144 | }; |
giovannivisentini | 13:685d95525ec8 | 145 | |
giovannivisentini | 13:685d95525ec8 | 146 | }; |
giovannivisentini | 13:685d95525ec8 | 147 | |
giovannivisentini | 13:685d95525ec8 | 148 | |
giovannivisentini | 13:685d95525ec8 | 149 | static volatile bool buttonPress=false; /// true when the user press the message |
giovannivisentini | 13:685d95525ec8 | 150 | static volatile bool nfcEvent=false; /// true when the user press the message |
giovannivisentini | 13:685d95525ec8 | 151 | |
giovannivisentini | 13:685d95525ec8 | 152 | /** |
giovannivisentini | 13:685d95525ec8 | 153 | * Call back called when the user press the button. |
giovannivisentini | 13:685d95525ec8 | 154 | */ |
giovannivisentini | 17:d8d3d2088cac | 155 | static void set_button_press(){ |
giovannivisentini | 13:685d95525ec8 | 156 | buttonPress=true; |
giovannivisentini | 13:685d95525ec8 | 157 | }//if buttonPress |
giovannivisentini | 13:685d95525ec8 | 158 | |
giovannivisentini | 17:d8d3d2088cac | 159 | static void set_nfc_event_callback(){ |
giovannivisentini | 13:685d95525ec8 | 160 | nfcEvent=true; |
giovannivisentini | 13:685d95525ec8 | 161 | }//if buttonPress |
giovannivisentini | 13:685d95525ec8 | 162 | |
giovannivisentini | 13:685d95525ec8 | 163 | /** |
giovannivisentini | 13:685d95525ec8 | 164 | * Write a custom record that count how many times the user press the button. |
giovannivisentini | 13:685d95525ec8 | 165 | */ |
giovannivisentini | 13:685d95525ec8 | 166 | void sampleAsync_countClick() { |
giovannivisentini | 13:685d95525ec8 | 167 | |
giovannivisentini | 13:685d95525ec8 | 168 | //instance the board with the default parameters |
giovannivisentini | 17:d8d3d2088cac | 169 | I2C i2cChannel(XNucleoNFC01A1::DEFAULT_SDA_PIN,XNucleoNFC01A1::DEFAULT_SDL_PIN); |
giovannivisentini | 17:d8d3d2088cac | 170 | XNucleoNFC01A1 *nfcNucleo = XNucleoNFC01A1::instance(i2cChannel,&set_nfc_event_callback); |
giovannivisentini | 13:685d95525ec8 | 171 | |
giovannivisentini | 13:685d95525ec8 | 172 | //set the button interrupt |
giovannivisentini | 17:d8d3d2088cac | 173 | #if defined(TARGET_STM) |
giovannivisentini | 17:d8d3d2088cac | 174 | InterruptIn userButton(USER_BUTTON); |
giovannivisentini | 17:d8d3d2088cac | 175 | #else |
giovannivisentini | 17:d8d3d2088cac | 176 | InterruptIn userButton(SW2); |
giovannivisentini | 17:d8d3d2088cac | 177 | #endif |
giovannivisentini | 17:d8d3d2088cac | 178 | userButton.fall(set_button_press); |
giovannivisentini | 13:685d95525ec8 | 179 | |
giovannivisentini | 17:d8d3d2088cac | 180 | M24SR &nfcTag =nfcNucleo->get_M24SR(); |
giovannivisentini | 17:d8d3d2088cac | 181 | |
giovannivisentini | 17:d8d3d2088cac | 182 | //No call back needed since default behavior is sync |
Davidroid | 18:d596eb3f575f | 183 | if(nfcTag.get_session()==M24SR::M24SR_SUCCESS) { |
giovannivisentini | 17:d8d3d2088cac | 184 | nfcTag.manage_I2C_GPO(M24SR::I2C_ANSWER_READY);//Set async mode |
Davidroid | 18:d596eb3f575f | 185 | } |
giovannivisentini | 13:685d95525ec8 | 186 | |
giovannivisentini | 13:685d95525ec8 | 187 | //create the NDef message and record |
giovannivisentini | 13:685d95525ec8 | 188 | NDefLib::Message msg; |
giovannivisentini | 13:685d95525ec8 | 189 | MyRecord rClickCount; |
giovannivisentini | 17:d8d3d2088cac | 190 | msg.add_record(&rClickCount); |
giovannivisentini | 13:685d95525ec8 | 191 | |
giovannivisentini | 13:685d95525ec8 | 192 | WriteMyRecordCallback writeMyRecordCallback(nfcNucleo,msg); |
giovannivisentini | 13:685d95525ec8 | 193 | |
giovannivisentini | 17:d8d3d2088cac | 194 | nfcNucleo->get_M24SR().get_NDef_tag().set_callback(&writeMyRecordCallback); |
giovannivisentini | 17:d8d3d2088cac | 195 | writeMyRecordCallback.update_message(); |
giovannivisentini | 13:685d95525ec8 | 196 | while(true){ |
giovannivisentini | 13:685d95525ec8 | 197 | |
giovannivisentini | 13:685d95525ec8 | 198 | if(buttonPress){ |
giovannivisentini | 13:685d95525ec8 | 199 | buttonPress=false; |
giovannivisentini | 13:685d95525ec8 | 200 | //change the record content |
giovannivisentini | 17:d8d3d2088cac | 201 | rClickCount.increment_click(); |
giovannivisentini | 15:94c98d2aa235 | 202 | printf("upgade Ndef message\r\n"); |
giovannivisentini | 17:d8d3d2088cac | 203 | writeMyRecordCallback.update_message(); |
giovannivisentini | 13:685d95525ec8 | 204 | }//if |
giovannivisentini | 13:685d95525ec8 | 205 | if(nfcEvent){ |
giovannivisentini | 13:685d95525ec8 | 206 | nfcEvent=false; |
giovannivisentini | 17:d8d3d2088cac | 207 | nfcNucleo->get_M24SR().manage_event(); |
giovannivisentini | 13:685d95525ec8 | 208 | }//if |
giovannivisentini | 13:685d95525ec8 | 209 | //wait next event |
giovannivisentini | 13:685d95525ec8 | 210 | __WFE(); |
giovannivisentini | 13:685d95525ec8 | 211 | }//while |
giovannivisentini | 13:685d95525ec8 | 212 | |
giovannivisentini | 13:685d95525ec8 | 213 | }//sample_countClick |