Simple NFC test projekt

Dependencies:   AppNearMe_MuNFC_PN532 mbed-rtos mbed

Fork of AppNearMe_MuNFC_PN532_Test by AppNearMe Official

Committer:
thomasjanke
Date:
Thu Jan 24 09:45:05 2013 +0000
Revision:
6:f14e71ffdc79
Parent:
5:502178f6a842
Minor changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AppNearMe 0:170d2659e354 1 #include "mbed.h"
AppNearMe 0:170d2659e354 2 #include "rtos/rtos.h"
thomasjanke 3:e35b0e15e6b3 3 #include <string>
AppNearMe 0:170d2659e354 4 #include "MuNFC.h"
AppNearMe 0:170d2659e354 5
AppNearMe 0:170d2659e354 6 DigitalOut led_alive(LED1);
AppNearMe 0:170d2659e354 7 DigitalOut led_progress(LED2);
AppNearMe 0:170d2659e354 8 DigitalOut led_ok(LED3);
AppNearMe 0:170d2659e354 9 DigitalOut led_failed(LED4);
AppNearMe 0:170d2659e354 10 Semaphore s(1);
AppNearMe 0:170d2659e354 11
AppNearMe 0:170d2659e354 12 MuNFC nfc("00000003RdfgT390", 1, p11, p12, p13, p19, p18);
thomasjanke 3:e35b0e15e6b3 13 Serial arduino(p9, p10); // tx, rx
AppNearMe 0:170d2659e354 14
AppNearMe 0:170d2659e354 15 #define DEFAULT_PACKET 1
AppNearMe 0:170d2659e354 16
thomasjanke 6:f14e71ffdc79 17 char messageFromNFC[300];
thomasjanke 3:e35b0e15e6b3 18 char c;
thomasjanke 3:e35b0e15e6b3 19 string messageFromArduino;
thomasjanke 3:e35b0e15e6b3 20 string messageToNFC;
AppNearMe 0:170d2659e354 21
AppNearMe 0:170d2659e354 22 //Encode callback
AppNearMe 0:170d2659e354 23 void encode_cb(TLVList* tlv, void *)
AppNearMe 0:170d2659e354 24 {
AppNearMe 0:170d2659e354 25 static uint32_t counter = 0;
AppNearMe 0:170d2659e354 26 counter++;
AppNearMe 0:170d2659e354 27 tlv->putUInt8( DEFAULT_PACKET ); //First uint8_t is packet type
thomasjanke 3:e35b0e15e6b3 28 tlv->putString((char*)messageToNFC.c_str());
AppNearMe 0:170d2659e354 29 tlv->putUInt32( counter );
AppNearMe 0:170d2659e354 30 }
AppNearMe 0:170d2659e354 31
AppNearMe 0:170d2659e354 32 //Decode callback
AppNearMe 0:170d2659e354 33 void decode_cb(TLVList* tlv, void *)
AppNearMe 0:170d2659e354 34 {
AppNearMe 0:170d2659e354 35 if(tlv->getNext() != UINT8)
AppNearMe 0:170d2659e354 36 {
AppNearMe 0:170d2659e354 37 return;
AppNearMe 0:170d2659e354 38 }
AppNearMe 0:170d2659e354 39 if(tlv->getUInt8() == DEFAULT_PACKET) //First uint8_t is packet type
AppNearMe 0:170d2659e354 40 {
AppNearMe 0:170d2659e354 41 if(tlv->getNext() != STRING)
AppNearMe 0:170d2659e354 42 {
AppNearMe 0:170d2659e354 43 return;
AppNearMe 0:170d2659e354 44 }
thomasjanke 6:f14e71ffdc79 45 tlv->getString(messageFromNFC, 299);
thomasjanke 6:f14e71ffdc79 46 printf("Message received from NFC: %s\n",messageFromNFC);
AppNearMe 0:170d2659e354 47 }
AppNearMe 0:170d2659e354 48 s.release();
AppNearMe 0:170d2659e354 49 }
AppNearMe 0:170d2659e354 50
AppNearMe 0:170d2659e354 51 //NFC event
AppNearMe 0:170d2659e354 52 void event_cb(NFCEvent event, void*)
AppNearMe 0:170d2659e354 53 {
AppNearMe 0:170d2659e354 54 switch(event)
AppNearMe 0:170d2659e354 55 {
AppNearMe 0:170d2659e354 56 case NFC_TRANSACTION_STARTED:
AppNearMe 0:170d2659e354 57 led_progress=1;
AppNearMe 0:170d2659e354 58 led_ok=0;
AppNearMe 0:170d2659e354 59 led_failed=0;
AppNearMe 0:170d2659e354 60 break;
AppNearMe 0:170d2659e354 61 case NFC_TRANSACTION_SUCCESSFUL:
AppNearMe 0:170d2659e354 62 led_progress=0;
AppNearMe 0:170d2659e354 63 led_ok=1;
AppNearMe 0:170d2659e354 64 led_failed=0;
AppNearMe 0:170d2659e354 65 break;
AppNearMe 0:170d2659e354 66 case NFC_TRANSACTION_FAILED:
AppNearMe 0:170d2659e354 67 led_progress=0;
AppNearMe 0:170d2659e354 68 led_ok=0;
AppNearMe 0:170d2659e354 69 led_failed=1;
thomasjanke 3:e35b0e15e6b3 70 break;
AppNearMe 0:170d2659e354 71 }
AppNearMe 0:170d2659e354 72 }
AppNearMe 0:170d2659e354 73
AppNearMe 0:170d2659e354 74 int main() {
AppNearMe 0:170d2659e354 75 nfc.encode(encode_cb, NULL);
AppNearMe 0:170d2659e354 76 nfc.decode(decode_cb, NULL);
AppNearMe 0:170d2659e354 77 nfc.event(event_cb, NULL);
AppNearMe 0:170d2659e354 78 s.wait();
AppNearMe 0:170d2659e354 79
AppNearMe 0:170d2659e354 80 bool ret = nfc.init();
AppNearMe 0:170d2659e354 81 if(ret)
AppNearMe 0:170d2659e354 82 {
thomasjanke 4:5ceb737b006b 83 //printf("MuNFC stack initialized\n");
AppNearMe 0:170d2659e354 84
AppNearMe 0:170d2659e354 85 }
AppNearMe 0:170d2659e354 86 else
AppNearMe 0:170d2659e354 87 {
thomasjanke 4:5ceb737b006b 88 //printf("Could not initialize stack\n");
AppNearMe 0:170d2659e354 89 }
AppNearMe 0:170d2659e354 90
thomasjanke 3:e35b0e15e6b3 91 messageFromNFC[0] = '\0';
thomasjanke 5:502178f6a842 92 //messageToNFC = "pay_666_EUR_202_123456789101112131";
thomasjanke 3:e35b0e15e6b3 93 messageToNFC = "cancel";
AppNearMe 0:170d2659e354 94 nfc.run(); //Start thread
AppNearMe 0:170d2659e354 95
thomasjanke 3:e35b0e15e6b3 96
thomasjanke 3:e35b0e15e6b3 97
AppNearMe 0:170d2659e354 98 while(1) {
thomasjanke 4:5ceb737b006b 99 led_alive = 1;
thomasjanke 3:e35b0e15e6b3 100
thomasjanke 3:e35b0e15e6b3 101
thomasjanke 3:e35b0e15e6b3 102 // read messages from arduino
thomasjanke 3:e35b0e15e6b3 103 while(arduino.readable()){
thomasjanke 3:e35b0e15e6b3 104
thomasjanke 3:e35b0e15e6b3 105 c = arduino.getc();
thomasjanke 3:e35b0e15e6b3 106 if (c != '\n'){
thomasjanke 3:e35b0e15e6b3 107 messageFromArduino += c;
thomasjanke 3:e35b0e15e6b3 108 } else {
thomasjanke 4:5ceb737b006b 109 if (messageFromArduino.length() > 1){
thomasjanke 4:5ceb737b006b 110
thomasjanke 4:5ceb737b006b 111 //blink a bit
thomasjanke 4:5ceb737b006b 112 led_alive = 0;
thomasjanke 4:5ceb737b006b 113 wait(0.05);
thomasjanke 4:5ceb737b006b 114 led_alive = 1;
thomasjanke 4:5ceb737b006b 115 wait(0.05);
thomasjanke 4:5ceb737b006b 116 led_alive = 0;
thomasjanke 4:5ceb737b006b 117
thomasjanke 3:e35b0e15e6b3 118 messageToNFC = messageFromArduino;
thomasjanke 4:5ceb737b006b 119 //printf("Set message to: %s\n",messageToNFC);
thomasjanke 3:e35b0e15e6b3 120 }
thomasjanke 3:e35b0e15e6b3 121 messageFromArduino = "";
thomasjanke 3:e35b0e15e6b3 122 }
thomasjanke 3:e35b0e15e6b3 123 }
thomasjanke 3:e35b0e15e6b3 124
thomasjanke 3:e35b0e15e6b3 125 if (messageFromNFC[0] != '\0'){
thomasjanke 3:e35b0e15e6b3 126 arduino.printf("%s\n",messageFromNFC);
thomasjanke 3:e35b0e15e6b3 127 messageFromNFC[0] = '\0';
thomasjanke 3:e35b0e15e6b3 128 }
thomasjanke 3:e35b0e15e6b3 129
thomasjanke 3:e35b0e15e6b3 130 /*
AppNearMe 0:170d2659e354 131 if(s.wait(200) > 0)
AppNearMe 0:170d2659e354 132 {
thomasjanke 3:e35b0e15e6b3 133 printf("Got: %s\n",messageFromNFC);
thomasjanke 3:e35b0e15e6b3 134 }*/
AppNearMe 0:170d2659e354 135 }
AppNearMe 0:170d2659e354 136 }