Add HardwareRfid class implementation and integrate with main loop
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
#ifndef HARDWARE_LED_HPP
|
#pragma once
|
||||||
#define HARDWARE_LED_HPP
|
|
||||||
|
|
||||||
#include "hardware.pb.h"
|
#include "hardware.pb.h"
|
||||||
#include <Adafruit_NeoPixel.h>
|
#include <Adafruit_NeoPixel.h>
|
||||||
@@ -33,5 +32,3 @@ private:
|
|||||||
unsigned long _fadeStartTime;
|
unsigned long _fadeStartTime;
|
||||||
bool _isActive;
|
bool _isActive;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
};
|
||||||
@@ -1,17 +1,28 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <hardware_led.hpp>
|
#include <hardware_led.hpp>
|
||||||
|
#include <hardware_rfid.hpp>
|
||||||
#include "hardware.pb.h"
|
#include "hardware.pb.h"
|
||||||
|
|
||||||
// Demo for HardwareLed
|
// Demo for HardwareLed
|
||||||
HardwareLed led(2); // Assuming NeoPixel on pin 4 (D2 on ESP8266)
|
HardwareLed led(2); // Assuming NeoPixel on pin 4 (D2 on ESP8266)
|
||||||
|
|
||||||
|
// Demo for HardwareRfid
|
||||||
|
HardwareRfid rfid(4, 5); // SS=D2 (GPIO4), RST=D1 (GPIO5)
|
||||||
|
|
||||||
hardware_LedConfig configs[4];
|
hardware_LedConfig configs[4];
|
||||||
unsigned long lastChange = 0;
|
unsigned long lastChange = 0;
|
||||||
int currentConfig = 0;
|
int currentConfig = 0;
|
||||||
|
|
||||||
|
void onRfidTag(const hardware_SensorToControlMessage& msg) {
|
||||||
|
Serial.print("RFID Tag detected: 0x");
|
||||||
|
Serial.println(msg.payload.rfid_reading.card_id, HEX);
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
led.begin();
|
led.begin();
|
||||||
|
rfid.begin();
|
||||||
|
rfid.setCallback(onRfidTag);
|
||||||
|
|
||||||
// Static config
|
// Static config
|
||||||
configs[0] = {0};
|
configs[0] = {0};
|
||||||
@@ -53,6 +64,7 @@ void setup() {
|
|||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
led.update();
|
led.update();
|
||||||
|
rfid.update();
|
||||||
|
|
||||||
if (millis() - lastChange >= 10000) {
|
if (millis() - lastChange >= 10000) {
|
||||||
lastChange = millis();
|
lastChange = millis();
|
||||||
|
|||||||
Reference in New Issue
Block a user