- Add one new state to LEDStates enum
- Added proper lookup-table based state mapping for led states
- created a handleLED method to call in the loop
This commit is contained in:
ZanzyTHEbar 2022-09-25 13:12:03 +01:00
parent 335abd1de8
commit 40ca34c382
5 changed files with 89 additions and 35 deletions

View File

@ -22,6 +22,7 @@ struct DeviceStates
_LEDOff,
_LEDOn,
_LEDBlink,
_LEDBlinkFast,
_SerialManager_Start,
_SerialManager_Stop,
_SerialManager_Error,

View File

@ -1,6 +1,51 @@
#include "LEDManager.hpp"
LEDManager::LEDManager(byte pin) : _ledPin(pin), _previousMillis(0), _ledState(false) {}
/**
*! @brief This is declared as a static member of the class - therefor it must be initialized outside of the class.
** @brief This is a map of the LEDStates and the BlinkPatterns.
** @brief The LEDStates are the keys and the BlinkPatterns are the values.
** @brief The BlinkPatterns are the number of times to blink and the delay between blinks.
*/
LEDManager::ledStateMap_t LEDManager::ledStateMap = {
{LEDStates_e::_LEDOff, {0, 0}},
{LEDStates_e::_LEDOn, {0, 0}},
{LEDStates_e::_LEDBlink, {1, 500}},
{LEDStates_e::_LEDBlinkFast, {1, 250}},
{LEDStates_e::_SerialManager_Start, {1, 500}},
{LEDStates_e::_SerialManager_Stop, {1, 500}},
{LEDStates_e::_SerialManager_Error, {1, 500}},
{LEDStates_e::_WiFiState_None, {1, 500}},
{LEDStates_e::_WiFiState_Connecting, {1, 500}},
{LEDStates_e::_WiFiState_Connected, {1, 500}},
{LEDStates_e::_WiFiState_Disconnected, {1, 500}},
{LEDStates_e::_WiFiState_Disconnecting, {1, 500}},
{LEDStates_e::_WiFiState_ADHOC, {1, 500}},
{LEDStates_e::_WiFiState_Error, {1, 500}},
{LEDStates_e::_WebServerState_None, {1, 500}},
{LEDStates_e::_WebServerState_Starting, {1, 500}},
{LEDStates_e::_WebServerState_Started, {1, 500}},
{LEDStates_e::_WebServerState_Stopping, {1, 500}},
{LEDStates_e::_WebServerState_Stopped, {1, 500}},
{LEDStates_e::_WebServerState_Error, {1, 500}},
{LEDStates_e::_MDNSState_None, {1, 500}},
{LEDStates_e::_MDNSState_Starting, {1, 500}},
{LEDStates_e::_MDNSState_Started, {1, 500}},
{LEDStates_e::_MDNSState_Stopping, {1, 500}},
{LEDStates_e::_MDNSState_Stopped, {1, 500}},
{LEDStates_e::_MDNSState_Error, {1, 500}},
{LEDStates_e::_Camera_Success, {1, 500}},
{LEDStates_e::_Camera_Connected, {1, 500}},
{LEDStates_e::_Camera_Disconnected, {1, 500}},
{LEDStates_e::_Camera_Error, {1, 500}},
};
//!TODO: Change the parameters for each LED state to be unique.
LEDManager::LEDManager(byte pin,
StateManager<LEDStates_e> *stateManager) : _ledPin(pin),
stateManager(stateManager),
_previousMillis(0),
_ledState(false) {}
LEDManager::~LEDManager() {}
@ -11,40 +56,43 @@ void LEDManager::begin()
}
/**
* @brief Control the LED timer
* @brief Control the LED
* @details This function must be called in the main loop
*
* @param time
*
*/
void LEDManager::handleLED(unsigned long time)
void LEDManager::handleLED()
{
unsigned long currentMillis = millis();
if (currentMillis - _previousMillis >= time)
if (ledStateMap.find(stateManager->getCurrentState()) != ledStateMap.end())
{
_previousMillis = currentMillis;
_ledState = !_ledState;
blinkPatterns = ledStateMap[stateManager->getCurrentState()]; // Get the blink pattern for the current state
unsigned long currentMillis = millis(); // Get the current time
if (currentMillis - _previousMillis >= blinkPatterns.delayTime) // Check if the current time is greater than the previous time plus the delay time
{
_previousMillis = currentMillis;
for (int i = 0; i < blinkPatterns.times; i++)
{
_ledState = !_ledState;
onOff(_ledState);
}
}
stateManager->setState(LEDStates_e::_LEDOff); // Set the state to off
onOff(false); // Turn the LED off
return;
}
log_e("LED State not found");
}
/**
* @brief Turn the LED on or off
*
* @param state
*
* @param state
*/
void LEDManager::onOff(bool state) const
{
digitalWrite(_ledPin, state);
}
/**
* @brief Blink the LED
* @details This function requires the handleLED function to be called in the main loop
*/
void LEDManager::blink()
{
onOff(_ledState);
}
/**
* @brief Blink the LED a number of times
* @details This function is blocking and does not require the handleLED function to be called in the main loop
@ -61,11 +109,3 @@ void LEDManager::blink(int times, int delayTime)
delay(delayTime);
}
}
/**
* @brief Display the status of the LED
* @details This function requires the handleLED function to be called in the main loop
*/
void LEDManager::displayStatus()
{
}

View File

@ -1,24 +1,37 @@
#ifndef LEDMANAGER_HPP
#define LEDMANAGER_HPP
#include <Arduino.h>
#include <data/StateManager/StateManager.hpp>
#include <unordered_map>
class LEDManager
{
public:
LEDManager(byte pin);
LEDManager(byte pin,
StateManager<LEDStates_e> *stateManager);
virtual ~LEDManager();
void begin();
void handleLED();
void onOff(bool state) const;
void blink();
void blink(int times, int delayTime);
void handleLED(unsigned long time);
void displayStatus();
private:
byte _ledPin;
StateManager<LEDStates_e> *stateManager;
unsigned long _previousMillis;
bool _ledState;
struct BlinkPatterns
{
int times;
int delayTime;
};
BlinkPatterns blinkPatterns;
typedef std::unordered_map<LEDStates_e, BlinkPatterns> ledStateMap_t;
static ledStateMap_t ledStateMap;
};
#endif // LEDMANAGER_HPP

View File

@ -22,7 +22,7 @@ ProjectConfig deviceConfig;
#if ENABLE_OTA
OTA ota(&deviceConfig);
#endif // ENABLE_OTA
LEDManager ledManager(33);
LEDManager ledManager(33, &ledStateManager);
CameraHandler cameraHandler(&deviceConfig);
// SerialManager serialManager(&deviceConfig);
WiFiHandler wifiHandler(&deviceConfig, &wifiStateManager, WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
@ -99,6 +99,6 @@ void loop()
#if ENABLE_OTA
ota.HandleOTAUpdate();
#endif // ENABLE_OTA
ledManager.displayStatus();
ledManager.handleLED();
// serialManager.handleSerial();
}

View File

@ -1 +1 @@
32
34