Updated wifi handler to use general wifiHandlerInterface
This commit is contained in:
parent
53fa7b7c87
commit
5cd4c6f379
7 changed files with 104 additions and 92 deletions
|
@ -1,7 +1,7 @@
|
||||||
// OMOTE Hardware Abstraction
|
// OMOTE Hardware Abstraction
|
||||||
// 2023 Matthew Colvin
|
// 2023 Matthew Colvin
|
||||||
#ifndef _HARDWAREABSTRACT_H_
|
#pragma once
|
||||||
#define _HARDWAREABSTRACT_H_
|
#include "wifiHandlerInterface.h"
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <lvgl.h>
|
#include <lvgl.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
@ -10,17 +10,6 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "Notification.hpp"
|
#include "Notification.hpp"
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
std::string ssid;
|
|
||||||
int rssi;
|
|
||||||
} WifiInfo;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
bool isConnected;
|
|
||||||
std::string IP;
|
|
||||||
std::string ssid;
|
|
||||||
}wifiStatus;
|
|
||||||
|
|
||||||
class HardwareAbstract {
|
class HardwareAbstract {
|
||||||
public:
|
public:
|
||||||
HardwareAbstract(
|
HardwareAbstract(
|
||||||
|
@ -40,7 +29,7 @@ public:
|
||||||
/// @param onBatteryStatusChangeHandler - Callable to be ran when batter status changes
|
/// @param onBatteryStatusChangeHandler - Callable to be ran when batter status changes
|
||||||
void onBatteryChange(std::function<void(batteryStatus)> onBatteryStatusChangeHandler);
|
void onBatteryChange(std::function<void(batteryStatus)> onBatteryStatusChangeHandler);
|
||||||
|
|
||||||
|
virtual std::shared_ptr<wifiHandlerInterface> wifi() = 0;
|
||||||
/// @brief Override in order to do setup of hardware devices
|
/// @brief Override in order to do setup of hardware devices
|
||||||
virtual void init() = 0;
|
virtual void init() = 0;
|
||||||
|
|
||||||
|
@ -48,15 +37,9 @@ public:
|
||||||
/// @param message - Debug message
|
/// @param message - Debug message
|
||||||
virtual void debugPrint(const char* fmt, ...) = 0;
|
virtual void debugPrint(const char* fmt, ...) = 0;
|
||||||
|
|
||||||
Notification<std::shared_ptr<std::vector<WifiInfo>>> wifi_scan_done;
|
|
||||||
Notification<> wifi_scan_start;
|
|
||||||
Notification<std::shared_ptr<std::string>, std::shared_ptr<std::string>> wifi_connect;
|
|
||||||
Notification<std::shared_ptr<wifiStatus>> wifi_status_update;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Notification<batteryStatus> mBatteryNotification;
|
Notification<batteryStatus> mBatteryNotification;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
};
|
};
|
||||||
#endif
|
|
|
@ -1,14 +1,24 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "HardwareAbstract.hpp"
|
#include <memory>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
std::string ssid;
|
||||||
|
int rssi;
|
||||||
|
} WifiInfo;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
bool isConnected;
|
||||||
|
std::string IP;
|
||||||
|
std::string ssid;
|
||||||
|
}wifiStatus;
|
||||||
|
|
||||||
class wifiHandlerInterface{
|
class wifiHandlerInterface{
|
||||||
public:
|
public:
|
||||||
virtual void begin() = 0;
|
virtual bool isAvailable() = 0;
|
||||||
//virtual void connect(const char* SSID, const char* password) = 0;
|
|
||||||
virtual void disconnect() = 0;
|
|
||||||
virtual bool isConnected() = 0;
|
|
||||||
virtual void turnOff() = 0;
|
|
||||||
virtual void scan() = 0;
|
virtual void scan() = 0;
|
||||||
virtual std::string getIP() = 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;
|
||||||
};
|
};
|
|
@ -71,9 +71,9 @@ void HardwareRevX::init() {
|
||||||
// Make sure ESP32 is running at full speed
|
// Make sure ESP32 is running at full speed
|
||||||
setCpuFrequencyMhz(240);
|
setCpuFrequencyMhz(240);
|
||||||
|
|
||||||
mDisplay = Display::getInstance(std::shared_ptr<HardwareAbstract>(this));
|
this->mDisplay = Display::getInstance(std::shared_ptr<HardwareAbstract>(this));
|
||||||
mBattery = std::make_shared<Battery>(ADC_BAT,CRG_STAT);
|
this->mBattery = std::make_shared<Battery>(ADC_BAT,CRG_STAT);
|
||||||
mWifiHandler = wifiHandler::getInstance(std::shared_ptr<HardwareAbstract>(this));
|
this->mWifiHandler = wifiHandler::getInstance(std::shared_ptr<HardwareAbstract>(this));
|
||||||
wakeup_reason = getWakeReason();
|
wakeup_reason = getWakeReason();
|
||||||
initIO();
|
initIO();
|
||||||
setupBacklight();
|
setupBacklight();
|
||||||
|
@ -111,6 +111,10 @@ std::shared_ptr<HardwareRevX> HardwareRevX::getInstance(){
|
||||||
return mInstance;
|
return mInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<wifiHandlerInterface> HardwareRevX::wifi()
|
||||||
|
{
|
||||||
|
return this->mWifiHandler;
|
||||||
|
}
|
||||||
|
|
||||||
void HardwareRevX::activityDetection() {
|
void HardwareRevX::activityDetection() {
|
||||||
static int accXold;
|
static int accXold;
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include "wifihandler.hpp"
|
||||||
|
|
||||||
|
|
||||||
#include "omoteconfig.h"
|
#include "omoteconfig.h"
|
||||||
|
@ -30,14 +31,12 @@ public:
|
||||||
|
|
||||||
// HardwareAbstract
|
// HardwareAbstract
|
||||||
virtual void init() override;
|
virtual void init() override;
|
||||||
#if 0
|
|
||||||
virtual void debugPrint(std::string aDebugMessage) override;
|
|
||||||
#else
|
|
||||||
void debugPrint(const char* fmt, ...);
|
void debugPrint(const char* fmt, ...);
|
||||||
#endif
|
|
||||||
|
|
||||||
void loopHandler();
|
void loopHandler();
|
||||||
|
|
||||||
|
std::shared_ptr<wifiHandlerInterface> wifi();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// Init Functions to setup hardware
|
// Init Functions to setup hardware
|
||||||
void initIO();
|
void initIO();
|
||||||
|
|
|
@ -36,7 +36,7 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event){
|
||||||
Serial.print(" found\n");
|
Serial.print(" found\n");
|
||||||
//this->display.wifi_scan_complete( no_networks);
|
//this->display.wifi_scan_complete( no_networks);
|
||||||
}
|
}
|
||||||
mHardware->wifi_scan_done.notify(info);
|
this->scan_notification.notify(info);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
||||||
|
@ -57,6 +57,9 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event){
|
||||||
Serial.println(WiFi.status());
|
Serial.println(WiFi.status());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool wifiHandler::isAvailable(){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
std::shared_ptr<wifiHandler> wifiHandler::getInstance(std::shared_ptr<HardwareAbstract> aHardware)
|
std::shared_ptr<wifiHandler> wifiHandler::getInstance(std::shared_ptr<HardwareAbstract> aHardware)
|
||||||
{
|
{
|
||||||
if(mInstance)
|
if(mInstance)
|
||||||
|
@ -70,8 +73,6 @@ std::shared_ptr<wifiHandler> wifiHandler::getInstance(std::shared_ptr<HardwareAb
|
||||||
wifiHandler::wifiHandler(std::shared_ptr<HardwareAbstract> aHardware)
|
wifiHandler::wifiHandler(std::shared_ptr<HardwareAbstract> aHardware)
|
||||||
{
|
{
|
||||||
this->mHardware = aHardware;
|
this->mHardware = aHardware;
|
||||||
this->mHardware->wifi_scan_start.onNotify([this](){this->mHardware->debugPrint("scan called\n"); this->scan();});
|
|
||||||
this->mHardware->wifi_connect.onNotify([this] (std::shared_ptr<std::string> ssid, std::shared_ptr<std::string> password){this->connect(ssid, password);});
|
|
||||||
this->password = "";
|
this->password = "";
|
||||||
this->SSID = "";
|
this->SSID = "";
|
||||||
this->begin();
|
this->begin();
|
||||||
|
@ -98,7 +99,7 @@ void wifiHandler::update_status()
|
||||||
|
|
||||||
|
|
||||||
//Serial.println(WiFi.localIP());
|
//Serial.println(WiFi.localIP());
|
||||||
this->mHardware->wifi_status_update.notify(status);
|
this->status_update.notify(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiHandler::update_credentials()
|
void wifiHandler::update_credentials()
|
||||||
|
@ -178,6 +179,14 @@ void wifiHandler::begin()
|
||||||
WiFi.setSleep(true);
|
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::shared_ptr<std::string> ssid, std::shared_ptr<std::string> password)
|
||||||
{
|
{
|
||||||
this->temporary_password = password;
|
this->temporary_password = password;
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "wifiHandlerInterface.h"
|
#include "wifiHandlerInterface.h"
|
||||||
#include "HardwareAbstract.hpp"
|
#include "HardwareAbstract.hpp"
|
||||||
|
#include "Notification.hpp"
|
||||||
|
#include "memory.h"
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
|
|
||||||
#define STRING_SIZE 50
|
#define STRING_SIZE 50
|
||||||
|
@ -21,8 +23,61 @@ class wifiHandler: public wifiHandlerInterface {
|
||||||
* @param SSID
|
* @param SSID
|
||||||
* @param password
|
* @param password
|
||||||
*/
|
*/
|
||||||
|
void connect(std::shared_ptr<std::string> ssid, std::shared_ptr<std::string> password);
|
||||||
//void connect(const char* SSID, const char* password);
|
//void connect(const char* SSID, const char* password);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief function to trigger asynchronous scan for wifi networks
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void scan();
|
||||||
|
bool isAvailable();
|
||||||
|
void onScanDone(std::function<void (std::shared_ptr<std::vector<WifiInfo>>)> function);
|
||||||
|
void onStatusUpdate(std::function<void (std::shared_ptr<wifiStatus>)> function);
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
Notification<std::shared_ptr<std::vector<WifiInfo>>> scan_notification;
|
||||||
|
Notification<std::shared_ptr<wifiStatus>> status_update;
|
||||||
|
/**
|
||||||
|
* @brief Function to update the wifi credentials. This function is called in the wifi event callback function
|
||||||
|
* after a connection is established. Only then is the new credentials stored and the old stored credentials
|
||||||
|
* 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;
|
||||||
|
std::shared_ptr<HardwareAbstract> mHardware;
|
||||||
|
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
|
* @brief Function to disconnect from the network
|
||||||
*
|
*
|
||||||
|
@ -36,54 +91,6 @@ class wifiHandler: public wifiHandlerInterface {
|
||||||
* @return false Device is not connected to wifi network
|
* @return false Device is not connected to wifi network
|
||||||
*/
|
*/
|
||||||
bool isConnected();
|
bool isConnected();
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Function to turn off wifi
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
void turnOff();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief function to trigger asynchronous scan for wifi networks
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
void scan();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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 get the IP address of this device
|
|
||||||
*
|
|
||||||
* @return String IP Address of the device
|
|
||||||
*/
|
|
||||||
std::string getIP();
|
|
||||||
Notification<std::shared_ptr<std::vector<WifiInfo>>> scan_done;
|
|
||||||
private:
|
|
||||||
|
|
||||||
wifiStatus wifi_status;
|
|
||||||
static std::shared_ptr<wifiHandler> mInstance;
|
|
||||||
std::shared_ptr<HardwareAbstract> mHardware;
|
|
||||||
std::shared_ptr<std::string> temporary_password;
|
|
||||||
std::shared_ptr<std::string> temporary_ssid;
|
|
||||||
|
|
||||||
void connect(std::shared_ptr<std::string> ssid, std::shared_ptr<std::string> password);
|
|
||||||
void update_status();
|
|
||||||
/**
|
|
||||||
* @brief Internal variable to store the wifi password
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
std::string password;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Internal variable to store the wifi SSID
|
* @brief Internal variable to store the wifi SSID
|
||||||
*
|
*
|
||||||
|
|
|
@ -52,7 +52,7 @@ void OmoteUI::password_field_event_cb(lv_event_t* e)
|
||||||
const char* password = lv_textarea_get_text(ta);
|
const char* password = lv_textarea_get_text(ta);
|
||||||
switch(code){
|
switch(code){
|
||||||
case LV_EVENT_READY:
|
case LV_EVENT_READY:
|
||||||
this->mHardware->wifi_connect.notify(std::make_shared<std::string>(std::string(ssid)), std::make_shared<std::string>(std::string(password)));
|
this->mHardware->wifi()->connect(std::make_shared<std::string>(std::string(ssid)), std::make_shared<std::string>(std::string(password)));
|
||||||
lv_obj_clear_state(ta, LV_STATE_FOCUSED);
|
lv_obj_clear_state(ta, LV_STATE_FOCUSED);
|
||||||
this->hide_keyboard();
|
this->hide_keyboard();
|
||||||
this->reset_settings_menu();
|
this->reset_settings_menu();
|
||||||
|
@ -73,7 +73,7 @@ void OmoteUI::connect_btn_cb(lv_event_t* event)
|
||||||
lv_obj_t* ta = (lv_obj_t*) event->user_data;
|
lv_obj_t* ta = (lv_obj_t*) event->user_data;
|
||||||
const char* password = lv_textarea_get_text(ta);
|
const char* password = lv_textarea_get_text(ta);
|
||||||
|
|
||||||
this->mHardware->wifi_connect.notify(std::make_shared<std::string>(std::string(ssid)), std::make_shared<std::string>(std::string(password)));
|
this->mHardware->wifi()->connect(std::make_shared<std::string>(std::string(ssid)), std::make_shared<std::string>(std::string(password)));
|
||||||
//Trigger wifi connection here
|
//Trigger wifi connection here
|
||||||
//wifihandler.connect(ssid, password);
|
//wifihandler.connect(ssid, password);
|
||||||
lv_obj_clear_state(ta, LV_STATE_FOCUSED);
|
lv_obj_clear_state(ta, LV_STATE_FOCUSED);
|
||||||
|
@ -233,8 +233,8 @@ void OmoteUI::create_wifi_settings(lv_obj_t* menu, lv_obj_t* parent)
|
||||||
this->wifi_selection_page = this->create_wifi_selection_page(menu);
|
this->wifi_selection_page = this->create_wifi_selection_page(menu);
|
||||||
this->wifi_password_page = this->create_wifi_password_page(this->settingsMenu);
|
this->wifi_password_page = this->create_wifi_password_page(this->settingsMenu);
|
||||||
this->create_wifi_main_page(parent);
|
this->create_wifi_main_page(parent);
|
||||||
this->mHardware->wifi_scan_done.onNotify([this] (std::shared_ptr<std::vector<WifiInfo>> info) {this->wifi_scan_done(info);});
|
this->mHardware->wifi()->onScanDone([] (std::shared_ptr<std::vector<WifiInfo>> info) {mInstance->wifi_scan_done(info);});
|
||||||
this->mHardware->wifi_status_update.onNotify([this] (std::shared_ptr<wifiStatus> status) {this->wifi_status(status);});
|
this->mHardware->wifi()->onStatusUpdate([] (std::shared_ptr<wifiStatus> status) {mInstance->wifi_status(status);});
|
||||||
}
|
}
|
||||||
|
|
||||||
void OmoteUI::wifi_status(std::shared_ptr<wifiStatus> status)
|
void OmoteUI::wifi_status(std::shared_ptr<wifiStatus> status)
|
||||||
|
@ -305,7 +305,7 @@ void OmoteUI::wifi_settings_cb(lv_event_t* event)
|
||||||
lv_obj_t* label = lv_label_create(cont);
|
lv_obj_t* label = lv_label_create(cont);
|
||||||
lv_label_set_text(label, "Searching for wifi networks");
|
lv_label_set_text(label, "Searching for wifi networks");
|
||||||
mHardware->debugPrint("Wifi settings cb called\n");
|
mHardware->debugPrint("Wifi settings cb called\n");
|
||||||
mHardware->wifi_scan_start.notify();
|
mHardware->wifi()->scan();
|
||||||
//This will trigger an asynchronouse network scan
|
//This will trigger an asynchronouse network scan
|
||||||
// We need to trigger wifi search via HAL
|
// We need to trigger wifi search via HAL
|
||||||
//wifihandler.scan();
|
//wifihandler.scan();
|
||||||
|
|
Loading…
Add table
Reference in a new issue