Finish led manager rewrite

This commit is contained in:
Lorow
2025-04-20 21:08:14 +02:00
parent a580d0f097
commit f1cf3baf46
4 changed files with 137 additions and 95 deletions

View File

@@ -2,44 +2,69 @@
const char *LED_MANAGER_TAG = "[LED_MANAGER]"; const char *LED_MANAGER_TAG = "[LED_MANAGER]";
// todo rethink the patterns
ledStateMap_t LEDManager::ledStateMap = { ledStateMap_t LEDManager::ledStateMap = {
{LEDStates_e::_LedStateNone, {{LED_OFF, 500}}}, {
{LEDStates_e::_LedStateStreaming, {{LED_ON, 500}}}, LEDStates_e::_LedStateNone,
{LEDStates_e::_LedStateStoppedStreaming, {{LED_OFF, 500}}}, {
{LEDStates_e::_WebServerState_Error, false,
{{LED_ON, 200}, {LED_OFF, 100}, {LED_ON, 200}, {LED_OFF, 100}, {LED_ON, 200}}}, true,
{LEDStates_e::_WiFiState_Error, {{LED_OFF, 1000}},
{{LED_ON, 200}, {LED_OFF, 100}, {LED_ON, 500}, {LED_OFF, 100}, {LED_ON, 200}}}, },
{LEDStates_e::_MDNSState_Error, },
{{LED_ON, 200}, {
{0, 100}, LEDStates_e::_LedStateStreaming,
{LED_ON, 200}, {
{0, 100}, false,
{LED_ON, 500}, true,
{0, 100}, {{LED_ON, 1000}},
{LED_ON, 200}, },
{0, 100}, },
{LED_ON, 200}}}, {
LEDStates_e::_LedStateStoppedStreaming,
{
false,
true,
{{LED_OFF, 1000}},
},
},
{
LEDStates_e::_Camera_Error,
{
true,
true,
{{{LED_ON, 300}, {LED_OFF, 300}, {LED_ON, 300}, {LED_OFF, 300}}},
},
},
{
LEDStates_e::_WiFiState_Connecting,
{
false,
true,
{{LED_ON, 400}, {LED_OFF, 400}},
},
},
{
LEDStates_e::_WiFiState_Connected,
{
false,
false,
{{LED_ON, 200}, {LED_OFF, 200}, {LED_ON, 200}, {LED_OFF, 200}, {LED_ON, 200}, {LED_OFF, 200}, {LED_ON, 200}, {LED_OFF, 200}, {LED_ON, 200}, {LED_OFF, 200}},
},
},
{
LEDStates_e::_WiFiState_Error,
{
true,
true,
{{LED_ON, 200}, {LED_OFF, 100}, {LED_ON, 500}, {LED_OFF, 100}, {LED_ON, 200}},
},
},
};
// lmao no, rethink this LEDManager::LEDManager(gpio_num_t pin, gpio_num_t illumninator_led_pin, QueueHandle_t ledStateQueue) : blink_led_pin(pin),
{LEDStates_e::_Camera_Error, illumninator_led_pin(illumninator_led_pin), ledStateQueue(ledStateQueue), currentState(LEDStates_e::_LedStateNone)
{{LED_ON, 5000}}}, // this also works as a more general error - something went {
// critically wrong? We go here }
{LEDStates_e::_WiFiState_Connecting, {{1, 100}, {0, 100}}},
{LEDStates_e::_WiFiState_Connected,
{{LED_ON, 100},
{0, 100},
{LED_ON, 100},
{0, 100},
{LED_ON, 100},
{0, 100},
{LED_ON, 100},
{0, 100},
{LED_ON, 100},
{0, 100}}}};
LEDManager::LEDManager(gpio_num_t pin, gpio_num_t illumninator_led_pin, QueueHandle_t ledStateQueue) : blink_led_pin(pin), illumninator_led_pin(illumninator_led_pin), ledStateQueue(ledStateQueue) {}
void LEDManager::setup() void LEDManager::setup()
{ {
@@ -78,62 +103,66 @@ void LEDManager::setup()
ESP_LOGD(LED_MANAGER_TAG, "Done."); ESP_LOGD(LED_MANAGER_TAG, "Done.");
} }
// todo - rethink how it should work with a queue and vtaskdelay
void LEDManager::handleLED() void LEDManager::handleLED()
{ {
// if (Helpers::getTimeInMillis() <= this->nextStateChangeMillis) if (!this->finishedPattern)
// { {
// return; displayCurrentPattern();
// } return;
}
// // !TODO what if we want a looping state? Or a state that needs to stay if (xQueueReceive(this->ledStateQueue, &buffer, 10))
// // bright? Am overthinking this, aren't I? I was not. {
this->updateState(buffer);
// // we've reached the timeout on that state, check if we can grab next one and }
// // start displaying it, or if we need to keep displaying the current one else
// if (this->currentPatternIndex > {
// this->ledStateMap[this->currentState].size() - 1) // we've finished displaying the pattern, so let's check if it's repeatable and if so - reset it
// { if (this->ledStateMap[this->currentState].isRepeatable || this->ledStateMap[this->currentState].isError)
// auto nextState = ledStateManager.getCurrentState(); {
// // we want to keep displaying the same state only if its an keepAlive one, this->currentPatternIndex = 0;
// // but we should change if the incoming one is also an errours state, maybe this->finishedPattern = false;
// // more serious one this time <- this may be a bad idea }
// if ((std::find(this->keepAliveStates.begin(), this->keepAliveStates.end(), }
// this->currentState) != this->keepAliveStates.end() || }
// std::find(this->keepAliveStates.begin(), this->keepAliveStates.end(),
// nextState) != this->keepAliveStates.end()) || void LEDManager::displayCurrentPattern()
// (this->currentState != nextState && {
// this->ledStateMap.find(nextState) != this->ledStateMap.end())) BlinkPatterns_t ledState = this->ledStateMap[this->currentState].patterns[this->currentPatternIndex];
// { this->toggleLED(ledState.state);
// ESP_LOGD(LED_MANAGER_TAG, "Updating the state and reseting"); this->timeToDelayFor = ledState.delayTime;
// this->toggleLED(false);
// this->currentState = nextState; if (!(this->currentPatternIndex >= this->ledStateMap[this->currentState].patterns.size() - 1))
// this->currentPatternIndex = 0; this->currentPatternIndex++;
// BlinkPatterns_t pattern = else
// this->ledStateMap[this->currentState][this->currentPatternIndex]; {
// this->nextStateChangeMillis = Helpers::getTimeInMillis() + pattern.delayTime; this->finishedPattern = true;
// return; this->toggleLED(LED_OFF);
// } }
// // it wasn't a keepAlive state, nor did we have another one ready, }
// // we're done for now
// this->toggleLED(false); void LEDManager::updateState(LEDStates_e newState)
// return; {
// } // we should change the displayed state
// // we can safely advance it and display the next stage // only if we finished displaying the current one - which is handled by the task
// BlinkPatterns_t pattern = // if the new state is not the same as the current one
// this->ledStateMap[this->currentState][this->currentPatternIndex]; // and if can actually display the state
// this->toggleLED(pattern.state);
// this->nextStateChangeMillis = Helpers::getTimeInMillis() + pattern.delayTime; // if we've got an error state - that's it, we'll just keep repeating it indefinitely
// ESP_LOGD(LED_MANAGER_TAG, "before updating stage %d", this->currentPatternIndex); if (this->ledStateMap[this->currentState].isError)
// this->currentPatternIndex++; return;
// ESP_LOGD(LED_MANAGER_TAG, "updated stage %d", this->currentPatternIndex);
if (newState == this->currentState)
return;
if (this->ledStateMap.contains(newState))
{
this->currentState = newState;
this->currentPatternIndex = 0;
this->finishedPattern = false;
}
} }
/**
* @brief Turn the LED on or off
*
* @param state
*/
void LEDManager::toggleLED(bool state) const void LEDManager::toggleLED(bool state) const
{ {
gpio_set_level(blink_led_pin, state); gpio_set_level(blink_led_pin, state);
@@ -145,8 +174,7 @@ void HandleLEDDisplayTask(void *pvParameter)
while (1) while (1)
{ {
printf("Hello from a task \n"); ledManager->handleLED();
printf("The led manager is %d \n", bool(ledManager)); vTaskDelay(ledManager->getTimeToDelayFor());
vTaskDelay(100);
} }
} }

