From 38ec26dce7271558cd7f941266bfad15d13f06cc Mon Sep 17 00:00:00 2001 From: MatthewColvin Date: Sun, 15 Oct 2023 00:22:43 -0500 Subject: [PATCH] rework Notifications by adding handler class that can unregister when it falls out of scope. rework wifi Interface around this new concept and patch up some old uses of notifications to follow new paradigm compile out old UI code because notification refactor broke it. --- .../HardwareModules/wifiHandlerInterface.h | 57 ++-- Platformio/HAL/Notification.hpp | 95 +++++- Platformio/HAL/Targets/ESP32/HardwareRevX.cpp | 5 +- Platformio/HAL/Targets/ESP32/HardwareRevX.hpp | 1 + .../HAL/Targets/ESP32/display/display.cpp | 6 +- .../HAL/Targets/ESP32/display/display.hpp | 111 ++++--- .../Targets/ESP32/wifiHandler/wifihandler.cpp | 290 +++++++----------- .../Targets/ESP32/wifiHandler/wifihandler.hpp | 145 ++++----- .../wifiHandlerSim/wifiHandlerSim.cpp | 69 +---- .../wifiHandlerSim/wifiHandlerSim.hpp | 43 +-- Platformio/platformio.ini | 7 +- Platformio/src/simMain.cpp | 1 - 12 files changed, 389 insertions(+), 441 deletions(-) diff --git a/Platformio/HAL/HardwareModules/wifiHandlerInterface.h b/Platformio/HAL/HardwareModules/wifiHandlerInterface.h index f6c2f80..fd61c4d 100644 --- a/Platformio/HAL/HardwareModules/wifiHandlerInterface.h +++ b/Platformio/HAL/HardwareModules/wifiHandlerInterface.h @@ -1,25 +1,46 @@ #pragma once -#include -#include #include +#include +#include -typedef struct { - std::string ssid; - int rssi; -} WifiInfo; +struct WifiInfo { + WifiInfo(){}; + WifiInfo(std::string aSsid, int aRssi) : ssid(aSsid), rssi(aRssi) {} + + std::string ssid = ""; + int rssi = 0; +}; + +struct wifiStatus { + wifiStatus(bool aConnected, std::string aIp, std::string aSsid) + : isConnected(aConnected), IP(aIp), ssid(aSsid){}; -typedef struct { bool isConnected; - std::string IP; - std::string ssid; -}wifiStatus; + std::string IP = ""; + std::string ssid = ""; +}; -class wifiHandlerInterface{ - public: - virtual bool isAvailable() = 0; - virtual void scan() = 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; - virtual void begin() = 0; +class wifiHandlerInterface { +public: + typedef std::vector ScanDoneDataTy; + typedef Notification ScanNotificationTy; + + virtual void begin() = 0; + virtual void scan() = 0; + virtual void connect(std::string ssid, std::string password) = 0; + virtual bool isAvailable() = 0; + + std::shared_ptr ScanCompleteNotification() { + return mScanNotification; + }; + + std::shared_ptr> WifiStatusNotification() { + return mStatusUpdate; + }; + +protected: + std::shared_ptr mScanNotification = + std::make_shared(); + std::shared_ptr> mStatusUpdate = + std::make_shared>(); }; \ No newline at end of file diff --git a/Platformio/HAL/Notification.hpp b/Platformio/HAL/Notification.hpp index 6b8920f..1665f25 100644 --- a/Platformio/HAL/Notification.hpp +++ b/Platformio/HAL/Notification.hpp @@ -1,29 +1,90 @@ #pragma once -#include #include +#include +#include -template -class Notification{ - public: - typedef std::function HandlerTy; +template class Handler; +template class Notification { + friend class Handler; - Notification() = default; - void onNotify(HandlerTy aHandler); - void notify(notifyData... notifySendData); +public: + typedef std::function HandlerTy; + typedef int HandlerID; - private: - std::vector mFunctionHandlers; + Notification() { mIdMaker = 0; }; + void notify(notifyData... notifySendData); + +protected: + HandlerID onNotify(HandlerTy aHandler); + void unregister(HandlerID aHandler); + +private: + std::map mFunctionHandlers; + HandlerID mIdMaker; }; - template -void Notification::onNotify(HandlerTy aHandler){ - mFunctionHandlers.push_back(std::move(aHandler)); +int Notification::onNotify(HandlerTy aHandler) { + if (aHandler) { + mFunctionHandlers[++mIdMaker] = std::move(aHandler); + return mIdMaker; + } else { + return -1; + } } template -void Notification::notify(outboundData... notifySendData){ - for (auto handler : mFunctionHandlers){ - handler(notifySendData...); +void Notification::notify(outboundData... notifySendData) { + for (auto handler : mFunctionHandlers) { + handler.second(notifySendData...); + } +} + +template +void Notification::unregister(HandlerID aHandlerId) { + auto handlerToUnRegister = + std::find_if(mFunctionHandlers.begin(), mFunctionHandlers.end(), + [aHandlerId](auto registeredHandler) { + return aHandlerId == registeredHandler.first; + }); + if (handlerToUnRegister != mFunctionHandlers.end()) { + mFunctionHandlers.erase(handlerToUnRegister); + } +} + +template class Handler { +public: + typedef std::function callableTy; + void operator=(Handler &other) = delete; + + Handler() = default; + Handler(std::shared_ptr> aNotification, + callableTy aCallable = nullptr) + : mNotification(aNotification), + mHandlerId(aNotification->onNotify(aCallable)) {} + + virtual ~Handler() { + if (mHandlerId >= 0) { + mNotification->unregister(mHandlerId); } -} \ No newline at end of file + } + + void operator=(callableTy aHandler) { + if (mHandlerId >= 0) { + mNotification->unregister(mHandlerId); + mHandlerId = -1; + } + if (aHandler) { + mHandlerId = mNotification->onNotify(aHandler); + } + } + + void + SetNotification(std::shared_ptr> aNotification) { + mNotification = aNotification; + } + +private: + std::shared_ptr> mNotification = nullptr; + int mHandlerId = -1; +}; \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp index e517360..fd467ef 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.cpp @@ -77,9 +77,10 @@ void HardwareRevX::init() { mKeys = std::make_shared(); restorePreferences(); - mDisplay->onTouch([this]([[maybe_unused]] auto touchPoint) { + mTouchHandler.SetNotification(mDisplay->TouchNotification()); + mTouchHandler = [this]([[maybe_unused]] auto touchPoint) { standbyTimer = this->getSleepTimeout(); - }); + }; setupIMU(); setupIR(); diff --git a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp index 892a711..43d424e 100644 --- a/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp +++ b/Platformio/HAL/Targets/ESP32/HardwareRevX.hpp @@ -90,4 +90,5 @@ private: IRrecv IrReceiver = IRrecv(IR_RX); static std::shared_ptr mInstance; + Handler mTouchHandler; }; \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/display/display.cpp b/Platformio/HAL/Targets/ESP32/display/display.cpp index 10932d1..41dd16d 100644 --- a/Platformio/HAL/Targets/ESP32/display/display.cpp +++ b/Platformio/HAL/Targets/ESP32/display/display.cpp @@ -58,10 +58,6 @@ void Display::setupBacklight() { ledc_timer_config(&ledc_timer); } -void Display::onTouch(Notification::HandlerTy aTouchHandler) { - mTouchEvent.onNotify(std::move(aTouchHandler)); -} - void Display::setupTFT() { delay(100); tft.init(); @@ -111,7 +107,7 @@ void Display::screenInput(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) { bool touched = false; if ((touchX > 0) || (touchY > 0)) { touched = true; - mTouchEvent.notify(touchPoint); + mTouchEvent->notify(touchPoint); } if (!touched) { diff --git a/Platformio/HAL/Targets/ESP32/display/display.hpp b/Platformio/HAL/Targets/ESP32/display/display.hpp index 214c8b8..a0abe3a 100644 --- a/Platformio/HAL/Targets/ESP32/display/display.hpp +++ b/Platformio/HAL/Targets/ESP32/display/display.hpp @@ -2,10 +2,10 @@ #include "DisplayAbstract.h" #include "HardwareAbstract.hpp" #include "Notification.hpp" -#include -#include -#include #include "driver/ledc.h" +#include +#include +#include /*LEDC Channel to use for the LCD backlight*/ #define LCD_BACKLIGHT_LEDC_CHANNEL LEDC_CHANNEL_5 @@ -16,58 +16,71 @@ #define DEFAULT_BACKLIGHT_BRIGHTNESS 128 +class Display : public DisplayAbstract { +public: + static std::shared_ptr getInstance(); -class Display: public DisplayAbstract -{ - public: - static std::shared_ptr getInstance(); - - /// @brief Set brightness setting and fade to it - /// @param brightness - virtual void setBrightness(uint8_t brightness) override; - virtual uint8_t getBrightness() override; - virtual void turnOff() override; - - void onTouch(Notification::HandlerTy aTouchHandler); + /// @brief Set brightness setting and fade to it + /// @param brightness + virtual void setBrightness(uint8_t brightness) override; + virtual uint8_t getBrightness() override; + virtual void turnOff() override; - inline void wake() {if(isAsleep) {isAsleep = false; startFade();}} - inline void sleep() {if(!isAsleep){isAsleep = true; startFade();}} + std::shared_ptr> TouchNotification() { + return mTouchEvent; + } - protected: - virtual void flushDisplay(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p); - virtual void screenInput(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) override; - - /// @brief Fade toward brightness based on isAwake - /// @return True - Fade complete - /// False - Fade set point not reached - bool fade(); - /// @brief Start the Fade task - void startFade(); + inline void wake() { + if (isAsleep) { + isAsleep = false; + startFade(); + } + } + inline void sleep() { + if (!isAsleep) { + isAsleep = true; + startFade(); + } + } - /// @brief Set the actual display brightness right now - /// @param brightness - void setCurrentBrightness(uint8_t brightness); +protected: + virtual void flushDisplay(lv_disp_drv_t *disp, const lv_area_t *area, + lv_color_t *color_p); + virtual void screenInput(lv_indev_drv_t *indev_driver, + lv_indev_data_t *data) override; - private: - Display(int backlight_pin, int enable_pin); - void setupTFT(); - void setupTouchScreen(); - void setupBacklight(); - - int mEnablePin; - int mBacklightPin; - TFT_eSPI tft; + /// @brief Fade toward brightness based on isAwake + /// @return True - Fade complete + /// False - Fade set point not reached + bool fade(); + /// @brief Start the Fade task + void startFade(); - Adafruit_FT6206 touch; - TS_Point touchPoint; - TS_Point oldPoint; - Notification mTouchEvent; + /// @brief Set the actual display brightness right now + /// @param brightness + void setCurrentBrightness(uint8_t brightness); - TaskHandle_t mDisplayFadeTask = nullptr; - SemaphoreHandle_t mFadeTaskMutex = nullptr; - static void fadeImpl(void* aBrightness); +private: + Display(int backlight_pin, int enable_pin); + void setupTFT(); + void setupTouchScreen(); + void setupBacklight(); - uint8_t mBrightness = 0; // Current display brightness - uint8_t mAwakeBrightness = 100; // Current setting for brightness when awake - bool isAsleep = false; + int mEnablePin; + int mBacklightPin; + TFT_eSPI tft; + + Adafruit_FT6206 touch; + TS_Point touchPoint; + TS_Point oldPoint; + std::shared_ptr> mTouchEvent = + std::make_shared>(); + + TaskHandle_t mDisplayFadeTask = nullptr; + SemaphoreHandle_t mFadeTaskMutex = nullptr; + static void fadeImpl(void *aBrightness); + + uint8_t mBrightness = 0; // Current display brightness + uint8_t mAwakeBrightness = 100; // Current setting for brightness when awake + bool isAsleep = false; }; \ No newline at end of file diff --git a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp index 7b55a67..9b9ddec 100644 --- a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp +++ b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.cpp @@ -1,230 +1,164 @@ #include "wifihandler.hpp" -#include -#include #include "HardwareAbstract.hpp" #include "WiFi.h" +#include +#include std::shared_ptr wifiHandler::mInstance = nullptr; // WiFi status event -void wifiHandler::WiFiEvent(WiFiEvent_t event){ +void wifiHandler::WiFiEvent(WiFiEvent_t event) { int no_networks = 0; - switch (event) - { - case ARDUINO_EVENT_WIFI_SCAN_DONE: - { - Serial.println("WIFI scan done\n"); - no_networks = WiFi.scanComplete(); - std::vector *vec = new std::vector(); - std::shared_ptr> info = std::shared_ptr>(vec); - - for (int i = 0; i < no_networks; i++) - { - info->push_back(WifiInfo { - .ssid = std::string(WiFi.SSID(i).c_str()), - .rssi = WiFi.RSSI(i) - }); - } - if (no_networks < 0) - { - Serial.println("Scan failed"); - } - else - { - // TODO Convert To callbacks - //this->display.clear_wifi_networks(); - Serial.print(no_networks); - Serial.print(" found\n"); - //this->display.wifi_scan_complete( no_networks); - } - this->scan_notification.notify(info); - if (WiFi.isConnected() == false) - { - WiFi.reconnect(); - } - break; + switch (event) { + case ARDUINO_EVENT_WIFI_SCAN_DONE: { + Serial.println("WIFI scan done\n"); + no_networks = WiFi.scanComplete(); + Serial.println("making notify vector"); + auto info = std::vector(no_networks); + Serial.println("loaing notify vector"); + for (int i = 0; i < no_networks; i++) { + auto ssid = + WiFi.SSID(i).c_str() ? std::string(WiFi.SSID(i).c_str()) : "No SSID"; + info[i] = WifiInfo(ssid, WiFi.RSSI(i)); } - case ARDUINO_EVENT_WIFI_STA_GOT_IP: - case ARDUINO_EVENT_WIFI_STA_GOT_IP6: - this->update_credentials(); - case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: - case ARDUINO_EVENT_WIFI_STA_LOST_IP: - case ARDUINO_EVENT_WIFI_STA_STOP: - this->update_status(); - default: - Serial.print("Wifi Status: "); - Serial.println(WiFi.status()); - break; + if (no_networks < 0) { + Serial.println("Scan failed"); + } else { + // TODO Convert To callbacks + // this->display.clear_wifi_networks(); + Serial.print(no_networks); + Serial.print(" found\n"); + // this->display.wifi_scan_complete( no_networks); + } + Serial.println("notifying"); + mScanNotification->notify(info); + Serial.println("notified"); + if (WiFi.isConnected() == false) { + WiFi.reconnect(); + } + break; } - if (WiFi.status() == WL_CONNECT_FAILED) - { + case ARDUINO_EVENT_WIFI_STA_GOT_IP: + case ARDUINO_EVENT_WIFI_STA_GOT_IP6: + this->update_credentials(); + case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: + case ARDUINO_EVENT_WIFI_STA_LOST_IP: + case ARDUINO_EVENT_WIFI_STA_STOP: + this->update_status(); + default: + Serial.print("Wifi Status: "); + Serial.println(WiFi.status()); + break; + } + if (WiFi.status() == WL_CONNECT_FAILED) { Serial.println("connection failed."); WiFi.disconnect(); } Serial.println(WiFi.status()); } -bool wifiHandler::isAvailable(){ - return true; -} -std::shared_ptr wifiHandler::getInstance() -{ - if(mInstance) - { - return mInstance; - } - mInstance = std::shared_ptr(new wifiHandler()); +bool wifiHandler::isAvailable() { return true; } +std::shared_ptr wifiHandler::getInstance() { + if (mInstance) { return mInstance; + } + mInstance = std::shared_ptr(new wifiHandler()); + return mInstance; }; -wifiHandler::wifiHandler() -{ - this->password = ""; - this->SSID = ""; +wifiHandler::wifiHandler() { + this->password = ""; + this->SSID = ""; } -void wifiHandler::update_status() -{ +void wifiHandler::update_status() { Serial.println("update_status"); - std::shared_ptr status = std::make_shared(wifiStatus()); - //wifiStatus *status = new wifiStatus(); - status->isConnected = WiFi.isConnected(); - //status->IP = WiFi.localIP(); - IPAddress ip = WiFi.localIP(); - String ip_str = ip.toString(); - status->IP = ip.toString().c_str(); - - //ip.copy(status->IP, ip.length()); - String ssid = WiFi.SSID(); - status->ssid = WiFi.SSID().c_str(); + auto isConnected = WiFi.isConnected(); + std::string ip(WiFi.localIP().toString().c_str()); + std::string ssid(WiFi.SSID().c_str()); - //this->wifi_status.isConnected = WiFi.isConnected(); - //this->wifi_status.IP = WiFi.localIP(); - //this->wifi_status.isConnected = true; - + wifiStatus status = wifiStatus(isConnected, ip, ssid); - //Serial.println(WiFi.localIP()); - this->status_update.notify(status); + // Serial.println(WiFi.localIP()); + mStatusUpdate->notify(status); } -void wifiHandler::update_credentials() -{ +void wifiHandler::update_credentials() { // No connection was attempted so don't try to to save the creds - if(!this->connect_attempt) return; -#if 0 - if (strcmp(temporary_password, wifiHandler::password) != 0 || strcmp(temporary_ssid, wifiHandler::SSID) != 0) - { - strcpy(wifiHandler::password, temporary_password); - strcpy(wifiHandler::SSID, temporary_ssid); + if (!connect_attempt) { + return; + } - Preferences preferences; - preferences.begin("wifiSettings", false); - String tempString = wifiHandler::password; - preferences.putString("password", tempString); - tempString = wifiHandler::SSID; - preferences.putString("SSID", tempString); - preferences.end(); - } -#else - if (this->temporary_password->compare(this->password) != 0 || this->temporary_ssid->compare(this->SSID)) - { - this->password = *(this->temporary_password); - this->SSID = *(this->temporary_ssid); + if (temporary_password == password || temporary_ssid == SSID) { + password = temporary_password; + SSID = temporary_ssid; Preferences preferences; preferences.begin("wifiSettings", false); - String tempString = this->temporary_password->c_str(); + String tempString = temporary_password.c_str(); preferences.putString("password", tempString); - tempString = this->temporary_ssid->c_str(); + tempString = temporary_ssid.c_str(); preferences.putString("SSID", tempString); preferences.end(); } -#endif -this->connect_attempt = false; + this->connect_attempt = false; } -void wifiHandler::scan() -{ +void wifiHandler::scan() { Serial.println("scan called"); - /* If the */ - WiFi.status(); - if (WiFi.isConnected() != true) - { + + WiFi.disconnect(); + WiFi.scanNetworks(true); +} + +void wifiHandler::begin() { + // this->display = display; + WiFi.setHostname("OMOTE"); + WiFi.mode(WIFI_STA); + // WiFi.onEvent([this] (WiFiEvent_t event) {mInstance->WiFiEvent(event);}); + WiFi.onEvent([](WiFiEvent_t event) { mInstance->WiFiEvent(event); }); + + Preferences preferences; + preferences.begin("wifiSettings", false); + String ssid = preferences.getString("SSID"); + String password = preferences.getString("password"); + preferences.end(); + + /* If the SSID is not empty, there was a value stored in the preferences and + * we try to use it.*/ + if (!ssid.isEmpty()) { + Serial.print("Connecting to wifi "); + Serial.println(ssid); + this->SSID = ssid.c_str(); + this->password = password.c_str(); + this->connect(SSID, this->password); + } else { + Serial.println("no SSID or password stored"); + /*Set first character to \0 indicates an empty string*/ + this->SSID[0] = '\0'; + this->password[0] = '\0'; WiFi.disconnect(); } - WiFi.scanNetworks(true); + + WiFi.setSleep(true); } - -void wifiHandler::begin() -{ - //this->display = display; - WiFi.setHostname("OMOTE"); - WiFi.mode(WIFI_STA); - //WiFi.onEvent([this] (WiFiEvent_t event) {mInstance->WiFiEvent(event);}); - WiFi.onEvent([] (WiFiEvent_t event) {mInstance->WiFiEvent(event);}); - - Preferences preferences; - preferences.begin("wifiSettings",false); - String ssid = preferences.getString("SSID"); - String password = preferences.getString("password"); - preferences.end(); - - /* If the SSID is not empty, there was a value stored in the preferences and we try to use it.*/ - if (!ssid.isEmpty()) - { - Serial.print("Connecting to wifi "); - Serial.println(ssid); - //strcpy(this->SSID, ssid.c_str()); - //strcpy(this->password, password.c_str()); - this->SSID = ssid.c_str(); - this->password = password.c_str(); - this->connect(std::make_shared(std::string(this->SSID)), std::make_shared(std::string(this->password))); - } - else - { - Serial.println("no SSID or password stored"); - /*Set first character to \0 indicates an empty string*/ - this->SSID[0] = '\0'; - this->password[0] = '\0'; - WiFi.disconnect(); - } - - 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) -{ +void wifiHandler::connect(std::string ssid, std::string password) { this->connect_attempt = true; - this->temporary_password = password; - this->temporary_ssid = ssid; - WiFi.begin(ssid->c_str(), password->c_str()); + // this->temporary_password = password; + // this->temporary_ssid = ssid; + WiFi.begin(ssid.c_str(), password.c_str()); } -void wifiHandler::turnOff() -{ +void wifiHandler::turnOff() { WiFi.disconnect(); WiFi.mode(WIFI_OFF); } -void wifiHandler::disconnect(){ - WiFi.disconnect(); -} +void wifiHandler::disconnect() { WiFi.disconnect(); } -bool wifiHandler::isConnected() -{ - return WiFi.isConnected(); -} +bool wifiHandler::isConnected() { return WiFi.isConnected(); } -std::string wifiHandler::getIP() -{ - return std::string(WiFi.localIP().toString().c_str()); +std::string wifiHandler::getIP() { + 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 b9a855f..cb09ed5 100644 --- a/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp +++ b/Platformio/HAL/Targets/ESP32/wifiHandler/wifihandler.hpp @@ -1,99 +1,76 @@ #pragma once -#include "wifiHandlerInterface.h" #include "Notification.hpp" #include "memory.h" +#include "wifiHandlerInterface.h" #include -#define STRING_SIZE 50 +class wifiHandler : public wifiHandlerInterface { +public: + wifiHandler(); + static std::shared_ptr getInstance(); + /** + * @brief Function to initialize the wifi handler + */ + void begin(); -class wifiHandler: public wifiHandlerInterface { - public: - wifiHandler(); - static std::shared_ptr getInstance(); - /** - * @brief Function to initialize the wifi handler - * - */ - void begin(); + /** + * @brief Connect to the wifi using the provided credetials + */ + void connect(std::string ssid, std::string password) override; - /** - * @brief Connect to the wifi using the provided credetials - * - * @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(); +private: + /** + * @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 trigger asynchronous scan for wifi networks - * - */ - void scan(); - bool isAvailable(); - void onScanDone(std::function>)> function); - void onStatusUpdate(std::function)> function); + /** + * @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(); + static std::shared_ptr mInstance; + bool connect_attempt = false; + std::string temporary_password; + std::string temporary_ssid; + std::string password; + std::string SSID; - private: + void update_status(); - 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; - bool connect_attempt = false; - 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 - * - */ - void disconnect(); - - /** - * @brief Function to determine wether or not we are connected to a network - * - * @return true Device is connected to wifi network - * @return false Device is not connected to wifi network - */ - bool isConnected(); - /** - * @brief Internal variable to store the wifi SSID - * - */ - std::string SSID; + /** + * @brief Function to disconnect from the network + */ + void disconnect(); + /** + * @brief Function to determine wether or not we are connected to a network + * + * @return true Device is connected to wifi network + * @return false Device is not connected to wifi network + */ + bool isConnected(); + /** + * @brief Internal variable to store the wifi SSID + * + */ }; \ No newline at end of file diff --git a/Platformio/HAL/Targets/Simulator/wifiHandlerSim/wifiHandlerSim.cpp b/Platformio/HAL/Targets/Simulator/wifiHandlerSim/wifiHandlerSim.cpp index 7534534..b7dd33a 100644 --- a/Platformio/HAL/Targets/Simulator/wifiHandlerSim/wifiHandlerSim.cpp +++ b/Platformio/HAL/Targets/Simulator/wifiHandlerSim/wifiHandlerSim.cpp @@ -1,68 +1,21 @@ #include "wifiHandlerSim.hpp" -std::shared_ptr mInstance; +wifiHandlerSim::wifiHandlerSim() {} -std::shared_ptr wifiHandlerSim::getInstance() -{ - if(mInstance) - { - return mInstance; - } - mInstance = std::make_shared(wifiHandlerSim()); - return mInstance; -}; +void wifiHandlerSim::begin() {} -wifiHandlerSim::wifiHandlerSim(){ - -} - -void wifiHandlerSim::begin(){ - -} - -static wifiStatus status = { - .isConnected = true - , .IP = "172.0.0.1" -}; - -void wifiHandlerSim::connect(std::shared_ptr ssid, std::shared_ptr password){ - status.ssid = *ssid; - std::shared_ptr new_status = std::make_shared (wifiStatus(std::move(status))); - this->status_update.notify(new_status); +void wifiHandlerSim::connect(std::string ssid, std::string password) { + status.ssid = ssid; + mStatusUpdate->notify(wifiStatus(status)); } static const WifiInfo wifis[] = { - { - .ssid = "High Signal Wifi" - , .rssi = -49 - } - , { - .ssid = "Mid Signal Wifi" - , .rssi = -55 - } - , { - .ssid = "Low Signal Wifi" - , .rssi = -65 - } - , { - .ssid = "No Signal Wifi" - , .rssi = -90 - } -}; + WifiInfo("High Signal Wifi", -49), WifiInfo("Mid Signal Wifi", -55), + WifiInfo("Low Signal Wifi", -65), WifiInfo("No Signal Wifi", -90)}; -void wifiHandlerSim::scan(){ - std::shared_ptr> info = std::make_shared>(std::vector(std::begin(wifis), std::end(wifis))); - this->scan_notification.notify(info); +void wifiHandlerSim::scan() { + std::vector info = std::vector(std::begin(wifis), std::end(wifis)); + mScanNotification->notify(info); } -bool wifiHandlerSim::isAvailable(){ - return false; -} - -void wifiHandlerSim::onScanDone(std::function>)> function){ - this->scan_notification.onNotify(std::move(function)); -} - -void wifiHandlerSim::onStatusUpdate(std::function)> function){ - this->status_update.onNotify(std::move(function)); -} \ No newline at end of file +bool wifiHandlerSim::isAvailable() { return false; } diff --git a/Platformio/HAL/Targets/Simulator/wifiHandlerSim/wifiHandlerSim.hpp b/Platformio/HAL/Targets/Simulator/wifiHandlerSim/wifiHandlerSim.hpp index e994450..5f76b6b 100644 --- a/Platformio/HAL/Targets/Simulator/wifiHandlerSim/wifiHandlerSim.hpp +++ b/Platformio/HAL/Targets/Simulator/wifiHandlerSim/wifiHandlerSim.hpp @@ -1,35 +1,24 @@ #pragma once -#include "wifiHandlerInterface.h" #include "Notification.hpp" +#include "wifiHandlerInterface.h" #include -class wifiHandlerSim: public wifiHandlerInterface { - public: - wifiHandlerSim(); - static std::shared_ptr getInstance(); +class wifiHandlerSim : public wifiHandlerInterface { +public: + wifiHandlerSim(); - - /** - * @brief Connect to the wifi using the provided credetials - * - * @param SSID - * @param password - */ - void connect(std::shared_ptr ssid, std::shared_ptr password); - //void connect(const char* SSID, const char* password); + /** + * @brief Connect to the wifi using the provided credetials + */ + void connect(std::string ssid, std::string password) override; + /** + * @brief function to trigger asynchronous scan for wifi networks + */ + void scan(); + bool isAvailable(); + void begin(); - - /** - * @brief function to trigger asynchronous scan for wifi networks - * - */ - void scan(); - bool isAvailable(); - void begin(); - void onScanDone(std::function>)> function); - void onStatusUpdate(std::function)> function); - private: - Notification>> scan_notification; - Notification> status_update; +private: + wifiStatus status = wifiStatus(true, "172.0.0.1", "FakeNet"); }; \ No newline at end of file diff --git a/Platformio/platformio.ini b/Platformio/platformio.ini index 64ae617..eab1049 100644 --- a/Platformio/platformio.ini +++ b/Platformio/platformio.ini @@ -44,7 +44,7 @@ build_flags = -I OmoteUI/core/widget -I OmoteUI/core/page -I OmoteUI/UIs - -I OmoteUI/UIs/Basic + ;-I OmoteUI/UIs/Basic -I OmoteUI/UIs/BasicRefactored -I OmoteUI/UIs/BasicRefactored/screen -I OmoteUI/UIs/BasicRefactored/page @@ -59,6 +59,7 @@ lib_deps = lib_archive = false build_src_filter = +<../OmoteUI/*> + -<../OmoteUI/UIs/Basic/*> +<../HAL/HardwareAbstract.cpp> +<../HAL/HardwareModules/*.cpp> @@ -107,7 +108,7 @@ build_flags = -D LV_TICK_CUSTOM=1 -D LV_TICK_CUSTOM_INCLUDE="\"Arduino.h\"" -D LV_TICK_CUSTOM_SYS_TIME_EXPR="'(millis())'" - + ; ------------- Includes -------------------------------------------- -I HAL/Targets/ESP32 -I HAL/Targets/ESP32/battery @@ -115,6 +116,8 @@ build_flags = -I HAL/Targets/ESP32/wifiHandler -I HAL/Targets/ESP32/keys +monitor_filters = esp32_exception_decoder + build_unflags = -std=gnu++11 build_src_filter = diff --git a/Platformio/src/simMain.cpp b/Platformio/src/simMain.cpp index b791800..f996b73 100644 --- a/Platformio/src/simMain.cpp +++ b/Platformio/src/simMain.cpp @@ -1,6 +1,5 @@ #include "BasicUI.hpp" #include "HardwareSimulator.hpp" -#include "OmoteUI.hpp" #include "omoteconfig.h" #include