Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NDefLib X_NUCLEO_NFC01A1 mbed
Fork of X-MBED-NFC1 by
SampleAsync_countClick.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file SampleAsync_countClick.cpp 00004 * @author ST / Central Labs 00005 * @date 03 Dic 2015 00006 * @brief This demo define a custom record that contains the number of time the user 00007 * press the user button 00008 ****************************************************************************** 00009 * 00010 * COPYRIGHT(c) 2015 STMicroelectronics 00011 * 00012 * Redistribution and use in source and binary forms, with or without modification, 00013 * are permitted provided that the following conditions are met: 00014 * 1. Redistributions of source code must retain the above copyright notice, 00015 * this list of conditions and the following disclaimer. 00016 * 2. Redistributions in binary form must reproduce the above copyright notice, 00017 * this list of conditions and the following disclaimer in the documentation 00018 * and/or other materials provided with the distribution. 00019 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00020 * may be used to endorse or promote products derived from this software 00021 * without specific prior written permission. 00022 * 00023 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00024 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00025 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00026 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00027 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00028 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00029 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00031 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00032 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 * 00034 ****************************************************************************** 00035 */ 00036 00037 #include "mbed.h" 00038 00039 #include "XNucleoNFC01A1.h" 00040 #include "NDefLib/NDefNfcTag.h" 00041 00042 #include "MyRecord.h" 00043 00044 /** 00045 * class with the callback needed to update the record in the tag 00046 */ 00047 class WriteMyRecordCallback : public NDefLib::NDefNfcTag::Callbacks { 00048 00049 /** 00050 * board where change the led status 00051 */ 00052 XNucleoNFC01A1 *mNfcNucleo; 00053 00054 /** 00055 * message to write 00056 */ 00057 NDefLib::Message &mMsg; 00058 00059 /** 00060 * true if the message is wrote in the tag 00061 */ 00062 bool mCommandFinish; 00063 00064 /** 00065 * true if an update request is done while writing a tag 00066 */ 00067 bool mRequestRefresh; 00068 00069 00070 public: 00071 00072 /** 00073 * build an object write an message into a nfc tag 00074 * @param nfcNucleo board with the leds and nfc tag 00075 * @param msg message to write 00076 */ 00077 WriteMyRecordCallback(XNucleoNFC01A1 *nfcNucleo,NDefLib::Message &msg): 00078 mNfcNucleo(nfcNucleo),mMsg(msg),mCommandFinish(true), 00079 mRequestRefresh(false){} 00080 00081 /** 00082 * start the write process 00083 */ 00084 void update_message(){ 00085 if(mCommandFinish){ 00086 mNfcNucleo->get_M24SR().get_NDef_tag().open_session(); 00087 }else{// if it is doing something remember this request 00088 mRequestRefresh=true; 00089 } 00090 }//updateMessage 00091 00092 private: 00093 00094 void on_error(){ 00095 printf("Error updating the tag\r\n"); 00096 mCommandFinish=false; 00097 } 00098 00099 /** 00100 * when the session is open change the led status and ask to write the message 00101 */ 00102 virtual void on_session_open(NDefLib::NDefNfcTag *tag,bool success){ 00103 if(!success) 00104 return on_error(); 00105 mNfcNucleo->get_led1()=!mNfcNucleo->get_led1(); 00106 printf("Session opened\r\n"); 00107 tag->write(mMsg); 00108 } 00109 00110 /** 00111 * if the user ask to update the value it write again the message, otherwise close the session 00112 */ 00113 virtual void on_message_write(NDefLib::NDefNfcTag *tag,bool success){ 00114 if(!success) { 00115 return on_error(); 00116 } 00117 mNfcNucleo->get_led2()=!mNfcNucleo->get_led2(); 00118 printf("Tag wrote\r\n"); 00119 if(mRequestRefresh){ 00120 mRequestRefresh=false; 00121 tag->write(mMsg); 00122 }else { 00123 tag->close_session(); 00124 } 00125 //if-else 00126 00127 }; 00128 00129 /** 00130 * if the user ask to update the value it open a new session 00131 */ 00132 virtual void on_session_close(NDefLib::NDefNfcTag *tag,bool success){ 00133 if(!success) 00134 return on_error(); 00135 mNfcNucleo->get_led3()=!mNfcNucleo->get_led3(); 00136 printf("Session Close\r\n"); 00137 if(mRequestRefresh){ 00138 mRequestRefresh=false; 00139 tag->open_session(); 00140 }else { 00141 mCommandFinish=true; 00142 } 00143 //if-else 00144 }; 00145 00146 }; 00147 00148 00149 static volatile bool buttonPress=false; /// true when the user press the message 00150 static volatile bool nfcEvent=false; /// true when the user press the message 00151 00152 /** 00153 * Call back called when the user press the button. 00154 */ 00155 static void set_button_press(){ 00156 buttonPress=true; 00157 }//if buttonPress 00158 00159 static void set_nfc_event_callback(){ 00160 nfcEvent=true; 00161 }//if buttonPress 00162 00163 /** 00164 * Write a custom record that count how many times the user press the button. 00165 */ 00166 void sampleAsync_countClick() { 00167 00168 //instance the board with the default parameters 00169 I2C i2cChannel(XNucleoNFC01A1::DEFAULT_SDA_PIN,XNucleoNFC01A1::DEFAULT_SDL_PIN); 00170 XNucleoNFC01A1 *nfcNucleo = XNucleoNFC01A1::instance(i2cChannel,&set_nfc_event_callback); 00171 00172 //set the button interrupt 00173 #if defined(TARGET_STM) 00174 InterruptIn userButton(USER_BUTTON); 00175 #else 00176 InterruptIn userButton(SW2); 00177 #endif 00178 userButton.fall(set_button_press); 00179 00180 M24SR &nfcTag =nfcNucleo->get_M24SR(); 00181 00182 //No call back needed since default behavior is sync 00183 if(nfcTag.get_session()==M24SR::M24SR_SUCCESS) { 00184 nfcTag.manage_I2C_GPO(M24SR::I2C_ANSWER_READY);//Set async mode 00185 } 00186 00187 //create the NDef message and record 00188 NDefLib::Message msg; 00189 MyRecord rClickCount; 00190 msg.add_record(&rClickCount); 00191 00192 WriteMyRecordCallback writeMyRecordCallback(nfcNucleo,msg); 00193 00194 nfcNucleo->get_M24SR().get_NDef_tag().set_callback(&writeMyRecordCallback); 00195 writeMyRecordCallback.update_message(); 00196 while(true){ 00197 00198 if(buttonPress){ 00199 buttonPress=false; 00200 //change the record content 00201 rClickCount.increment_click(); 00202 printf("upgade Ndef message\r\n"); 00203 writeMyRecordCallback.update_message(); 00204 }//if 00205 if(nfcEvent){ 00206 nfcEvent=false; 00207 nfcNucleo->get_M24SR().manage_event(); 00208 }//if 00209 //wait next event 00210 __WFE(); 00211 }//while 00212 00213 }//sample_countClick
Generated on Wed Jul 13 2022 03:11:25 by
1.7.2
