ST / Mbed 2 deprecated NFC01A1_Demos

Dependencies:   NDefLib X_NUCLEO_NFC01A1 mbed

Fork of X-MBED-NFC1 by Giovanni Visentini

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SampleSync_countClick.cpp Source File

SampleSync_countClick.cpp

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file       SampleSync_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 #include "MyRecord.h"
00042 
00043 /* Write a nfc message into a tag
00044  * @param nfcNucleo Board where write the data.
00045  * @param msg Message to write.
00046  */
00047 static void write_message(XNucleoNFC01A1 *nfcNucleo,NDefLib::Message &msg){
00048     NDefLib::NDefNfcTag& tag = nfcNucleo->get_M24SR().get_NDef_tag();
00049     //open the i2c session with the nfc chip
00050     if(tag.open_session()){
00051         printf("Session opened\r\n");
00052 
00053         nfcNucleo->get_led1()=! nfcNucleo->get_led1();
00054         
00055         //write the tag
00056         if(tag.write(msg)){
00057             printf("message wrote\r\n");
00058             nfcNucleo->get_led2()=!nfcNucleo->get_led2();
00059         }//if
00060 
00061         //close the i2c session
00062         if(tag.close_session()){
00063             printf("Session closed\r\n");
00064             nfcNucleo->get_led3()=!nfcNucleo->get_led3();
00065         }
00066     }//if open session
00067 }//writeMessage
00068 
00069 
00070 static volatile bool buttonPress=false; /// true when the user press the message
00071 
00072 /**
00073  * Call back called when the user press the button.
00074  */
00075 static void set_button_press(){
00076     buttonPress=true;
00077 }//if buttonPress
00078 
00079 /**
00080  * Write a castum record that count how many times the user press the button.
00081  */
00082 void sampleSync_countClick() {
00083     
00084     //instance the board with the default paramiters
00085     I2C i2cChannel(XNucleoNFC01A1::DEFAULT_SDA_PIN,XNucleoNFC01A1::DEFAULT_SDL_PIN);
00086     XNucleoNFC01A1 *nfcNucleo = XNucleoNFC01A1::instance(i2cChannel);
00087 
00088     //set the button interrupt
00089     #if defined(TARGET_STM)
00090         InterruptIn userButton(USER_BUTTON);
00091     #else
00092         InterruptIn userButton(SW2);
00093     #endif
00094     userButton.fall(set_button_press);
00095     
00096     printf("Init Done\r\n");
00097     
00098     //create the NDef message and record
00099     NDefLib::Message msg;
00100     MyRecord rClickCount;
00101     msg.add_record(&rClickCount);
00102     
00103     write_message(nfcNucleo,msg);
00104     
00105     printf("Start Main Loop\r\n");
00106     while(true){
00107         
00108         if(buttonPress){
00109             //change the record content
00110             rClickCount.increment_click();
00111             //write the new record content
00112             write_message(nfcNucleo,msg);  
00113             //wait a new button press          
00114             buttonPress=false;
00115         }//if
00116         //wait next event
00117         __WFE();
00118     }//while
00119 
00120 }//sample_countClick