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:
parent
5d0533c18a
commit
38ec26dce7
12 changed files with 389 additions and 441 deletions
|
@ -1,25 +1,46 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
|
||||||
#include <memory>
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
typedef struct {
|
struct WifiInfo {
|
||||||
std::string ssid;
|
WifiInfo(){};
|
||||||
int rssi;
|
WifiInfo(std::string aSsid, int aRssi) : ssid(aSsid), rssi(aRssi) {}
|
||||||
} WifiInfo;
|
|
||||||
|
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;
|
bool isConnected;
|
||||||
std::string IP;
|
std::string IP = "";
|
||||||
std::string ssid;
|
std::string ssid = "";
|
||||||
}wifiStatus;
|
};
|
||||||
|
|
||||||
class wifiHandlerInterface {
|
class wifiHandlerInterface {
|
||||||
public:
|
public:
|
||||||
virtual bool isAvailable() = 0;
|
typedef std::vector<WifiInfo> ScanDoneDataTy;
|
||||||
virtual void scan() = 0;
|
typedef Notification<ScanDoneDataTy> ScanNotificationTy;
|
||||||
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;
|
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>>();
|
||||||
};
|
};
|
|
@ -1,29 +1,90 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <vector>
|
|
||||||
#include <functional>
|
#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:
|
public:
|
||||||
typedef std::function<void(notifyData...)> HandlerTy;
|
typedef std::function<void(notifyData...)> HandlerTy;
|
||||||
|
typedef int HandlerID;
|
||||||
|
|
||||||
Notification() = default;
|
Notification() { mIdMaker = 0; };
|
||||||
void onNotify(HandlerTy aHandler);
|
|
||||||
void notify(notifyData... notifySendData);
|
void notify(notifyData... notifySendData);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
HandlerID onNotify(HandlerTy aHandler);
|
||||||
|
void unregister(HandlerID aHandler);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<HandlerTy> mFunctionHandlers;
|
std::map<HandlerID, HandlerTy> mFunctionHandlers;
|
||||||
|
HandlerID mIdMaker;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template <class... handlerData>
|
template <class... handlerData>
|
||||||
void Notification<handlerData...>::onNotify(HandlerTy aHandler){
|
int Notification<handlerData...>::onNotify(HandlerTy aHandler) {
|
||||||
mFunctionHandlers.push_back(std::move(aHandler));
|
if (aHandler) {
|
||||||
|
mFunctionHandlers[++mIdMaker] = std::move(aHandler);
|
||||||
|
return mIdMaker;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class... outboundData>
|
template <class... outboundData>
|
||||||
void Notification<outboundData...>::notify(outboundData... notifySendData) {
|
void Notification<outboundData...>::notify(outboundData... notifySendData) {
|
||||||
for (auto handler : mFunctionHandlers) {
|
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;
|
||||||
|
};
|
|
@ -77,9 +77,10 @@ void HardwareRevX::init() {
|
||||||
mKeys = std::make_shared<Keys>();
|
mKeys = std::make_shared<Keys>();
|
||||||
restorePreferences();
|
restorePreferences();
|
||||||
|
|
||||||
mDisplay->onTouch([this]([[maybe_unused]] auto touchPoint) {
|
mTouchHandler.SetNotification(mDisplay->TouchNotification());
|
||||||
|
mTouchHandler = [this]([[maybe_unused]] auto touchPoint) {
|
||||||
standbyTimer = this->getSleepTimeout();
|
standbyTimer = this->getSleepTimeout();
|
||||||
});
|
};
|
||||||
|
|
||||||
setupIMU();
|
setupIMU();
|
||||||
setupIR();
|
setupIR();
|
||||||
|
|
|
@ -90,4 +90,5 @@ private:
|
||||||
IRrecv IrReceiver = IRrecv(IR_RX);
|
IRrecv IrReceiver = IRrecv(IR_RX);
|
||||||
|
|
||||||
static std::shared_ptr<HardwareRevX> mInstance;
|
static std::shared_ptr<HardwareRevX> mInstance;
|
||||||
|
Handler<TS_Point> mTouchHandler;
|
||||||
};
|
};
|
|
@ -58,10 +58,6 @@ void Display::setupBacklight() {
|
||||||
ledc_timer_config(&ledc_timer);
|
ledc_timer_config(&ledc_timer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Display::onTouch(Notification<TS_Point>::HandlerTy aTouchHandler) {
|
|
||||||
mTouchEvent.onNotify(std::move(aTouchHandler));
|
|
||||||
}
|
|
||||||
|
|
||||||
void Display::setupTFT() {
|
void Display::setupTFT() {
|
||||||
delay(100);
|
delay(100);
|
||||||
tft.init();
|
tft.init();
|
||||||
|
@ -111,7 +107,7 @@ void Display::screenInput(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) {
|
||||||
bool touched = false;
|
bool touched = false;
|
||||||
if ((touchX > 0) || (touchY > 0)) {
|
if ((touchX > 0) || (touchY > 0)) {
|
||||||
touched = true;
|
touched = true;
|
||||||
mTouchEvent.notify(touchPoint);
|
mTouchEvent->notify(touchPoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!touched) {
|
if (!touched) {
|
||||||
|
|
|
@ -2,10 +2,10 @@
|
||||||
#include "DisplayAbstract.h"
|
#include "DisplayAbstract.h"
|
||||||
#include "HardwareAbstract.hpp"
|
#include "HardwareAbstract.hpp"
|
||||||
#include "Notification.hpp"
|
#include "Notification.hpp"
|
||||||
#include <Adafruit_FT6206.h>
|
|
||||||
#include <memory>
|
|
||||||
#include <TFT_eSPI.h>
|
|
||||||
#include "driver/ledc.h"
|
#include "driver/ledc.h"
|
||||||
|
#include <Adafruit_FT6206.h>
|
||||||
|
#include <TFT_eSPI.h>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
/*LEDC Channel to use for the LCD backlight*/
|
/*LEDC Channel to use for the LCD backlight*/
|
||||||
#define LCD_BACKLIGHT_LEDC_CHANNEL LEDC_CHANNEL_5
|
#define LCD_BACKLIGHT_LEDC_CHANNEL LEDC_CHANNEL_5
|
||||||
|
@ -16,9 +16,7 @@
|
||||||
|
|
||||||
#define DEFAULT_BACKLIGHT_BRIGHTNESS 128
|
#define DEFAULT_BACKLIGHT_BRIGHTNESS 128
|
||||||
|
|
||||||
|
class Display : public DisplayAbstract {
|
||||||
class Display: public DisplayAbstract
|
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
static std::shared_ptr<Display> getInstance();
|
static std::shared_ptr<Display> getInstance();
|
||||||
|
|
||||||
|
@ -28,14 +26,28 @@ class Display: public DisplayAbstract
|
||||||
virtual uint8_t getBrightness() override;
|
virtual uint8_t getBrightness() override;
|
||||||
virtual void turnOff() 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 wake() {
|
||||||
inline void sleep() {if(!isAsleep){isAsleep = true; startFade();}}
|
if (isAsleep) {
|
||||||
|
isAsleep = false;
|
||||||
|
startFade();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inline void sleep() {
|
||||||
|
if (!isAsleep) {
|
||||||
|
isAsleep = true;
|
||||||
|
startFade();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void flushDisplay(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p);
|
virtual void flushDisplay(lv_disp_drv_t *disp, const lv_area_t *area,
|
||||||
virtual void screenInput(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) override;
|
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
|
/// @brief Fade toward brightness based on isAwake
|
||||||
/// @return True - Fade complete
|
/// @return True - Fade complete
|
||||||
|
@ -61,7 +73,8 @@ class Display: public DisplayAbstract
|
||||||
Adafruit_FT6206 touch;
|
Adafruit_FT6206 touch;
|
||||||
TS_Point touchPoint;
|
TS_Point touchPoint;
|
||||||
TS_Point oldPoint;
|
TS_Point oldPoint;
|
||||||
Notification<TS_Point> mTouchEvent;
|
std::shared_ptr<Notification<TS_Point>> mTouchEvent =
|
||||||
|
std::make_shared<Notification<TS_Point>>();
|
||||||
|
|
||||||
TaskHandle_t mDisplayFadeTask = nullptr;
|
TaskHandle_t mDisplayFadeTask = nullptr;
|
||||||
SemaphoreHandle_t mFadeTaskMutex = nullptr;
|
SemaphoreHandle_t mFadeTaskMutex = nullptr;
|
||||||
|
|
|
@ -1,45 +1,39 @@
|
||||||
#include "wifihandler.hpp"
|
#include "wifihandler.hpp"
|
||||||
#include <Arduino.h>
|
|
||||||
#include <Preferences.h>
|
|
||||||
#include "HardwareAbstract.hpp"
|
#include "HardwareAbstract.hpp"
|
||||||
#include "WiFi.h"
|
#include "WiFi.h"
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <Preferences.h>
|
||||||
|
|
||||||
std::shared_ptr<wifiHandler> wifiHandler::mInstance = nullptr;
|
std::shared_ptr<wifiHandler> wifiHandler::mInstance = nullptr;
|
||||||
|
|
||||||
// WiFi status event
|
// WiFi status event
|
||||||
void wifiHandler::WiFiEvent(WiFiEvent_t event) {
|
void wifiHandler::WiFiEvent(WiFiEvent_t event) {
|
||||||
int no_networks = 0;
|
int no_networks = 0;
|
||||||
switch (event)
|
switch (event) {
|
||||||
{
|
case ARDUINO_EVENT_WIFI_SCAN_DONE: {
|
||||||
case ARDUINO_EVENT_WIFI_SCAN_DONE:
|
|
||||||
{
|
|
||||||
Serial.println("WIFI scan done\n");
|
Serial.println("WIFI scan done\n");
|
||||||
no_networks = WiFi.scanComplete();
|
no_networks = WiFi.scanComplete();
|
||||||
std::vector<WifiInfo> *vec = new std::vector<WifiInfo>();
|
Serial.println("making notify vector");
|
||||||
std::shared_ptr<std::vector<WifiInfo>> info = std::shared_ptr<std::vector<WifiInfo>>(vec);
|
auto info = std::vector<WifiInfo>(no_networks);
|
||||||
|
Serial.println("loaing notify vector");
|
||||||
for (int i = 0; i < no_networks; i++)
|
for (int i = 0; i < no_networks; i++) {
|
||||||
{
|
auto ssid =
|
||||||
info->push_back(WifiInfo {
|
WiFi.SSID(i).c_str() ? std::string(WiFi.SSID(i).c_str()) : "No SSID";
|
||||||
.ssid = std::string(WiFi.SSID(i).c_str()),
|
info[i] = WifiInfo(ssid, WiFi.RSSI(i));
|
||||||
.rssi = WiFi.RSSI(i)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
if (no_networks < 0)
|
if (no_networks < 0) {
|
||||||
{
|
|
||||||
Serial.println("Scan failed");
|
Serial.println("Scan failed");
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
// TODO Convert To callbacks
|
// TODO Convert To callbacks
|
||||||
// this->display.clear_wifi_networks();
|
// this->display.clear_wifi_networks();
|
||||||
Serial.print(no_networks);
|
Serial.print(no_networks);
|
||||||
Serial.print(" found\n");
|
Serial.print(" found\n");
|
||||||
// this->display.wifi_scan_complete( no_networks);
|
// this->display.wifi_scan_complete( no_networks);
|
||||||
}
|
}
|
||||||
this->scan_notification.notify(info);
|
Serial.println("notifying");
|
||||||
if (WiFi.isConnected() == false)
|
mScanNotification->notify(info);
|
||||||
{
|
Serial.println("notified");
|
||||||
|
if (WiFi.isConnected() == false) {
|
||||||
WiFi.reconnect();
|
WiFi.reconnect();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -56,108 +50,68 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event){
|
||||||
Serial.println(WiFi.status());
|
Serial.println(WiFi.status());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (WiFi.status() == WL_CONNECT_FAILED)
|
if (WiFi.status() == WL_CONNECT_FAILED) {
|
||||||
{
|
|
||||||
Serial.println("connection failed.");
|
Serial.println("connection failed.");
|
||||||
WiFi.disconnect();
|
WiFi.disconnect();
|
||||||
}
|
}
|
||||||
Serial.println(WiFi.status());
|
Serial.println(WiFi.status());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wifiHandler::isAvailable(){
|
bool wifiHandler::isAvailable() { return true; }
|
||||||
return true;
|
std::shared_ptr<wifiHandler> wifiHandler::getInstance() {
|
||||||
}
|
if (mInstance) {
|
||||||
std::shared_ptr<wifiHandler> wifiHandler::getInstance()
|
|
||||||
{
|
|
||||||
if(mInstance)
|
|
||||||
{
|
|
||||||
return mInstance;
|
return mInstance;
|
||||||
}
|
}
|
||||||
mInstance = std::shared_ptr<wifiHandler>(new wifiHandler());
|
mInstance = std::shared_ptr<wifiHandler>(new wifiHandler());
|
||||||
return mInstance;
|
return mInstance;
|
||||||
};
|
};
|
||||||
|
|
||||||
wifiHandler::wifiHandler()
|
wifiHandler::wifiHandler() {
|
||||||
{
|
|
||||||
this->password = "";
|
this->password = "";
|
||||||
this->SSID = "";
|
this->SSID = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiHandler::update_status()
|
void wifiHandler::update_status() {
|
||||||
{
|
|
||||||
Serial.println("update_status");
|
Serial.println("update_status");
|
||||||
std::shared_ptr<wifiStatus> status = std::make_shared<wifiStatus>(wifiStatus());
|
auto isConnected = WiFi.isConnected();
|
||||||
//wifiStatus *status = new wifiStatus();
|
std::string ip(WiFi.localIP().toString().c_str());
|
||||||
status->isConnected = WiFi.isConnected();
|
std::string ssid(WiFi.SSID().c_str());
|
||||||
//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;
|
|
||||||
|
|
||||||
|
wifiStatus status = wifiStatus(isConnected, ip, ssid);
|
||||||
|
|
||||||
// Serial.println(WiFi.localIP());
|
// 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
|
// No connection was attempted so don't try to to save the creds
|
||||||
if(!this->connect_attempt) return;
|
if (!connect_attempt) {
|
||||||
#if 0
|
return;
|
||||||
if (strcmp(temporary_password, wifiHandler::password) != 0 || strcmp(temporary_ssid, wifiHandler::SSID) != 0)
|
}
|
||||||
{
|
|
||||||
strcpy(wifiHandler::password, temporary_password);
|
if (temporary_password == password || temporary_ssid == SSID) {
|
||||||
strcpy(wifiHandler::SSID, temporary_ssid);
|
password = temporary_password;
|
||||||
|
SSID = temporary_ssid;
|
||||||
|
|
||||||
Preferences preferences;
|
Preferences preferences;
|
||||||
preferences.begin("wifiSettings", false);
|
preferences.begin("wifiSettings", false);
|
||||||
String tempString = wifiHandler::password;
|
String tempString = temporary_password.c_str();
|
||||||
preferences.putString("password", tempString);
|
preferences.putString("password", tempString);
|
||||||
tempString = wifiHandler::SSID;
|
tempString = temporary_ssid.c_str();
|
||||||
preferences.putString("SSID", tempString);
|
preferences.putString("SSID", tempString);
|
||||||
preferences.end();
|
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;
|
this->connect_attempt = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiHandler::scan()
|
void wifiHandler::scan() {
|
||||||
{
|
|
||||||
Serial.println("scan called");
|
Serial.println("scan called");
|
||||||
/* If the */
|
|
||||||
WiFi.status();
|
|
||||||
if (WiFi.isConnected() != true)
|
|
||||||
{
|
|
||||||
WiFi.disconnect();
|
WiFi.disconnect();
|
||||||
}
|
|
||||||
WiFi.scanNetworks(true);
|
WiFi.scanNetworks(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wifiHandler::begin() {
|
||||||
void wifiHandler::begin()
|
|
||||||
{
|
|
||||||
// this->display = display;
|
// this->display = display;
|
||||||
WiFi.setHostname("OMOTE");
|
WiFi.setHostname("OMOTE");
|
||||||
WiFi.mode(WIFI_STA);
|
WiFi.mode(WIFI_STA);
|
||||||
|
@ -170,19 +124,15 @@ void wifiHandler::begin()
|
||||||
String password = preferences.getString("password");
|
String password = preferences.getString("password");
|
||||||
preferences.end();
|
preferences.end();
|
||||||
|
|
||||||
/* If the SSID is not empty, there was a value stored in the preferences and we try to use it.*/
|
/* If the SSID is not empty, there was a value stored in the preferences and
|
||||||
if (!ssid.isEmpty())
|
* we try to use it.*/
|
||||||
{
|
if (!ssid.isEmpty()) {
|
||||||
Serial.print("Connecting to wifi ");
|
Serial.print("Connecting to wifi ");
|
||||||
Serial.println(ssid);
|
Serial.println(ssid);
|
||||||
//strcpy(this->SSID, ssid.c_str());
|
|
||||||
//strcpy(this->password, password.c_str());
|
|
||||||
this->SSID = ssid.c_str();
|
this->SSID = ssid.c_str();
|
||||||
this->password = password.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)));
|
this->connect(SSID, this->password);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
Serial.println("no SSID or password stored");
|
Serial.println("no SSID or password stored");
|
||||||
/*Set first character to \0 indicates an empty string*/
|
/*Set first character to \0 indicates an empty string*/
|
||||||
this->SSID[0] = '\0';
|
this->SSID[0] = '\0';
|
||||||
|
@ -193,38 +143,22 @@ void wifiHandler::begin()
|
||||||
WiFi.setSleep(true);
|
WiFi.setSleep(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiHandler::onScanDone(std::function<void (std::shared_ptr<std::vector<WifiInfo>>)> function){
|
void wifiHandler::connect(std::string ssid, std::string password) {
|
||||||
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)
|
|
||||||
{
|
|
||||||
this->connect_attempt = true;
|
this->connect_attempt = true;
|
||||||
this->temporary_password = password;
|
// this->temporary_password = password;
|
||||||
this->temporary_ssid = ssid;
|
// this->temporary_ssid = ssid;
|
||||||
WiFi.begin(ssid->c_str(), password->c_str());
|
WiFi.begin(ssid.c_str(), password.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiHandler::turnOff()
|
void wifiHandler::turnOff() {
|
||||||
{
|
|
||||||
WiFi.disconnect();
|
WiFi.disconnect();
|
||||||
WiFi.mode(WIFI_OFF);
|
WiFi.mode(WIFI_OFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiHandler::disconnect(){
|
void wifiHandler::disconnect() { WiFi.disconnect(); }
|
||||||
WiFi.disconnect();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool wifiHandler::isConnected()
|
bool wifiHandler::isConnected() { return WiFi.isConnected(); }
|
||||||
{
|
|
||||||
return WiFi.isConnected();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string wifiHandler::getIP()
|
std::string wifiHandler::getIP() {
|
||||||
{
|
|
||||||
return std::string(WiFi.localIP().toString().c_str());
|
return std::string(WiFi.localIP().toString().c_str());
|
||||||
}
|
}
|
|
@ -1,49 +1,34 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "wifiHandlerInterface.h"
|
|
||||||
#include "Notification.hpp"
|
#include "Notification.hpp"
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
|
#include "wifiHandlerInterface.h"
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
|
||||||
#define STRING_SIZE 50
|
|
||||||
|
|
||||||
class wifiHandler : public wifiHandlerInterface {
|
class wifiHandler : public wifiHandlerInterface {
|
||||||
public:
|
public:
|
||||||
wifiHandler();
|
wifiHandler();
|
||||||
static std::shared_ptr<wifiHandler> getInstance();
|
static std::shared_ptr<wifiHandler> getInstance();
|
||||||
/**
|
/**
|
||||||
* @brief Function to initialize the wifi handler
|
* @brief Function to initialize the wifi handler
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
void begin();
|
void begin();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Connect to the wifi using the provided credetials
|
* @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(std::string ssid, std::string password) override;
|
||||||
//void connect(const char* SSID, const char* password);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief function to trigger asynchronous scan for wifi networks
|
* @brief function to trigger asynchronous scan for wifi networks
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
void scan();
|
void scan();
|
||||||
bool isAvailable();
|
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:
|
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
|
* @brief Function to update the wifi credentials. This function is called in
|
||||||
* after a connection is established. Only then is the new credentials stored and the old stored credentials
|
* the wifi event callback function after a connection is established. Only
|
||||||
|
* then is the new credentials stored and the old stored credentials
|
||||||
* overwritten.
|
* overwritten.
|
||||||
*
|
*
|
||||||
* @param temporary_ssid
|
* @param temporary_ssid
|
||||||
|
@ -55,31 +40,25 @@ class wifiHandler: public wifiHandlerInterface {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Function to turn off wifi
|
* @brief Function to turn off wifi
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
void turnOff();
|
void turnOff();
|
||||||
/**
|
/**
|
||||||
* @brief Function to get the IP address of this device
|
* @brief Function to get the IP address of this device
|
||||||
*
|
|
||||||
* @return String IP Address of the device
|
* @return String IP Address of the device
|
||||||
*/
|
*/
|
||||||
std::string getIP();
|
std::string getIP();
|
||||||
wifiStatus wifi_status;
|
|
||||||
static std::shared_ptr<wifiHandler> mInstance;
|
static std::shared_ptr<wifiHandler> mInstance;
|
||||||
bool connect_attempt = false;
|
bool connect_attempt = false;
|
||||||
std::shared_ptr<std::string> temporary_password;
|
std::string temporary_password;
|
||||||
std::shared_ptr<std::string> temporary_ssid;
|
std::string temporary_ssid;
|
||||||
|
|
||||||
|
std::string password;
|
||||||
|
std::string SSID;
|
||||||
|
|
||||||
void update_status();
|
void update_status();
|
||||||
/**
|
|
||||||
* @brief Internal variable to store the wifi password
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
std::string password;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Function to disconnect from the network
|
* @brief Function to disconnect from the network
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
void disconnect();
|
void disconnect();
|
||||||
|
|
||||||
|
@ -94,6 +73,4 @@ class wifiHandler: public wifiHandlerInterface {
|
||||||
* @brief Internal variable to store the wifi SSID
|
* @brief Internal variable to store the wifi SSID
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
std::string SSID;
|
|
||||||
|
|
||||||
};
|
};
|
|
@ -1,68 +1,21 @@
|
||||||
#include "wifiHandlerSim.hpp"
|
#include "wifiHandlerSim.hpp"
|
||||||
|
|
||||||
std::shared_ptr<wifiHandlerSim> mInstance;
|
wifiHandlerSim::wifiHandlerSim() {}
|
||||||
|
|
||||||
std::shared_ptr<wifiHandlerSim> wifiHandlerSim::getInstance()
|
void wifiHandlerSim::begin() {}
|
||||||
{
|
|
||||||
if(mInstance)
|
|
||||||
{
|
|
||||||
return mInstance;
|
|
||||||
}
|
|
||||||
mInstance = std::make_shared<wifiHandlerSim>(wifiHandlerSim());
|
|
||||||
return mInstance;
|
|
||||||
};
|
|
||||||
|
|
||||||
wifiHandlerSim::wifiHandlerSim(){
|
void wifiHandlerSim::connect(std::string ssid, std::string password) {
|
||||||
|
status.ssid = ssid;
|
||||||
}
|
mStatusUpdate->notify(wifiStatus(status));
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static const WifiInfo wifis[] = {
|
static const WifiInfo wifis[] = {
|
||||||
{
|
WifiInfo("High Signal Wifi", -49), WifiInfo("Mid Signal Wifi", -55),
|
||||||
.ssid = "High Signal Wifi"
|
WifiInfo("Low Signal Wifi", -65), WifiInfo("No Signal Wifi", -90)};
|
||||||
, .rssi = -49
|
|
||||||
}
|
|
||||||
, {
|
|
||||||
.ssid = "Mid Signal Wifi"
|
|
||||||
, .rssi = -55
|
|
||||||
}
|
|
||||||
, {
|
|
||||||
.ssid = "Low Signal Wifi"
|
|
||||||
, .rssi = -65
|
|
||||||
}
|
|
||||||
, {
|
|
||||||
.ssid = "No Signal Wifi"
|
|
||||||
, .rssi = -90
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
void wifiHandlerSim::scan() {
|
void wifiHandlerSim::scan() {
|
||||||
std::shared_ptr<std::vector<WifiInfo>> info = std::make_shared<std::vector<WifiInfo>>(std::vector(std::begin(wifis), std::end(wifis)));
|
std::vector<WifiInfo> info = std::vector(std::begin(wifis), std::end(wifis));
|
||||||
this->scan_notification.notify(info);
|
mScanNotification->notify(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wifiHandlerSim::isAvailable(){
|
bool wifiHandlerSim::isAvailable() { return false; }
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,35 +1,24 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "wifiHandlerInterface.h"
|
|
||||||
#include "Notification.hpp"
|
#include "Notification.hpp"
|
||||||
|
#include "wifiHandlerInterface.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
class wifiHandlerSim : public wifiHandlerInterface {
|
class wifiHandlerSim : public wifiHandlerInterface {
|
||||||
public:
|
public:
|
||||||
wifiHandlerSim();
|
wifiHandlerSim();
|
||||||
static std::shared_ptr<wifiHandlerSim> getInstance();
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Connect to the wifi using the provided credetials
|
* @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(std::string ssid, std::string password) override;
|
||||||
//void connect(const char* SSID, const char* password);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief function to trigger asynchronous scan for wifi networks
|
* @brief function to trigger asynchronous scan for wifi networks
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
void scan();
|
void scan();
|
||||||
bool isAvailable();
|
bool isAvailable();
|
||||||
void begin();
|
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:
|
private:
|
||||||
Notification<std::shared_ptr<std::vector<WifiInfo>>> scan_notification;
|
wifiStatus status = wifiStatus(true, "172.0.0.1", "FakeNet");
|
||||||
Notification<std::shared_ptr<wifiStatus>> status_update;
|
|
||||||
};
|
};
|
|
@ -44,7 +44,7 @@ build_flags =
|
||||||
-I OmoteUI/core/widget
|
-I OmoteUI/core/widget
|
||||||
-I OmoteUI/core/page
|
-I OmoteUI/core/page
|
||||||
-I OmoteUI/UIs
|
-I OmoteUI/UIs
|
||||||
-I OmoteUI/UIs/Basic
|
;-I OmoteUI/UIs/Basic
|
||||||
-I OmoteUI/UIs/BasicRefactored
|
-I OmoteUI/UIs/BasicRefactored
|
||||||
-I OmoteUI/UIs/BasicRefactored/screen
|
-I OmoteUI/UIs/BasicRefactored/screen
|
||||||
-I OmoteUI/UIs/BasicRefactored/page
|
-I OmoteUI/UIs/BasicRefactored/page
|
||||||
|
@ -59,6 +59,7 @@ lib_deps =
|
||||||
lib_archive = false
|
lib_archive = false
|
||||||
build_src_filter =
|
build_src_filter =
|
||||||
+<../OmoteUI/*>
|
+<../OmoteUI/*>
|
||||||
|
-<../OmoteUI/UIs/Basic/*>
|
||||||
+<../HAL/HardwareAbstract.cpp>
|
+<../HAL/HardwareAbstract.cpp>
|
||||||
+<../HAL/HardwareModules/*.cpp>
|
+<../HAL/HardwareModules/*.cpp>
|
||||||
|
|
||||||
|
@ -115,6 +116,8 @@ build_flags =
|
||||||
-I HAL/Targets/ESP32/wifiHandler
|
-I HAL/Targets/ESP32/wifiHandler
|
||||||
-I HAL/Targets/ESP32/keys
|
-I HAL/Targets/ESP32/keys
|
||||||
|
|
||||||
|
monitor_filters = esp32_exception_decoder
|
||||||
|
|
||||||
build_unflags =
|
build_unflags =
|
||||||
-std=gnu++11
|
-std=gnu++11
|
||||||
build_src_filter =
|
build_src_filter =
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
#include "BasicUI.hpp"
|
#include "BasicUI.hpp"
|
||||||
#include "HardwareSimulator.hpp"
|
#include "HardwareSimulator.hpp"
|
||||||
#include "OmoteUI.hpp"
|
|
||||||
#include "omoteconfig.h"
|
#include "omoteconfig.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue