Updated wifi handler to use general wifiHandlerInterface

This commit is contained in:
Thomas Bittner 2023-08-21 21:53:04 +02:00 committed by MatthewColvin
parent 53fa7b7c87
commit 5cd4c6f379
7 changed files with 104 additions and 92 deletions

View file

@ -1,7 +1,7 @@
// OMOTE Hardware Abstraction
// 2023 Matthew Colvin
#ifndef _HARDWAREABSTRACT_H_
#define _HARDWAREABSTRACT_H_
#pragma once
#include "wifiHandlerInterface.h"
#include <functional>
#include <lvgl.h>
#include <memory>
@ -10,17 +10,6 @@
#include <vector>
#include "Notification.hpp"
typedef struct {
std::string ssid;
int rssi;
} WifiInfo;
typedef struct {
bool isConnected;
std::string IP;
std::string ssid;
}wifiStatus;
class HardwareAbstract {
public:
HardwareAbstract(
@ -40,7 +29,7 @@ public:
/// @param onBatteryStatusChangeHandler - Callable to be ran when batter status changes
void onBatteryChange(std::function<void(batteryStatus)> onBatteryStatusChangeHandler);
virtual std::shared_ptr<wifiHandlerInterface> wifi() = 0;
/// @brief Override in order to do setup of hardware devices
virtual void init() = 0;
@ -48,15 +37,9 @@ public:
/// @param message - Debug message
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:
Notification<batteryStatus> mBatteryNotification;
private:
};
#endif
};

View file

@ -1,14 +1,24 @@
#pragma once
#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{
public:
virtual void begin() = 0;
//virtual void connect(const char* SSID, const char* password) = 0;
virtual void disconnect() = 0;
virtual bool isConnected() = 0;
virtual void turnOff() = 0;
virtual bool isAvailable() = 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;
};

View file

@ -71,9 +71,9 @@ void HardwareRevX::init() {
// Make sure ESP32 is running at full speed
setCpuFrequencyMhz(240);
mDisplay = Display::getInstance(std::shared_ptr<HardwareAbstract>(this));
mBattery = std::make_shared<Battery>(ADC_BAT,CRG_STAT);
mWifiHandler = wifiHandler::getInstance(std::shared_ptr<HardwareAbstract>(this));
this->mDisplay = Display::getInstance(std::shared_ptr<HardwareAbstract>(this));
this->mBattery = std::make_shared<Battery>(ADC_BAT,CRG_STAT);
this->mWifiHandler = wifiHandler::getInstance(std::shared_ptr<HardwareAbstract>(this));
wakeup_reason = getWakeReason();
initIO();
setupBacklight();
@ -111,6 +111,10 @@ std::shared_ptr<HardwareRevX> HardwareRevX::getInstance(){
return mInstance;
}
std::shared_ptr<wifiHandlerInterface> HardwareRevX::wifi()
{
return this->mWifiHandler;
}
void HardwareRevX::activityDetection() {
static int accXold;

View file

@ -13,6 +13,7 @@
#include <PubSubClient.h>
#include <functional>
#include <memory>
#include "wifihandler.hpp"
#include "omoteconfig.h"
@ -30,14 +31,12 @@ public:
// HardwareAbstract
virtual void init() override;
#if 0
virtual void debugPrint(std::string aDebugMessage) override;
#else
void debugPrint(const char* fmt, ...);
#endif
void loopHandler();
std::shared_ptr<wifiHandlerInterface> wifi();
protected:
// Init Functions to setup hardware
void initIO();

View file

@ -36,7 +36,7 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event){
Serial.print(" found\n");
//this->display.wifi_scan_complete( no_networks);
}
mHardware->wifi_scan_done.notify(info);
this->scan_notification.notify(info);
break;
}
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
@ -57,6 +57,9 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event){
Serial.println(WiFi.status());
}
bool wifiHandler::isAvailable(){
return true;
}
std::shared_ptr<wifiHandler> wifiHandler::getInstance(std::shared_ptr<HardwareAbstract> aHardware)
{
if(mInstance)
@ -70,8 +73,6 @@ std::shared_ptr<wifiHandler> wifiHandler::getInstance(std::shared_ptr<HardwareAb
wifiHandler::wifiHandler(std::shared_ptr<HardwareAbstract> 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->SSID = "";
this->begin();
@ -98,7 +99,7 @@ void wifiHandler::update_status()
//Serial.println(WiFi.localIP());
this->mHardware->wifi_status_update.notify(status);
this->status_update.notify(status);
}
void wifiHandler::update_credentials()
@ -178,6 +179,14 @@ void wifiHandler::begin()
WiFi.setSleep(true);
}
void wifiHandler::onScanDone(std::function<void (std::shared_ptr<std::vector<WifiInfo>>)> function){
this->scan_notification.onNotify(std::move(function));
}
void wifiHandler::onStatusUpdate(std::function<void (std::shared_ptr<wifiStatus>)> function){
this->status_update.onNotify(std::move(function));
}
void wifiHandler::connect(std::shared_ptr<std::string> ssid, std::shared_ptr<std::string> password)
{
this->temporary_password = password;

View file

@ -1,6 +1,8 @@
#pragma once
#include "wifiHandlerInterface.h"
#include "HardwareAbstract.hpp"
#include "Notification.hpp"
#include "memory.h"
#include <WiFi.h>
#define STRING_SIZE 50
@ -21,8 +23,61 @@ class wifiHandler: public wifiHandlerInterface {
* @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();
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
*
@ -36,54 +91,6 @@ class wifiHandler: public wifiHandlerInterface {
* @return false Device is not connected to wifi network
*/
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
*

View file

@ -52,7 +52,7 @@ void OmoteUI::password_field_event_cb(lv_event_t* e)
const char* password = lv_textarea_get_text(ta);
switch(code){
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);
this->hide_keyboard();
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;
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
//wifihandler.connect(ssid, password);
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_password_page = this->create_wifi_password_page(this->settingsMenu);
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_status_update.onNotify([this] (std::shared_ptr<wifiStatus> status) {this->wifi_status(status);});
this->mHardware->wifi()->onScanDone([] (std::shared_ptr<std::vector<WifiInfo>> info) {mInstance->wifi_scan_done(info);});
this->mHardware->wifi()->onStatusUpdate([] (std::shared_ptr<wifiStatus> status) {mInstance->wifi_status(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_label_set_text(label, "Searching for wifi networks");
mHardware->debugPrint("Wifi settings cb called\n");
mHardware->wifi_scan_start.notify();
mHardware->wifi()->scan();
//This will trigger an asynchronouse network scan
// We need to trigger wifi search via HAL
//wifihandler.scan();