M2M im2ag M2PGI

Dependencies:   mbed X_NUCLEO_NFC01A1 NDefLib

main.cpp

Committer:
ThibaudB
Date:
2016-03-08
Revision:
2:7183a6189b65
Parent:
1:20d7b4998087
Child:
4:47b9a09a74bf

File content as of revision 2:7183a6189b65:


#include "mbed.h"

#include "X_NUCLEO_NFC01A1.h"
#include "NDefLib/NDefNfcTag.h"
#include "NDefLib/RecordType/RecordURI.h"

/**
 * Write a Ndef URI message linking to st.com site.
 */
int main(void){
    
    //use default board pinout
    I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN);
    X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel,NULL,
            X_NUCLEO_NFC01A1::DEFAULT_GPO_PIN,X_NUCLEO_NFC01A1::DEFAULT_RF_DISABLE_PIN,
            X_NUCLEO_NFC01A1::DEFAULT_LED1_PIN,X_NUCLEO_NFC01A1::DEFAULT_LED2_PIN,
            X_NUCLEO_NFC01A1::DEFAULT_LED3_PIN);
    
    //retrieve the NdefLib interface
    NDefLib::NDefNfcTag& tag =nfcNucleo->getM24SR().getNDefTag();
    printf("System Init done: !\n\r");
    
    //open the i2c session with the nfc chip
    if(tag.openSession()){
        printf("Session opened\n\r");
        nfcNucleo->getLed1()=1;
        
        //create the NDef message and record
        NDefLib::Message msg;
        NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"st.com");
        msg.addRecord(&rUri);

        //write the tag
        if(tag.write(msg)){
            printf("Tag written\n\r");
            nfcNucleo->getLed2()=1;
        }else{
            printf("Error writing \n\r");
        }//if-else

        //close the i2c session
        if(tag.closeSession()){
           printf("Session closed\n\r");
           nfcNucleo->getLed3()=1;
        }else{
           printf("Error closing the session\n\r");
        }//if-else
            
    }else
        printf("Error opening the session\n\r");
}