some big change using state machine
This commit is contained in:
57
src/main.cpp
57
src/main.cpp
@@ -11,15 +11,16 @@
|
||||
#include "TaskScheduler.h"
|
||||
#include "wifimanager.h"
|
||||
|
||||
Network* network = nullptr;
|
||||
OTAHandler* otaHandler = nullptr;
|
||||
Mqtt* mqttClient = nullptr;
|
||||
Light *light = nullptr;
|
||||
// Network* network = nullptr;
|
||||
// OTAHandler* otaHandler = nullptr;
|
||||
// Mqtt* mqttClient = nullptr;
|
||||
// Light *light = nullptr;
|
||||
|
||||
|
||||
Task *updateTask = nullptr;
|
||||
Task *mqttTickTask = nullptr;
|
||||
Task *mqttCheckConnectionTask = nullptr;
|
||||
// Task *updateTask = nullptr;
|
||||
// Task *mqttTickTask = nullptr;
|
||||
// Task *mqttCheckConnectionTask = nullptr;
|
||||
Task *appStateMachineUpdateTask = nullptr;
|
||||
|
||||
Pin *pinR = new Pin(ledPinR, true, true, 5000, 0);
|
||||
Pin *pinG = new Pin(ledPinG, true, true, 5000, 1);
|
||||
@@ -29,6 +30,9 @@ Pin *pinWW = new Pin(ledPinWW, true, true, 5000, 4);
|
||||
|
||||
AppContext *appContext = new AppContext();
|
||||
StartState *startState = new StartState(appContext);
|
||||
NetworkInitializeState *networkInitializeState = new NetworkInitializeState(appContext);
|
||||
RunningState *runningState = new RunningState(appContext);
|
||||
|
||||
|
||||
StateMachine<maxNumberOfStates> *stateMachine = nullptr;
|
||||
|
||||
@@ -38,8 +42,9 @@ void initializeScheduler();
|
||||
|
||||
void setup() {
|
||||
// put your setup code here, to run once:
|
||||
// Serial.begin(115200);
|
||||
// Serial.println("Starting Smart RGB ESP32...");
|
||||
Serial.begin(115200);
|
||||
Serial.println("Starting Smart RGB ESP32...");
|
||||
initializeScheduler();
|
||||
stateMachine = new StateMachine<maxNumberOfStates>();
|
||||
appContext->pinR = pinR;
|
||||
appContext->pinG = pinG;
|
||||
@@ -47,14 +52,15 @@ void setup() {
|
||||
appContext->pinCW = pinCW;
|
||||
appContext->pinWW = pinWW;
|
||||
stateMachine->addStateRaw(startState);
|
||||
stateMachine->addStateRaw(networkInitializeState);
|
||||
stateMachine->addTransition(StateId::StartState, EventId::PinInitialized, StateId::NetworkInitializeState);
|
||||
stateMachine->addStateRaw(runningState);
|
||||
stateMachine->addTransition(StateId::NetworkInitializeState, EventId::WifiConnected, StateId::RunningState);
|
||||
stateMachine->setInitialState(StateId::StartState);
|
||||
network = new Network("smart-rgb");
|
||||
otaHandler = new OTAHandler("smart-rgb-ota");
|
||||
network->registerMDNS();
|
||||
Mqtt::connect("10.238.75.81", 1883, "smart_rgb_client", "mqtt", "mqtt");
|
||||
delay(1000); // Wait for MQTT connection to stabilize
|
||||
light = new Light(pinR, pinG, pinB, pinCW, pinWW, mqttClient, "smart_rgb_light");
|
||||
initializeScheduler();
|
||||
|
||||
// Mqtt::connect("10.238.75.81", 1883, "smart_rgb_client", "mqtt", "mqtt");
|
||||
// delay(1000); // Wait for MQTT connection to stabilize
|
||||
// light = new Light(pinR, pinG, pinB, pinCW, pinWW, mqttClient, "smart_rgb_light");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
@@ -64,14 +70,15 @@ void loop() {
|
||||
|
||||
void initializeScheduler() {
|
||||
scheduler = new Scheduler();
|
||||
updateTask = new Task(TASK_SECOND, TASK_FOREVER, []() {
|
||||
otaHandler->poll(); // Poll for OTA updates
|
||||
|
||||
}, scheduler, true, nullptr, nullptr);
|
||||
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
|
||||
appStateMachineUpdateTask = new Task(TASK_MILLISECOND, TASK_FOREVER, []() {
|
||||
if (stateMachine) {
|
||||
stateMachine->update();
|
||||
}
|
||||
}, scheduler, true, nullptr, nullptr);
|
||||
// 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);
|
||||
}
|
||||
Reference in New Issue
Block a user