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;
|
|
||||||
|
|
||||||
typedef struct {
|
std::string ssid = "";
|
||||||
bool isConnected;
|
int rssi = 0;
|
||||||
std::string IP;
|
};
|
||||||
std::string ssid;
|
|
||||||
}wifiStatus;
|
struct wifiStatus {
|
||||||
|
wifiStatus(bool aConnected, std::string aIp, std::string aSsid)
|
||||||
class wifiHandlerInterface{
|
: isConnected(aConnected), IP(aIp), ssid(aSsid){};
|
||||||
public:
|
|
||||||
virtual bool isAvailable() = 0;
|
bool isConnected;
|
||||||
virtual void scan() = 0;
|
std::string IP = "";
|
||||||
virtual void connect(std::shared_ptr<std::string> ssid, std::shared_ptr<std::string> password) = 0;
|
std::string ssid = "";
|
||||||
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>>();
|
||||||
};
|
};
|
|
@ -1,29 +1,90 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <vector>
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
template <class... notifyData>
|
template <class... notifyData> class Handler;
|
||||||
class Notification{
|
template <class... notifyData> class Notification {
|
||||||
public:
|
friend class Handler<notifyData...>;
|
||||||
typedef std::function<void(notifyData...)> HandlerTy;
|
|
||||||
|
|
||||||
Notification() = default;
|
public:
|
||||||
void onNotify(HandlerTy aHandler);
|
typedef std::function<void(notifyData...)> HandlerTy;
|
||||||
void notify(notifyData... notifySendData);
|
typedef int HandlerID;
|
||||||
|
|
||||||
private:
|
Notification() { mIdMaker = 0; };
|
||||||
std::vector<HandlerTy> mFunctionHandlers;
|
void notify(notifyData... notifySendData);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
HandlerID onNotify(HandlerTy aHandler);
|
||||||
|
void unregister(HandlerID aHandler);
|
||||||
|
|
||||||
|
private:
|
||||||
|
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,58 +16,71 @@
|
||||||
|
|
||||||
#define DEFAULT_BACKLIGHT_BRIGHTNESS 128
|
#define DEFAULT_BACKLIGHT_BRIGHTNESS 128
|
||||||
|
|
||||||
|
class Display : public DisplayAbstract {
|
||||||
|
public:
|
||||||
|
static std::shared_ptr<Display> getInstance();
|
||||||
|
|
||||||
class Display: public DisplayAbstract
|
/// @brief Set brightness setting and fade to it
|
||||||
{
|
/// @param brightness
|
||||||
public:
|
virtual void setBrightness(uint8_t brightness) override;
|
||||||
static std::shared_ptr<Display> getInstance();
|
virtual uint8_t getBrightness() override;
|
||||||
|
virtual void turnOff() override;
|
||||||
|
|
||||||
/// @brief Set brightness setting and fade to it
|
std::shared_ptr<Notification<TS_Point>> TouchNotification() {
|
||||||
/// @param brightness
|
return mTouchEvent;
|
||||||
virtual void setBrightness(uint8_t brightness) override;
|
}
|
||||||
virtual uint8_t getBrightness() override;
|
|
||||||
virtual void turnOff() override;
|
|
||||||
|
|
||||||
void onTouch(Notification<TS_Point>::HandlerTy aTouchHandler);
|
inline void wake() {
|
||||||
|
if (isAsleep) {
|
||||||
|
isAsleep = false;
|
||||||
|
startFade();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inline void sleep() {
|
||||||
|
if (!isAsleep) {
|
||||||
|
isAsleep = true;
|
||||||
|
startFade();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
inline void wake() {if(isAsleep) {isAsleep = false; startFade();}}
|
protected:
|
||||||
inline void sleep() {if(!isAsleep){isAsleep = true; startFade();}}
|
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;
|
||||||
|
|
||||||
protected:
|
/// @brief Fade toward brightness based on isAwake
|
||||||
virtual void flushDisplay(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p);
|
/// @return True - Fade complete
|
||||||
virtual void screenInput(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) override;
|
/// False - Fade set point not reached
|
||||||
|
bool fade();
|
||||||
|
/// @brief Start the Fade task
|
||||||
|
void startFade();
|
||||||
|
|
||||||
/// @brief Fade toward brightness based on isAwake
|
/// @brief Set the actual display brightness right now
|
||||||
/// @return True - Fade complete
|
/// @param brightness
|
||||||
/// False - Fade set point not reached
|
void setCurrentBrightness(uint8_t brightness);
|
||||||
bool fade();
|
|
||||||
/// @brief Start the Fade task
|
|
||||||
void startFade();
|
|
||||||
|
|
||||||
/// @brief Set the actual display brightness right now
|
private:
|
||||||
/// @param brightness
|
Display(int backlight_pin, int enable_pin);
|
||||||
void setCurrentBrightness(uint8_t brightness);
|
void setupTFT();
|
||||||
|
void setupTouchScreen();
|
||||||
|
void setupBacklight();
|
||||||
|
|
||||||
private:
|
int mEnablePin;
|
||||||
Display(int backlight_pin, int enable_pin);
|
int mBacklightPin;
|
||||||
void setupTFT();
|
TFT_eSPI tft;
|
||||||
void setupTouchScreen();
|
|
||||||
void setupBacklight();
|
|
||||||
|
|
||||||
int mEnablePin;
|
Adafruit_FT6206 touch;
|
||||||
int mBacklightPin;
|
TS_Point touchPoint;
|
||||||
TFT_eSPI tft;
|
TS_Point oldPoint;
|
||||||
|
std::shared_ptr<Notification<TS_Point>> mTouchEvent =
|
||||||
|
std::make_shared<Notification<TS_Point>>();
|
||||||
|
|
||||||
Adafruit_FT6206 touch;
|
TaskHandle_t mDisplayFadeTask = nullptr;
|
||||||
TS_Point touchPoint;
|
SemaphoreHandle_t mFadeTaskMutex = nullptr;
|
||||||
TS_Point oldPoint;
|
static void fadeImpl(void *aBrightness);
|
||||||
Notification<TS_Point> mTouchEvent;
|
|
||||||
|
|
||||||
TaskHandle_t mDisplayFadeTask = nullptr;
|
uint8_t mBrightness = 0; // Current display brightness
|
||||||
SemaphoreHandle_t mFadeTaskMutex = nullptr;
|
uint8_t mAwakeBrightness = 100; // Current setting for brightness when awake
|
||||||
static void fadeImpl(void* aBrightness);
|
bool isAsleep = false;
|
||||||
|
|
||||||
uint8_t mBrightness = 0; // Current display brightness
|
|
||||||
uint8_t mAwakeBrightness = 100; // Current setting for brightness when awake
|
|
||||||
bool isAsleep = false;
|
|
||||||
};
|
};
|
|
@ -1,230 +1,164 @@
|
||||||
#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");
|
||||||
{
|
no_networks = WiFi.scanComplete();
|
||||||
Serial.println("WIFI scan done\n");
|
Serial.println("making notify vector");
|
||||||
no_networks = WiFi.scanComplete();
|
auto info = std::vector<WifiInfo>(no_networks);
|
||||||
std::vector<WifiInfo> *vec = new std::vector<WifiInfo>();
|
Serial.println("loaing notify vector");
|
||||||
std::shared_ptr<std::vector<WifiInfo>> info = std::shared_ptr<std::vector<WifiInfo>>(vec);
|
for (int i = 0; i < no_networks; i++) {
|
||||||
|
auto ssid =
|
||||||
for (int i = 0; i < no_networks; i++)
|
WiFi.SSID(i).c_str() ? std::string(WiFi.SSID(i).c_str()) : "No SSID";
|
||||||
{
|
info[i] = WifiInfo(ssid, WiFi.RSSI(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;
|
|
||||||
}
|
}
|
||||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
if (no_networks < 0) {
|
||||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
|
Serial.println("Scan failed");
|
||||||
this->update_credentials();
|
} else {
|
||||||
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
|
// TODO Convert To callbacks
|
||||||
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
|
// this->display.clear_wifi_networks();
|
||||||
case ARDUINO_EVENT_WIFI_STA_STOP:
|
Serial.print(no_networks);
|
||||||
this->update_status();
|
Serial.print(" found\n");
|
||||||
default:
|
// this->display.wifi_scan_complete( no_networks);
|
||||||
Serial.print("Wifi Status: ");
|
}
|
||||||
Serial.println(WiFi.status());
|
Serial.println("notifying");
|
||||||
break;
|
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.");
|
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;
|
|
||||||
}
|
|
||||||
mInstance = std::shared_ptr<wifiHandler>(new wifiHandler());
|
|
||||||
return mInstance;
|
return mInstance;
|
||||||
|
}
|
||||||
|
mInstance = std::shared_ptr<wifiHandler>(new wifiHandler());
|
||||||
|
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());
|
wifiStatus status = wifiStatus(isConnected, ip, ssid);
|
||||||
String ssid = WiFi.SSID();
|
|
||||||
status->ssid = WiFi.SSID().c_str();
|
|
||||||
|
|
||||||
//this->wifi_status.isConnected = WiFi.isConnected();
|
// Serial.println(WiFi.localIP());
|
||||||
//this->wifi_status.IP = WiFi.localIP();
|
mStatusUpdate->notify(status);
|
||||||
//this->wifi_status.isConnected = true;
|
|
||||||
|
|
||||||
|
|
||||||
//Serial.println(WiFi.localIP());
|
|
||||||
this->status_update.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);
|
|
||||||
strcpy(wifiHandler::SSID, temporary_ssid);
|
|
||||||
|
|
||||||
Preferences preferences;
|
if (temporary_password == password || temporary_ssid == SSID) {
|
||||||
preferences.begin("wifiSettings", false);
|
password = temporary_password;
|
||||||
String tempString = wifiHandler::password;
|
SSID = temporary_ssid;
|
||||||
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);
|
|
||||||
|
|
||||||
Preferences preferences;
|
Preferences preferences;
|
||||||
preferences.begin("wifiSettings", false);
|
preferences.begin("wifiSettings", false);
|
||||||
String tempString = this->temporary_password->c_str();
|
String tempString = temporary_password.c_str();
|
||||||
preferences.putString("password", tempString);
|
preferences.putString("password", tempString);
|
||||||
tempString = this->temporary_ssid->c_str();
|
tempString = temporary_ssid.c_str();
|
||||||
preferences.putString("SSID", tempString);
|
preferences.putString("SSID", tempString);
|
||||||
preferences.end();
|
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();
|
WiFi.disconnect();
|
||||||
if (WiFi.isConnected() != true)
|
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.disconnect();
|
||||||
}
|
}
|
||||||
WiFi.scanNetworks(true);
|
|
||||||
|
WiFi.setSleep(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wifiHandler::connect(std::string ssid, std::string password) {
|
||||||
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)
|
|
||||||
{
|
|
||||||
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,99 +1,76 @@
|
||||||
#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 {
|
||||||
|
public:
|
||||||
|
wifiHandler();
|
||||||
|
static std::shared_ptr<wifiHandler> getInstance();
|
||||||
|
/**
|
||||||
|
* @brief Function to initialize the wifi handler
|
||||||
|
*/
|
||||||
|
void begin();
|
||||||
|
|
||||||
class wifiHandler: public wifiHandlerInterface {
|
/**
|
||||||
public:
|
* @brief Connect to the wifi using the provided credetials
|
||||||
wifiHandler();
|
*/
|
||||||
static std::shared_ptr<wifiHandler> getInstance();
|
void connect(std::string ssid, std::string password) override;
|
||||||
/**
|
|
||||||
* @brief Function to initialize the wifi handler
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
void begin();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Connect to the wifi using the provided credetials
|
* @brief function to trigger asynchronous scan for wifi networks
|
||||||
*
|
*/
|
||||||
* @param SSID
|
void scan();
|
||||||
* @param password
|
bool isAvailable();
|
||||||
*/
|
|
||||||
void connect(std::shared_ptr<std::string> ssid, std::shared_ptr<std::string> password);
|
|
||||||
//void connect(const char* SSID, const char* password);
|
|
||||||
|
|
||||||
|
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
|
* @brief Function to turn off wifi
|
||||||
*
|
*/
|
||||||
*/
|
void turnOff();
|
||||||
void scan();
|
/**
|
||||||
bool isAvailable();
|
* @brief Function to get the IP address of this device
|
||||||
void onScanDone(std::function<void (std::shared_ptr<std::vector<WifiInfo>>)> function);
|
* @return String IP Address of the device
|
||||||
void onStatusUpdate(std::function<void (std::shared_ptr<wifiStatus>)> function);
|
*/
|
||||||
|
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 disconnect from the network
|
||||||
/**
|
*/
|
||||||
* @brief Function to update the wifi credentials. This function is called in the wifi event callback function
|
void disconnect();
|
||||||
* 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 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
|
||||||
|
*
|
||||||
|
*/
|
||||||
};
|
};
|
|
@ -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
|
||||||
|
*/
|
||||||
|
void connect(std::string ssid, std::string password) override;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Connect to the wifi using the provided credetials
|
* @brief function to trigger asynchronous scan for wifi networks
|
||||||
*
|
*/
|
||||||
* @param SSID
|
void scan();
|
||||||
* @param password
|
bool isAvailable();
|
||||||
*/
|
void begin();
|
||||||
void connect(std::shared_ptr<std::string> ssid, std::shared_ptr<std::string> password);
|
|
||||||
//void connect(const char* SSID, const char* password);
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
wifiStatus status = wifiStatus(true, "172.0.0.1", "FakeNet");
|
||||||
/**
|
|
||||||
* @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;
|
|
||||||
};
|
};
|
|
@ -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