diff --git a/Platformio/HAL/HardwareAbstract.hpp b/Platformio/HAL/HardwareAbstract.hpp index 65b79a9..694ae4d 100644 --- a/Platformio/HAL/HardwareAbstract.hpp +++ b/Platformio/HAL/HardwareAbstract.hpp @@ -1,7 +1,7 @@ // OMOTE Hardware Abstraction // 2023 Matthew Colvin -#ifndef _HARDWAREABSTRACT_H_ -#define _HARDWAREABSTRACT_H_ +#pragma once +#include "wifiHandlerInterface.h" #include #include #include @@ -10,17 +10,6 @@ #include #include "Notification.hpp" -typedef struct { - std::string ssid; - int rssi; -} WifiInfo; - -typedef struct { - bool isConnected; - std::string IP; - std::string ssid; -}wifiStatus; - class HardwareAbstract { public: HardwareAbstract( @@ -40,7 +29,7 @@ public: /// @param onBatteryStatusChangeHandler - Callable to be ran when batter status changes void onBatteryChange(std::function onBatteryStatusChangeHandler); - + virtual std::shared_ptr wifi() = 0; /// @brief Override in order to do setup of hardware devices virtual void init() = 0; @@ -48,15 +37,9 @@ public: /// @param message - Debug message virtual void debugPrint(const char* fmt, ...) = 0; - Notification>> wifi_scan_done; - Notification<> wifi_scan_start; - Notification, std::shared_ptr> wifi_connect; - Notification> wifi_status_update; - protected: Notification mBatteryNotification; private: -}; -#endif \ No newline at end of file +}; \ No newline at end of file diff --git a/Platformio/HAL/HardwareModules/wifiHandlerInterface.h b/Platformio/HAL/HardwareModules/wifiHandlerInterface.h index 38caa54..a7b3640 100644 --- a/Platformio/HAL/HardwareModules/wifiHandlerInterface.h +++ b/Platformio/HAL/HardwareModules/wifiHandlerInterface.h @@ -1,14 +1,24 @@ #pragma once #include -#include "HardwareAbstract.hpp" +#include +#include + +typedef struct { + std::string ssid; + int rssi; +} WifiInfo; + +typedef struct { + bool isConnected; + std::string IP; + std::string ssid; +}wifiStatus; class wifiHandlerInterface{ public: - virtual void begin() = 0; - //virtual void connect(const char* SSID, const char* password) = 0; - virtual void disconnect() = 0; - virtual bool isConnected() = 0; - virtual void turnOff() = 0; + virtual bool isAvailable() = 0; virtual void scan() = 0; - virtual std::string getIP() = 0; + virtual void connect(std::shared_ptr ssid, std::shared_ptr password) = 0; + virtual void onScanDone(std::function>)> function) = 0; + virtual void onStatusUpdate(std::function)> function) = 0; }; \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp index e29c29b..94bbb6d 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp @@ -71,9 +71,9 @@ void HardwareRevX::init() { // Make sure ESP32 is running at full speed setCpuFrequencyMhz(240); - mDisplay = Display::getInstance(std::shared_ptr(this)); - mBattery = std::make_shared(ADC_BAT,CRG_STAT); - mWifiHandler = wifiHandler::getInstance(std::shared_ptr(this)); + this->mDisplay = Display::getInstance(std::shared_ptr(this)); + this->mBattery = std::make_shared(ADC_BAT,CRG_STAT); + this->mWifiHandler = wifiHandler::getInstance(std::shared_ptr(this)); wakeup_reason = getWakeReason(); initIO(); setupBacklight(); @@ -111,6 +111,10 @@ std::shared_ptr HardwareRevX::getInstance(){ return mInstance; } +std::shared_ptr HardwareRevX::wifi() +{ + return this->mWifiHandler; +} void HardwareRevX::activityDetection() { static int accXold; diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp index ed5fa67..79d0329 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp @@ -13,6 +13,7 @@ #include #include #include +#include "wifihandler.hpp" #include "omoteconfig.h" @@ -30,14 +31,12 @@ public: // HardwareAbstract virtual void init() override; - #if 0 - virtual void debugPrint(std::string aDebugMessage) override; - #else void debugPrint(const char* fmt, ...); - #endif void loopHandler(); + std::shared_ptr wifi(); + protected: // Init Functions to setup hardware void initIO(); diff --git a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp index bde29b9..9488a35 100644 --- a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp +++ b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp @@ -36,7 +36,7 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event){ Serial.print(" found\n"); //this->display.wifi_scan_complete( no_networks); } - mHardware->wifi_scan_done.notify(info); + this->scan_notification.notify(info); break; } case ARDUINO_EVENT_WIFI_STA_GOT_IP: @@ -57,6 +57,9 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event){ Serial.println(WiFi.status()); } +bool wifiHandler::isAvailable(){ + return true; +} std::shared_ptr wifiHandler::getInstance(std::shared_ptr aHardware) { if(mInstance) @@ -70,8 +73,6 @@ std::shared_ptr wifiHandler::getInstance(std::shared_ptr aHardware) { this->mHardware = aHardware; - this->mHardware->wifi_scan_start.onNotify([this](){this->mHardware->debugPrint("scan called\n"); this->scan();}); - this->mHardware->wifi_connect.onNotify([this] (std::shared_ptr ssid, std::shared_ptr password){this->connect(ssid, password);}); this->password = ""; this->SSID = ""; this->begin(); @@ -98,7 +99,7 @@ void wifiHandler::update_status() //Serial.println(WiFi.localIP()); - this->mHardware->wifi_status_update.notify(status); + this->status_update.notify(status); } void wifiHandler::update_credentials() @@ -178,6 +179,14 @@ void wifiHandler::begin() WiFi.setSleep(true); } +void wifiHandler::onScanDone(std::function>)> function){ + this->scan_notification.onNotify(std::move(function)); +} + +void wifiHandler::onStatusUpdate(std::function)> function){ + this->status_update.onNotify(std::move(function)); +} + void wifiHandler::connect(std::shared_ptr ssid, std::shared_ptr password) { this->temporary_password = password; diff --git a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp index 458e245..bd4bf7c 100644 --- a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp +++ b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp @@ -1,6 +1,8 @@ #pragma once #include "wifiHandlerInterface.h" #include "HardwareAbstract.hpp" +#include "Notification.hpp" +#include "memory.h" #include #define STRING_SIZE 50 @@ -21,8 +23,61 @@ class wifiHandler: public wifiHandlerInterface { * @param SSID * @param password */ + void connect(std::shared_ptr ssid, std::shared_ptr password); //void connect(const char* SSID, const char* password); + + + /** + * @brief function to trigger asynchronous scan for wifi networks + * + */ + void scan(); + bool isAvailable(); + void onScanDone(std::function>)> function); + void onStatusUpdate(std::function)> function); + + + private: + + Notification>> scan_notification; + Notification> status_update; + /** + * @brief Function to update the wifi credentials. This function is called in the wifi event callback function + * after a connection is established. Only then is the new credentials stored and the old stored credentials + * overwritten. + * + * @param temporary_ssid + * @param temporary_password + */ + void update_credentials(); + + void WiFiEvent(WiFiEvent_t event); + + /** + * @brief Function to turn off wifi + * + */ + void turnOff(); + /** + * @brief Function to get the IP address of this device + * + * @return String IP Address of the device + */ + std::string getIP(); + wifiStatus wifi_status; + static std::shared_ptr mInstance; + std::shared_ptr mHardware; + std::shared_ptr temporary_password; + std::shared_ptr temporary_ssid; + + void update_status(); + /** + * @brief Internal variable to store the wifi password + * + */ + std::string password; + /** * @brief Function to disconnect from the network * @@ -36,54 +91,6 @@ class wifiHandler: public wifiHandlerInterface { * @return false Device is not connected to wifi network */ bool isConnected(); - - /** - * @brief Function to turn off wifi - * - */ - void turnOff(); - - /** - * @brief function to trigger asynchronous scan for wifi networks - * - */ - void scan(); - - /** - * @brief Function to update the wifi credentials. This function is called in the wifi event callback function - * after a connection is established. Only then is the new credentials stored and the old stored credentials - * overwritten. - * - * @param temporary_ssid - * @param temporary_password - */ - void update_credentials(); - - void WiFiEvent(WiFiEvent_t event); - - /** - * @brief Function to get the IP address of this device - * - * @return String IP Address of the device - */ - std::string getIP(); - Notification>> scan_done; - private: - - wifiStatus wifi_status; - static std::shared_ptr mInstance; - std::shared_ptr mHardware; - std::shared_ptr temporary_password; - std::shared_ptr temporary_ssid; - - void connect(std::shared_ptr ssid, std::shared_ptr password); - void update_status(); - /** - * @brief Internal variable to store the wifi password - * - */ - std::string password; - /** * @brief Internal variable to store the wifi SSID * diff --git a/Platformio/OmoteUI/wifiSettings.cpp b/Platformio/OmoteUI/wifiSettings.cpp index f975490..355adae 100644 --- a/Platformio/OmoteUI/wifiSettings.cpp +++ b/Platformio/OmoteUI/wifiSettings.cpp @@ -52,7 +52,7 @@ void OmoteUI::password_field_event_cb(lv_event_t* e) const char* password = lv_textarea_get_text(ta); switch(code){ case LV_EVENT_READY: - this->mHardware->wifi_connect.notify(std::make_shared(std::string(ssid)), std::make_shared(std::string(password))); + this->mHardware->wifi()->connect(std::make_shared(std::string(ssid)), std::make_shared(std::string(password))); lv_obj_clear_state(ta, LV_STATE_FOCUSED); this->hide_keyboard(); this->reset_settings_menu(); @@ -73,7 +73,7 @@ void OmoteUI::connect_btn_cb(lv_event_t* event) lv_obj_t* ta = (lv_obj_t*) event->user_data; const char* password = lv_textarea_get_text(ta); - this->mHardware->wifi_connect.notify(std::make_shared(std::string(ssid)), std::make_shared(std::string(password))); + this->mHardware->wifi()->connect(std::make_shared(std::string(ssid)), std::make_shared(std::string(password))); //Trigger wifi connection here //wifihandler.connect(ssid, password); lv_obj_clear_state(ta, LV_STATE_FOCUSED); @@ -233,8 +233,8 @@ void OmoteUI::create_wifi_settings(lv_obj_t* menu, lv_obj_t* parent) this->wifi_selection_page = this->create_wifi_selection_page(menu); this->wifi_password_page = this->create_wifi_password_page(this->settingsMenu); this->create_wifi_main_page(parent); - this->mHardware->wifi_scan_done.onNotify([this] (std::shared_ptr> info) {this->wifi_scan_done(info);}); - this->mHardware->wifi_status_update.onNotify([this] (std::shared_ptr status) {this->wifi_status(status);}); + this->mHardware->wifi()->onScanDone([] (std::shared_ptr> info) {mInstance->wifi_scan_done(info);}); + this->mHardware->wifi()->onStatusUpdate([] (std::shared_ptr status) {mInstance->wifi_status(status);}); } void OmoteUI::wifi_status(std::shared_ptr status) @@ -305,7 +305,7 @@ void OmoteUI::wifi_settings_cb(lv_event_t* event) lv_obj_t* label = lv_label_create(cont); lv_label_set_text(label, "Searching for wifi networks"); mHardware->debugPrint("Wifi settings cb called\n"); - mHardware->wifi_scan_start.notify(); + mHardware->wifi()->scan(); //This will trigger an asynchronouse network scan // We need to trigger wifi search via HAL //wifihandler.scan();