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 SampleAsync_writeUrl.cpp Source File

SampleAsync_writeUrl.cpp

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file       SampleAsync_writeUrl.cpp
00004   * @author     ST / Central Labs
00005   * @date       03 Dic 2015
00006   * @brief      This demo write an ndef message with an url inside
00007   ******************************************************************************
00008   *
00009   * COPYRIGHT(c) 2015 STMicroelectronics
00010   *
00011   * Redistribution and use in source and binary forms, with or without modification,
00012   * are permitted provided that the following conditions are met:
00013   *   1. Redistributions of source code must retain the above copyright notice,
00014   *      this list of conditions and the following disclaimer.
00015   *   2. Redistributions in binary form must reproduce the above copyright notice,
00016   *      this list of conditions and the following disclaimer in the documentation
00017   *      and/or other materials provided with the distribution.
00018   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00019   *      may be used to endorse or promote products derived from this software
00020   *      without specific prior written permission.
00021   *
00022   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00023   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00024   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00025   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00026   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00027   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00028   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00030   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00031   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032   *
00033   ******************************************************************************
00034   */
00035   
00036 #include "mbed.h"
00037 
00038 #include "XNucleoNFC01A1.h"
00039 #include "NDefLib/NDefNfcTag.h"
00040 #include "NDefLib/RecordType/RecordURI.h"
00041 
00042 /**
00043  * Chain of callback that will crate a Uri record and write it.
00044  * After each operation the class will switch on a led
00045  */
00046 class WriteUriCallbacks : public NDefLib::NDefNfcTag::Callbacks{
00047 
00048     DigitalOut &mOnOpenSession;
00049     DigitalOut &mOnWrite;
00050     DigitalOut &mOnCloseSession;
00051 
00052     NDefLib::Message msg;
00053 
00054 public:
00055 
00056     /**
00057      * create the callback chain
00058      * @param onOpenSession led to switch on when the session open
00059      * @param onWrite led to switch on when the write end
00060      * @param onCloseSession led to switch on when the session end
00061      */
00062     WriteUriCallbacks(DigitalOut &onOpenSession,DigitalOut &onWrite,
00063             DigitalOut &onCloseSession):mOnOpenSession(onOpenSession),
00064                     mOnWrite(onWrite),mOnCloseSession(onCloseSession){};
00065 
00066     /**
00067      * crate the new message and write it
00068      * @param tag tag where write the message
00069      * @param success true if the session correctly open
00070      */
00071     virtual void on_session_open(NDefLib::NDefNfcTag *tag,bool success){
00072         if(!success){
00073             printf("Error OpenSession\n\r");
00074         }//else
00075         printf("Session Open\n\r");
00076         //ask to have an interrupt when the command finish
00077         mOnOpenSession=1;
00078 
00079         NDefLib::RecordURI *rUri = 
00080             new NDefLib::RecordURI(NDefLib::RecordURI::HTTP_WWW,"http://www.st.com");
00081         msg.add_record(rUri);
00082 
00083         tag->write(msg);
00084     }
00085 
00086     /**
00087      * request to close the session
00088      * @param tag tag where close the session
00089      * @param success true if the message is correctly wrote
00090      * @param message wrote
00091      */
00092     virtual void on_message_write(NDefLib::NDefNfcTag *tag,bool success){
00093         NDefLib::Message::remove_and_delete_all_record(msg);
00094         if(!success) {
00095             printf("Error writing tag!\n\r");
00096         } else {
00097             printf("Tag Written!\n\r");
00098             mOnWrite=1;
00099         }//if-else
00100         tag->close_session();
00101     }
00102 
00103     /**
00104      * switch on the led
00105      * @param tag where the session is closed
00106      * @param success true if the session is correctly close
00107      */
00108     virtual void on_session_close(NDefLib::NDefNfcTag*,bool success){
00109         if(success) {
00110             printf("Session closed\n\r");
00111             mOnCloseSession=1;
00112         } else {
00113             printf("Error Session closed\n\r");
00114         }
00115     }
00116 
00117 };
00118 
00119 
00120 /** variable set to true when we receive an interrupt from the nfc component*/
00121 static volatile bool nfcInterruptFlag;
00122 
00123 /** Nfc ISR called when the nfc component has a message ready*/
00124 static void nfcInterruptCallback(){
00125     nfcInterruptFlag=true;
00126 }//nfcInterruptCallback
00127 
00128 void sampleAsync_writeUrl(){
00129     //create the nfc component
00130     I2C i2cChannel(XNucleoNFC01A1::DEFAULT_SDA_PIN,XNucleoNFC01A1::DEFAULT_SDL_PIN);
00131     XNucleoNFC01A1 *nfcNucleo = XNucleoNFC01A1::instance(i2cChannel,&nfcInterruptCallback);
00132 
00133 
00134     M24SR &nfcTag =nfcNucleo->get_M24SR();
00135 
00136     //No call back needed since default behavior is sync
00137     if(nfcTag.get_session()==M24SR::M24SR_SUCCESS) {
00138         nfcTag.manage_I2C_GPO(M24SR::I2C_ANSWER_READY);//Set async mode
00139     }
00140 
00141     NDefLib::NDefNfcTag &tag = nfcNucleo->get_M24SR().get_NDef_tag();
00142 
00143     //crate the callback to use for write a tag
00144     WriteUriCallbacks NDefCallback(nfcNucleo->get_led1(),nfcNucleo->get_led2(),nfcNucleo->get_led3());
00145     tag.set_callback(&NDefCallback); //set the callback
00146     tag.open_session(); //start the callback chain
00147 
00148     printf("Start Main Loop\n\r");
00149     while(true){
00150         if(nfcInterruptFlag){
00151             nfcInterruptFlag=false;
00152             //manage an async event from the nfc component
00153             nfcTag.manage_event();
00154         }//if
00155         __WFE();
00156     }//while
00157 }