M2M im2ag M2PGI
Dependencies: mbed X_NUCLEO_NFC01A1 NDefLib
main.cpp@9:3e718f4a0fbc, 2016-03-22 (annotated)
- Committer:
- Bayle38
- Date:
- Tue Mar 22 14:26:10 2016 +0000
- Revision:
- 9:3e718f4a0fbc
- Parent:
- 8:ccf083f26f3a
Asynchronous !!!!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ThibaudB | 2:7183a6189b65 | 1 | |
hellmak | 0:4d794bbc5de1 | 2 | #include "mbed.h" |
hellmak | 0:4d794bbc5de1 | 3 | |
ThibaudB | 2:7183a6189b65 | 4 | #include "X_NUCLEO_NFC01A1.h" |
ThibaudB | 2:7183a6189b65 | 5 | #include "NDefLib/NDefNfcTag.h" |
hellmak | 4:47b9a09a74bf | 6 | #include "NDefLib/Message.h" |
ThibaudB | 2:7183a6189b65 | 7 | #include "NDefLib/RecordType/RecordURI.h" |
hellmak | 4:47b9a09a74bf | 8 | Serial pc(SERIAL_TX, SERIAL_RX); |
hellmak | 4:47b9a09a74bf | 9 | DigitalOut myled(LED1); |
Bayle38 | 7:536aa5009d53 | 10 | InterruptIn interrupt(D12); |
ThibaudB | 2:7183a6189b65 | 11 | /** |
ThibaudB | 2:7183a6189b65 | 12 | * Write a Ndef URI message linking to st.com site. |
ThibaudB | 2:7183a6189b65 | 13 | */ |
Bayle38 | 9:3e718f4a0fbc | 14 | |
Bayle38 | 9:3e718f4a0fbc | 15 | |
Bayle38 | 9:3e718f4a0fbc | 16 | class WriteUriCallbacks : public NDefLib::NDefNfcTag::Callbacks{ |
Bayle38 | 9:3e718f4a0fbc | 17 | |
Bayle38 | 9:3e718f4a0fbc | 18 | DigitalOut &mOnOpenSession; |
Bayle38 | 9:3e718f4a0fbc | 19 | DigitalOut &mOnWrite; |
Bayle38 | 9:3e718f4a0fbc | 20 | DigitalOut &mOnCloseSession; |
Bayle38 | 9:3e718f4a0fbc | 21 | |
Bayle38 | 9:3e718f4a0fbc | 22 | public: |
Bayle38 | 9:3e718f4a0fbc | 23 | |
Bayle38 | 9:3e718f4a0fbc | 24 | /** |
Bayle38 | 9:3e718f4a0fbc | 25 | * create the callback chain |
Bayle38 | 9:3e718f4a0fbc | 26 | * @param onOpenSession led to switch on when the session open |
Bayle38 | 9:3e718f4a0fbc | 27 | * @param onWrite led to switch on when the write end |
Bayle38 | 9:3e718f4a0fbc | 28 | * @param onCloseSession led to switch on when the session end |
Bayle38 | 9:3e718f4a0fbc | 29 | */ |
Bayle38 | 9:3e718f4a0fbc | 30 | WriteUriCallbacks(DigitalOut &onOpenSession,DigitalOut &onWrite, |
Bayle38 | 9:3e718f4a0fbc | 31 | DigitalOut &onCloseSession):mOnOpenSession(onOpenSession), |
Bayle38 | 9:3e718f4a0fbc | 32 | mOnWrite(onWrite),mOnCloseSession(onCloseSession){}; |
Bayle38 | 9:3e718f4a0fbc | 33 | |
Bayle38 | 9:3e718f4a0fbc | 34 | /** |
Bayle38 | 9:3e718f4a0fbc | 35 | * crate the new message and write it |
Bayle38 | 9:3e718f4a0fbc | 36 | * @param tag tag where write the message |
Bayle38 | 9:3e718f4a0fbc | 37 | * @param success true if the session correctly open |
Bayle38 | 9:3e718f4a0fbc | 38 | */ |
Bayle38 | 9:3e718f4a0fbc | 39 | virtual void onSessionOpen(NDefLib::NDefNfcTag *tag,bool success){ |
Bayle38 | 9:3e718f4a0fbc | 40 | if(!success){ |
Bayle38 | 9:3e718f4a0fbc | 41 | printf("Error opening the session\r\n"); |
Bayle38 | 9:3e718f4a0fbc | 42 | }//else |
Bayle38 | 9:3e718f4a0fbc | 43 | printf("Session opened\r\n"); |
Bayle38 | 9:3e718f4a0fbc | 44 | //ask to have an interrupt when the command finish |
Bayle38 | 9:3e718f4a0fbc | 45 | mOnOpenSession=1; |
Bayle38 | 9:3e718f4a0fbc | 46 | NDefLib::Message msg; |
Bayle38 | 9:3e718f4a0fbc | 47 | |
Bayle38 | 9:3e718f4a0fbc | 48 | NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"http://www.st.com"); |
Bayle38 | 9:3e718f4a0fbc | 49 | msg.addRecord(&rUri); |
Bayle38 | 9:3e718f4a0fbc | 50 | |
Bayle38 | 9:3e718f4a0fbc | 51 | tag->write(msg); |
Bayle38 | 9:3e718f4a0fbc | 52 | } |
Bayle38 | 9:3e718f4a0fbc | 53 | |
Bayle38 | 9:3e718f4a0fbc | 54 | /** |
Bayle38 | 9:3e718f4a0fbc | 55 | * request to close the session |
Bayle38 | 9:3e718f4a0fbc | 56 | * @param tag tag where close the session |
Bayle38 | 9:3e718f4a0fbc | 57 | * @param success true if the message is correctly wrote |
Bayle38 | 9:3e718f4a0fbc | 58 | * @param message wrote |
Bayle38 | 9:3e718f4a0fbc | 59 | */ |
Bayle38 | 9:3e718f4a0fbc | 60 | virtual void onMessageWrite(NDefLib::NDefNfcTag *tag,bool success, |
Bayle38 | 9:3e718f4a0fbc | 61 | const NDefLib::Message&){ |
Bayle38 | 9:3e718f4a0fbc | 62 | |
Bayle38 | 9:3e718f4a0fbc | 63 | if(!success) |
Bayle38 | 9:3e718f4a0fbc | 64 | printf("Error writing tag!\r\n"); |
Bayle38 | 9:3e718f4a0fbc | 65 | else{ |
Bayle38 | 9:3e718f4a0fbc | 66 | printf("Tag written!\r\n"); |
Bayle38 | 9:3e718f4a0fbc | 67 | mOnWrite=1; |
Bayle38 | 9:3e718f4a0fbc | 68 | }//if-else |
Bayle38 | 9:3e718f4a0fbc | 69 | tag->closeSession(); |
Bayle38 | 9:3e718f4a0fbc | 70 | } |
Bayle38 | 9:3e718f4a0fbc | 71 | |
Bayle38 | 9:3e718f4a0fbc | 72 | /** |
Bayle38 | 9:3e718f4a0fbc | 73 | * switch on the led |
Bayle38 | 9:3e718f4a0fbc | 74 | * @param tag where the session is closed |
Bayle38 | 9:3e718f4a0fbc | 75 | * @param success true if the session is correctly close |
Bayle38 | 9:3e718f4a0fbc | 76 | */ |
Bayle38 | 9:3e718f4a0fbc | 77 | virtual void onSessionClose(NDefLib::NDefNfcTag*,bool success){ |
Bayle38 | 9:3e718f4a0fbc | 78 | if(success){ |
Bayle38 | 9:3e718f4a0fbc | 79 | printf("Error closing the session\r\n"); |
Bayle38 | 9:3e718f4a0fbc | 80 | mOnCloseSession=1; |
Bayle38 | 9:3e718f4a0fbc | 81 | }else |
Bayle38 | 9:3e718f4a0fbc | 82 | printf("Error opening the session\r\n"); |
Bayle38 | 9:3e718f4a0fbc | 83 | } |
Bayle38 | 9:3e718f4a0fbc | 84 | |
Bayle38 | 9:3e718f4a0fbc | 85 | }; |
Bayle38 | 9:3e718f4a0fbc | 86 | |
Bayle38 | 9:3e718f4a0fbc | 87 | /** variable set to true when we receive an interrupt from the nfc component*/ |
Bayle38 | 9:3e718f4a0fbc | 88 | static bool nfcInterruptFlag; |
Bayle38 | 9:3e718f4a0fbc | 89 | |
Bayle38 | 9:3e718f4a0fbc | 90 | /** Nfc ISR called when the nfc component has a message ready*/ |
Bayle38 | 9:3e718f4a0fbc | 91 | static void nfcInterruptCallback(){ |
Bayle38 | 9:3e718f4a0fbc | 92 | nfcInterruptFlag=true; |
Bayle38 | 9:3e718f4a0fbc | 93 | }//nfcInterruptCallback |
Bayle38 | 9:3e718f4a0fbc | 94 | |
hellmak | 4:47b9a09a74bf | 95 | void init(){ |
hellmak | 4:47b9a09a74bf | 96 | pc.baud(115200); |
hellmak | 4:47b9a09a74bf | 97 | pc.format(8,SerialBase::None, 1); |
hellmak | 4:47b9a09a74bf | 98 | } |
hellmak | 4:47b9a09a74bf | 99 | |
Bayle38 | 6:67e0ae676619 | 100 | void tagDetect(){ |
Bayle38 | 6:67e0ae676619 | 101 | pc.printf("Detected !!! \n"); |
Bayle38 | 6:67e0ae676619 | 102 | } |
Bayle38 | 9:3e718f4a0fbc | 103 | int main(int argc,char *args[]){ |
Bayle38 | 9:3e718f4a0fbc | 104 | (void)argc; (void)args; |
Bayle38 | 9:3e718f4a0fbc | 105 | |
Bayle38 | 9:3e718f4a0fbc | 106 | //create the nfc component |
ThibaudB | 2:7183a6189b65 | 107 | I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN); |
Bayle38 | 9:3e718f4a0fbc | 108 | X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel,&nfcInterruptCallback, |
ThibaudB | 2:7183a6189b65 | 109 | X_NUCLEO_NFC01A1::DEFAULT_GPO_PIN,X_NUCLEO_NFC01A1::DEFAULT_RF_DISABLE_PIN, |
ThibaudB | 2:7183a6189b65 | 110 | X_NUCLEO_NFC01A1::DEFAULT_LED1_PIN,X_NUCLEO_NFC01A1::DEFAULT_LED2_PIN, |
ThibaudB | 2:7183a6189b65 | 111 | X_NUCLEO_NFC01A1::DEFAULT_LED3_PIN); |
Bayle38 | 9:3e718f4a0fbc | 112 | |
Bayle38 | 9:3e718f4a0fbc | 113 | //No call back needed since default behavior is sync |
Bayle38 | 9:3e718f4a0fbc | 114 | nfcNucleo->getM24SR().GetSession(); |
Bayle38 | 9:3e718f4a0fbc | 115 | nfcNucleo->getM24SR().ManageI2CGPO(I2C_ANSWER_READY); //switch to async mode |
Bayle38 | 9:3e718f4a0fbc | 116 | |
Bayle38 | 9:3e718f4a0fbc | 117 | NDefLib::NDefNfcTag &tag = nfcNucleo->getM24SR().getNDefTag(); |
Bayle38 | 9:3e718f4a0fbc | 118 | printf("System Init done!\n\r"); |
Bayle38 | 9:3e718f4a0fbc | 119 | |
Bayle38 | 9:3e718f4a0fbc | 120 | //crate the callback to use for write a tag |
Bayle38 | 9:3e718f4a0fbc | 121 | WriteUriCallbacks NDefCallback(nfcNucleo->getLed1(),nfcNucleo->getLed2(),nfcNucleo->getLed3()); |
Bayle38 | 9:3e718f4a0fbc | 122 | tag.setCallback(&NDefCallback); //set the callback |
Bayle38 | 9:3e718f4a0fbc | 123 | tag.openSession(); //start the callback chain |
Bayle38 | 9:3e718f4a0fbc | 124 | |
Bayle38 | 9:3e718f4a0fbc | 125 | printf("Start Main Loop\n\r"); |
Bayle38 | 9:3e718f4a0fbc | 126 | while(true){ |
Bayle38 | 9:3e718f4a0fbc | 127 | if(nfcInterruptFlag){ |
Bayle38 | 9:3e718f4a0fbc | 128 | nfcInterruptFlag=false; |
Bayle38 | 9:3e718f4a0fbc | 129 | //manage an async event from the nfc component |
Bayle38 | 9:3e718f4a0fbc | 130 | nfcNucleo->getM24SR().ManageEvent(); |
Bayle38 | 9:3e718f4a0fbc | 131 | |
Bayle38 | 9:3e718f4a0fbc | 132 | }//if |
Bayle38 | 9:3e718f4a0fbc | 133 | __WFE(); |
Bayle38 | 9:3e718f4a0fbc | 134 | }//while |
Bayle38 | 9:3e718f4a0fbc | 135 | |
Bayle38 | 9:3e718f4a0fbc | 136 | //return 0; |
ThibaudB | 2:7183a6189b65 | 137 | } |