SOLID design for wifi module

This commit is contained in:
2025-05-26 14:44:08 -04:00
parent aa89bef583
commit a40931b791
6 changed files with 39 additions and 27 deletions

2
.idea/firmware.iml generated Normal file
View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/firmware.iml" filepath="$PROJECT_DIR$/.idea/firmware.iml" />
</modules>
</component>
</project>

View File

@@ -8,26 +8,17 @@
#include "freertos/semphr.h"
#include "esp_wifi.h"
int WifiManager::init() {
WifiManager::WifiManager() {
esp_netif_init();
esp_event_loop_create_default();
esp_netif_create_default_wifi_sta();
printf("finished esp init");
this->m_mutex = xSemaphoreCreateMutex();
this->m_state = wifi_state::connect;
this->m_state = wifi_state::disconnected;
this->m_attempts = 0;
printf("finished variable init");
printf("lambda");
this->m_task = nullptr; // suppress warning
xTaskCreate(reinterpret_cast<TaskFunction_t>(s_manage), "wifi_task", 4096, this, 5, &m_task);
printf("task create");
return 0;
}
int WifiManager::connect() {
@@ -51,25 +42,20 @@ int WifiManager::disconnect() {
switch (state) {
case wifi_state::connect:
printf("connect state\n");
init_connection();
update_state(wifi_state::connecting);
break;
case wifi_state::connecting:
printf("connecting state\n");
handle_connecting();
break;
case wifi_state::searching:
printf("searching state\n");
handle_search();
break;
case wifi_state::disconnect:
printf("disconnect state\n");
handle_disconnect();
update_state(wifi_state::disconnected);
break;
default:
printf("sleep state\n");
vTaskSuspend(nullptr);
break;
}
@@ -128,6 +114,8 @@ int WifiManager::handle_disconnect() {
}
int WifiManager::handle_search() {
// todo: softap
printf("Waiting for configuration");
return 0;
}

View File

@@ -0,0 +1,15 @@
//
// Created by Johnathon Slightham on 2025-05-26.
//
#ifndef IWIFIMANAGER_H
#define IWIFIMANAGER_H
class IWifiManager {
public:
virtual ~IWifiManager() {};
virtual int connect();
virtual int disconnect();
};
#endif //IWIFIMANAGER_H

View File

@@ -6,13 +6,14 @@
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
class WifiManager {
#include "IWifiManager.h"
class WifiManager final : IWifiManager {
public:
WifiManager() = default;
~WifiManager() = default;
int init();
int connect();
int disconnect();
WifiManager();
~WifiManager() override = default;
int connect() override;
int disconnect() override;
[[noreturn]] void manage();
@@ -28,7 +29,6 @@ private:
static void wifi_event_handler(void *event_handler_arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
SemaphoreHandle_t m_mutex;
wifi_state m_state;
TaskHandle_t m_task;

View File

@@ -1,21 +1,20 @@
#include <cstdio>
#include <cinttypes>
#include <memory>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"
#include "nvs_flash.h"
#include "WifiManager.h"
#include "IWifiManager.h"
extern "C" [[noreturn]] void app_main(void) {
nvs_flash_init();
const auto manager = std::make_unique<WifiManager>();
manager->init();
manager->connect();
printf("Hello world!\n");