View File

@@ -28,7 +28,15 @@ struct BlinkPatterns_t
int delayTime; int delayTime;
}; };
typedef std::unordered_map<LEDStates_e, std::vector<BlinkPatterns_t>> ledStateMap_t; struct LEDStage
{
bool isError;
bool isRepeatable;
std::vector<BlinkPatterns_t> patterns;
};
typedef std::unordered_map<LEDStates_e, LEDStage>
ledStateMap_t;
class LEDManager class LEDManager
{ {
@@ -37,16 +45,25 @@ public:
void setup(); void setup();
void handleLED(); void handleLED();
size_t getTimeToDelayFor() const { return timeToDelayFor; }
private: private:
void toggleLED(bool state) const;
void displayCurrentPattern();
void updateState(LEDStates_e newState);
gpio_num_t blink_led_pin; gpio_num_t blink_led_pin;
gpio_num_t illumninator_led_pin; gpio_num_t illumninator_led_pin;
QueueHandle_t ledStateQueue; QueueHandle_t ledStateQueue;
static ledStateMap_t ledStateMap; static ledStateMap_t ledStateMap;
LEDStates_e buffer;
LEDStates_e currentState; LEDStates_e currentState;
void toggleLED(bool state) const; size_t currentPatternIndex = 0;
size_t timeToDelayFor = 100;
bool finishedPattern = false;
}; };
void HandleLEDDisplayTask(void *pvParameter); void HandleLEDDisplayTask(void *pvParameter);

View File

@@ -126,7 +126,6 @@ void ProjectConfig::setWifiConfig(const std::string &networkName,
{ {
size_t size = this->config.networks.size(); size_t size = this->config.networks.size();
// rewrite it to std::find
auto it = std::find_if( auto it = std::find_if(
this->config.networks.begin(), this->config.networks.begin(),
this->config.networks.end(), this->config.networks.end(),

View File

@@ -12,9 +12,7 @@ struct DeviceStates
_LedStateNone, _LedStateNone,
_LedStateStreaming, _LedStateStreaming,
_LedStateStoppedStreaming, _LedStateStoppedStreaming,
_WebServerState_Error,
_WiFiState_Error, _WiFiState_Error,
_MDNSState_Error,
_Camera_Error, _Camera_Error,
_WiFiState_Connecting, _WiFiState_Connecting,
_WiFiState_Connected _WiFiState_Connected