ST / Mbed 2 deprecated HelloWorld_Async_M24SR

Dependencies:   M24SR NDefLib mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "M24SR.h"
00003 #include "ReadUriCallbacks.h"
00004 #include "WriteUriCallbacks.h"
00005 
00006 
00007 #ifdef TARGET_DISCO_L475VG_IOT01A
00008 // Pins definition for the DISCO_L475VG_IOT01A board
00009 #define NFC_I2C_SDA_PIN     PB_11
00010 #define NFC_I2C_SCL_PIN     PB_10
00011 #define NFC_GPO_PIN         PE_4
00012 #define NFC_RF_DISABLE_PIN  PE_2
00013 
00014 #define LED1_PIN    PA_5
00015 #define LED2_PIN    PB_14
00016 #define LED3_PIN    PC_9
00017 
00018 #else // X-NUCLEO-NFC01A1
00019 
00020 #define NFC_I2C_SDA_PIN     D14
00021 #define NFC_I2C_SCL_PIN     D15
00022 #define NFC_GPO_PIN         D12
00023 #define NFC_RF_DISABLE_PIN  D11
00024 
00025 #define LED1_PIN    D5
00026 #define LED2_PIN    D4
00027 #define LED3_PIN    D2
00028 
00029 #endif // DISCO_L475VG_IOT01A
00030 
00031 #define M24SR_ADDR          0xAC 
00032 
00033 /* Status PIN */
00034 DigitalOut sessionOpenLed(LED1_PIN); // tag session is open
00035 DigitalOut ongoingOperationLed(LED2_PIN); // ongoing read/write operation
00036 DigitalOut sessionClosedLed(LED3_PIN); // tag session is closed
00037 
00038 
00039 /** Variable set to true when we receive an interrupt from the NFC component */
00040 static volatile bool nfcInterruptFlag = false;
00041 
00042 /** Variable set to true when the user press the board user button */
00043 static volatile bool buttonPress = false;
00044 
00045 /** NFC ISR called when the NFC component has a message ready */
00046 static void nfc_interrupt_callback()
00047 {
00048     nfcInterruptFlag = true;
00049 }
00050 
00051 static void set_button_press()
00052 {
00053     buttonPress = true;
00054 }
00055 
00056 int main()
00057 {
00058     printf("\n\rNFC HelloWord Async example started\n\r");
00059         
00060     InterruptIn userButton(USER_BUTTON);
00061     userButton.fall(set_button_press);
00062 
00063     // Create the NFC component
00064     I2C i2cChannel(NFC_I2C_SDA_PIN, NFC_I2C_SCL_PIN);
00065     
00066     M24SR nfcTag(M24SR_ADDR,i2cChannel, &nfc_interrupt_callback, NFC_GPO_PIN, NFC_RF_DISABLE_PIN);
00067 
00068     // init needed to enable the component
00069     nfcTag.init(NULL);
00070     // No call back needed since default behavior is sync
00071     nfcTag.get_session();
00072     nfcTag.manage_I2C_GPO(M24SR::I2C_ANSWER_READY); // Switch to async mode
00073 
00074     NDefLib::NDefNfcTag &tag = nfcTag.get_NDef_tag();
00075 
00076     printf("Init done\n\r");
00077 
00078     // Create the callback to use to write a tag
00079     WriteUriCallbacks NDefWriteCallback(sessionOpenLed,ongoingOperationLed,sessionClosedLed);
00080     ReadUriCallbacks NDefReadCallback(sessionOpenLed,ongoingOperationLed,sessionClosedLed);
00081     tag.set_callback(&NDefWriteCallback); // Set the callback
00082     tag.open_session(); // Start the callback chain
00083 
00084     printf("Start main loop\n\r");
00085 
00086     while(true) {
00087         
00088         if (nfcInterruptFlag) {
00089             nfcInterruptFlag = false;
00090             // Manage an async event from the NFC component
00091             nfcTag.manage_event();
00092         }
00093 
00094         if (buttonPress) {
00095             buttonPress = false;
00096             printf("\n\rPressed\n\r");
00097             tag.set_callback(&NDefReadCallback);
00098             tag.open_session();
00099         }
00100         
00101         __WFE(); // Wait For Event
00102     }
00103 }