-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRFIDManager.cpp
More file actions
60 lines (48 loc) · 1.67 KB
/
Copy pathRFIDManager.cpp
File metadata and controls
60 lines (48 loc) · 1.67 KB
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
#include "RFIDManager.h"
bool RFIDManager::begin() {
// IMPORTANT: PN532 V3 module must be physically configured for SPI mode
// via its onboard DIP switches before this will work. See README wiring
// section. If SS/SCK/MISO/MOSI are wired correctly but the switches are
// still set to I2C or HSU, getFirmwareVersion() below will return 0.
SPI.begin(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
nfc_.begin();
uint32_t versiondata = nfc_.getFirmwareVersion();
if (!versiondata) {
ready_ = false;
return false;
}
// Configure the max retries to 0xFF (infinite) internally is not desired;
// we want SAMConfig for normal passive reads.
nfc_.SAMConfig();
ready_ = true;
return true;
}
String RFIDManager::uidBytesToHex_(const uint8_t* uid, uint8_t len) {
String out;
out.reserve(len * 2);
const char* hexChars = "0123456789ABCDEF";
for (uint8_t i = 0; i < len; i++) {
out += hexChars[(uid[i] >> 4) & 0x0F];
out += hexChars[uid[i] & 0x0F];
}
return out;
}
bool RFIDManager::poll(String& uidOut) {
if (!ready_) return false;
uint8_t uid[7];
uint8_t uidLength = 0;
bool found = nfc_.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength,
RFID_POLL_TIMEOUT_MS);
if (!found || uidLength == 0) return false;
String hex = uidBytesToHex_(uid, uidLength);
uint32_t now = millis();
// Debounce: the same physical card held on the reader will be detected
// repeatedly every poll cycle. Suppress repeats within CARD_COOLDOWN_MS.
if (hex == lastUid_ && (now - lastDetectMs_) < CARD_COOLDOWN_MS) {
return false;
}
lastUid_ = hex;
lastDetectMs_ = now;
uidOut = hex;
return true;
}