diff --git a/components/LEDManager/LEDManager/LEDManager.cpp b/components/LEDManager/LEDManager/LEDManager.cpp index 96337ca..7fbb8d5 100644 --- a/components/LEDManager/LEDManager/LEDManager.cpp +++ b/components/LEDManager/LEDManager/LEDManager.cpp @@ -2,44 +2,69 @@ const char *LED_MANAGER_TAG = "[LED_MANAGER]"; -// todo rethink the patterns ledStateMap_t LEDManager::ledStateMap = { - {LEDStates_e::_LedStateNone, {{LED_OFF, 500}}}, - {LEDStates_e::_LedStateStreaming, {{LED_ON, 500}}}, - {LEDStates_e::_LedStateStoppedStreaming, {{LED_OFF, 500}}}, - {LEDStates_e::_WebServerState_Error, - {{LED_ON, 200}, {LED_OFF, 100}, {LED_ON, 200}, {LED_OFF, 100}, {LED_ON, 200}}}, - {LEDStates_e::_WiFiState_Error, - {{LED_ON, 200}, {LED_OFF, 100}, {LED_ON, 500}, {LED_OFF, 100}, {LED_ON, 200}}}, - {LEDStates_e::_MDNSState_Error, - {{LED_ON, 200}, - {0, 100}, - {LED_ON, 200}, - {0, 100}, - {LED_ON, 500}, - {0, 100}, - {LED_ON, 200}, - {0, 100}, - {LED_ON, 200}}}, + { + LEDStates_e::_LedStateNone, + { + false, + true, + {{LED_OFF, 1000}}, + }, + }, + { + LEDStates_e::_LedStateStreaming, + { + false, + true, + {{LED_ON, 1000}}, + }, + }, + { + 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 - {LEDStates_e::_Camera_Error, - {{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) {} +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), currentState(LEDStates_e::_LedStateNone) +{ +} void LEDManager::setup() { @@ -78,62 +103,66 @@ void LEDManager::setup() ESP_LOGD(LED_MANAGER_TAG, "Done."); } -// todo - rethink how it should work with a queue and vtaskdelay void LEDManager::handleLED() { - // if (Helpers::getTimeInMillis() <= this->nextStateChangeMillis) - // { - // return; - // } + if (!this->finishedPattern) + { + displayCurrentPattern(); + return; + } - // // !TODO what if we want a looping state? Or a state that needs to stay - // // bright? Am overthinking this, aren't I? I was not. - - // // 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 - // if (this->currentPatternIndex > - // this->ledStateMap[this->currentState].size() - 1) - // { - // auto nextState = ledStateManager.getCurrentState(); - // // we want to keep displaying the same state only if its an keepAlive one, - // // but we should change if the incoming one is also an errours state, maybe - // // 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()) || - // (this->currentState != nextState && - // this->ledStateMap.find(nextState) != this->ledStateMap.end())) - // { - // ESP_LOGD(LED_MANAGER_TAG, "Updating the state and reseting"); - // this->toggleLED(false); - // this->currentState = nextState; - // this->currentPatternIndex = 0; - // BlinkPatterns_t pattern = - // this->ledStateMap[this->currentState][this->currentPatternIndex]; - // this->nextStateChangeMillis = Helpers::getTimeInMillis() + pattern.delayTime; - // return; - // } - // // it wasn't a keepAlive state, nor did we have another one ready, - // // we're done for now - // this->toggleLED(false); - // return; - // } - // // we can safely advance it and display the next stage - // BlinkPatterns_t pattern = - // this->ledStateMap[this->currentState][this->currentPatternIndex]; - // this->toggleLED(pattern.state); - // this->nextStateChangeMillis = Helpers::getTimeInMillis() + pattern.delayTime; - // ESP_LOGD(LED_MANAGER_TAG, "before updating stage %d", this->currentPatternIndex); - // this->currentPatternIndex++; - // ESP_LOGD(LED_MANAGER_TAG, "updated stage %d", this->currentPatternIndex); + if (xQueueReceive(this->ledStateQueue, &buffer, 10)) + { + this->updateState(buffer); + } + else + { + // 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) + { + this->currentPatternIndex = 0; + this->finishedPattern = false; + } + } +} + +void LEDManager::displayCurrentPattern() +{ + BlinkPatterns_t ledState = this->ledStateMap[this->currentState].patterns[this->currentPatternIndex]; + this->toggleLED(ledState.state); + this->timeToDelayFor = ledState.delayTime; + + if (!(this->currentPatternIndex >= this->ledStateMap[this->currentState].patterns.size() - 1)) + this->currentPatternIndex++; + else + { + this->finishedPattern = true; + this->toggleLED(LED_OFF); + } +} + +void LEDManager::updateState(LEDStates_e newState) +{ + // we should change the displayed state + // only if we finished displaying the current one - which is handled by the task + // if the new state is not the same as the current one + // and if can actually display the state + + // if we've got an error state - that's it, we'll just keep repeating it indefinitely + if (this->ledStateMap[this->currentState].isError) + return; + + 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 { gpio_set_level(blink_led_pin, state); @@ -145,8 +174,7 @@ void HandleLEDDisplayTask(void *pvParameter) while (1) { - printf("Hello from a task \n"); - printf("The led manager is %d \n", bool(ledManager)); - vTaskDelay(100); + ledManager->handleLED(); + vTaskDelay(ledManager->getTimeToDelayFor()); } } \ No newline at end of file diff --git a/components/LEDManager/LEDManager/LEDManager.hpp b/components/LEDManager/LEDManager/LEDManager.hpp index c533665..801295f 100644 --- a/components/LEDManager/LEDManager/LEDManager.hpp +++ b/components/LEDManager/LEDManager/LEDManager.hpp @@ -28,7 +28,15 @@ struct BlinkPatterns_t int delayTime; }; -typedef std::unordered_map> ledStateMap_t; +struct LEDStage +{ + bool isError; + bool isRepeatable; + std::vector patterns; +}; + +typedef std::unordered_map + ledStateMap_t; class LEDManager { @@ -37,16 +45,25 @@ public: void setup(); void handleLED(); + size_t getTimeToDelayFor() const { return timeToDelayFor; } private: + void toggleLED(bool state) const; + void displayCurrentPattern(); + void updateState(LEDStates_e newState); + gpio_num_t blink_led_pin; gpio_num_t illumninator_led_pin; QueueHandle_t ledStateQueue; static ledStateMap_t ledStateMap; + + LEDStates_e buffer; LEDStates_e currentState; - void toggleLED(bool state) const; + size_t currentPatternIndex = 0; + size_t timeToDelayFor = 100; + bool finishedPattern = false; }; void HandleLEDDisplayTask(void *pvParameter); diff --git a/components/ProjectConfig/ProjectConfig/ProjectConfig.cpp b/components/ProjectConfig/ProjectConfig/ProjectConfig.cpp index 59c43d8..df642e2 100644 --- a/components/ProjectConfig/ProjectConfig/ProjectConfig.cpp +++ b/components/ProjectConfig/ProjectConfig/ProjectConfig.cpp @@ -126,7 +126,6 @@ void ProjectConfig::setWifiConfig(const std::string &networkName, { size_t size = this->config.networks.size(); - // rewrite it to std::find auto it = std::find_if( this->config.networks.begin(), this->config.networks.end(), diff --git a/components/StateManager/StateManager/StateManager.hpp b/components/StateManager/StateManager/StateManager.hpp index 1729279..34a3136 100644 --- a/components/StateManager/StateManager/StateManager.hpp +++ b/components/StateManager/StateManager/StateManager.hpp @@ -12,9 +12,7 @@ struct DeviceStates _LedStateNone, _LedStateStreaming, _LedStateStoppedStreaming, - _WebServerState_Error, _WiFiState_Error, - _MDNSState_Error, _Camera_Error, _WiFiState_Connecting, _WiFiState_Connected