From 1bbafd4bb5578a6c1559e9446e62b68818a2d704 Mon Sep 17 00:00:00 2001 From: Matthew Colvin <35540398+Mc067415@users.noreply.github.com> Date: Fri, 11 Aug 2023 22:50:26 -0500 Subject: [PATCH] update wifi interface and handler add the wifi interface to the hardware abstract --- Platformio/HAL/HardwareAbstract.cpp | 11 +++++--- Platformio/HAL/HardwareAbstract.hpp | 6 ++++- .../HardwareInterfaces/wifiHandlerInterface.h | 5 ++-- Platformio/HAL/Targets/ESP32/HardwareRevX.cpp | 9 ++++--- .../Targets/ESP32/wifiHandler/wifihandler.cpp | 25 +++++++++++-------- .../Targets/ESP32/wifiHandler/wifihandler.hpp | 20 +++++++-------- 6 files changed, 43 insertions(+), 33 deletions(-) diff --git a/Platformio/HAL/HardwareAbstract.cpp b/Platformio/HAL/HardwareAbstract.cpp index 9e29e64..68caaae 100644 --- a/Platformio/HAL/HardwareAbstract.cpp +++ b/Platformio/HAL/HardwareAbstract.cpp @@ -1,9 +1,12 @@ #include "HardwareAbstract.hpp" -HardwareAbstract::HardwareAbstract(std::shared_ptr aBattery) -: mBattery(std::move(aBattery)){ - -} +HardwareAbstract::HardwareAbstract( + std::shared_ptr aBattery, + std::shared_ptr aWifiHandler +) +: mBattery(std::move(aBattery)), + mWifiHandler(std::move(aWifiHandler)) +{} std::optional HardwareAbstract::getBatteryStatus(){ if(mBattery){ diff --git a/Platformio/HAL/HardwareAbstract.hpp b/Platformio/HAL/HardwareAbstract.hpp index 8962afd..3e1971f 100644 --- a/Platformio/HAL/HardwareAbstract.hpp +++ b/Platformio/HAL/HardwareAbstract.hpp @@ -7,6 +7,7 @@ #include #include #include "BatteryInterface.h" +#include "wifiHandlerInterface.h" class HardwareAbstract { public: @@ -20,7 +21,9 @@ public: }; virtual std::optional getBatteryStatus(); - HardwareAbstract(std::shared_ptr aBattery = nullptr); + HardwareAbstract(std::shared_ptr aBattery = nullptr, + std::shared_ptr aWifiHandler = nullptr + ); /// @brief Override in order to do setup of hardware devices virtual void init() = 0; @@ -31,4 +34,5 @@ public: private: std::shared_ptr mBattery; + std::shared_ptr mWifiHandler; }; diff --git a/Platformio/HAL/HardwareInterfaces/wifiHandlerInterface.h b/Platformio/HAL/HardwareInterfaces/wifiHandlerInterface.h index 08a48aa..e8c2a31 100644 --- a/Platformio/HAL/HardwareInterfaces/wifiHandlerInterface.h +++ b/Platformio/HAL/HardwareInterfaces/wifiHandlerInterface.h @@ -1,6 +1,5 @@ #pragma once -#include -#include "DisplayInterface.h" +#include class wifiHandlerInterface{ public: @@ -11,5 +10,5 @@ class wifiHandlerInterface{ virtual void turnOff() = 0; virtual void scan() = 0; virtual char* getSSID() = 0; - virtual String getIP() = 0; + virtual std::string getIP() = 0; }; \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp index bed04a4..040d339 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp @@ -1,4 +1,5 @@ #include "HardwareRevX.hpp" +#include "wifihandler.hpp" #include "driver/ledc.h" std::shared_ptr HardwareRevX::mInstance = nullptr; @@ -49,9 +50,11 @@ void HardwareRevX::initIO() { gpio_deep_sleep_hold_dis(); } -HardwareRevX::HardwareRevX():HardwareAbstract(std::make_shared(ADC_BAT,CRG_STAT)){ - -} +HardwareRevX::HardwareRevX(): + HardwareAbstract( + std::make_shared(ADC_BAT,CRG_STAT), + wifiHandler::getInstance() + ){} HardwareRevX::WakeReason getWakeReason() { // Find out wakeup cause diff --git a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp index 97526c0..e75bbec 100644 --- a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp +++ b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp @@ -2,7 +2,8 @@ #include #include -wifiHandler* mInstance; +std::shared_ptr wifiHandler::mInstance = nullptr; + // WiFi status event void wifiHandler::WiFiEvent(WiFiEvent_t event){ @@ -29,7 +30,7 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event){ case ARDUINO_EVENT_WIFI_STA_GOT_IP6: // TODO convert to callbacks //display.update_wifi(true); - this->update_credentials(temporary_ssid, temporary_password); + //update_credentials(temporary_ssid, temporary_password); break; case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: case ARDUINO_EVENT_WIFI_STA_LOST_IP: @@ -41,15 +42,14 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event){ } } -wifiHandler* wifiHandler::getInstance() +std::shared_ptr wifiHandler::getInstance() { - if(instance == nullptr) + if(mInstance) { - instance = new wifiHandler(); + return mInstance; } - - return instance; -} + return std::shared_ptr(new wifiHandler()); +}; String wifiHandler::getFoundSSID(unsigned int index) { @@ -92,7 +92,6 @@ void wifiHandler::scan() void wifiHandler::begin() { //this->display = display; - mInstance = wifiHandler::getInstance(); WiFi.setHostname("OMOTE"); WiFi.mode(WIFI_STA); WiFi.onEvent([] (WiFiEvent_t event) {mInstance->WiFiEvent(event);}); @@ -137,6 +136,10 @@ void wifiHandler::turnOff() WiFi.mode(WIFI_OFF); } +void wifiHandler::disconnect(){ + WiFi.disconnect(); +} + bool wifiHandler::isConnected() { return WiFi.isConnected(); @@ -147,7 +150,7 @@ char* wifiHandler::getSSID() return this->SSID; } -String wifiHandler::getIP() +std::string wifiHandler::getIP() { - return WiFi.localIP().toString(); + return std::string(WiFi.localIP().toString().c_str()); } \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp index 1ac8a08..11748a9 100644 --- a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp +++ b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp @@ -6,7 +6,7 @@ class wifiHandler: public wifiHandlerInterface { public: - static wifiHandler* getInstance(); + static std::shared_ptr getInstance(); /** * @brief Function to initialize the wifi handler * @@ -78,7 +78,7 @@ class wifiHandler: public wifiHandlerInterface { * @param temporary_ssid * @param temporary_password */ - static void update_credentials(const char* temporary_ssid, const char* temporary_password); + void update_credentials(const char* temporary_ssid, const char* temporary_password); void WiFiEvent(WiFiEvent_t event); @@ -87,27 +87,25 @@ class wifiHandler: public wifiHandlerInterface { * * @return String IP Address of the device */ - String getIP(); + std::string getIP(); private: - static wifiHandler* instance; - - static char temporary_password[STRING_SIZE]; - static char temporary_ssid[STRING_SIZE]; - - wifiHandler(); + static std::shared_ptr mInstance; + char temporary_password[STRING_SIZE]; + char temporary_ssid[STRING_SIZE]; + /** * @brief Internal variable to store the wifi password * */ - static char password[STRING_SIZE]; + char password[STRING_SIZE]; /** * @brief Internal variable to store the wifi SSID * */ - static char SSID[STRING_SIZE]; + char SSID[STRING_SIZE]; }; \ No newline at end of file