M2M im2ag M2PGI
Dependencies: mbed X_NUCLEO_NFC01A1 NDefLib
main.cpp
00001 00002 #include "mbed.h" 00003 00004 #include "X_NUCLEO_NFC01A1.h" 00005 #include "NDefLib/NDefNfcTag.h" 00006 #include "NDefLib/Message.h" 00007 #include "NDefLib/RecordType/RecordURI.h" 00008 Serial pc(SERIAL_TX, SERIAL_RX); 00009 DigitalOut myled(LED1); 00010 InterruptIn interrupt(D12); 00011 /** 00012 * Write a Ndef URI message linking to st.com site. 00013 */ 00014 00015 00016 class WriteUriCallbacks : public NDefLib::NDefNfcTag::Callbacks{ 00017 00018 DigitalOut &mOnOpenSession; 00019 DigitalOut &mOnWrite; 00020 DigitalOut &mOnCloseSession; 00021 00022 public: 00023 00024 /** 00025 * create the callback chain 00026 * @param onOpenSession led to switch on when the session open 00027 * @param onWrite led to switch on when the write end 00028 * @param onCloseSession led to switch on when the session end 00029 */ 00030 WriteUriCallbacks(DigitalOut &onOpenSession,DigitalOut &onWrite, 00031 DigitalOut &onCloseSession):mOnOpenSession(onOpenSession), 00032 mOnWrite(onWrite),mOnCloseSession(onCloseSession){}; 00033 00034 /** 00035 * crate the new message and write it 00036 * @param tag tag where write the message 00037 * @param success true if the session correctly open 00038 */ 00039 virtual void onSessionOpen(NDefLib::NDefNfcTag *tag,bool success){ 00040 if(!success){ 00041 printf("Error opening the session\r\n"); 00042 }//else 00043 printf("Session opened\r\n"); 00044 //ask to have an interrupt when the command finish 00045 mOnOpenSession=1; 00046 NDefLib::Message msg; 00047 00048 NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"http://www.st.com"); 00049 msg.addRecord(&rUri); 00050 00051 tag->write(msg); 00052 } 00053 00054 /** 00055 * request to close the session 00056 * @param tag tag where close the session 00057 * @param success true if the message is correctly wrote 00058 * @param message wrote 00059 */ 00060 virtual void onMessageWrite(NDefLib::NDefNfcTag *tag,bool success, 00061 const NDefLib::Message&){ 00062 00063 if(!success) 00064 printf("Error writing tag!\r\n"); 00065 else{ 00066 printf("Tag written!\r\n"); 00067 mOnWrite=1; 00068 }//if-else 00069 tag->closeSession(); 00070 } 00071 00072 /** 00073 * switch on the led 00074 * @param tag where the session is closed 00075 * @param success true if the session is correctly close 00076 */ 00077 virtual void onSessionClose(NDefLib::NDefNfcTag*,bool success){ 00078 if(success){ 00079 printf("Error closing the session\r\n"); 00080 mOnCloseSession=1; 00081 }else 00082 printf("Error opening the session\r\n"); 00083 } 00084 00085 }; 00086 00087 /** variable set to true when we receive an interrupt from the nfc component*/ 00088 static bool nfcInterruptFlag; 00089 00090 /** Nfc ISR called when the nfc component has a message ready*/ 00091 static void nfcInterruptCallback(){ 00092 nfcInterruptFlag=true; 00093 }//nfcInterruptCallback 00094 00095 void init(){ 00096 pc.baud(115200); 00097 pc.format(8,SerialBase::None, 1); 00098 } 00099 00100 void tagDetect(){ 00101 pc.printf("Detected !!! \n"); 00102 } 00103 int main(int argc,char *args[]){ 00104 (void)argc; (void)args; 00105 00106 //create the nfc component 00107 I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN); 00108 X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel,&nfcInterruptCallback, 00109 X_NUCLEO_NFC01A1::DEFAULT_GPO_PIN,X_NUCLEO_NFC01A1::DEFAULT_RF_DISABLE_PIN, 00110 X_NUCLEO_NFC01A1::DEFAULT_LED1_PIN,X_NUCLEO_NFC01A1::DEFAULT_LED2_PIN, 00111 X_NUCLEO_NFC01A1::DEFAULT_LED3_PIN); 00112 00113 //No call back needed since default behavior is sync 00114 nfcNucleo->getM24SR().GetSession(); 00115 nfcNucleo->getM24SR().ManageI2CGPO(I2C_ANSWER_READY); //switch to async mode 00116 00117 NDefLib::NDefNfcTag &tag = nfcNucleo->getM24SR().getNDefTag(); 00118 printf("System Init done!\n\r"); 00119 00120 //crate the callback to use for write a tag 00121 WriteUriCallbacks NDefCallback(nfcNucleo->getLed1(),nfcNucleo->getLed2(),nfcNucleo->getLed3()); 00122 tag.setCallback(&NDefCallback); //set the callback 00123 tag.openSession(); //start the callback chain 00124 00125 printf("Start Main Loop\n\r"); 00126 while(true){ 00127 if(nfcInterruptFlag){ 00128 nfcInterruptFlag=false; 00129 //manage an async event from the nfc component 00130 nfcNucleo->getM24SR().ManageEvent(); 00131 00132 }//if 00133 __WFE(); 00134 }//while 00135 00136 //return 0; 00137 }
Generated on Fri Jul 15 2022 17:13:02 by 1.7.2