Example program for the X-NUCLEO-NFC04A1

Dependencies:   BSP libNDEF ST25DV

Committer:
DiegoOstuni
Date:
Thu Nov 14 11:23:21 2019 +0000
Revision:
0:3f1a69fc394e
Fork of the GitHub

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DiegoOstuni 0:3f1a69fc394e 1 /**
DiegoOstuni 0:3f1a69fc394e 2 ******************************************************************************
DiegoOstuni 0:3f1a69fc394e 3 * @file main.cpp
DiegoOstuni 0:3f1a69fc394e 4 * @author STMicroelectronics
DiegoOstuni 0:3f1a69fc394e 5 * @version V1.0.0
DiegoOstuni 0:3f1a69fc394e 6 * @date 05 April 2019
DiegoOstuni 0:3f1a69fc394e 7 * @brief test application for the STMicrolectronics
DiegoOstuni 0:3f1a69fc394e 8 * X-NUCLEO-NFC04A1. NFC tag based on ST25DV device.
DiegoOstuni 0:3f1a69fc394e 9 * This application makes use of C++ classes obtained from the C
DiegoOstuni 0:3f1a69fc394e 10 * components' drivers.
DiegoOstuni 0:3f1a69fc394e 11 ******************************************************************************
DiegoOstuni 0:3f1a69fc394e 12 */
DiegoOstuni 0:3f1a69fc394e 13 #include "mbed.h"
DiegoOstuni 0:3f1a69fc394e 14 #include "x_nucleo_nfc04.h"
DiegoOstuni 0:3f1a69fc394e 15 #include "stdio.h"
DiegoOstuni 0:3f1a69fc394e 16 #include "string.h"
DiegoOstuni 0:3f1a69fc394e 17
DiegoOstuni 0:3f1a69fc394e 18 using namespace std;
DiegoOstuni 0:3f1a69fc394e 19
DiegoOstuni 0:3f1a69fc394e 20 DigitalOut led_1(D5);
DiegoOstuni 0:3f1a69fc394e 21 DigitalOut led_2(D4);
DiegoOstuni 0:3f1a69fc394e 22 DigitalOut led_3(D2);
DiegoOstuni 0:3f1a69fc394e 23 DigitalOut LPD(D7);
DiegoOstuni 0:3f1a69fc394e 24 DigitalIn MISO(D12);
DiegoOstuni 0:3f1a69fc394e 25 I2C i2cChannel(I2C_SDA, I2C_SCL);
DiegoOstuni 0:3f1a69fc394e 26 DigitalIn uButton(BUTTON1);
DiegoOstuni 0:3f1a69fc394e 27 X_Nucleo_NFC04 X_Nucleo_Nfc04(&i2cChannel, &led_1, &led_2, &led_3, &LPD, &MISO);
DiegoOstuni 0:3f1a69fc394e 28
DiegoOstuni 0:3f1a69fc394e 29 int main()
DiegoOstuni 0:3f1a69fc394e 30 {
DiegoOstuni 0:3f1a69fc394e 31
DiegoOstuni 0:3f1a69fc394e 32 const char uri_write_message[] = "polito.it"; // Uri message to write in the tag
DiegoOstuni 0:3f1a69fc394e 33 const char uri_write_protocol[] = URI_ID_0x01_STRING; // Uri protocol to write in the tag
DiegoOstuni 0:3f1a69fc394e 34 string uri_write = string(uri_write_protocol) + string(uri_write_message);
DiegoOstuni 0:3f1a69fc394e 35
DiegoOstuni 0:3f1a69fc394e 36
DiegoOstuni 0:3f1a69fc394e 37
DiegoOstuni 0:3f1a69fc394e 38 const char SSID[] = "CLAB1"; // WiFi SSID to write in the tag
DiegoOstuni 0:3f1a69fc394e 39 Ndef_Wifi_Authentication_t Auth = NDEF_WIFI_AUTHENTICATION_WPA2;
DiegoOstuni 0:3f1a69fc394e 40 Ndef_Wifi_Encryption_t Encrypt = NDEF_WIFI_ENCRYPTION_AES;
DiegoOstuni 0:3f1a69fc394e 41 const char NetKey[] = "STM32ODE"; // WiFi password to write in the tag
DiegoOstuni 0:3f1a69fc394e 42
DiegoOstuni 0:3f1a69fc394e 43
DiegoOstuni 0:3f1a69fc394e 44 if(X_Nucleo_Nfc04.begin(&i2cChannel, &LPD) == 0) {
DiegoOstuni 0:3f1a69fc394e 45 printf("System Init done!");
DiegoOstuni 0:3f1a69fc394e 46 X_Nucleo_Nfc04.ledOn(&led_3);
DiegoOstuni 0:3f1a69fc394e 47 } else {
DiegoOstuni 0:3f1a69fc394e 48 printf("System Init failed!");
DiegoOstuni 0:3f1a69fc394e 49 while(1);
DiegoOstuni 0:3f1a69fc394e 50 }
DiegoOstuni 0:3f1a69fc394e 51
DiegoOstuni 0:3f1a69fc394e 52
DiegoOstuni 0:3f1a69fc394e 53 int ButtonPressed = 1;
DiegoOstuni 0:3f1a69fc394e 54
DiegoOstuni 0:3f1a69fc394e 55 /* BY pressing the button the option to write the WiFi on the tag, or to write a link( or read from it ) is enabled */
DiegoOstuni 0:3f1a69fc394e 56 while(1)
DiegoOstuni 0:3f1a69fc394e 57 {
DiegoOstuni 0:3f1a69fc394e 58 /* Toggle ButtonPressed variable everytime button is pressed */
DiegoOstuni 0:3f1a69fc394e 59 if(uButton.read() == 0)
DiegoOstuni 0:3f1a69fc394e 60 {
DiegoOstuni 0:3f1a69fc394e 61 ButtonPressed++;
DiegoOstuni 0:3f1a69fc394e 62 if(ButtonPressed == 4)
DiegoOstuni 0:3f1a69fc394e 63 ButtonPressed = 1;
DiegoOstuni 0:3f1a69fc394e 64 while( uButton.read() == 0 ); //Debouncing
DiegoOstuni 0:3f1a69fc394e 65 }
DiegoOstuni 0:3f1a69fc394e 66
DiegoOstuni 0:3f1a69fc394e 67
DiegoOstuni 0:3f1a69fc394e 68 if(ButtonPressed == 1)
DiegoOstuni 0:3f1a69fc394e 69 {
DiegoOstuni 0:3f1a69fc394e 70
DiegoOstuni 0:3f1a69fc394e 71 X_Nucleo_Nfc04.ledOn(&led_1);
DiegoOstuni 0:3f1a69fc394e 72 X_Nucleo_Nfc04.ledOff(&led_2);
DiegoOstuni 0:3f1a69fc394e 73 X_Nucleo_Nfc04.ledOff(&led_3);
DiegoOstuni 0:3f1a69fc394e 74 wait_ms(100);
DiegoOstuni 0:3f1a69fc394e 75 if(X_Nucleo_Nfc04.writeWiFi(SSID, Auth, Encrypt, NetKey, &i2cChannel)) {
DiegoOstuni 0:3f1a69fc394e 76 /* Write failed! */
DiegoOstuni 0:3f1a69fc394e 77 }
DiegoOstuni 0:3f1a69fc394e 78 }
DiegoOstuni 0:3f1a69fc394e 79 if(ButtonPressed == 2)
DiegoOstuni 0:3f1a69fc394e 80 {
DiegoOstuni 0:3f1a69fc394e 81
DiegoOstuni 0:3f1a69fc394e 82 X_Nucleo_Nfc04.ledOn(&led_2);
DiegoOstuni 0:3f1a69fc394e 83 X_Nucleo_Nfc04.ledOff(&led_1);
DiegoOstuni 0:3f1a69fc394e 84 X_Nucleo_Nfc04.ledOff(&led_3);
DiegoOstuni 0:3f1a69fc394e 85 wait_ms(100);
DiegoOstuni 0:3f1a69fc394e 86 if(X_Nucleo_Nfc04.writeURI(uri_write_protocol, uri_write_message, "", &i2cChannel)) {
DiegoOstuni 0:3f1a69fc394e 87 /* Write failed! */
DiegoOstuni 0:3f1a69fc394e 88 }
DiegoOstuni 0:3f1a69fc394e 89 }
DiegoOstuni 0:3f1a69fc394e 90 if(ButtonPressed == 3)
DiegoOstuni 0:3f1a69fc394e 91 {
DiegoOstuni 0:3f1a69fc394e 92 X_Nucleo_Nfc04.ledOn(&led_3);
DiegoOstuni 0:3f1a69fc394e 93 X_Nucleo_Nfc04.ledOff(&led_1);
DiegoOstuni 0:3f1a69fc394e 94 X_Nucleo_Nfc04.ledOff(&led_2);
DiegoOstuni 0:3f1a69fc394e 95 // Prepare device to readURI from NFC device
DiegoOstuni 0:3f1a69fc394e 96 string uri_read = X_Nucleo_Nfc04.readURI( &i2cChannel );
DiegoOstuni 0:3f1a69fc394e 97
DiegoOstuni 0:3f1a69fc394e 98 if(uri_read == "\0") {
DiegoOstuni 0:3f1a69fc394e 99 /* Read failed! */
DiegoOstuni 0:3f1a69fc394e 100 }
DiegoOstuni 0:3f1a69fc394e 101
DiegoOstuni 0:3f1a69fc394e 102
DiegoOstuni 0:3f1a69fc394e 103 if(strcmp( uri_read.c_str(), uri_write.c_str() ) == 0) {
DiegoOstuni 0:3f1a69fc394e 104 /* Successfully written and read! */
DiegoOstuni 0:3f1a69fc394e 105 ButtonPressed = 0;
DiegoOstuni 0:3f1a69fc394e 106
DiegoOstuni 0:3f1a69fc394e 107 } else {
DiegoOstuni 0:3f1a69fc394e 108 /* Read bad string! */
DiegoOstuni 0:3f1a69fc394e 109 X_Nucleo_Nfc04.ledOff(&led_3);
DiegoOstuni 0:3f1a69fc394e 110 X_Nucleo_Nfc04.ledOff(&led_1);
DiegoOstuni 0:3f1a69fc394e 111 X_Nucleo_Nfc04.ledOff(&led_2);
DiegoOstuni 0:3f1a69fc394e 112 }
DiegoOstuni 0:3f1a69fc394e 113
DiegoOstuni 0:3f1a69fc394e 114 }
DiegoOstuni 0:3f1a69fc394e 115 }
DiegoOstuni 0:3f1a69fc394e 116
DiegoOstuni 0:3f1a69fc394e 117
DiegoOstuni 0:3f1a69fc394e 118
DiegoOstuni 0:3f1a69fc394e 119
DiegoOstuni 0:3f1a69fc394e 120
DiegoOstuni 0:3f1a69fc394e 121 }//end of main