Blame view

tweekd_nfc.ino 2.8 KB
55af57c0   henyxia   Customized versio...
1
2
3
4
  #include <SPI.h>
  #include <PN532_SPI.h>
  #include "PN532.h"
  
25fb98d3   henyxia   Rev 1
5
6
7
8
9
10
11
12
13
14
15
16
17
  #define	MODEL_QUERY			0x80
  #define	SERIAL_ERROR		0x55
  #define	NFC_TAGQUERY		0x82
  #define	NFC_TAGQUERY_UID	0x84
  #define	NFC_ARDUINO			0x02
  #define	NFC_NOTAG			0x81
  
  #define	NFC_TYPE_PROFESSOR	0x00
  #define	NFC_TYPE_STUDENT	0x01
  
  boolean	tagDetected;
  boolean	tagType;
  
55af57c0   henyxia   Customized versio...
18
19
20
  PN532_SPI pn532spi(SPI, 10);
  PN532 nfc(pn532spi);
  
25fb98d3   henyxia   Rev 1
21
22
23
24
  void setup(void)
  {
  	int ser;
  
55af57c0   henyxia   Customized versio...
25
  	Serial.begin(115200);
55af57c0   henyxia   Customized versio...
26
27
28
29
  
  	nfc.begin();
  
  	uint32_t versiondata = nfc.getFirmwareVersion();
25fb98d3   henyxia   Rev 1
30
  	if(!versiondata)
55af57c0   henyxia   Customized versio...
31
  	{
25fb98d3   henyxia   Rev 1
32
  		Serial.print(SERIAL_ERROR);
55af57c0   henyxia   Customized versio...
33
  		while (1);
25fb98d3   henyxia   Rev 1
34
35
  	}  
  
55af57c0   henyxia   Customized versio...
36
37
  	nfc.SAMConfig();
    
25fb98d3   henyxia   Rev 1
38
39
40
41
42
43
44
45
46
47
48
  	tagDetected = false;
  
  	do
  	{
  		while(!(Serial.available() > 0));
  		ser = Serial.read();
  		if(ser == MODEL_QUERY)
  			Serial.print(NFC_ARDUINO);
  		else
  			Serial.print(SERIAL_ERROR);
  	}while(ser != MODEL_QUERY);
55af57c0   henyxia   Customized versio...
49
50
51
52
53
54
  }
  
  
  void loop(void)
  {
  	uint8_t success;
25fb98d3   henyxia   Rev 1
55
56
57
58
59
60
  	uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };
  	uint8_t uidLength;
  	uint8_t data1[16];
  	uint8_t data2[16];
  	uint8_t dataf[8];
  	int ser;
55af57c0   henyxia   Customized versio...
61
      
55af57c0   henyxia   Customized versio...
62
  	success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
25fb98d3   henyxia   Rev 1
63
64
  
  	if(success && !tagDetected)
55af57c0   henyxia   Customized versio...
65
  	{
55af57c0   henyxia   Customized versio...
66
67
  		if(uidLength == 4)
  		{
55af57c0   henyxia   Customized versio...
68
  			uint8_t keya[6] = { 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5 };
25fb98d3   henyxia   Rev 1
69
  			success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 44, 0, keya);  
55af57c0   henyxia   Customized versio...
70
71
  			if(success)
  			{
25fb98d3   henyxia   Rev 1
72
  				success = nfc.mifareclassic_ReadDataBlock(44, data1);
55af57c0   henyxia   Customized versio...
73
74
75
  		
  				if(success)
  				{
25fb98d3   henyxia   Rev 1
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  					//nfc.PrintHexChar(data1, 16);
  					success = nfc.mifareclassic_ReadDataBlock(45, data2);
  
  					if(success)
  					{
  						tagDetected = true;
  						if(data1[4] == data1[15] &&
  						data1[5] == data2[0] &&
  						data1[6] == data2[1] &&
  						data1[7] == data2[2] &&
  						data1[8] == data2[3] &&
  						data1[9] == data2[4])
  						{
  							tagType = NFC_TYPE_PROFESSOR;
  							dataf[0] = data1[4];
  							dataf[1] = data1[5];
  							dataf[2] = data1[6];
  							dataf[3] = data1[7];
  							dataf[4] = data1[8];
  							dataf[5] = data1[9];
  						}
  						else
  						{
  							tagType = NFC_TYPE_STUDENT;
  							dataf[0] = data1[15];
  							dataf[1] = data2[0];
  							dataf[2] = data2[1];
  							dataf[3] = data2[2];
  							dataf[4] = data2[3];
  							dataf[5] = data2[4];
  							dataf[6] = data2[5];
  							dataf[7] = data2[6];
  						}
  					}
55af57c0   henyxia   Customized versio...
110
111
  				}
  			}
55af57c0   henyxia   Customized versio...
112
  		}
25fb98d3   henyxia   Rev 1
113
114
115
116
117
118
119
120
  	}
  	else if(tagDetected)
  	{
  		while(!(Serial.available() > 0));
  		ser = Serial.read();
  		if(ser == NFC_TAGQUERY)
  			Serial.print(tagType);
  		else if(ser == NFC_TAGQUERY_UID)
55af57c0   henyxia   Customized versio...
121
  		{
25fb98d3   henyxia   Rev 1
122
123
124
125
126
127
128
  			Serial.print((char) dataf[0]);
  			Serial.print((char) dataf[1]);
  			Serial.print((char) dataf[2]);
  			Serial.print((char) dataf[3]);
  			Serial.print((char) dataf[4]);
  			Serial.print((char) dataf[5]);
  			if(tagType == NFC_TYPE_STUDENT)
55af57c0   henyxia   Customized versio...
129
  			{
25fb98d3   henyxia   Rev 1
130
131
  				Serial.print((char) dataf[6]);
  				Serial.print((char) dataf[7]);
55af57c0   henyxia   Customized versio...
132
133
  			}
  		}
25fb98d3   henyxia   Rev 1
134
135
136
137
138
139
140
141
  		else
  			Serial.print(SERIAL_ERROR);
  	}
  	else
  	{
  		if(Serial.available() > 0)
  			if(Serial.read() == NFC_TAGQUERY)
  				Serial.print(NFC_NOTAG);
55af57c0   henyxia   Customized versio...
142
143
  	}
  }