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.
WriteUriCallbacks.h@2:e06d881b4b3c, 2017-08-11 (annotated)
- Committer:
- nikapov
- Date:
- Fri Aug 11 09:19:31 2017 +0000
- Revision:
- 2:e06d881b4b3c
- Parent:
- 0:3e88cb856f63
Use one thread only to handle the events.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nikapov | 0:3e88cb856f63 | 1 | /** |
nikapov | 0:3e88cb856f63 | 2 | ****************************************************************************** |
nikapov | 0:3e88cb856f63 | 3 | * @file WriteUriCallbacks.h |
nikapov | 0:3e88cb856f63 | 4 | * @date 12/07/2017 |
nikapov | 0:3e88cb856f63 | 5 | * @brief Class to write a URI tag. |
nikapov | 0:3e88cb856f63 | 6 | ****************************************************************************** |
nikapov | 0:3e88cb856f63 | 7 | * |
nikapov | 0:3e88cb856f63 | 8 | * COPYRIGHT(c) 2017 STMicroelectronics |
nikapov | 0:3e88cb856f63 | 9 | * |
nikapov | 0:3e88cb856f63 | 10 | * Redistribution and use in source and binary forms, with or without modification, |
nikapov | 0:3e88cb856f63 | 11 | * are permitted provided that the following conditions are met: |
nikapov | 0:3e88cb856f63 | 12 | * 1. Redistributions of source code must retain the above copyright notice, |
nikapov | 0:3e88cb856f63 | 13 | * this list of conditions and the following disclaimer. |
nikapov | 0:3e88cb856f63 | 14 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
nikapov | 0:3e88cb856f63 | 15 | * this list of conditions and the following disclaimer in the documentation |
nikapov | 0:3e88cb856f63 | 16 | * and/or other materials provided with the distribution. |
nikapov | 0:3e88cb856f63 | 17 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
nikapov | 0:3e88cb856f63 | 18 | * may be used to endorse or promote products derived from this software |
nikapov | 0:3e88cb856f63 | 19 | * without specific prior written permission. |
nikapov | 0:3e88cb856f63 | 20 | * |
nikapov | 0:3e88cb856f63 | 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
nikapov | 0:3e88cb856f63 | 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
nikapov | 0:3e88cb856f63 | 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
nikapov | 0:3e88cb856f63 | 24 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
nikapov | 0:3e88cb856f63 | 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
nikapov | 0:3e88cb856f63 | 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
nikapov | 0:3e88cb856f63 | 27 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
nikapov | 0:3e88cb856f63 | 28 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
nikapov | 0:3e88cb856f63 | 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
nikapov | 0:3e88cb856f63 | 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
nikapov | 0:3e88cb856f63 | 31 | * |
nikapov | 0:3e88cb856f63 | 32 | ****************************************************************************** |
nikapov | 0:3e88cb856f63 | 33 | */ |
nikapov | 0:3e88cb856f63 | 34 | |
nikapov | 0:3e88cb856f63 | 35 | #include "mbed.h" |
nikapov | 0:3e88cb856f63 | 36 | #include "NDefLib/RecordType/RecordURI.h" |
nikapov | 0:3e88cb856f63 | 37 | |
nikapov | 0:3e88cb856f63 | 38 | /** |
nikapov | 0:3e88cb856f63 | 39 | * Chain of callback that will crate a Uri record and write it. |
nikapov | 0:3e88cb856f63 | 40 | * After each operation the class will switch on a led |
nikapov | 0:3e88cb856f63 | 41 | */ |
nikapov | 0:3e88cb856f63 | 42 | class WriteUriCallbacks : public NDefLib::NDefNfcTag::Callbacks { |
nikapov | 0:3e88cb856f63 | 43 | |
nikapov | 0:3e88cb856f63 | 44 | DigitalOut &mOnOpenSession; |
nikapov | 0:3e88cb856f63 | 45 | DigitalOut &mOnWrite; |
nikapov | 0:3e88cb856f63 | 46 | DigitalOut &mOnCloseSession; |
nikapov | 0:3e88cb856f63 | 47 | NDefLib::Message *msg; |
nikapov | 0:3e88cb856f63 | 48 | |
nikapov | 0:3e88cb856f63 | 49 | public: |
nikapov | 0:3e88cb856f63 | 50 | |
nikapov | 0:3e88cb856f63 | 51 | /** |
nikapov | 0:3e88cb856f63 | 52 | * create the callback chain |
nikapov | 0:3e88cb856f63 | 53 | * @param onOpenSession led to switch on when the session open |
nikapov | 0:3e88cb856f63 | 54 | * @param onWrite led to switch on when the write end |
nikapov | 0:3e88cb856f63 | 55 | * @param onCloseSession led to switch on when the session end |
nikapov | 0:3e88cb856f63 | 56 | */ |
nikapov | 0:3e88cb856f63 | 57 | WriteUriCallbacks(DigitalOut &onOpenSession,DigitalOut &onWrite, |
nikapov | 0:3e88cb856f63 | 58 | DigitalOut &onCloseSession):mOnOpenSession(onOpenSession), |
nikapov | 0:3e88cb856f63 | 59 | mOnWrite(onWrite),mOnCloseSession(onCloseSession){}; |
nikapov | 0:3e88cb856f63 | 60 | |
nikapov | 0:3e88cb856f63 | 61 | /** |
nikapov | 0:3e88cb856f63 | 62 | * crate the new message and write it |
nikapov | 0:3e88cb856f63 | 63 | * @param tag tag where write the message |
nikapov | 0:3e88cb856f63 | 64 | * @param success true if the session correctly open |
nikapov | 0:3e88cb856f63 | 65 | */ |
nikapov | 0:3e88cb856f63 | 66 | virtual void on_session_open(NDefLib::NDefNfcTag *tag,bool success) { |
nikapov | 0:3e88cb856f63 | 67 | if (!success) { |
nikapov | 0:3e88cb856f63 | 68 | printf("Error opening the session\r\n"); |
nikapov | 0:3e88cb856f63 | 69 | }//else |
nikapov | 0:3e88cb856f63 | 70 | printf("Session opened\r\n"); |
nikapov | 0:3e88cb856f63 | 71 | //ask to have an interrupt when the command finish |
nikapov | 0:3e88cb856f63 | 72 | mOnOpenSession=1; |
nikapov | 0:3e88cb856f63 | 73 | mOnCloseSession=0; |
nikapov | 0:3e88cb856f63 | 74 | |
nikapov | 0:3e88cb856f63 | 75 | NDefLib::RecordURI *rUri = new NDefLib::RecordURI(NDefLib::RecordURI::HTTP_WWW,"http://www.st.com"); |
nikapov | 0:3e88cb856f63 | 76 | msg = new NDefLib::Message(); |
nikapov | 0:3e88cb856f63 | 77 | msg->add_record(rUri); |
nikapov | 0:3e88cb856f63 | 78 | |
nikapov | 0:3e88cb856f63 | 79 | tag->write(*msg); |
nikapov | 0:3e88cb856f63 | 80 | } |
nikapov | 0:3e88cb856f63 | 81 | |
nikapov | 0:3e88cb856f63 | 82 | /** |
nikapov | 0:3e88cb856f63 | 83 | * request to close the session |
nikapov | 0:3e88cb856f63 | 84 | * @param tag tag where close the session |
nikapov | 0:3e88cb856f63 | 85 | * @param success true if the message is correctly wrote |
nikapov | 0:3e88cb856f63 | 86 | * @param message wrote |
nikapov | 0:3e88cb856f63 | 87 | */ |
nikapov | 0:3e88cb856f63 | 88 | virtual void on_message_write(NDefLib::NDefNfcTag *tag,bool success, |
nikapov | 0:3e88cb856f63 | 89 | const NDefLib::Message&) { |
nikapov | 0:3e88cb856f63 | 90 | |
nikapov | 0:3e88cb856f63 | 91 | if (!success) { |
nikapov | 0:3e88cb856f63 | 92 | printf("Error writing tag!\r\n"); |
nikapov | 0:3e88cb856f63 | 93 | } else { |
nikapov | 0:3e88cb856f63 | 94 | printf("Tag written!\r\n"); |
nikapov | 0:3e88cb856f63 | 95 | mOnWrite=1; |
nikapov | 0:3e88cb856f63 | 96 | }//if-else |
nikapov | 0:3e88cb856f63 | 97 | |
nikapov | 0:3e88cb856f63 | 98 | NDefLib::Message::remove_and_delete_all_record(*msg); |
nikapov | 0:3e88cb856f63 | 99 | delete msg; |
nikapov | 0:3e88cb856f63 | 100 | tag->close_session(); |
nikapov | 0:3e88cb856f63 | 101 | } |
nikapov | 0:3e88cb856f63 | 102 | |
nikapov | 0:3e88cb856f63 | 103 | /** |
nikapov | 0:3e88cb856f63 | 104 | * switch on the led |
nikapov | 0:3e88cb856f63 | 105 | * @param tag where the session is closed |
nikapov | 0:3e88cb856f63 | 106 | * @param success true if the session is correctly close |
nikapov | 0:3e88cb856f63 | 107 | */ |
nikapov | 0:3e88cb856f63 | 108 | virtual void on_session_close(NDefLib::NDefNfcTag*, bool success) { |
nikapov | 0:3e88cb856f63 | 109 | if (success) { |
nikapov | 0:3e88cb856f63 | 110 | printf("Session closed\r\n"); |
nikapov | 0:3e88cb856f63 | 111 | mOnCloseSession=1; |
nikapov | 0:3e88cb856f63 | 112 | mOnOpenSession=0; |
nikapov | 0:3e88cb856f63 | 113 | mOnWrite=0; |
nikapov | 0:3e88cb856f63 | 114 | } else { |
nikapov | 0:3e88cb856f63 | 115 | printf("Error closing the session\r\n"); |
nikapov | 0:3e88cb856f63 | 116 | } |
nikapov | 0:3e88cb856f63 | 117 | } |
nikapov | 0:3e88cb856f63 | 118 | }; |