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;
virtual void begin() = 0;
class wifiHandlerInterface {
public:
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 Notification{
public:
typedef std::function<void(notifyData...)> HandlerTy;
template <class... notifyData> class Handler;
template <class... notifyData> class Notification {
friend class Handler<notifyData...>;
Notification() = default;
void onNotify(HandlerTy aHandler);
void notify(notifyData... notifySendData);
public:
typedef std::function<void(notifyData...)> HandlerTy;
typedef int HandlerID;
private:
std::vector<HandlerTy> mFunctionHandlers;
Notification() { mIdMaker = 0; };
void notify(notifyData... notifySendData);
protected:
HandlerID onNotify(HandlerTy aHandler);
void unregister(HandlerID aHandler);
private:
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...);
void Notification<outboundData...>::notify(outboundData... notifySendData) {
for (auto handler : mFunctionHandlers) {
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,58 +16,71 @@
#define DEFAULT_BACKLIGHT_BRIGHTNESS 128
class Display : public DisplayAbstract {
public:
static std::shared_ptr<Display> getInstance();
class Display: public DisplayAbstract
{
public:
static std::shared_ptr<Display> 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<TS_Point>::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<Notification<TS_Point>> 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<TS_Point> 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<Notification<TS_Point>> mTouchEvent =
std::make_shared<Notification<TS_Point>>();
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;
};

View file

@ -1,230 +1,164 @@
#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){
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<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)
});
}
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<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));
}
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> wifiHandler::getInstance()
{
if(mInstance)
{
return mInstance;
}
mInstance = std::shared_ptr<wifiHandler>(new wifiHandler());
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()
{
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<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();
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>(std::string(this->SSID)), std::make_shared<std::string>(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<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()
{
return std::string(WiFi.localIP().toString().c_str());
std::string wifiHandler::getIP() {
return std::string(WiFi.localIP().toString().c_str());
}

View file

@ -1,99 +1,76 @@
#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();
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
*/
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<std::string> ssid, std::shared_ptr<std::string> 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<void (std::shared_ptr<std::vector<WifiInfo>>)> function);
void onStatusUpdate(std::function<void (std::shared_ptr<wifiStatus>)> 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<wifiHandler> mInstance;
bool connect_attempt = false;
std::string temporary_password;
std::string temporary_ssid;
std::string password;
std::string SSID;
private:
void update_status();
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
* 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<wifiHandler> mInstance;
bool connect_attempt = false;
std::shared_ptr<std::string> temporary_password;
std::shared_ptr<std::string> 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
*
*/
};

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);
void wifiHandlerSim::scan() {
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();
class wifiHandlerSim : public wifiHandlerInterface {
public:
wifiHandlerSim();
/**
* @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);
/**
* @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<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;
private:
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>
@ -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 =

View file

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