mirror of
https://github.com/MrUnknownDE/OpenIris-ESPIDF.git
synced 2026-04-14 04:03:44 +02:00
Add proper state manager
This commit is contained in:
3
components/StateManager/CMakeLists.txt
Normal file
3
components/StateManager/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "StateManager/StateManager.cpp"
|
||||
INCLUDE_DIRS "StateManager"
|
||||
)
|
||||
9
components/StateManager/StateManager/StateManager.cpp
Normal file
9
components/StateManager/StateManager/StateManager.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "StateManager.hpp"
|
||||
|
||||
StateManager<WiFiState_e> wifiStateManager;
|
||||
StateManager<WebServerState_e> webServerStateManager;
|
||||
StateManager<MDNSState_e> mdnsStateManager;
|
||||
StateManager<CameraState_e> cameraStateManager;
|
||||
StateManager<LEDStates_e> ledStateManager;
|
||||
StateManager<StreamState_e> streamStateManager;
|
||||
StateManager<ConfigState_e> configStateManager;
|
||||
128
components/StateManager/StateManager/StateManager.hpp
Normal file
128
components/StateManager/StateManager/StateManager.hpp
Normal file
@@ -0,0 +1,128 @@
|
||||
#pragma once
|
||||
#ifndef STATEMANAGER_HPP
|
||||
#define STATEMANAGER_HPP
|
||||
/*
|
||||
* StateManager
|
||||
* All Project States are managed here
|
||||
*/
|
||||
struct DeviceStates
|
||||
{
|
||||
enum LEDStates_e
|
||||
{
|
||||
_LedStateNone,
|
||||
_Improv_Start,
|
||||
_Improv_Stop,
|
||||
_Improv_Processing,
|
||||
_Improv_Error,
|
||||
_WebServerState_Error,
|
||||
_WiFiState_Error,
|
||||
_MDNSState_Error,
|
||||
_Camera_Error,
|
||||
_WiFiState_Connecting,
|
||||
_WiFiState_Connected
|
||||
};
|
||||
|
||||
enum ConfigState_e
|
||||
{
|
||||
configLoaded,
|
||||
deviceConfigUpdated,
|
||||
mdnsConfigUpdated,
|
||||
networksConfigUpdated,
|
||||
apConfigUpdated,
|
||||
wifiTxPowerUpdated,
|
||||
cameraConfigUpdated
|
||||
};
|
||||
|
||||
enum WiFiState_e
|
||||
{
|
||||
WiFiState_NotInitialized,
|
||||
WiFiState_Initialized,
|
||||
WiFiState_ReadyToConect,
|
||||
WiFiState_Connecting,
|
||||
WiFiState_WaitingForIp,
|
||||
WiFiState_Connected,
|
||||
WiFiState_Disconnected,
|
||||
WiFiState_ADHOC,
|
||||
WiFiState_Error
|
||||
};
|
||||
|
||||
enum WebServerState_e
|
||||
{
|
||||
WebServerState_Stopped,
|
||||
WebServerState_Starting,
|
||||
WebServerState_Started,
|
||||
WebServerState_Stopping,
|
||||
WebServerState_Error
|
||||
};
|
||||
|
||||
enum MDNSState_e
|
||||
{
|
||||
MDNSState_Stopped,
|
||||
MDNSState_Starting,
|
||||
MDNSState_Started,
|
||||
MDNSState_Stopping,
|
||||
MDNSState_Error,
|
||||
MDNSState_QueryStarted,
|
||||
MDNSState_QueryComplete
|
||||
};
|
||||
|
||||
enum CameraState_e
|
||||
{
|
||||
Camera_Disconnected,
|
||||
Camera_Success,
|
||||
Camera_Connected,
|
||||
Camera_Error
|
||||
};
|
||||
|
||||
enum StreamState_e
|
||||
{
|
||||
Stream_OFF,
|
||||
Stream_ON,
|
||||
Stream_Error
|
||||
};
|
||||
};
|
||||
|
||||
/*
|
||||
* EventManager
|
||||
* All Project Events are managed here
|
||||
*/
|
||||
template <class T>
|
||||
class StateManager
|
||||
{
|
||||
public:
|
||||
StateManager() { this->_current_state = static_cast<T>(0); }
|
||||
|
||||
virtual ~StateManager() {}
|
||||
|
||||
/*
|
||||
* @brief Sets the state of the stateManager
|
||||
* @param T state - the state to be set
|
||||
*/
|
||||
void setState(T state) { _current_state = state; }
|
||||
|
||||
/*
|
||||
* @brief Returns the current state of the stateManager
|
||||
*/
|
||||
T getCurrentState() { return _current_state; }
|
||||
|
||||
private:
|
||||
T _current_state;
|
||||
};
|
||||
|
||||
typedef DeviceStates::WiFiState_e WiFiState_e;
|
||||
typedef DeviceStates::WebServerState_e WebServerState_e;
|
||||
typedef DeviceStates::MDNSState_e MDNSState_e;
|
||||
typedef DeviceStates::CameraState_e CameraState_e;
|
||||
typedef DeviceStates::LEDStates_e LEDStates_e;
|
||||
typedef DeviceStates::StreamState_e StreamState_e;
|
||||
typedef DeviceStates::ConfigState_e ConfigState_e;
|
||||
|
||||
extern StateManager<WiFiState_e> wifiStateManager;
|
||||
extern StateManager<WebServerState_e> webServerStateManager;
|
||||
extern StateManager<MDNSState_e> mdnsStateManager;
|
||||
extern StateManager<CameraState_e> cameraStateManager;
|
||||
extern StateManager<LEDStates_e> ledStateManager;
|
||||
extern StateManager<StreamState_e> streamStateManager;
|
||||
extern StateManager<ConfigState_e> configStateManager;
|
||||
|
||||
#endif // STATEMANAGER_HPP
|
||||
@@ -1,4 +1,4 @@
|
||||
idf_component_register(SRCS "wifiManager/wifiManager.cpp"
|
||||
INCLUDE_DIRS "wifiManager"
|
||||
REQUIRES esp_wifi nvs_flash esp_event esp_netif lwip
|
||||
REQUIRES esp_wifi nvs_flash esp_event esp_netif lwip StateManager
|
||||
)
|
||||
@@ -1,7 +1,5 @@
|
||||
#include "wifiManager.hpp"
|
||||
|
||||
WiFiManager::state_e WiFiManager::_state = {state_e::NOT_INITIALIZED};
|
||||
|
||||
void event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data)
|
||||
{
|
||||
@@ -80,11 +78,11 @@ void WiFiManager::Begin()
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &_wifi_cfg));
|
||||
|
||||
_state = state_e::READY_TO_CONNECT;
|
||||
wifiStateManager.setState(WiFiState_e::WiFiState_ReadyToConect);
|
||||
esp_wifi_start();
|
||||
ESP_LOGI(WIFI_MAMANGER_TAG, "wifi_init_sta finished.");
|
||||
|
||||
_state = state_e::CONNECTING;
|
||||
wifiStateManager.setState(WiFiState_e::WiFiState_Connecting);
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
pdFALSE,
|
||||
@@ -97,14 +95,15 @@ void WiFiManager::Begin()
|
||||
{
|
||||
ESP_LOGI(WIFI_MAMANGER_TAG, "connected to ap SSID:%s password:%s",
|
||||
_wifi_cfg.sta.ssid, _wifi_cfg.sta.password);
|
||||
_state = state_e::CONNECTED;
|
||||
|
||||
wifiStateManager.setState(WiFiState_e::WiFiState_Connected);
|
||||
}
|
||||
|
||||
else if (bits & WIFI_FAIL_BIT)
|
||||
{
|
||||
ESP_LOGE(WIFI_MAMANGER_TAG, "Failed to connect to SSID:%s, password:%s",
|
||||
_wifi_cfg.sta.ssid, _wifi_cfg.sta.password);
|
||||
_state = state_e::ERROR;
|
||||
wifiStateManager.setState(WiFiState_e::WiFiState_Error);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <string>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <StateManager.hpp>
|
||||
|
||||
#include "esp_event.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_log.h"
|
||||
@@ -19,20 +21,6 @@ static const char *WIFI_MAMANGER_TAG = "[WIFI_MANAGER]";
|
||||
|
||||
class WiFiManager
|
||||
{
|
||||
public:
|
||||
public:
|
||||
enum class state_e
|
||||
{
|
||||
NOT_INITIALIZED,
|
||||
INITIALIZED,
|
||||
READY_TO_CONNECT,
|
||||
CONNECTING,
|
||||
WAITING_FOR_IP,
|
||||
CONNECTED,
|
||||
DISCONNECTED,
|
||||
ERROR
|
||||
};
|
||||
|
||||
private:
|
||||
uint8_t channel;
|
||||
wifi_init_config_t _wifi_init_cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
@@ -41,8 +29,6 @@ private:
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
|
||||
static state_e _state;
|
||||
|
||||
int8_t power;
|
||||
bool _enable_adhoc;
|
||||
|
||||
@@ -51,7 +37,6 @@ private:
|
||||
|
||||
public:
|
||||
void SetCredentials(const char *ssid, const char *password);
|
||||
constexpr const state_e &GetState(void) { return _state; }
|
||||
void Begin();
|
||||
};
|
||||
|
||||
|
||||
@@ -65,7 +65,9 @@ extern "C" void app_main(void)
|
||||
// port the logo - done
|
||||
// port preferences lib - DONE; prolly temporary
|
||||
// then port the config - done, needs todos done
|
||||
// then port the led manager as this will be fairly easy
|
||||
// State Management - done
|
||||
// then port the led manager as this will be fairly easy - in progress
|
||||
// then add ADHOC and support for more networks in wifi manager
|
||||
// then port the serial manager
|
||||
// then port the camera manager
|
||||
// then port the streaming stuff (web and uvc)
|
||||
|
||||
Reference in New Issue
Block a user