![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
0
Dependencies: mbed NDefLib X_NUCLEO_NFC01A1
Dependents: Projekt KPG Program_KPG_2019
main.cpp@17:22e6c00f3a4c, 2016-08-31 (annotated)
- Committer:
- giovannivisentini
- Date:
- Wed Aug 31 11:23:41 2016 +0000
- Revision:
- 17:22e6c00f3a4c
- Parent:
- 14:1b99908360c5
- Child:
- 20:3a6e6ae2d691
add example for read the NFC message from the tag
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
giovannivisentini | 0:151ae20d098f | 1 | /** |
giovannivisentini | 0:151ae20d098f | 2 | ****************************************************************************** |
giovannivisentini | 0:151ae20d098f | 3 | * @file main.cpp |
giovannivisentini | 4:4145a2db3349 | 4 | * @author ST Central Labs |
giovannivisentini | 7:435297dd022d | 5 | * @version V1.0.0 |
giovannivisentini | 4:4145a2db3349 | 6 | * @date 21 Dic 2015 |
giovannivisentini | 6:44204e733f96 | 7 | * @brief This demo writes an ndef message with an url inside. |
giovannivisentini | 0:151ae20d098f | 8 | ****************************************************************************** |
giovannivisentini | 6:44204e733f96 | 9 | * @attention |
giovannivisentini | 0:151ae20d098f | 10 | * |
giovannivisentini | 6:44204e733f96 | 11 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
giovannivisentini | 0:151ae20d098f | 12 | * |
giovannivisentini | 0:151ae20d098f | 13 | * Redistribution and use in source and binary forms, with or without modification, |
giovannivisentini | 0:151ae20d098f | 14 | * are permitted provided that the following conditions are met: |
giovannivisentini | 0:151ae20d098f | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
giovannivisentini | 0:151ae20d098f | 16 | * this list of conditions and the following disclaimer. |
giovannivisentini | 0:151ae20d098f | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
giovannivisentini | 0:151ae20d098f | 18 | * this list of conditions and the following disclaimer in the documentation |
giovannivisentini | 0:151ae20d098f | 19 | * and/or other materials provided with the distribution. |
giovannivisentini | 0:151ae20d098f | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
giovannivisentini | 0:151ae20d098f | 21 | * may be used to endorse or promote products derived from this software |
giovannivisentini | 0:151ae20d098f | 22 | * without specific prior written permission. |
giovannivisentini | 0:151ae20d098f | 23 | * |
giovannivisentini | 0:151ae20d098f | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
giovannivisentini | 0:151ae20d098f | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
giovannivisentini | 0:151ae20d098f | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
giovannivisentini | 0:151ae20d098f | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
giovannivisentini | 0:151ae20d098f | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
giovannivisentini | 0:151ae20d098f | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
giovannivisentini | 0:151ae20d098f | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
giovannivisentini | 0:151ae20d098f | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
giovannivisentini | 0:151ae20d098f | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
giovannivisentini | 0:151ae20d098f | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
giovannivisentini | 0:151ae20d098f | 34 | * |
giovannivisentini | 0:151ae20d098f | 35 | ****************************************************************************** |
giovannivisentini | 0:151ae20d098f | 36 | */ |
giovannivisentini | 11:925a5a491a5c | 37 | |
giovannivisentini | 0:151ae20d098f | 38 | #include "mbed.h" |
giovannivisentini | 0:151ae20d098f | 39 | |
giovannivisentini | 0:151ae20d098f | 40 | #include "X_NUCLEO_NFC01A1.h" |
giovannivisentini | 4:4145a2db3349 | 41 | #include "NDefLib/NDefNfcTag.h" |
giovannivisentini | 0:151ae20d098f | 42 | #include "NDefLib/RecordType/RecordURI.h" |
giovannivisentini | 0:151ae20d098f | 43 | |
giovannivisentini | 0:151ae20d098f | 44 | /** |
giovannivisentini | 6:44204e733f96 | 45 | * Write a Ndef URI message linking to st.com site. |
giovannivisentini | 17:22e6c00f3a4c | 46 | * Write an NDef message with a Uri record linking the st.com site |
giovannivisentini | 17:22e6c00f3a4c | 47 | * @param nfcNucleo expansion board where write the NDef message |
giovannivisentini | 17:22e6c00f3a4c | 48 | */ |
giovannivisentini | 17:22e6c00f3a4c | 49 | static void writeUrl(X_NUCLEO_NFC01A1 *nfcNucleo){ |
giovannivisentini | 6:44204e733f96 | 50 | //retrieve the NdefLib interface |
giovannivisentini | 17:22e6c00f3a4c | 51 | NDefLib::NDefNfcTag& tag = nfcNucleo->getM24SR().getNDefTag(); |
giovannivisentini | 6:44204e733f96 | 52 | |
giovannivisentini | 0:151ae20d098f | 53 | //open the i2c session with the nfc chip |
giovannivisentini | 0:151ae20d098f | 54 | if(tag.openSession()){ |
giovannivisentini | 14:1b99908360c5 | 55 | printf("Session opened\n\r"); |
giovannivisentini | 0:151ae20d098f | 56 | nfcNucleo->getLed1()=1; |
giovannivisentini | 0:151ae20d098f | 57 | |
giovannivisentini | 0:151ae20d098f | 58 | //create the NDef message and record |
giovannivisentini | 0:151ae20d098f | 59 | NDefLib::Message msg; |
giovannivisentini | 0:151ae20d098f | 60 | NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"st.com"); |
giovannivisentini | 0:151ae20d098f | 61 | msg.addRecord(&rUri); |
giovannivisentini | 0:151ae20d098f | 62 | |
giovannivisentini | 0:151ae20d098f | 63 | //write the tag |
giovannivisentini | 0:151ae20d098f | 64 | if(tag.write(msg)){ |
giovannivisentini | 14:1b99908360c5 | 65 | printf("Tag written\n\r"); |
giovannivisentini | 0:151ae20d098f | 66 | nfcNucleo->getLed2()=1; |
giovannivisentini | 1:eff1b8998438 | 67 | }else{ |
giovannivisentini | 7:435297dd022d | 68 | printf("Error writing \n\r"); |
giovannivisentini | 1:eff1b8998438 | 69 | }//if-else |
giovannivisentini | 0:151ae20d098f | 70 | |
giovannivisentini | 0:151ae20d098f | 71 | //close the i2c session |
giovannivisentini | 1:eff1b8998438 | 72 | if(tag.closeSession()){ |
giovannivisentini | 7:435297dd022d | 73 | printf("Session closed\n\r"); |
giovannivisentini | 1:eff1b8998438 | 74 | nfcNucleo->getLed3()=1; |
giovannivisentini | 1:eff1b8998438 | 75 | }else{ |
giovannivisentini | 14:1b99908360c5 | 76 | printf("Error closing the session\n\r"); |
giovannivisentini | 1:eff1b8998438 | 77 | }//if-else |
giovannivisentini | 1:eff1b8998438 | 78 | |
giovannivisentini | 0:151ae20d098f | 79 | }else |
giovannivisentini | 14:1b99908360c5 | 80 | printf("Error opening the session\n\r"); |
giovannivisentini | 17:22e6c00f3a4c | 81 | } |
giovannivisentini | 17:22e6c00f3a4c | 82 | |
giovannivisentini | 17:22e6c00f3a4c | 83 | /** |
giovannivisentini | 17:22e6c00f3a4c | 84 | * Read a NDef message and print content and type of all the uri record inside |
giovannivisentini | 17:22e6c00f3a4c | 85 | * the message |
giovannivisentini | 17:22e6c00f3a4c | 86 | * @param nfcNucleo expansion board from where read the message |
giovannivisentini | 17:22e6c00f3a4c | 87 | */ |
giovannivisentini | 17:22e6c00f3a4c | 88 | static void readUrl(X_NUCLEO_NFC01A1 *nfcNucleo){ |
giovannivisentini | 17:22e6c00f3a4c | 89 | //retrieve the NdefLib interface |
giovannivisentini | 17:22e6c00f3a4c | 90 | NDefLib::NDefNfcTag& tag = nfcNucleo->getM24SR().getNDefTag(); |
giovannivisentini | 17:22e6c00f3a4c | 91 | |
giovannivisentini | 17:22e6c00f3a4c | 92 | //open the i2c session with the nfc chip |
giovannivisentini | 17:22e6c00f3a4c | 93 | if(tag.openSession()){ |
giovannivisentini | 17:22e6c00f3a4c | 94 | printf("Session opened\n\r"); |
giovannivisentini | 17:22e6c00f3a4c | 95 | nfcNucleo->getLed1()=1; |
giovannivisentini | 17:22e6c00f3a4c | 96 | |
giovannivisentini | 17:22e6c00f3a4c | 97 | //create the NDef message and record |
giovannivisentini | 17:22e6c00f3a4c | 98 | NDefLib::Message msg; |
giovannivisentini | 17:22e6c00f3a4c | 99 | |
giovannivisentini | 17:22e6c00f3a4c | 100 | //read the tag |
giovannivisentini | 17:22e6c00f3a4c | 101 | if(tag.read(&msg)){ |
giovannivisentini | 17:22e6c00f3a4c | 102 | const uint32_t nRecords = msg.getNRecords(); |
giovannivisentini | 17:22e6c00f3a4c | 103 | printf("Tag read: %d record\n\r",msg.getNRecords()); |
giovannivisentini | 17:22e6c00f3a4c | 104 | for(int i =0 ;i<nRecords ;i++){ |
giovannivisentini | 17:22e6c00f3a4c | 105 | if(msg[i]->getType()== NDefLib::Record::TYPE_URI){ |
giovannivisentini | 17:22e6c00f3a4c | 106 | NDefLib::RecordURI *rUri = (NDefLib::RecordURI *)msg[i]; |
giovannivisentini | 17:22e6c00f3a4c | 107 | printf("UriType: %x\n\rUriContent: %s\n\r", |
giovannivisentini | 17:22e6c00f3a4c | 108 | rUri->getUriId(), |
giovannivisentini | 17:22e6c00f3a4c | 109 | rUri->getContent().c_str()); |
giovannivisentini | 17:22e6c00f3a4c | 110 | }//if |
giovannivisentini | 17:22e6c00f3a4c | 111 | }//for |
giovannivisentini | 17:22e6c00f3a4c | 112 | //free the read records |
giovannivisentini | 17:22e6c00f3a4c | 113 | NDefLib::Message::removeAndDeleteAllRecord(msg); |
giovannivisentini | 17:22e6c00f3a4c | 114 | }else{ |
giovannivisentini | 17:22e6c00f3a4c | 115 | printf("Error Reading \n\r"); |
giovannivisentini | 17:22e6c00f3a4c | 116 | }//if-else |
giovannivisentini | 17:22e6c00f3a4c | 117 | |
giovannivisentini | 17:22e6c00f3a4c | 118 | //close the i2c session |
giovannivisentini | 17:22e6c00f3a4c | 119 | if(tag.closeSession()){ |
giovannivisentini | 17:22e6c00f3a4c | 120 | printf("Session closed\n\r"); |
giovannivisentini | 17:22e6c00f3a4c | 121 | nfcNucleo->getLed3()=1; |
giovannivisentini | 17:22e6c00f3a4c | 122 | }else{ |
giovannivisentini | 17:22e6c00f3a4c | 123 | printf("Error closing the session\n\r"); |
giovannivisentini | 17:22e6c00f3a4c | 124 | }//if-else |
giovannivisentini | 17:22e6c00f3a4c | 125 | |
giovannivisentini | 17:22e6c00f3a4c | 126 | }else |
giovannivisentini | 17:22e6c00f3a4c | 127 | printf("Error opening the session\n\r"); |
giovannivisentini | 17:22e6c00f3a4c | 128 | } |
giovannivisentini | 17:22e6c00f3a4c | 129 | |
giovannivisentini | 17:22e6c00f3a4c | 130 | static volatile bool buttonPress=false; |
giovannivisentini | 17:22e6c00f3a4c | 131 | |
giovannivisentini | 17:22e6c00f3a4c | 132 | static void setButtonPress(){ |
giovannivisentini | 17:22e6c00f3a4c | 133 | buttonPress=true; |
giovannivisentini | 17:22e6c00f3a4c | 134 | }//if buttonPress |
giovannivisentini | 17:22e6c00f3a4c | 135 | |
giovannivisentini | 17:22e6c00f3a4c | 136 | /** |
giovannivisentini | 17:22e6c00f3a4c | 137 | * Write a Ndef URI message linking to st.com site and than read the message from |
giovannivisentini | 17:22e6c00f3a4c | 138 | * the Nfc tag. |
giovannivisentini | 17:22e6c00f3a4c | 139 | * |
giovannivisentini | 17:22e6c00f3a4c | 140 | */ |
giovannivisentini | 17:22e6c00f3a4c | 141 | int main(void){ |
giovannivisentini | 17:22e6c00f3a4c | 142 | |
giovannivisentini | 17:22e6c00f3a4c | 143 | //use default board pinout |
giovannivisentini | 17:22e6c00f3a4c | 144 | I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN); |
giovannivisentini | 17:22e6c00f3a4c | 145 | X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel,NULL, |
giovannivisentini | 17:22e6c00f3a4c | 146 | X_NUCLEO_NFC01A1::DEFAULT_GPO_PIN,X_NUCLEO_NFC01A1::DEFAULT_RF_DISABLE_PIN, |
giovannivisentini | 17:22e6c00f3a4c | 147 | X_NUCLEO_NFC01A1::DEFAULT_LED1_PIN,X_NUCLEO_NFC01A1::DEFAULT_LED2_PIN, |
giovannivisentini | 17:22e6c00f3a4c | 148 | X_NUCLEO_NFC01A1::DEFAULT_LED3_PIN); |
giovannivisentini | 17:22e6c00f3a4c | 149 | |
giovannivisentini | 17:22e6c00f3a4c | 150 | printf("System Init done: !\n\r"); |
giovannivisentini | 17:22e6c00f3a4c | 151 | |
giovannivisentini | 17:22e6c00f3a4c | 152 | // write an URI message |
giovannivisentini | 17:22e6c00f3a4c | 153 | writeUrl(nfcNucleo); |
giovannivisentini | 17:22e6c00f3a4c | 154 | |
giovannivisentini | 17:22e6c00f3a4c | 155 | //if run on a nucleo board enable the user button for read the ndef record |
giovannivisentini | 17:22e6c00f3a4c | 156 | #if defined(TARGET_STM) |
giovannivisentini | 17:22e6c00f3a4c | 157 | InterruptIn userButton(USER_BUTTON); |
giovannivisentini | 17:22e6c00f3a4c | 158 | userButton.fall(setButtonPress); |
giovannivisentini | 17:22e6c00f3a4c | 159 | while(true){ |
giovannivisentini | 17:22e6c00f3a4c | 160 | if(buttonPress){ |
giovannivisentini | 17:22e6c00f3a4c | 161 | readUrl(nfcNucleo); |
giovannivisentini | 17:22e6c00f3a4c | 162 | buttonPress=false; |
giovannivisentini | 17:22e6c00f3a4c | 163 | }//if |
giovannivisentini | 17:22e6c00f3a4c | 164 | //wait next event |
giovannivisentini | 17:22e6c00f3a4c | 165 | __WFE(); |
giovannivisentini | 17:22e6c00f3a4c | 166 | }//while |
giovannivisentini | 17:22e6c00f3a4c | 167 | #else |
giovannivisentini | 17:22e6c00f3a4c | 168 | readUrl(nfcNucleo); |
giovannivisentini | 17:22e6c00f3a4c | 169 | #endif |
giovannivisentini | 17:22e6c00f3a4c | 170 | } |