Add HardwareRfid class implementation and integrate with main loop

This commit is contained in:
2025-10-04 11:39:26 +02:00
parent 505367d6a8
commit 697e6fd11f
4 changed files with 77 additions and 4 deletions
+1 -4
View File
@@ -1,5 +1,4 @@
#ifndef HARDWARE_LED_HPP
#define HARDWARE_LED_HPP
#pragma once
#include "hardware.pb.h"
#include <Adafruit_NeoPixel.h>
@@ -33,5 +32,3 @@ private:
unsigned long _fadeStartTime;
bool _isActive;
};
#endif
+44
View File
@@ -0,0 +1,44 @@
#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;
}
+20
View File
@@ -0,0 +1,20 @@
#pragma once
#include <MFRC522.h>
#include <functional>
#include "hardware.pb.h"
typedef std::function<void(const hardware_SensorToControlMessage&)> RfidCallback;
class HardwareRfid {
public:
HardwareRfid(uint8_t ssPin, uint8_t rstPin);
void begin();
void end();
void update();
void setCallback(RfidCallback cb);
private:
MFRC522 _mfrc;
RfidCallback _callback;
};