Thijs Buuron / Mbed 2 deprecated HelloWorld_NFC02A1laatste

Dependencies:   NDefLib X_NUCLEO_NFC02A1 hdc1080 mbed

Fork of HelloWorld_NFC02A1 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file       main.cpp
00004   * @author     AMG 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>&copy; 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 #include "NDefLib/RecordType/RecordText.h"
00044 //Pin Defines for I2C Bus
00045 #define D_SDA                  D14 // specific for Nucleo-F303K8
00046 #define D_SCL                  D15 // specific for Nucleo-F303K8
00047 I2C hdc_i2c(D_SDA, D_SCL);
00048 #include "hdc1080.h"
00049 static volatile bool buttonPress = false; // true when the user press the message
00050 
00051 /**
00052  * Callback for button press.
00053  */
00054 static void set_button_press() {
00055   buttonPress = true;
00056 } //if buttonPress
00057 
00058 /**
00059  * Reading and printing NFC tag.
00060  */
00061 static void read_and_print_nfc_tag(NDefLib::NDefNfcTag &tag) {
00062   using namespace NDefLib;
00063 
00064   if (tag.open_session() == true) {
00065     printf("Open Session\r\n");
00066     NDefLib::Message readMsg;
00067 
00068     tag.read(&readMsg);
00069     printf("Message Read\r\n");
00070 
00071     if (readMsg.get_N_records()==0) {
00072       printf("Error Read\r\n");
00073     } else {
00074       for (uint32_t i=0;i<readMsg.get_N_records();i++) {
00075         Record *r = readMsg[i];
00076         //printRecord(r);
00077         RecordURI *const temp = (RecordURI*)r;
00078         printf("Read uriId: %d\r\n",temp->get_uri_id());
00079         printf("Read uriType: %s\r\n",temp->get_uri_type().c_str());
00080         printf("Read uriContent: %s\r\n",temp->get_content().c_str());
00081         delete r;
00082       } //for
00083     } //if-else
00084 
00085     tag.close_session();
00086     printf("Close session\r\n");
00087   } else {
00088     printf("Error open read Session\n\r");
00089   }
00090 }
00091 
00092 /**
00093  * Writing a Ndef URI message linking to the "st.com" website.
00094  */
00095 int main(void)
00096 {
00097   /* Using default board pinout. */
00098   DevI2C i2cChannel(XNucleoNFC02A1::DEFAULT_SDA_PIN,XNucleoNFC02A1::DEFAULT_SDL_PIN);
00099   XNucleoNFC02A1 *nfcNucleo = XNucleoNFC02A1::instance(i2cChannel,
00100                                                        XNucleoNFC02A1::DEFAULT_GPO_PIN,XNucleoNFC02A1::DEFAULT_RF_DISABLE_PIN,
00101                                                        XNucleoNFC02A1::DEFAULT_LED1_PIN,XNucleoNFC02A1::DEFAULT_LED2_PIN,
00102                                                        XNucleoNFC02A1::DEFAULT_LED3_PIN);
00103   
00104   NDefLib::NDefNfcTag& tag =nfcNucleo->get_M24LR().get_NDef_tag();
00105   M24LR & mM24LRp = nfcNucleo->get_M24LR();
00106   
00107   /* Enabling Energy Harvesting. */
00108   mM24LRp.enable_energy_harvesting();
00109   
00110   printf("System Initialization done: !\n\r");
00111   
00112   /* Opening the i2c session with the nfc chip. */
00113   if (tag.open_session() == true) {
00114     printf("Session opened\n\r");
00115     nfcNucleo->get_led1() = 1;
00116     
00117     /* Creating the NDef message and record. */
00118     NDefLib::Message msg;
00119     NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"st.com/test");
00120     msg.add_record(&rUri);
00121     
00122     /* Writing the tag. */
00123     if (tag.write(msg) == true) {
00124       printf("Tag written\n\r");
00125       nfcNucleo->get_led2() = 1;
00126     } else {
00127       printf("Error writing \n\r");
00128       nfcNucleo->get_led1() = 0;
00129     } //if-else
00130     
00131     /* Closing the i2c session. */
00132     if (tag.close_session() == true) {
00133       printf("Session closed\n\r");
00134       nfcNucleo->get_led3() = 1;
00135     } else {
00136       printf("Error closing the session\n\r");
00137     } //if-else
00138   } else {
00139     printf("Error opening the session\n\r");
00140   }
00141   
00142 #if defined(TARGET_STM)
00143   /* enable the button */
00144   InterruptIn mybutton(USER_BUTTON);
00145   
00146   mybutton.fall(set_button_press);
00147 
00148   /* Each second change the led status and see if the user press the button. */
00149   while(1) {
00150     if (buttonPress) {
00151         /* Writing the read message on console. */
00152         read_and_print_nfc_tag(tag);
00153         buttonPress=false;
00154        
00155         printf("\r\\nHDC1080 Test\r\n");
00156         hdc_begin();
00157         uint16_t manId = hdc_readManufactId();
00158         float tempC = hdc_readTemp();     
00159         float humid = hdc_readHumid();  
00160         unsigned long  serNum = hdc_readSerial();
00161         printf("manId=x%x, tempC=%0.3f humid=%0.3f serfNum=%ld\r\n",
00162         manId, tempC, humid, serNum);
00163         char temp = tempC;
00164   NDefLib::NDefNfcTag& tag =nfcNucleo->get_M24LR().get_NDef_tag();
00165   M24LR & mM24LRp = nfcNucleo->get_M24LR();
00166   
00167   /* Enabling Energy Harvesting. */
00168   mM24LRp.enable_energy_harvesting();
00169   
00170   printf("System Initialization done: !\n\r");
00171   
00172   /* Opening the i2c session with the nfc chip. */
00173   if (tag.open_session() == true) {
00174     printf("Session opened\n\r");
00175     nfcNucleo->get_led1() = 1;
00176     
00177     /* Creating the NDef message and record. */
00178     NDefLib::Message msg;
00179     NDefLib::RecordText test(&temp);
00180     msg.add_record(&test);
00181     
00182     /* Writing the tag. */
00183     if (tag.write(msg) == true) {
00184       printf("Tag written\n\r");
00185       nfcNucleo->get_led2() = 1;
00186     } else {
00187       printf("Error writing \n\r");
00188       nfcNucleo->get_led1() = 0;
00189     } //if-else
00190     
00191     /* Closing the i2c session. */
00192     if (tag.close_session() == true) {
00193       printf("Session closed\n\r");
00194       nfcNucleo->get_led3() = 1;
00195     } else {
00196       printf("Error closing the session\n\r");
00197     } //if-else
00198   } else {
00199     printf("Error opening the session\n\r");
00200   }
00201     }
00202   }
00203 #else
00204   read_and_print_nfc_tag(tag);
00205 #endif
00206 }
00207 
00208 
00209 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/