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 Giovanni Visentini

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 .

Committer:
giovannivisentini
Date:
Mon Feb 01 15:33:26 2016 +0000
Revision:
13:685d95525ec8
Child:
15:94c98d2aa235
add sync and async demos

Who changed what in which revision?

UserRevisionLine numberNew contents of line
giovannivisentini 13:685d95525ec8 1 /**
giovannivisentini 13:685d95525ec8 2 ******************************************************************************
giovannivisentini 13:685d95525ec8 3 * @file Sample_countClick.cpp
giovannivisentini 13:685d95525ec8 4 * @author ST / Central Labs
giovannivisentini 13:685d95525ec8 5 * @date 03 Dic 2015
giovannivisentini 13:685d95525ec8 6 * @brief This demo define a custom record that contains the number of time the user
giovannivisentini 13:685d95525ec8 7 * press the user button
giovannivisentini 13:685d95525ec8 8 ******************************************************************************
giovannivisentini 13:685d95525ec8 9 *
giovannivisentini 13:685d95525ec8 10 * COPYRIGHT(c) 2015 STMicroelectronics
giovannivisentini 13:685d95525ec8 11 *
giovannivisentini 13:685d95525ec8 12 * Redistribution and use in source and binary forms, with or without modification,
giovannivisentini 13:685d95525ec8 13 * are permitted provided that the following conditions are met:
giovannivisentini 13:685d95525ec8 14 * 1. Redistributions of source code must retain the above copyright notice,
giovannivisentini 13:685d95525ec8 15 * this list of conditions and the following disclaimer.
giovannivisentini 13:685d95525ec8 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
giovannivisentini 13:685d95525ec8 17 * this list of conditions and the following disclaimer in the documentation
giovannivisentini 13:685d95525ec8 18 * and/or other materials provided with the distribution.
giovannivisentini 13:685d95525ec8 19 * 3. Neither the name of STMicroelectronics nor the names of its contributors
giovannivisentini 13:685d95525ec8 20 * may be used to endorse or promote products derived from this software
giovannivisentini 13:685d95525ec8 21 * without specific prior written permission.
giovannivisentini 13:685d95525ec8 22 *
giovannivisentini 13:685d95525ec8 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
giovannivisentini 13:685d95525ec8 24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
giovannivisentini 13:685d95525ec8 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
giovannivisentini 13:685d95525ec8 26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
giovannivisentini 13:685d95525ec8 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
giovannivisentini 13:685d95525ec8 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
giovannivisentini 13:685d95525ec8 29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
giovannivisentini 13:685d95525ec8 30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
giovannivisentini 13:685d95525ec8 31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
giovannivisentini 13:685d95525ec8 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
giovannivisentini 13:685d95525ec8 33 *
giovannivisentini 13:685d95525ec8 34 ******************************************************************************
giovannivisentini 13:685d95525ec8 35 */
giovannivisentini 13:685d95525ec8 36
giovannivisentini 13:685d95525ec8 37 #include "mbed.h"
giovannivisentini 13:685d95525ec8 38
giovannivisentini 13:685d95525ec8 39 #include "X_NUCLEO_NFC01A1.h"
giovannivisentini 13:685d95525ec8 40 #include "NDefLib/NDefNfcTag.h"
giovannivisentini 13:685d95525ec8 41 #include "MyRecord.h"
giovannivisentini 13:685d95525ec8 42
giovannivisentini 13:685d95525ec8 43 /* Write a nfc message into a tag
giovannivisentini 13:685d95525ec8 44 * @param nfcNucleo Board where write the data.
giovannivisentini 13:685d95525ec8 45 * @param msg Message to write.
giovannivisentini 13:685d95525ec8 46 */
giovannivisentini 13:685d95525ec8 47 static void writeMessage(X_NUCLEO_NFC01A1 *nfcNucleo,NDefLib::Message &msg){
giovannivisentini 13:685d95525ec8 48 NDefLib::NDefNfcTag& tag = nfcNucleo->getM24SR().getNDefTag();
giovannivisentini 13:685d95525ec8 49 //open the i2c session with the nfc chip
giovannivisentini 13:685d95525ec8 50 if(tag.openSession()){
giovannivisentini 13:685d95525ec8 51 nfcNucleo->getLed1()=! nfcNucleo->getLed1();
giovannivisentini 13:685d95525ec8 52
giovannivisentini 13:685d95525ec8 53 //write the tag
giovannivisentini 13:685d95525ec8 54 if(tag.write(msg)){
giovannivisentini 13:685d95525ec8 55 nfcNucleo->getLed2()=!nfcNucleo->getLed2();
giovannivisentini 13:685d95525ec8 56 }//if
giovannivisentini 13:685d95525ec8 57
giovannivisentini 13:685d95525ec8 58 //close the i2c session
giovannivisentini 13:685d95525ec8 59 if(tag.closeSession())
giovannivisentini 13:685d95525ec8 60 nfcNucleo->getLed3()=!nfcNucleo->getLed3();
giovannivisentini 13:685d95525ec8 61 }//if open session
giovannivisentini 13:685d95525ec8 62 }//writeMessage
giovannivisentini 13:685d95525ec8 63
giovannivisentini 13:685d95525ec8 64
giovannivisentini 13:685d95525ec8 65 static volatile bool buttonPress=false; /// true when the user press the message
giovannivisentini 13:685d95525ec8 66
giovannivisentini 13:685d95525ec8 67 /**
giovannivisentini 13:685d95525ec8 68 * Call back called when the user press the button.
giovannivisentini 13:685d95525ec8 69 */
giovannivisentini 13:685d95525ec8 70 static void setButtonPress(){
giovannivisentini 13:685d95525ec8 71 buttonPress=true;
giovannivisentini 13:685d95525ec8 72 }//if buttonPress
giovannivisentini 13:685d95525ec8 73
giovannivisentini 13:685d95525ec8 74 /**
giovannivisentini 13:685d95525ec8 75 * Write a castum record that count how many times the user press the button.
giovannivisentini 13:685d95525ec8 76 */
giovannivisentini 13:685d95525ec8 77 void sampleSync_countClick() {
giovannivisentini 13:685d95525ec8 78
giovannivisentini 13:685d95525ec8 79 //instance the board with the default paramiters
giovannivisentini 13:685d95525ec8 80 I2C i2cChannel(X_NUCLEO_NFC01A1::DEFAULT_SDA_PIN,X_NUCLEO_NFC01A1::DEFAULT_SDL_PIN);
giovannivisentini 13:685d95525ec8 81 X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel);
giovannivisentini 13:685d95525ec8 82
giovannivisentini 13:685d95525ec8 83 //retrieve the NdefLib interface
giovannivisentini 13:685d95525ec8 84 NDefLib::NDefNfcTag& tag = nfcNucleo->getM24SR().getNDefTag();
giovannivisentini 13:685d95525ec8 85
giovannivisentini 13:685d95525ec8 86 //set the button interrupt
giovannivisentini 13:685d95525ec8 87 InterruptIn userButton(USER_BUTTON);
giovannivisentini 13:685d95525ec8 88 //InterruptIn userButton(SW1);
giovannivisentini 13:685d95525ec8 89 userButton.fall(setButtonPress);
giovannivisentini 13:685d95525ec8 90
giovannivisentini 13:685d95525ec8 91 //create the NDef message and record
giovannivisentini 13:685d95525ec8 92 NDefLib::Message msg;
giovannivisentini 13:685d95525ec8 93 MyRecord rClickCount;
giovannivisentini 13:685d95525ec8 94 msg.addRecord(&rClickCount);
giovannivisentini 13:685d95525ec8 95
giovannivisentini 13:685d95525ec8 96 writeMessage(nfcNucleo,msg);
giovannivisentini 13:685d95525ec8 97
giovannivisentini 13:685d95525ec8 98 while(true){
giovannivisentini 13:685d95525ec8 99
giovannivisentini 13:685d95525ec8 100 if(buttonPress){
giovannivisentini 13:685d95525ec8 101 //change the record content
giovannivisentini 13:685d95525ec8 102 rClickCount.incrementClick();
giovannivisentini 13:685d95525ec8 103 //write the new record content
giovannivisentini 13:685d95525ec8 104 writeMessage(nfcNucleo,msg);
giovannivisentini 13:685d95525ec8 105 //wait a new button press
giovannivisentini 13:685d95525ec8 106 buttonPress=false;
giovannivisentini 13:685d95525ec8 107 }//if
giovannivisentini 13:685d95525ec8 108 //wait next event
giovannivisentini 13:685d95525ec8 109 __WFE();
giovannivisentini 13:685d95525ec8 110 }//while
giovannivisentini 13:685d95525ec8 111
giovannivisentini 13:685d95525ec8 112 }//sample_countClick