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 .
Diff: main.cpp
- Revision:
- 2:0648c1561eb2
- Parent:
- 1:6d202b62ed68
- Child:
- 3:f7f818ee694e
--- a/main.cpp Fri Nov 27 15:10:25 2015 +0000 +++ b/main.cpp Tue Dec 01 08:30:54 2015 +0000 @@ -21,6 +21,7 @@ DigitalOut myled(LED1); + void shiftLed(DigitalOut &led1,DigitalOut &led2,DigitalOut &led3){ const uint8_t prevLed1=led1; const uint8_t prevLed2=led2; @@ -30,18 +31,13 @@ led3=prevLed2; } - static const PinName M24SR_SDA=D14; static const PinName M24SR_SDL=D15; static const uint32_t MAX_WRITE_TRY=1; -I2C i2cChannel(M24SR_SDA,M24SR_SDL); -M24SR *m24srDrv; +void setNFCTag(NDefLib::Type4NfcTag &tag){ -void setNFCTag(){ - - Type4NfcTagSTM24SR tag(*m24srDrv); bool writeStatus,closeStatus; if(tag.openSession()){ NDefLib::Message msg; @@ -52,7 +48,7 @@ NDefLib::RecordSMS rSMS("123456789","st.com.BlueMS"); msg.addRecord(&rSMS); - NDefLib::RecordGeo rGeo("123.123","456.789"); + NDefLib::RecordGeo rGeo(123.123,-456.789); msg.addRecord(&rGeo); NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"http://www.st.com"); @@ -61,22 +57,21 @@ NDefLib::RecordMail rMail("mail@st.com","ciao","da nfc tag"); msg.addRecord(&rMail); - NDefLib::RecordMimeType rText1("text/plain","ciao"); + NDefLib::RecordMimeType rText1("text/plain",(const uint8_t*)"ciao",4); msg.addRecord(&rText1); NDefLib::RecordText rText3(NDefLib::RecordText::UTF8,"it","ciao"); msg.addRecord(&rText3); - NDefLib::RecordVCard::VCardInfo_t cardInfo; - cardInfo[NDefLib::RecordVCard::FORMATTED_NAME]="prova2 prova1"; + cardInfo[NDefLib::RecordVCard::FORMATTED_NAME]="prova prova1"; cardInfo[NDefLib::RecordVCard::ADDRESS_HOME]=";;1 Main St.;Springfield;IL;12345;USA"; cardInfo[NDefLib::RecordVCard::ADDRESS_WORK]=";;2 Main St.;Springfield;IL;12345;USA"; cardInfo[NDefLib::RecordVCard::EMAIL_WORK]="workmail@st.com"; cardInfo[NDefLib::RecordVCard::EMAIL_HOME]="homemail@st.com"; cardInfo[NDefLib::RecordVCard::GEO]="39.95;-75.1667"; cardInfo[NDefLib::RecordVCard::IMPP]="aim:johndoe@aol.com"; - cardInfo[NDefLib::RecordVCard::NAME]="prova4;prova5"; + cardInfo[NDefLib::RecordVCard::NAME]="prova2;prova3"; cardInfo[NDefLib::RecordVCard::NICKNAME]="test"; cardInfo[NDefLib::RecordVCard::NOTE]="A good test"; cardInfo[NDefLib::RecordVCard::ORGANIZATION]="STM"; @@ -163,25 +158,25 @@ break;} case Record::TYPE_URI_MAIL:{ RecordMail *temp = (RecordMail*)r; - pc.printf("Read Dest: %s\r\n",temp->getDest().c_str()); + pc.printf("Read Dest: %s\r\n",temp->getToAddress().c_str()); pc.printf("Read Subject: %s\r\n",temp->getSubject().c_str()); pc.printf("Read Body: %s\r\n",temp->getBody().c_str()); break;} - case Record::TYPE_SMS:{ + case Record::TYPE_URI_SMS:{ RecordSMS *temp = (RecordSMS*)r; pc.printf("Read number: %s\r\n",temp->getNumber().c_str()); pc.printf("Read message: %s\r\n",temp->getMessagge().c_str()); break;} - case Record::TYPE_GEOLOCATION:{ + case Record::TYPE_URI_GEOLOCATION:{ RecordGeo *temp = (RecordGeo*)r; - pc.printf("Read lat: %s\r\n",temp->getLatitude().c_str()); - pc.printf("Read long: %s\r\n",temp->getLongitude().c_str()); + pc.printf("Read lat: %f\r\n",temp->getLatitude()); + pc.printf("Read long: %f\r\n",temp->getLongitude()); break;} case Record::TYPE_MIME_VCARD:{ - RecordVCard *temp = (RecordVCard*)r; + const RecordVCard *temp = (RecordVCard*)r; pc.printf("Read Name: %s\r\n",(*temp)[RecordVCard::NAME].c_str()); pc.printf("Read Mail: %s\r\n",(*temp)[RecordVCard::EMAIL_WORK].c_str()); - pc.printf("Read org: %s\r\n",(*temp)[RecordVCard::ORGANIZATION].c_str()); + pc.printf("Read ORG: %s\r\n",(*temp)[RecordVCard::ORGANIZATION].c_str()); break;} case Record::TYPE_UNKNOWN:{ pc.printf("Unknown record\r\n"); @@ -189,9 +184,55 @@ }//switch } -void readNfcTag(){ +void changeRecord(NDefLib::Record *r){ using namespace NDefLib; - Type4NfcTagSTM24SR tag(*m24srDrv); + switch(r->getType()){ + case Record::TYPE_TEXT: { + RecordText *temp = (RecordText*)r; + temp->setText("CIAOCiao"); + break; } + case Record::TYPE_AAR:{ + RecordAAR *temp = (RecordAAR*)r; + temp->setPackage("set Package Ok"); + break; } + case Record::TYPE_MIME:{ + RecordMimeType *temp = (RecordMimeType*)r; + temp->copyMimeData((const uint8_t *)"String2",sizeof("String2")); + break;} + case Record::TYPE_URI:{ + RecordURI *temp = (RecordURI*)r; + temp->setContent("google.it"); + break;} + case Record::TYPE_URI_MAIL:{ + RecordMail *temp = (RecordMail*)r; + temp->setToAddress("newMail@st.com"); + temp->setSubject("tag change"); + temp->setBody("read/change Works!"); + break;} + case Record::TYPE_URI_SMS:{ + RecordSMS *temp = (RecordSMS*)r; + temp->setMessage("Message Change"); + temp->setNumber("0987654321"); + break;} + case Record::TYPE_URI_GEOLOCATION:{ + RecordGeo *temp = (RecordGeo*)r; + temp->setLatitude(-temp->getLatitude()); + temp->setLongitude(-temp->getLongitude()); + break;} + case Record::TYPE_MIME_VCARD:{ + RecordVCard *temp = (RecordVCard*)r; + (*temp)[RecordVCard::NAME]="name change"; + (*temp)[RecordVCard::NICKNAME]="nic change"; + break;} + case Record::TYPE_UNKNOWN:{ + pc.printf("Unknown record\r\n"); + break;} + }//switch +} + +void readNfcTag(NDefLib::Type4NfcTag &tag){ + using namespace NDefLib; + if(tag.openSession()){ NDefLib::Message readMsg; @@ -201,7 +242,7 @@ pc.printf("Error Read\r\n"); }else{ for(uint32_t i=0;i<readMsg.getNRecords();i++){ - Record *r = readMsg.getRecord(i); + Record *r = readMsg[i]; printRecord(r); delete r; }//for @@ -213,25 +254,62 @@ } } + +void changeNfcTag(NDefLib::Type4NfcTag &tag){ + using namespace NDefLib; + + if(tag.openSession()){ + NDefLib::Message readMsg; + + tag.read(&readMsg); + + if(readMsg.getNRecords()==0){ + pc.printf("Error Read\r\n"); + }else{ + for(uint32_t i=0;i<readMsg.getNRecords();i++){ + Record *r = readMsg[i]; + changeRecord(r); + }//for + tag.write(readMsg); + }//if-else + + tag.closeSession(); + }else{ + pc.printf("Error open SessionChange\n\r"); + } +} + +static bool buttonPress=false; +void setButtonPress(){ + buttonPress=true; +} + int main() { + I2C i2cChannel(M24SR_SDA,M24SR_SDL); i2cChannel.frequency(400000); X_NUCLEO_NFC01A1 *nfcNucleo = X_NUCLEO_NFC01A1::Instance(i2cChannel); - m24srDrv=&nfcNucleo->getM24SR(); - + Type4NfcTagSTM24SR tag(nfcNucleo->getM24SR()); + InterruptIn mybutton(USER_BUTTON); pc.printf("Hello World !\n\r"); nfcNucleo->getLed1()=1; nfcNucleo->getLed2()=0; nfcNucleo->getLed3()=0; - setNFCTag(); - readNfcTag(); - //pc.printf("2!\n\r"); - //setNFCTag(); - //readNfcTag(); + setNFCTag(tag); + readNfcTag(tag); + + mybutton.fall(setButtonPress); + while(1) { wait(1); myled = !myled; shiftLed(nfcNucleo->getLed1(),nfcNucleo->getLed2(),nfcNucleo->getLed3()); + if(buttonPress){ + changeNfcTag(tag); + readNfcTag(tag); + buttonPress=false; + } } } +