-
Notifications
You must be signed in to change notification settings - Fork 0
/
XReader.cpp
204 lines (159 loc) · 4.33 KB
/
XReader.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "Api\XReader.h"
XReader::XReader()
{
_eepromStorage = new EEPROMStorageHandler();
const uint8_t _pn532SS_Pin = 10;
_board = new Adafruit_PN532(_pn532SS_Pin);
const uint8_t _buzzerPin = 2;
_soundHelper = new SoundHelper(_buzzerPin);
const uint8_t _redLedPin = 9;
const uint8_t _blueLedPin = 8;
const uint8_t _greenLedPin = 7;
_ledHelper = new LedHelper(_blueLedPin, _greenLedPin, _redLedPin);
}
void XReader::begin()
{
#ifndef ESP8266
// ReSharper disable once CppPossiblyErroneousEmptyStatements
while (!Serial); // for Leonardo/Micro/Zero
#endif
Serial.begin(115200);
pinMode(_openDoorPin, OUTPUT);
pinMode(_button1Pin, INPUT);
_board->begin();
checkConnectionToPn532();
_ledHelper->switchPowerLedOn();
//Test section starts
//_eepromStorage->deleteMemory();
/*
uint8_t masterCardId[7] = { 240, 39, 150, 187, 0, 0, 0 };
_eepromStorage->setMasterCard(masterCardId, 4 * sizeof(uint8_t));
_eepromStorage->setPin(0, 123456789);
*/
// Serial.print("MasterCard: "); Serial.println(_eepromStorage->getMasterCardId());
//4,045,903,547
//uint8_t newCardId[4] = { 241, 39, 150, 187 };
//_eepromStorage->registerNew4BCard(&newCardId[0]);
//_eepromStorage->getNew4BCardAddress();
_eepromStorage->printMemory();
}
void XReader::checkConnectionToPn532() const
{
const uint32_t versionData = _board->getFirmwareVersion();
if (!versionData) {
Serial.print("Didn't find PN53x board");
_ledHelper->switchFailLedOn();
while (true); // halt
}
initBoard();
}
void XReader::loopProcedure()
{
RfidCard card;
uint8_t uidLength;
if(digitalRead(_button1Pin) == HIGH)
{
// ReSharper disable once CppPossiblyErroneousEmptyStatements
while (digitalRead(_button1Pin) == HIGH);
#ifdef DEBUG
Serial.println("BUTTON 1 PRESSED.");
#endif
//TODO: BUTTON 1 PRESSED
}
const boolean success = _board->readPassiveTargetID(PN532_MIFARE_ISO14443A, card.cardUid, &uidLength);
card.length = UidSize(uidLength);
if (success)
{
if (_eepromStorage->isMasterCard(card))
{
registeringNewCard();
}
else if (_eepromStorage->isCardRegistered(card))
{
successfulAuth();
}
else
{
unsuccessfulAuth();
}
}
else
{
#ifdef DEBUG
Serial.println("Timed out waiting for a card");
Serial.print("Time: "); Serial.println(millis());
#endif
}
}
void XReader::initBoard() const
{
//0xFF
_board->setPassiveActivationRetries(0x01);
// configure board to read RFID tags
_board->SAMConfig();
Serial.println("Running...");
}
void XReader::unsuccessfulAuth()
{
Serial.println("###### THIS IS NOT REGISTERED CARD ######");
_consecutiveFails++;
_ledHelper->switchFailAuthIndicationOn();
_soundHelper->soundUnsuccessAuthBuzzerOn();
//incremental delay
const unsigned int logDelay = log(_consecutiveFails) * 1000;
delay(logDelay);
_ledHelper->switchFailAuthIndicationOff();
}
void XReader::successfulAuth()
{
Serial.println("=======THIS IS REGISTERED CARD======");
_consecutiveFails = 0;
_ledHelper->switchSuccessAuthIndicationOn();
openDoor();
_ledHelper->switchSuccessAuthIndicationOff();
}
void XReader::registeringNewCard()
{
RfidCard card;
uint8_t uidLength;
Serial.println("=======THIS IS MASTERCARD======");
_consecutiveFails = 0;
Serial.println("Waiting for a new card to register...");
_ledHelper->switchSuccessAuthIndicationOn();
_soundHelper->soundSuccessNoticeSound();
delay(500);
_soundHelper->waitingForNewCardSound();
_board->setPassiveActivationRetries(0xFF);//wait for new card until it is read.
#ifdef DEBUG
const unsigned long mills = millis();
#endif
const bool success = _board->readPassiveTargetID(PN532_MIFARE_ISO14443A, card.cardUid, &uidLength, 10000);
card.length = UidSize(uidLength);
_soundHelper->stopSound();
#ifdef DEBUG
Serial.print("Milliseconds: "); Serial.print(millis() - mills); Serial.println("ms.");
#endif
if(success)
{
_eepromStorage->registerNewCard(card);
delay(300);
_soundHelper->switchSuccessAuthBuzzerOn();
_ledHelper->switchSuccessRegistrationIndicationOn();
}
else
{
_ledHelper->switchFailedRegistrationIndicationOn();
_soundHelper->soundUnsuccessAuthBuzzerOn();
delay(500);
_ledHelper->switchFailLedOff();
}
delay(1000);
_ledHelper->switchPowerLedOn();
_board->setPassiveActivationRetries(0x01);
}
void XReader::openDoor() const
{
switchPinOn(_openDoorPin);
delay(DOOR_OPENED_INTERVAL);
switchPinOff(_openDoorPin);
}