Add proper state manager

This commit is contained in:
Lorow
2024-09-21 14:13:44 +02:00
parent e6e884b54c
commit 336db18f97
7 changed files with 151 additions and 25 deletions

View File

@@ -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
)

View File

@@ -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
{

View File

@@ -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();
};