Files
smart-rgb-esp32/src/main.cpp

82 lines
2.9 KiB
C++
Raw Normal View History

2025-07-29 14:58:45 +02:00
#include <Arduino.h>
2025-08-28 14:58:55 +02:00
#include "appcontext.hpp"
#include "config.h"
2025-08-03 00:34:05 +02:00
#include "light.h"
#include "mqtt.h"
2025-07-29 17:41:21 +02:00
#include "network.h"
#include "ota.h"
2025-08-03 00:34:05 +02:00
#include "pin.h"
2025-08-28 14:13:20 +02:00
#include "statemachine.hpp"
2025-08-28 14:58:55 +02:00
#include "states.hpp"
2025-08-03 00:34:05 +02:00
#include "TaskScheduler.h"
2025-07-29 17:41:21 +02:00
#include "wifimanager.h"
2025-08-28 15:58:50 +02:00
// Network* network = nullptr;
// OTAHandler* otaHandler = nullptr;
// Mqtt* mqttClient = nullptr;
// Light *light = nullptr;
2025-07-29 14:58:45 +02:00
2025-08-28 14:58:55 +02:00
2025-08-28 15:58:50 +02:00
// Task *updateTask = nullptr;
// Task *mqttTickTask = nullptr;
// Task *mqttCheckConnectionTask = nullptr;
Task *appStateMachineUpdateTask = nullptr;
2025-08-03 00:34:05 +02:00
2025-08-28 14:58:55 +02:00
Pin *pinR = new Pin(ledPinR, true, true, 5000, 0);
Pin *pinG = new Pin(ledPinG, true, true, 5000, 1);
Pin *pinB = new Pin(ledPinB, true, true, 5000, 2);
Pin *pinCW = new Pin(ledPinCW, true, true, 5000, 3);
Pin *pinWW = new Pin(ledPinWW, true, true, 5000, 4);
AppContext *appContext = new AppContext();
StartState *startState = new StartState(appContext);
2025-08-28 15:58:50 +02:00
NetworkInitializeState *networkInitializeState = new NetworkInitializeState(appContext);
RunningState *runningState = new RunningState(appContext);
2025-08-28 14:58:55 +02:00
StateMachine<maxNumberOfStates> *stateMachine = nullptr;
2025-08-03 00:34:05 +02:00
2025-08-28 14:58:55 +02:00
Scheduler *scheduler = nullptr;
2025-08-03 00:34:05 +02:00
void initializeScheduler();
2025-07-29 14:58:45 +02:00
void setup() {
2025-07-29 17:41:21 +02:00
// put your setup code here, to run once:
2025-08-28 15:58:50 +02:00
Serial.begin(115200);
Serial.println("Starting Smart RGB ESP32...");
initializeScheduler();
2025-08-28 14:58:55 +02:00
stateMachine = new StateMachine<maxNumberOfStates>();
appContext->pinR = pinR;
appContext->pinG = pinG;
appContext->pinB = pinB;
appContext->pinCW = pinCW;
appContext->pinWW = pinWW;
stateMachine->addStateRaw(startState);
2025-08-28 15:58:50 +02:00
stateMachine->addStateRaw(networkInitializeState);
stateMachine->addTransition(StateId::StartState, EventId::PinInitialized, StateId::NetworkInitializeState);
stateMachine->addStateRaw(runningState);
stateMachine->addTransition(StateId::NetworkInitializeState, EventId::WifiConnected, StateId::RunningState);
stateMachine->addTransition(StateId::RunningState, EventId::WifiDisconnected, StateId::NetworkInitializeState);
2025-08-28 14:58:55 +02:00
stateMachine->setInitialState(StateId::StartState);
2025-08-28 15:58:50 +02:00
// light = new Light(pinR, pinG, pinB, pinCW, pinWW, mqttClient, "smart_rgb_light");
2025-07-29 14:58:45 +02:00
}
void loop() {
2025-08-03 00:34:05 +02:00
scheduler->execute(); // Execute the scheduler to run tasks
yield(); // Yield to allow other tasks to run
}
void initializeScheduler() {
scheduler = new Scheduler();
2025-08-28 15:58:50 +02:00
appStateMachineUpdateTask = new Task(TASK_MILLISECOND, TASK_FOREVER, []() {
if (stateMachine) {
stateMachine->update();
}
2025-08-03 00:34:05 +02:00
}, scheduler, true, nullptr, nullptr);
2025-08-28 15:58:50 +02:00
// mqttTickTask = new Task(TASK_MILLISECOND * 100, TASK_FOREVER, []() {
// Mqtt::poll(); // Poll MQTT client for messages
// }, scheduler, true, nullptr, nullptr);
// mqttCheckConnectionTask = new Task(TASK_SECOND * 30, TASK_FOREVER, []() {
// Mqtt::checkConnection(); // Check MQTT connection status
// }, scheduler, true, nullptr, nullptr);
2025-07-29 14:58:45 +02:00
}