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 .
main.cpp
- Committer:
- giovannivisentini
- Date:
- 2015-12-01
- Revision:
- 2:0648c1561eb2
- Parent:
- 1:6d202b62ed68
- Child:
- 3:f7f818ee694e
File content as of revision 2:0648c1561eb2:
#include <cstring> #include "mbed.h" #include "m24sr/m24sr_class.h" //#include "lib_NDEF/lib_TagType4.h" #include "Type4NfcTagSTM24SR.h" #include "NDefLib/RecordType/RecordAAR.h" #include "NDefLib/RecordType/RecordSMS.h" #include "NDefLib/RecordType/RecordGeo.h" #include "NDefLib/RecordType/RecordURI.h" #include "NDefLib/RecordType/RecordMail.h" #include "NDefLib/RecordType/RecordText.h" #include "NDefLib/RecordType/RecordMimeType.h" #include "NDefLib/RecordType/RecordVCard.h" #include "X_NUCLEO_NFC01A1.h" Serial pc(SERIAL_TX, SERIAL_RX); DigitalOut myled(LED1); void shiftLed(DigitalOut &led1,DigitalOut &led2,DigitalOut &led3){ const uint8_t prevLed1=led1; const uint8_t prevLed2=led2; const uint8_t prevLed3=led3; led1=prevLed3; led2=prevLed1; led3=prevLed2; } static const PinName M24SR_SDA=D14; static const PinName M24SR_SDL=D15; static const uint32_t MAX_WRITE_TRY=1; void setNFCTag(NDefLib::Type4NfcTag &tag){ bool writeStatus,closeStatus; if(tag.openSession()){ NDefLib::Message msg; NDefLib::RecordAAR rAAR("com.st.BlueMS"); msg.addRecord(&rAAR); NDefLib::RecordSMS rSMS("123456789","st.com.BlueMS"); msg.addRecord(&rSMS); NDefLib::RecordGeo rGeo(123.123,-456.789); msg.addRecord(&rGeo); NDefLib::RecordURI rUri(NDefLib::RecordURI::HTTP_WWW,"http://www.st.com"); msg.addRecord(&rUri); NDefLib::RecordMail rMail("mail@st.com","ciao","da nfc tag"); msg.addRecord(&rMail); 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]="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]="prova2;prova3"; cardInfo[NDefLib::RecordVCard::NICKNAME]="test"; cardInfo[NDefLib::RecordVCard::NOTE]="A good test"; cardInfo[NDefLib::RecordVCard::ORGANIZATION]="STM"; cardInfo[NDefLib::RecordVCard::TEL_HOME]="123"; cardInfo[NDefLib::RecordVCard::TEL_MOBILE]="456"; cardInfo[NDefLib::RecordVCard::TEL_WORK]="789"; cardInfo[NDefLib::RecordVCard::TITLE]="King"; cardInfo[NDefLib::RecordVCard::URL]="www.st.com"; cardInfo[NDefLib::RecordVCard::PHOTO_URI]="http://www.st.com/st-web-ui/static/active/en/fragment/multimedia/image/picture/customer_focus.jpg"; NDefLib::RecordVCard rVCard(cardInfo); msg.addRecord(&rVCard); uint32_t writeTry=0; do{ writeStatus = tag.write(msg); writeTry++; }while(!writeStatus && writeTry<MAX_WRITE_TRY); closeStatus = tag.closeSession(); }else pc.printf("Error open Session\n\r"); if(writeStatus) pc.printf("writeOk\n\r"); else pc.printf("writeFail\n\r"); if(closeStatus) pc.printf("closeOk\n\r"); else pc.printf("CloseFail\n\r"); /* sURI_Info URI; sAARInfo aar; sSMSInfo sms; std::strcpy(sms.PhoneNumber,"123456789\0"); std::strcpy(sms.Message,"st.com.BlueMS\0"); std::strcpy(sms.Information,"\0"); std::strcpy(aar.PakageName,"st.com.BlueMS"); // Prepare URI NDEF message content std::strcpy(URI.protocol,URI_ID_0x01_STRING); std::strcpy(URI.URI_Message,"st3.com"); std::strcpy(URI.Information,"\0"); while (TT4_Init() != NFC_OK); // Prepare URI NDEF message content std::strcpy(URI.protocol,URI_ID_0x01_STRING); std::strcpy(URI.URI_Message,"st2.com"); std::strcpy(URI.Information,"\0"); //while (TT4_WriteURI(&URI) != NFC_OK); while (TT4_WriteSMS(&sms) != NFC_OK); //while (TT4_Write(&sms) != NFC_OK); */ } void printRecord(NDefLib::Record *r){ using namespace NDefLib; switch(r->getType()){ case Record::TYPE_TEXT: { RecordText *temp = (RecordText*)r; pc.printf("Read Text: %s\r\n",temp->getText().c_str()); break; } case Record::TYPE_AAR:{ RecordAAR *temp = (RecordAAR*)r; pc.printf("Read ARR: %s\r\n",temp->getPackage().c_str()); break; } case Record::TYPE_MIME:{ RecordMimeType *temp = (RecordMimeType*)r; pc.printf("Read mimeType: %s\r\n",temp->getMimeType().c_str()); pc.printf("Read mimeData: %s\r\n", std::string((const char*)temp->getMimeData(), temp->getMimeDataLenght()).c_str()); break;} case Record::TYPE_URI:{ RecordURI *temp = (RecordURI*)r; pc.printf("Read uriId: %d\r\n",temp->getUriId()); pc.printf("Read uriType: %s\r\n",temp->getUriType().c_str()); pc.printf("Read uriContent: %s\r\n",temp->getContent().c_str()); break;} case Record::TYPE_URI_MAIL:{ RecordMail *temp = (RecordMail*)r; 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_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_URI_GEOLOCATION:{ RecordGeo *temp = (RecordGeo*)r; pc.printf("Read lat: %f\r\n",temp->getLatitude()); pc.printf("Read long: %f\r\n",temp->getLongitude()); break;} case Record::TYPE_MIME_VCARD:{ 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()); break;} case Record::TYPE_UNKNOWN:{ pc.printf("Unknown record\r\n"); break;} }//switch } void changeRecord(NDefLib::Record *r){ using namespace NDefLib; 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; 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]; printRecord(r); delete r; }//for }//if-else tag.closeSession(); }else{ pc.printf("Error open Session2\n\r"); } } 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); Type4NfcTagSTM24SR tag(nfcNucleo->getM24SR()); InterruptIn mybutton(USER_BUTTON); pc.printf("Hello World !\n\r"); nfcNucleo->getLed1()=1; nfcNucleo->getLed2()=0; nfcNucleo->getLed3()=0; 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; } } }