-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfid_reader.cpp
40 lines (31 loc) · 1.15 KB
/
rfid_reader.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
// rfid_reader.cpp
// github.com/jacoblukewood/ems
// Copyright 2020 Jacob Wood
#include "rfid_reader.h"
#include <Arduino.h>
#include <SPI.h>
RFIDReader::RFIDReader(unsigned int const pin_ss, unsigned int const pin_rst) {
MFRC522 rfid_protocol(pin_ss, pin_rst); // Instance of the class
SPI.begin(); // Init SPI bus
rfid_protocol.PCD_Init(); // Init MFRC522
}
bool RFIDReader::IsValidKeyPresent() {
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
rfid_protocol.PICC_IsNewCardPresent();
// Verify if the NUID has been read
if (rfid_protocol.PICC_ReadCardSerial()) {
for (int i = 0; i < (sizeof &keys_ / sizeof keys_[0]); i++) {
if (rfid_protocol.uid.uidByte[0] == keys_[i][0] &&
rfid_protocol.uid.uidByte[1] == keys_[i][1] &&
rfid_protocol.uid.uidByte[2] == keys_[i][3] &&
rfid_protocol.uid.uidByte[3] == keys_[i][4]) {
return true;
}
}
}
// Halt PICC
rfid_protocol.PICC_HaltA();
// Stop encryption on PCD
rfid_protocol.PCD_StopCrypto1();
return false;
}