mbed OS 5 example application using X-NUCLEO-NFC02A1
Dependencies: NDefLib X_NUCLEO_NFC02A1
Fork of HelloWorld_NFC02A1 by
main.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file main.cpp 00004 * @author Central Lab 00005 * @version V1.0.0 00006 * @date 30 Aug 2016 00007 * @brief This demo writes an ndef message with an url inside. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> 00012 * 00013 * Redistribution and use in source and binary forms, with or without modification, 00014 * are permitted provided that the following conditions are met: 00015 * 1. Redistributions of source code must retain the above copyright notice, 00016 * this list of conditions and the following disclaimer. 00017 * 2. Redistributions in binary form must reproduce the above copyright notice, 00018 * this list of conditions and the following disclaimer in the documentation 00019 * and/or other materials provided with the distribution. 00020 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00021 * may be used to endorse or promote products derived from this software 00022 * without specific prior written permission. 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00025 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00026 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00028 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00029 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00030 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00032 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 ****************************************************************************** 00036 */ 00037 00038 #include "mbed.h" 00039 #include "XNucleoNFC02A1.h" 00040 #include "DevI2C.h" 00041 #include "NDefLib/NDefNfcTag.h" 00042 #include "NDefLib/RecordType/RecordURI.h" 00043 00044 static volatile bool buttonPress = false; // true when the user press the message 00045 00046 /** 00047 * Callback for button press. 00048 */ 00049 static void set_button_press() { 00050 buttonPress = true; 00051 } //if buttonPress 00052 00053 /** 00054 * Reading and printing NFC tag. 00055 */ 00056 static void read_and_print_nfc_tag(NDefLib::NDefNfcTag &tag) { 00057 using namespace NDefLib; 00058 00059 if (tag.open_session() == true) { 00060 printf("Open Session\r\n"); 00061 NDefLib::Message readMsg; 00062 00063 tag.read(&readMsg); 00064 printf("Message Read\r\n"); 00065 00066 if (readMsg.get_N_records()==0) { 00067 printf("Error Read\r\n"); 00068 } else { 00069 for (uint32_t i=0;i<readMsg.get_N_records();i++) { 00070 Record *r = readMsg[i]; 00071 //printRecord(r); 00072 RecordURI *const temp = (RecordURI*)r; 00073 printf("Read uriId: %d\r\n",temp->get_uri_id()); 00074 printf("Read uriType: %s\r\n",temp->get_uri_type().c_str()); 00075 printf("Read uriContent: %s\r\n",temp->get_content().c_str()); 00076 delete r; 00077 } //for 00078 } //if-else 00079 00080 tag.close_session(); 00081 printf("Close session\r\n"); 00082 } else { 00083 printf("Error open read Session\n\r"); 00084 } 00085 } 00086 00087 /** 00088 * Writing a Ndef URI message linking to the "st.com" website. 00089 */ 00090 int main(void) 00091 { 00092 /* Using default board pinout. */ 00093 DevI2C i2cChannel(XNucleoNFC02A1::DEFAULT_SDA_PIN,XNucleoNFC02A1::DEFAULT_SDL_PIN); 00094 XNucleoNFC02A1 *nfcNucleo = XNucleoNFC02A1::instance(i2cChannel, 00095 XNucleoNFC02A1::DEFAULT_GPO_PIN,XNucleoNFC02A1::DEFAULT_RF_DISABLE_PIN, 00096 XNucleoNFC02A1::DEFAULT_LED1_PIN,XNucleoNFC02A1::DEFAULT_LED2_PIN, 00097 XNucleoNFC02A1::DEFAULT_LED3_PIN); 00098 00099 NDefLib::NDefNfcTag& tag =nfcNucleo->get_M24LR().get_NDef_tag(); 00100 M24LR & mM24LRp = nfcNucleo->get_M24LR(); 00101 00102 /* Enabling Energy Harvesting. */ 00103 mM24LRp.enable_energy_harvesting(); 00104 00105 printf("System Initialization done: !\n\r"); 00106 00107 /* Opening the i2c session with the nfc chip. */ 00108 if (tag.open_session() == true) { 00109 printf("Session opened\n\r"); 00110 nfcNucleo->get_led1() = 1; 00111 00112 /* Creating the NDef message and record. */ 00113 NDefLib::Message msg; 00114 NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"st.com/st25"); 00115 msg.add_record(&rUri); 00116 00117 /* Writing the tag. */ 00118 if (tag.write(msg) == true) { 00119 printf("Tag written\n\r"); 00120 nfcNucleo->get_led2() = 1; 00121 } else { 00122 printf("Error writing \n\r"); 00123 nfcNucleo->get_led1() = 0; 00124 } //if-else 00125 00126 /* Closing the i2c session. */ 00127 if (tag.close_session() == true) { 00128 printf("Session closed\n\r"); 00129 nfcNucleo->get_led3() = 1; 00130 } else { 00131 printf("Error closing the session\n\r"); 00132 } //if-else 00133 } else { 00134 printf("Error opening the session\n\r"); 00135 } 00136 00137 #if defined(TARGET_STM) 00138 /* enable the button */ 00139 InterruptIn mybutton(USER_BUTTON); 00140 00141 mybutton.fall(set_button_press); 00142 00143 /* Each second change the led status and see if the user press the button. */ 00144 while(1) { 00145 if (buttonPress) { 00146 /* Writing the read message on console. */ 00147 read_and_print_nfc_tag(tag); 00148 buttonPress=false; 00149 } 00150 } 00151 #else 00152 read_and_print_nfc_tag(tag); 00153 #endif 00154 } 00155 00156 00157 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Tue Jul 26 2022 08:36:49 by 1.7.2