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.
This commit is contained in:
MatthewColvin 2023-10-15 00:22:43 -05:00
parent 5d0533c18a
commit 38ec26dce7
12 changed files with 389 additions and 441 deletions

View file

@ -1,25 +1,46 @@
#pragma once
#include <string>
#include <memory>
#include <functional>
#include <memory>
#include <string>
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<std::string> ssid, std::shared_ptr<std::string> password) = 0;
virtual void onScanDone(std::function<void (std::shared_ptr<std::vector<WifiInfo>>)> function) = 0;
virtual void onStatusUpdate(std::function<void (std::shared_ptr<wifiStatus>)> function) = 0;
typedef std::vector<WifiInfo> ScanDoneDataTy;
typedef Notification<ScanDoneDataTy> 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<ScanNotificationTy> ScanCompleteNotification() {
return mScanNotification;
};
std::shared_ptr<Notification<wifiStatus>> WifiStatusNotification() {
return mStatusUpdate;
};
protected:
std::shared_ptr<ScanNotificationTy> mScanNotification =
std::make_shared<ScanNotificationTy>();
std::shared_ptr<Notification<wifiStatus>> mStatusUpdate =
std::make_shared<Notification<wifiStatus>>();
};

View file

@ -1,29 +1,90 @@
#pragma once
#include <vector>
#include <functional>
#include <map>
#include <memory>
template <class... notifyData> class Handler;
template <class... notifyData> class Notification {
friend class Handler<notifyData...>;
template <class... notifyData>
class Notification{
public:
typedef std::function<void(notifyData...)> HandlerTy;
typedef int HandlerID;
Notification() = default;
void onNotify(HandlerTy aHandler);
Notification() { mIdMaker = 0; };
void notify(notifyData... notifySendData);
protected:
HandlerID onNotify(HandlerTy aHandler);
void unregister(HandlerID aHandler);
private:
std::vector<HandlerTy> mFunctionHandlers;
std::map<HandlerID, HandlerTy> mFunctionHandlers;
HandlerID mIdMaker;
};
template <class... handlerData>
void Notification<handlerData...>::onNotify(HandlerTy aHandler){
mFunctionHandlers.push_back(std::move(aHandler));
int Notification<handlerData...>::onNotify(HandlerTy aHandler) {
if (aHandler) {
mFunctionHandlers[++mIdMaker] = std::move(aHandler);
return mIdMaker;
} else {
return -1;
}
}
template <class... outboundData>
void Notification<outboundData...>::notify(outboundData... notifySendData) {
for (auto handler : mFunctionHandlers) {
handler(notifySendData...);
handler.second(notifySendData...);
}
}
template <class... handlerData>
void Notification<handlerData...>::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... notifyData> class Handler {
public:
typedef std::function<void(notifyData...)> callableTy;
void operator=(Handler &other) = delete;
Handler() = default;
Handler(std::shared_ptr<Notification<notifyData...>> aNotification,
callableTy aCallable = nullptr)
: mNotification(aNotification),
mHandlerId(aNotification->onNotify(aCallable)) {}
virtual ~Handler() {
if (mHandlerId >= 0) {
mNotification->unregister(mHandlerId);
}
}
void operator=(callableTy aHandler) {
if (mHandlerId >= 0) {
mNotification->unregister(mHandlerId);
mHandlerId = -1;
}
if (aHandler) {
mHandlerId = mNotification->onNotify(aHandler);
}
}
void
SetNotification(std::shared_ptr<Notification<notifyData...>> aNotification) {
mNotification = aNotification;
}
private:
std::shared_ptr<Notification<notifyData...>> mNotification = nullptr;
int mHandlerId = -1;
};

View file

@ -77,9 +77,10 @@ void HardwareRevX::init() {
mKeys = std::make_shared<Keys>();
restorePreferences();
mDisplay->onTouch([this]([[maybe_unused]] auto touchPoint) {
mTouchHandler.SetNotification(mDisplay->TouchNotification());
mTouchHandler = [this]([[maybe_unused]] auto touchPoint) {
standbyTimer = this->getSleepTimeout();
});
};
setupIMU();
setupIR();

View file

@ -90,4 +90,5 @@ private:
IRrecv IrReceiver = IRrecv(IR_RX);
static std::shared_ptr<HardwareRevX> mInstance;
Handler<TS_Point> mTouchHandler;
};

View file

@ -58,10 +58,6 @@ void Display::setupBacklight() {
ledc_timer_config(&ledc_timer);
}
void Display::onTouch(Notification<TS_Point>::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) {

View file

@ -2,10 +2,10 @@
#include "DisplayAbstract.h"
#include "HardwareAbstract.hpp"
#include "Notification.hpp"
#include <Adafruit_FT6206.h>
#include <memory>
#include <TFT_eSPI.h>
#include "driver/ledc.h"
#include <Adafruit_FT6206.h>
#include <TFT_eSPI.h>
#include <memory>
/*LEDC Channel to use for the LCD backlight*/
#define LCD_BACKLIGHT_LEDC_CHANNEL LEDC_CHANNEL_5
@ -16,9 +16,7 @@
#define DEFAULT_BACKLIGHT_BRIGHTNESS 128
class Display: public DisplayAbstract
{
class Display : public DisplayAbstract {
public:
static std::shared_ptr<Display> getInstance();
@ -28,14 +26,28 @@ class Display: public DisplayAbstract
virtual uint8_t getBrightness() override;
virtual void turnOff() override;
void onTouch(Notification<TS_Point>::HandlerTy aTouchHandler);
std::shared_ptr<Notification<TS_Point>> TouchNotification() {
return mTouchEvent;
}
inline void wake() {if(isAsleep) {isAsleep = false; startFade();}}
inline void sleep() {if(!isAsleep){isAsleep = true; startFade();}}
inline void wake() {
if (isAsleep) {
isAsleep = false;
startFade();
}
}
inline void sleep() {
if (!isAsleep) {
isAsleep = true;
startFade();
}
}
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;
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
@ -61,7 +73,8 @@ class Display: public DisplayAbstract
Adafruit_FT6206 touch;
TS_Point touchPoint;
TS_Point oldPoint;
Notification<TS_Point> mTouchEvent;
std::shared_ptr<Notification<TS_Point>> mTouchEvent =
std::make_shared<Notification<TS_Point>>();
TaskHandle_t mDisplayFadeTask = nullptr;
SemaphoreHandle_t mFadeTaskMutex = nullptr;

View file

@ -1,45 +1,39 @@
#include "wifihandler.hpp"
#include <Arduino.h>
#include <Preferences.h>
#include "HardwareAbstract.hpp"
#include "WiFi.h"
#include <Arduino.h>
#include <Preferences.h>
std::shared_ptr<wifiHandler> wifiHandler::mInstance = nullptr;
// WiFi status event
void wifiHandler::WiFiEvent(WiFiEvent_t event) {
int no_networks = 0;
switch (event)
{
case ARDUINO_EVENT_WIFI_SCAN_DONE:
{
switch (event) {
case ARDUINO_EVENT_WIFI_SCAN_DONE: {
Serial.println("WIFI scan done\n");
no_networks = WiFi.scanComplete();
std::vector<WifiInfo> *vec = new std::vector<WifiInfo>();
std::shared_ptr<std::vector<WifiInfo>> info = std::shared_ptr<std::vector<WifiInfo>>(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)
});
Serial.println("making notify vector");
auto info = std::vector<WifiInfo>(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));
}
if (no_networks < 0)
{
if (no_networks < 0) {
Serial.println("Scan failed");
}
else
{
} 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)
{
Serial.println("notifying");
mScanNotification->notify(info);
Serial.println("notified");
if (WiFi.isConnected() == false) {
WiFi.reconnect();
}
break;
@ -56,108 +50,68 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event){
Serial.println(WiFi.status());
break;
}
if (WiFi.status() == WL_CONNECT_FAILED)
{
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> wifiHandler::getInstance()
{
if(mInstance)
{
bool wifiHandler::isAvailable() { return true; }
std::shared_ptr<wifiHandler> wifiHandler::getInstance() {
if (mInstance) {
return mInstance;
}
mInstance = std::shared_ptr<wifiHandler>(new wifiHandler());
return mInstance;
};
wifiHandler::wifiHandler()
{
wifiHandler::wifiHandler() {
this->password = "";
this->SSID = "";
}
void wifiHandler::update_status()
{
void wifiHandler::update_status() {
Serial.println("update_status");
std::shared_ptr<wifiStatus> status = std::make_shared<wifiStatus>(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();
//this->wifi_status.isConnected = WiFi.isConnected();
//this->wifi_status.IP = WiFi.localIP();
//this->wifi_status.isConnected = true;
auto isConnected = WiFi.isConnected();
std::string ip(WiFi.localIP().toString().c_str());
std::string ssid(WiFi.SSID().c_str());
wifiStatus status = wifiStatus(isConnected, ip, ssid);
// Serial.println(WiFi.localIP());
this->status_update.notify(status);
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;
}
if (temporary_password == password || temporary_ssid == SSID) {
password = temporary_password;
SSID = temporary_ssid;
Preferences preferences;
preferences.begin("wifiSettings", false);
String tempString = wifiHandler::password;
String tempString = temporary_password.c_str();
preferences.putString("password", tempString);
tempString = wifiHandler::SSID;
tempString = temporary_ssid.c_str();
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);
Preferences preferences;
preferences.begin("wifiSettings", false);
String tempString = this->temporary_password->c_str();
preferences.putString("password", tempString);
tempString = this->temporary_ssid->c_str();
preferences.putString("SSID", tempString);
preferences.end();
}
#endif
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()
{
void wifiHandler::begin() {
// this->display = display;
WiFi.setHostname("OMOTE");
WiFi.mode(WIFI_STA);
@ -170,19 +124,15 @@ void wifiHandler::begin()
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())
{
/* 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>(std::string(this->SSID)), std::make_shared<std::string>(std::string(this->password)));
}
else
{
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';
@ -193,38 +143,22 @@ void wifiHandler::begin()
WiFi.setSleep(true);
}
void wifiHandler::onScanDone(std::function<void (std::shared_ptr<std::vector<WifiInfo>>)> function){
this->scan_notification.onNotify(std::move(function));
}
void wifiHandler::onStatusUpdate(std::function<void (std::shared_ptr<wifiStatus>)> function){
this->status_update.onNotify(std::move(function));
}
void wifiHandler::connect(std::shared_ptr<std::string> ssid, std::shared_ptr<std::string> 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()
{
std::string wifiHandler::getIP() {
return std::string(WiFi.localIP().toString().c_str());
}

View file

@ -1,49 +1,34 @@
#pragma once
#include "wifiHandlerInterface.h"
#include "Notification.hpp"
#include "memory.h"
#include "wifiHandlerInterface.h"
#include <WiFi.h>
#define STRING_SIZE 50
class wifiHandler : public wifiHandlerInterface {
public:
wifiHandler();
static std::shared_ptr<wifiHandler> getInstance();
/**
* @brief Function to initialize the wifi handler
*
*/
void begin();
/**
* @brief Connect to the wifi using the provided credetials
*
* @param SSID
* @param password
*/
void connect(std::shared_ptr<std::string> ssid, std::shared_ptr<std::string> password);
//void connect(const char* SSID, const char* password);
void connect(std::string ssid, std::string password) override;
/**
* @brief function to trigger asynchronous scan for wifi networks
*
*/
void scan();
bool isAvailable();
void onScanDone(std::function<void (std::shared_ptr<std::vector<WifiInfo>>)> function);
void onStatusUpdate(std::function<void (std::shared_ptr<wifiStatus>)> function);
private:
Notification<std::shared_ptr<std::vector<WifiInfo>>> scan_notification;
Notification<std::shared_ptr<wifiStatus>> 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
* @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
@ -55,31 +40,25 @@ class wifiHandler: public wifiHandlerInterface {
/**
* @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<wifiHandler> mInstance;
bool connect_attempt = false;
std::shared_ptr<std::string> temporary_password;
std::shared_ptr<std::string> temporary_ssid;
std::string temporary_password;
std::string temporary_ssid;
std::string password;
std::string SSID;
void update_status();
/**
* @brief Internal variable to store the wifi password
*
*/
std::string password;
/**
* @brief Function to disconnect from the network
*
*/
void disconnect();
@ -94,6 +73,4 @@ class wifiHandler: public wifiHandlerInterface {
* @brief Internal variable to store the wifi SSID
*
*/
std::string SSID;
};

View file

@ -1,68 +1,21 @@
#include "wifiHandlerSim.hpp"
std::shared_ptr<wifiHandlerSim> mInstance;
wifiHandlerSim::wifiHandlerSim() {}
std::shared_ptr<wifiHandlerSim> wifiHandlerSim::getInstance()
{
if(mInstance)
{
return mInstance;
}
mInstance = std::make_shared<wifiHandlerSim>(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<std::string> ssid, std::shared_ptr<std::string> password){
status.ssid = *ssid;
std::shared_ptr<wifiStatus> new_status = std::make_shared<wifiStatus> (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<std::vector<WifiInfo>> info = std::make_shared<std::vector<WifiInfo>>(std::vector(std::begin(wifis), std::end(wifis)));
this->scan_notification.notify(info);
std::vector<WifiInfo> info = std::vector(std::begin(wifis), std::end(wifis));
mScanNotification->notify(info);
}
bool wifiHandlerSim::isAvailable(){
return false;
}
void wifiHandlerSim::onScanDone(std::function<void (std::shared_ptr<std::vector<WifiInfo>>)> function){
this->scan_notification.onNotify(std::move(function));
}
void wifiHandlerSim::onStatusUpdate(std::function<void (std::shared_ptr<wifiStatus>)> function){
this->status_update.onNotify(std::move(function));
}
bool wifiHandlerSim::isAvailable() { return false; }

View file

@ -1,35 +1,24 @@
#pragma once
#include "wifiHandlerInterface.h"
#include "Notification.hpp"
#include "wifiHandlerInterface.h"
#include <memory>
class wifiHandlerSim : public wifiHandlerInterface {
public:
wifiHandlerSim();
static std::shared_ptr<wifiHandlerSim> getInstance();
/**
* @brief Connect to the wifi using the provided credetials
*
* @param SSID
* @param password
*/
void connect(std::shared_ptr<std::string> ssid, std::shared_ptr<std::string> password);
//void connect(const char* SSID, const char* password);
void connect(std::string ssid, std::string password) override;
/**
* @brief function to trigger asynchronous scan for wifi networks
*
*/
void scan();
bool isAvailable();
void begin();
void onScanDone(std::function<void (std::shared_ptr<std::vector<WifiInfo>>)> function);
void onStatusUpdate(std::function<void (std::shared_ptr<wifiStatus>)> function);
private:
Notification<std::shared_ptr<std::vector<WifiInfo>>> scan_notification;
Notification<std::shared_ptr<wifiStatus>> status_update;
wifiStatus status = wifiStatus(true, "172.0.0.1", "FakeNet");
};

View file

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

View file

@ -1,6 +1,5 @@
#include "BasicUI.hpp"
#include "HardwareSimulator.hpp"
#include "OmoteUI.hpp"
#include "omoteconfig.h"
#include <memory>