M2M im2ag M2PGI

Dependencies:   mbed X_NUCLEO_NFC01A1 NDefLib

main.cpp

Committer:
Bayle38
Date:
2016-03-22
Revision:
8:ccf083f26f3a
Parent:
7:536aa5009d53
Child:
9:3e718f4a0fbc

File content as of revision 8:ccf083f26f3a:


#include "mbed.h"

#include "X_NUCLEO_NFC01A1.h"
#include "NDefLib/NDefNfcTag.h"
#include "NDefLib/Message.h"
#include "NDefLib/RecordType/RecordURI.h"
Serial pc(SERIAL_TX, SERIAL_RX);
DigitalOut myled(LED1);
InterruptIn interrupt(D12);
/**
 * Write a Ndef URI message linking to st.com site.
 */
void init(){
    pc.baud(115200);
    pc.format(8,SerialBase::None, 1);
}
 
void tagDetect(){
    pc.printf("Detected !!! \n");
} 

int main(void){
    init();
    pc.printf("System Init now !\n");
    myled = 0; // LED is OFF
    
    //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);
    nfcNucleo->getLed1()=0;
    nfcNucleo->getM24SR().ManageI2CGPO(I2C_ANSWER_READY);
    
    //retrieve the NdefLib interface
    NDefLib::NDefNfcTag& tag =nfcNucleo->getM24SR().getNDefTag();
    printf("System Init done: !\n");
    
    //open the i2c session with the nfc chip
    if(tag.openSession()){
        printf("Session opened\n");
        interrupt.fall(&tagDetect);

        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");
            nfcNucleo->getLed2()=1;
            
        }else{
            printf("Error writing \n");
        }//if-else
        NDefLib::Message message;
        while(!tag.read(&message)){
        }
        
        
        uint8_t buffer[64];
        buffer[0]=0;
        buffer[1]=3;
        
        message.write(buffer);
        buffer[63]=0;
        for(int i=0; i< 63; i++){
            printf("%c",buffer[i]);
        }
        printf("\n");
        //close the i2c session
        if(tag.closeSession()){
           myled = 1; // LED is ON
           printf("Session closed\n");
           nfcNucleo->getLed3()=1;
        }else{
           printf("Error closing the session\n");
        }//if-else
            
    }else
        printf("Error opening the session\n");
    
}