44 lines
1.0 KiB
C++
44 lines
1.0 KiB
C++
#include "hardware_rfid.hpp"
|
|
#include <Arduino.h>
|
|
|
|
HardwareRfid::HardwareRfid(uint8_t ssPin, uint8_t rstPin)
|
|
: _mfrc(ssPin, rstPin) {}
|
|
|
|
void HardwareRfid::begin() {
|
|
_mfrc.PCD_Init();
|
|
}
|
|
|
|
void HardwareRfid::end() {
|
|
// Nothing special to do
|
|
}
|
|
|
|
void HardwareRfid::update() {
|
|
if (!_mfrc.PICC_IsNewCardPresent() || !_mfrc.PICC_ReadCardSerial()) {
|
|
return;
|
|
}
|
|
|
|
// Get the UID
|
|
uint32_t cardId = 0;
|
|
for (byte i = 0; i < _mfrc.uid.size; i++) {
|
|
cardId = (cardId << 8) | _mfrc.uid.uidByte[i];
|
|
}
|
|
|
|
// Create the message
|
|
hardware_SensorToControlMessage msg = {0};
|
|
msg.sensor_id = 0; // Assuming single sensor
|
|
msg.which_payload = hardware_SensorToControlMessage_rfid_reading_tag; // Assuming this is the tag
|
|
msg.payload.rfid_reading.card_id = cardId;
|
|
|
|
// Call the callback if set
|
|
if (_callback) {
|
|
_callback(msg);
|
|
}
|
|
|
|
// Halt the card
|
|
_mfrc.PICC_HaltA();
|
|
_mfrc.PCD_StopCrypto1();
|
|
}
|
|
|
|
void HardwareRfid::setCallback(RfidCallback cb) {
|
|
_callback = cb;
|
|
} |