A simple mbed OS application providing an example of asynchronous access to the X-NUCLEO_NFC01A1 Dynamic NFC Tag board.
Dependencies: NDefLib X_NUCLEO_NFC01A1
Fork of mbed-os-example-NFC01A1 by
The application provides a simple example of asynchronous access to the X-NUCLEO-NFC01A1 Dynamic NFC Tag Expansion Board. It represents the multi-threaded mbed OS 5 version of the mbed classic HelloWorld_Async_NFC01A1 application.
The program writes a URI link to the M24SR dynamic tag using the asynchronous programming model.
The URI can then be read and printed on the serial console by pressing the user button and/or via RF from an NFC enabled smartphone/tablet.
Revision 2:e06d881b4b3c, committed 2017-08-11
- Comitter:
- nikapov
- Date:
- Fri Aug 11 09:19:31 2017 +0000
- Parent:
- 1:951cd8fbff34
- Commit message:
- Use one thread only to handle the events.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 951cd8fbff34 -r e06d881b4b3c main.cpp --- a/main.cpp Fri Aug 11 07:58:12 2017 +0000 +++ b/main.cpp Fri Aug 11 09:19:31 2017 +0000 @@ -39,8 +39,7 @@ #include "NDefNfcTagM24SR.h" #include "NDefLib/RecordType/RecordURI.h" -Thread nfcReadThread; -Thread nfcEventsThread; +Thread thread; /** flag to signal an interrupt from the nfc component*/ #define NFC_INTERRUPT_FLAG 0x1 @@ -55,40 +54,28 @@ /** Nfc ISR called when the nfc component has a message ready*/ static void nfc_interrupt_callback() { - nfcEventsThread.signal_set(NFC_INTERRUPT_FLAG); + thread.signal_set(NFC_INTERRUPT_FLAG); }//nfcInterruptCallback /** ISR handling button pressure **/ static void set_button_press() { - nfcReadThread.signal_set(BUTTON_PRESSED_FLAG); + thread.signal_set(BUTTON_PRESSED_FLAG); }//buttonPressed event -void nfcEvents() { - while (true) { - osEvent event = nfcEventsThread.signal_wait(NFC_INTERRUPT_FLAG); // wait for nfc event - //printf("Got an NFC event!\n\r"); - nfcNucleo->get_M24SR().manage_event(); - } -} - -void readNfc() { - +void handleEvents() { //create the callback to read a tag ReadUriCallbacks NDefReadCallback(nfcNucleo->get_led1(),nfcNucleo->get_led2(),nfcNucleo->get_led3()); - //create the callback to write a tag - WriteUriCallbacks NDefWriteCallback(nfcNucleo->get_led1(),nfcNucleo->get_led2(),nfcNucleo->get_led3()); - // write the tag - printf("Writing Tag\n\r"); - tag->open_session(); - while (true) { - osEvent event = nfcReadThread.signal_wait(BUTTON_PRESSED_FLAG); // wait for button pressed - printf("Button pressed! Reading the Tag.\n\r"); - tag->set_callback(&NDefReadCallback); - tag->open_session(); + osEvent event = thread.signal_wait(0); // wait for any event + if (event.value.v == NFC_INTERRUPT_FLAG) { + nfcNucleo->get_M24SR().manage_event(); + } else if (event.value.v == BUTTON_PRESSED_FLAG) { + printf("Button pressed! Reading the Tag.\n\r"); + tag->set_callback(&NDefReadCallback); + tag->open_session(); + } } - } int main() { @@ -113,10 +100,15 @@ tag = &(nfcNucleo->get_M24SR().get_NDef_tag()); printf("System Init done!\n\r"); - //Start the nfc event handling thread - nfcEventsThread.start(nfcEvents); - //Start the nfc reading thread - also writes the tag once - nfcReadThread.start(readNfc); + //Start the event handling thread + thread.start(handleEvents); + + //create the callback to write a tag + WriteUriCallbacks NDefWriteCallback(nfcNucleo->get_led1(),nfcNucleo->get_led2(),nfcNucleo->get_led3()); + // write the tag + tag->set_callback(&NDefWriteCallback); //set the callback + printf("Writing Tag\n\r"); + tag->open_session(); while (true) { __WFE();