This repository has been archived by the owner on Apr 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
RFIDDB.cpp
107 lines (101 loc) · 2.38 KB
/
RFIDDB.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
/*
RFIDDB.h - HackPittsburgh RFID System DB
Author: Marty McGuire <marty@hackpittsburgh.org>, Nov. 2009
Released under CC3.0-BY-SA - http://creativecommons.org/licenses/by-sa/3.0/
*/
#include "WProgram.h"
#include "RFIDDB.h"
#include "../EEPROM/EEPROM.h"
RFIDDB::RFIDDB()
{
numTags = EEPROM.read(0);
if((numTags == 0) || (numTags > MAX_TAGS)){
Serial.print("Bogus number of tags reported: ");
Serial.println(numTags, DEC);
numTags = 0;
}
}
int RFIDDB::getNumTags()
{
return numTags;
}
void RFIDDB::printTags()
{
Serial.print("Printing ");
Serial.print(numTags);
Serial.println(" tags.");
for(int i = 0; i < numTags; i++){
Serial.print("Tag #");
Serial.print(i + 1);
Serial.print(": ");
for(int j = 0; j < TAG_LENGTH; j++){
char buffer;
buffer = (char) EEPROM.read(i*TAG_LENGTH+j+1);
Serial.print(buffer, BYTE);
}
Serial.println();
}
}
void RFIDDB::readTags()
{
char inByte;
int done = 0;
numTags = -1;
int currTag = 0;
int idx = 0;
while(!done){
if(Serial.available() > 0){
inByte = Serial.read();
// get the total number of records, if we don't already have it
if(numTags == -1){
numTags = (int) inByte;
Serial.print("Expecting ");
Serial.print(numTags, DEC);
Serial.println(" tags.");
EEPROM.write(0, numTags);
if(numTags == 0) { done = 1; } // no tags to write
} else {
// otherwise, put this byte wherever it goes
EEPROM.write(currTag * TAG_LENGTH + idx + 1, inByte); // EEPROM
// then, increment counters and/or set that we're done
idx++;
if(idx >= TAG_LENGTH){
Serial.print("Wrote ID #: ");
Serial.println(currTag + 1);
idx = 0;
currTag++;
if(currTag >= numTags){
done = 1;
}
}
}
}
}
Serial.println("Finished!");
}
bool RFIDDB::validTag(char* tag){
bool ok = true;
for(int i = 0; i < numTags; i++){
#if 0
Serial.print("reading tag_no=");
Serial.print(i);
Serial.print(" id=");
#endif
ok = true;
for(int j = 0; j < TAG_LENGTH; j++){
char buffer;
buffer = (char) EEPROM.read(i*TAG_LENGTH+j+1);
#if 0
Serial.print(buffer);
#endif
if(tag[j] != buffer) {
ok = false;
}
}
#if 0
Serial.print("\n");
#endif
if(ok) { return true; }
}
return false;
}