This application provides a set of demos with X-NUCLEO-NFC01A1 expansion board.
Dependencies: NDefLib X_NUCLEO_NFC01A1 mbed
Fork of X-MBED-NFC1 by
This application provides a set of demos with X-NUCLEO-NFC01A1 expansion board.
The available demos are:
- SAMPLE_WRITE_URL: write a tag with the ST home page URL
- SAMPLE_COUNT_CLICK: create a custom tag to count and report the user button clicks.
- SAMPLE_WRITE_AND_CHANGE_ALL: write a tag with all the supported records and update the tag contents when the user button is pressed.
- SAMPLE_LOCK_TAG_CONTENT: use the M24SR component API to set the NFC tag as read-only.
To enable the different demos comment/uncomment the SAMPLE_* macros provided in main.cpp .
Samples/sample_lockWriteTag.cpp@8:a9ecd7687de7, 2015-12-21 (annotated)
- Committer:
- giovannivisentini
- Date:
- Mon Dec 21 13:01:41 2015 +0000
- Revision:
- 8:a9ecd7687de7
- Child:
- 9:9742acd0c015
remove already imported library and add a new demo
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| giovannivisentini | 8:a9ecd7687de7 | 1 | /** |
| giovannivisentini | 8:a9ecd7687de7 | 2 | ****************************************************************************** |
| giovannivisentini | 8:a9ecd7687de7 | 3 | * @file Sample_lockWriteTag.cpp |
| giovannivisentini | 8:a9ecd7687de7 | 4 | * @date 03/12/2015 |
| giovannivisentini | 8:a9ecd7687de7 | 5 | * @brief This demo define a lock/unlock the tag content when the user press |
| giovannivisentini | 8:a9ecd7687de7 | 6 | * the user button |
| giovannivisentini | 8:a9ecd7687de7 | 7 | ****************************************************************************** |
| giovannivisentini | 8:a9ecd7687de7 | 8 | * |
| giovannivisentini | 8:a9ecd7687de7 | 9 | * COPYRIGHT(c) 2015 STMicroelectronics |
| giovannivisentini | 8:a9ecd7687de7 | 10 | * |
| giovannivisentini | 8:a9ecd7687de7 | 11 | * Redistribution and use in source and binary forms, with or without modification, |
| giovannivisentini | 8:a9ecd7687de7 | 12 | * are permitted provided that the following conditions are met: |
| giovannivisentini | 8:a9ecd7687de7 | 13 | * 1. Redistributions of source code must retain the above copyright notice, |
| giovannivisentini | 8:a9ecd7687de7 | 14 | * this list of conditions and the following disclaimer. |
| giovannivisentini | 8:a9ecd7687de7 | 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| giovannivisentini | 8:a9ecd7687de7 | 16 | * this list of conditions and the following disclaimer in the documentation |
| giovannivisentini | 8:a9ecd7687de7 | 17 | * and/or other materials provided with the distribution. |
| giovannivisentini | 8:a9ecd7687de7 | 18 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
| giovannivisentini | 8:a9ecd7687de7 | 19 | * may be used to endorse or promote products derived from this software |
| giovannivisentini | 8:a9ecd7687de7 | 20 | * without specific prior written permission. |
| giovannivisentini | 8:a9ecd7687de7 | 21 | * |
| giovannivisentini | 8:a9ecd7687de7 | 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| giovannivisentini | 8:a9ecd7687de7 | 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| giovannivisentini | 8:a9ecd7687de7 | 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| giovannivisentini | 8:a9ecd7687de7 | 25 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| giovannivisentini | 8:a9ecd7687de7 | 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| giovannivisentini | 8:a9ecd7687de7 | 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| giovannivisentini | 8:a9ecd7687de7 | 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| giovannivisentini | 8:a9ecd7687de7 | 29 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| giovannivisentini | 8:a9ecd7687de7 | 30 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| giovannivisentini | 8:a9ecd7687de7 | 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| giovannivisentini | 8:a9ecd7687de7 | 32 | * |
| giovannivisentini | 8:a9ecd7687de7 | 33 | ****************************************************************************** |
| giovannivisentini | 8:a9ecd7687de7 | 34 | */ |
| giovannivisentini | 8:a9ecd7687de7 | 35 | |
| giovannivisentini | 8:a9ecd7687de7 | 36 | #include "mbed.h" |
| giovannivisentini | 8:a9ecd7687de7 | 37 | |
| giovannivisentini | 8:a9ecd7687de7 | 38 | #include "X_NUCLEO_NFC01A1.h" |
| giovannivisentini | 8:a9ecd7687de7 | 39 | #include "Type4NfcTagM24SR.h" |
| giovannivisentini | 8:a9ecd7687de7 | 40 | #include "NDefLib/RecordType/RecordText.h" |
| giovannivisentini | 8:a9ecd7687de7 | 41 | |
| giovannivisentini | 8:a9ecd7687de7 | 42 | static volatile bool buttonPress=false; |
| giovannivisentini | 8:a9ecd7687de7 | 43 | |
| giovannivisentini | 8:a9ecd7687de7 | 44 | static void setButtonPress(){ |
| giovannivisentini | 8:a9ecd7687de7 | 45 | buttonPress=true; |
| giovannivisentini | 8:a9ecd7687de7 | 46 | }//if buttonPress |
| giovannivisentini | 8:a9ecd7687de7 | 47 | |
| giovannivisentini | 8:a9ecd7687de7 | 48 | /** |
| giovannivisentini | 8:a9ecd7687de7 | 49 | * return the led that tell if the tag is read only |
| giovannivisentini | 8:a9ecd7687de7 | 50 | * @param nfcNucleo board where the lead lives |
| giovannivisentini | 8:a9ecd7687de7 | 51 | * @return let to switch on if the tag is read only |
| giovannivisentini | 8:a9ecd7687de7 | 52 | */ |
| giovannivisentini | 8:a9ecd7687de7 | 53 | static inline DigitalOut& getReadOnlyLed(X_NUCLEO_NFC01A1 *nfcNucleo){ |
| giovannivisentini | 8:a9ecd7687de7 | 54 | return nfcNucleo->getLed2(); |
| giovannivisentini | 8:a9ecd7687de7 | 55 | } |
| giovannivisentini | 8:a9ecd7687de7 | 56 | |
| giovannivisentini | 8:a9ecd7687de7 | 57 | /** |
| giovannivisentini | 8:a9ecd7687de7 | 58 | * return the led that tell if the tag is write only |
| giovannivisentini | 8:a9ecd7687de7 | 59 | * @param nfcNucleo board where the lead lives |
| giovannivisentini | 8:a9ecd7687de7 | 60 | * @return let to switch on if the tag is write only |
| giovannivisentini | 8:a9ecd7687de7 | 61 | */ |
| giovannivisentini | 8:a9ecd7687de7 | 62 | static inline DigitalOut& getWriteableLed(X_NUCLEO_NFC01A1 *nfcNucleo){ |
| giovannivisentini | 8:a9ecd7687de7 | 63 | return nfcNucleo->getLed3(); |
| giovannivisentini | 8:a9ecd7687de7 | 64 | } |
| giovannivisentini | 8:a9ecd7687de7 | 65 | |
| giovannivisentini | 8:a9ecd7687de7 | 66 | /** |
| giovannivisentini | 8:a9ecd7687de7 | 67 | * set the tag as read only and switch the led on |
| giovannivisentini | 8:a9ecd7687de7 | 68 | * @param nfcNucleo board where the lead lives |
| giovannivisentini | 8:a9ecd7687de7 | 69 | * @param tag helper class for manage the nfc tag |
| giovannivisentini | 8:a9ecd7687de7 | 70 | * @return true if the change has effect |
| giovannivisentini | 8:a9ecd7687de7 | 71 | */ |
| giovannivisentini | 8:a9ecd7687de7 | 72 | bool enableReadOnly( X_NUCLEO_NFC01A1 *nfcNucleo,Type4NfcTagM24SR &tag){ |
| giovannivisentini | 8:a9ecd7687de7 | 73 | |
| giovannivisentini | 8:a9ecd7687de7 | 74 | if(tag.enableReadOnly()){ |
| giovannivisentini | 8:a9ecd7687de7 | 75 | getReadOnlyLed(nfcNucleo)=true; |
| giovannivisentini | 8:a9ecd7687de7 | 76 | getWriteableLed(nfcNucleo)=false; |
| giovannivisentini | 8:a9ecd7687de7 | 77 | return true; |
| giovannivisentini | 8:a9ecd7687de7 | 78 | }else |
| giovannivisentini | 8:a9ecd7687de7 | 79 | return false; |
| giovannivisentini | 8:a9ecd7687de7 | 80 | } |
| giovannivisentini | 8:a9ecd7687de7 | 81 | |
| giovannivisentini | 8:a9ecd7687de7 | 82 | /** |
| giovannivisentini | 8:a9ecd7687de7 | 83 | * set the tag as read/write and switch the led on |
| giovannivisentini | 8:a9ecd7687de7 | 84 | * @param nfcNucleo board where the lead lives |
| giovannivisentini | 8:a9ecd7687de7 | 85 | * @param tag helper class for manage the nfc tag |
| giovannivisentini | 8:a9ecd7687de7 | 86 | * @return true if the change has effect |
| giovannivisentini | 8:a9ecd7687de7 | 87 | */ |
| giovannivisentini | 8:a9ecd7687de7 | 88 | bool disableReadOnly( X_NUCLEO_NFC01A1 *nfcNucleo,Type4NfcTagM24SR &tag){ |
| giovannivisentini | 8:a9ecd7687de7 | 89 | |
| giovannivisentini | 8:a9ecd7687de7 | 90 | if(tag.disableReadOnly()){ |
| giovannivisentini | 8:a9ecd7687de7 | 91 | getReadOnlyLed(nfcNucleo)=false; |
| giovannivisentini | 8:a9ecd7687de7 | 92 | getWriteableLed(nfcNucleo)=true; |
| giovannivisentini | 8:a9ecd7687de7 | 93 | return true; |
| giovannivisentini | 8:a9ecd7687de7 | 94 | }else |
| giovannivisentini | 8:a9ecd7687de7 | 95 | return false; |
| giovannivisentini | 8:a9ecd7687de7 | 96 | } |
| giovannivisentini | 8:a9ecd7687de7 | 97 | |
| giovannivisentini | 8:a9ecd7687de7 | 98 | void sample_lockTagContent() { |
| giovannivisentini | 8:a9ecd7687de7 | 99 | Serial console(SERIAL_TX, SERIAL_RX); |
| giovannivisentini | 8:a9ecd7687de7 | 100 | |
| giovannivisentini | 8:a9ecd7687de7 | 101 | I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN); |
| giovannivisentini | 8:a9ecd7687de7 | 102 | |
| giovannivisentini | 8:a9ecd7687de7 | 103 | X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel); |
| giovannivisentini | 8:a9ecd7687de7 | 104 | //create the wrapper for use the NdefLib |
| giovannivisentini | 8:a9ecd7687de7 | 105 | Type4NfcTagM24SR tag(nfcNucleo->getM24SR()); |
| giovannivisentini | 8:a9ecd7687de7 | 106 | |
| giovannivisentini | 8:a9ecd7687de7 | 107 | //when the user press the button set buttonPress to true |
| giovannivisentini | 8:a9ecd7687de7 | 108 | InterruptIn userButton(USER_BUTTON); |
| giovannivisentini | 8:a9ecd7687de7 | 109 | userButton.fall(setButtonPress); |
| giovannivisentini | 8:a9ecd7687de7 | 110 | |
| giovannivisentini | 8:a9ecd7687de7 | 111 | //restore the board as read/write |
| giovannivisentini | 8:a9ecd7687de7 | 112 | tag.openSession(true); |
| giovannivisentini | 8:a9ecd7687de7 | 113 | nfcNucleo->getLed1()=tag.disableAllPassword(); |
| giovannivisentini | 8:a9ecd7687de7 | 114 | tag.closeSession(); |
| giovannivisentini | 8:a9ecd7687de7 | 115 | getReadOnlyLed(nfcNucleo)=false; |
| giovannivisentini | 8:a9ecd7687de7 | 116 | getWriteableLed(nfcNucleo)=true; |
| giovannivisentini | 8:a9ecd7687de7 | 117 | bool tagIsLock=false; |
| giovannivisentini | 8:a9ecd7687de7 | 118 | |
| giovannivisentini | 8:a9ecd7687de7 | 119 | |
| giovannivisentini | 8:a9ecd7687de7 | 120 | while(true){ |
| giovannivisentini | 8:a9ecd7687de7 | 121 | |
| giovannivisentini | 8:a9ecd7687de7 | 122 | if(buttonPress){ |
| giovannivisentini | 8:a9ecd7687de7 | 123 | if(tag.openSession()){ |
| giovannivisentini | 8:a9ecd7687de7 | 124 | bool changeStatusResult; |
| giovannivisentini | 8:a9ecd7687de7 | 125 | |
| giovannivisentini | 8:a9ecd7687de7 | 126 | if(tagIsLock) |
| giovannivisentini | 8:a9ecd7687de7 | 127 | changeStatusResult=enableReadOnly(nfcNucleo,tag); |
| giovannivisentini | 8:a9ecd7687de7 | 128 | else |
| giovannivisentini | 8:a9ecd7687de7 | 129 | changeStatusResult=disableReadOnly(nfcNucleo,tag); |
| giovannivisentini | 8:a9ecd7687de7 | 130 | |
| giovannivisentini | 8:a9ecd7687de7 | 131 | if(changeStatusResult) |
| giovannivisentini | 8:a9ecd7687de7 | 132 | tagIsLock=!tagIsLock; |
| giovannivisentini | 8:a9ecd7687de7 | 133 | else |
| giovannivisentini | 8:a9ecd7687de7 | 134 | console.printf("Error change the tag status"); |
| giovannivisentini | 8:a9ecd7687de7 | 135 | tag.closeSession(); |
| giovannivisentini | 8:a9ecd7687de7 | 136 | }else{ |
| giovannivisentini | 8:a9ecd7687de7 | 137 | console.printf("Error opening the session, tag is in use"); |
| giovannivisentini | 8:a9ecd7687de7 | 138 | } |
| giovannivisentini | 8:a9ecd7687de7 | 139 | buttonPress=false; |
| giovannivisentini | 8:a9ecd7687de7 | 140 | }//if |
| giovannivisentini | 8:a9ecd7687de7 | 141 | |
| giovannivisentini | 8:a9ecd7687de7 | 142 | //wait next event |
| giovannivisentini | 8:a9ecd7687de7 | 143 | __WFE(); |
| giovannivisentini | 8:a9ecd7687de7 | 144 | }//while |
| giovannivisentini | 8:a9ecd7687de7 | 145 | |
| giovannivisentini | 8:a9ecd7687de7 | 146 | }//sample_countClick |
| giovannivisentini | 8:a9ecd7687de7 | 147 | |
| giovannivisentini | 8:a9ecd7687de7 | 148 | |
| giovannivisentini | 8:a9ecd7687de7 | 149 | |
| giovannivisentini | 8:a9ecd7687de7 | 150 | |
| giovannivisentini | 8:a9ecd7687de7 | 151 |
