-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNFCReader.cpp
74 lines (53 loc) · 1.7 KB
/
NFCReader.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
#include "NFCReader.hpp"
#define NFC_READER_CHECK_INTERVAL 10
#define NFC_READER_TASK_NAME "NFC-Reader task"
#define NFC_READER_TASK_PRIORITY 5
#define NFC_READER_TASK_STACK_SIZE 4096
#define RANDOM_UID_BYTE_0 0x80
namespace DC {
NFCReader::NFCReader(const NFCPinConfig& config, NFCTokenHandler& handler)
: handler(handler), rfid(config.ssPin, config.rstPin), taskHandle(NULL) {
SPI.begin();
this->rfid.PCD_Init();
}
void NFCReader::start() {
if (taskHandle) {
return;
}
xTaskCreate(
taskRoutine,
NFC_READER_TASK_NAME,
NFC_READER_TASK_STACK_SIZE,
(void*)this,
NFC_READER_TASK_PRIORITY | portPRIVILEGE_BIT,
&taskHandle
);
if (this->taskHandle) {
this->requestTaskStop = false;
}
}
void NFCReader::stop() {
this->requestTaskStop = true;
}
/*static*/ void NFCReader::taskRoutine(void* arg) {
NFCReader* _this = (NFCReader*)arg;
while(!_this->requestTaskStop) {
delay(NFC_READER_CHECK_INTERVAL);
if (!_this->rfid.PICC_IsNewCardPresent()) {
continue;
}
if (!_this->rfid.PICC_ReadCardSerial()) {
continue;
}
if (_this->rfid.uid.uidByte[0] == RANDOM_UID_BYTE_0) {
continue;
}
NFCToken token(&_this->rfid.uid.uidByte[0], _this->rfid.uid.size);
Serial.print("Read token: ");
Serial.println(token.toHexRepr());
_this->handler.processToken(token);
}
_this->taskHandle = NULL;
vTaskDelete(NULL);
}
}