94 lines
2.5 KiB
C++
94 lines
2.5 KiB
C++
#include <Arduino.h>
|
|
#include <hardware_led.hpp>
|
|
#include <hardware_rfid.hpp>
|
|
#include <hardware_serial.hpp>
|
|
#include <ota_update.hpp>
|
|
#include "hardware.pb.h"
|
|
|
|
OtaUpdate ota;
|
|
|
|
// Demo for HardwareLed
|
|
HardwareLed<2> led; // Assuming NeoPixel on pin 2 (D2 on ESP8266)
|
|
|
|
// Demo for HardwareRfid
|
|
HardwareRfid rfid(4, 5); // SS=D2 (GPIO4), RST=D1 (GPIO5)
|
|
|
|
// Demo for HardwareSerial
|
|
ProtoSerial serial;
|
|
|
|
hardware_LedConfig configs[2];
|
|
unsigned long lastChange = 0;
|
|
int currentConfig = 0;
|
|
|
|
void onRfidTag(const hardware_SensorToControlMessage& msg) {
|
|
serial.sendMessage(msg);
|
|
// Serial.println("Callback onRfidTag triggered");
|
|
// Serial.print("RFID Tag detected: 0x");
|
|
// Serial.println(msg.payload.rfid_reading.card_id, HEX);
|
|
}
|
|
|
|
void onOtaResponse(bool success, const char* ip_address, const char* error_message) {
|
|
hardware_SensorOTAEnableResponse response = {0};
|
|
response.success = success;
|
|
if (success) {
|
|
strcpy(response.ip_address, ip_address);
|
|
} else {
|
|
strcpy(response.error_message, error_message);
|
|
}
|
|
|
|
hardware_SensorToControlMessage responseMsg = {0};
|
|
responseMsg.sensor_id = 0; // or some id
|
|
responseMsg.which_payload = hardware_SensorToControlMessage_ota_response_tag;
|
|
responseMsg.payload.ota_response = response;
|
|
|
|
led.set(configs[1]);
|
|
|
|
serial.sendMessage(responseMsg);
|
|
}
|
|
|
|
void onSerialMessage(const IncomingMessage& msg) {
|
|
if (msg.which_payload == hardware_ControlToSensorMessage_led_config_tag) {
|
|
led.set(msg.payload.led_config);
|
|
} else if (msg.which_payload == hardware_ControlToSensorMessage_ota_enable_tag) {
|
|
ota.set(msg.payload.ota_enable);
|
|
} else if (msg.which_payload == hardware_ControlToSensorMessage_restart_tag) {
|
|
ESP.restart();
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
|
|
ota.setResponseCallback(onOtaResponse);
|
|
|
|
led.begin();
|
|
rfid.begin();
|
|
rfid.setCallback(onRfidTag);
|
|
serial.begin(Serial);
|
|
serial.setCallback(onSerialMessage);
|
|
|
|
// Static config
|
|
configs[0] = {0};
|
|
configs[0].brightness = 64;
|
|
configs[0].which_animation_params = hardware_LedConfig_static_params_tag;
|
|
configs[0].animation_params.static_params.color = 0x55FF00; // Soft green
|
|
|
|
// Pulse config
|
|
configs[1] = {0};
|
|
configs[1].brightness = 128;
|
|
configs[1].duration_ms = 0;
|
|
configs[1].which_animation_params = 4;
|
|
configs[1].animation_params.pulse_params.color = 0xFF0000; // Red
|
|
configs[1].animation_params.pulse_params.speed_ms = 500;
|
|
|
|
|
|
|
|
led.set(configs[0]);
|
|
}
|
|
|
|
void loop() {
|
|
led.update();
|
|
rfid.update();
|
|
serial.update();
|
|
ota.update();
|
|
} |