update wifi interface and handler

add the wifi interface to the hardware abstract
This commit is contained in:
Matthew Colvin 2023-08-11 22:50:26 -05:00 committed by MatthewColvin
parent a4e35a2219
commit 1bbafd4bb5
6 changed files with 43 additions and 33 deletions

View file

@ -1,9 +1,12 @@
#include "HardwareAbstract.hpp" #include "HardwareAbstract.hpp"
HardwareAbstract::HardwareAbstract(std::shared_ptr<BatteryInterface> aBattery) HardwareAbstract::HardwareAbstract(
: mBattery(std::move(aBattery)){ std::shared_ptr<BatteryInterface> aBattery,
std::shared_ptr<wifiHandlerInterface> aWifiHandler
} )
: mBattery(std::move(aBattery)),
mWifiHandler(std::move(aWifiHandler))
{}
std::optional<HardwareAbstract::batteryStatus> HardwareAbstract::getBatteryStatus(){ std::optional<HardwareAbstract::batteryStatus> HardwareAbstract::getBatteryStatus(){
if(mBattery){ if(mBattery){

View file

@ -7,6 +7,7 @@
#include <optional> #include <optional>
#include <string> #include <string>
#include "BatteryInterface.h" #include "BatteryInterface.h"
#include "wifiHandlerInterface.h"
class HardwareAbstract { class HardwareAbstract {
public: public:
@ -20,7 +21,9 @@ public:
}; };
virtual std::optional<batteryStatus> getBatteryStatus(); virtual std::optional<batteryStatus> getBatteryStatus();
HardwareAbstract(std::shared_ptr<BatteryInterface> aBattery = nullptr); HardwareAbstract(std::shared_ptr<BatteryInterface> aBattery = nullptr,
std::shared_ptr<wifiHandlerInterface> aWifiHandler = nullptr
);
/// @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;
@ -31,4 +34,5 @@ public:
private: private:
std::shared_ptr<BatteryInterface> mBattery; std::shared_ptr<BatteryInterface> mBattery;
std::shared_ptr<wifiHandlerInterface> mWifiHandler;
}; };

View file

@ -1,6 +1,5 @@
#pragma once #pragma once
#include <Arduino.h> #include <string>
#include "DisplayInterface.h"
class wifiHandlerInterface{ class wifiHandlerInterface{
public: public:
@ -11,5 +10,5 @@ class wifiHandlerInterface{
virtual void turnOff() = 0; virtual void turnOff() = 0;
virtual void scan() = 0; virtual void scan() = 0;
virtual char* getSSID() = 0; virtual char* getSSID() = 0;
virtual String getIP() = 0; virtual std::string getIP() = 0;
}; };

View file

@ -1,4 +1,5 @@
#include "HardwareRevX.hpp" #include "HardwareRevX.hpp"
#include "wifihandler.hpp"
#include "driver/ledc.h" #include "driver/ledc.h"
std::shared_ptr<HardwareRevX> HardwareRevX::mInstance = nullptr; std::shared_ptr<HardwareRevX> HardwareRevX::mInstance = nullptr;
@ -49,9 +50,11 @@ void HardwareRevX::initIO() {
gpio_deep_sleep_hold_dis(); gpio_deep_sleep_hold_dis();
} }
HardwareRevX::HardwareRevX():HardwareAbstract(std::make_shared<Battery>(ADC_BAT,CRG_STAT)){ HardwareRevX::HardwareRevX():
HardwareAbstract(
} std::make_shared<Battery>(ADC_BAT,CRG_STAT),
wifiHandler::getInstance()
){}
HardwareRevX::WakeReason getWakeReason() { HardwareRevX::WakeReason getWakeReason() {
// Find out wakeup cause // Find out wakeup cause

View file

@ -2,7 +2,8 @@
#include <Arduino.h> #include <Arduino.h>
#include <Preferences.h> #include <Preferences.h>
wifiHandler* mInstance; 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){
@ -29,7 +30,7 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event){
case ARDUINO_EVENT_WIFI_STA_GOT_IP6: case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
// TODO convert to callbacks // TODO convert to callbacks
//display.update_wifi(true); //display.update_wifi(true);
this->update_credentials(temporary_ssid, temporary_password); //update_credentials(temporary_ssid, temporary_password);
break; break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
case ARDUINO_EVENT_WIFI_STA_LOST_IP: case ARDUINO_EVENT_WIFI_STA_LOST_IP:
@ -41,15 +42,14 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event){
} }
} }
wifiHandler* wifiHandler::getInstance() std::shared_ptr<wifiHandler> wifiHandler::getInstance()
{ {
if(instance == nullptr) if(mInstance)
{ {
instance = new wifiHandler(); return mInstance;
} }
return std::shared_ptr<wifiHandler>(new wifiHandler());
return instance; };
}
String wifiHandler::getFoundSSID(unsigned int index) String wifiHandler::getFoundSSID(unsigned int index)
{ {
@ -92,7 +92,6 @@ void wifiHandler::scan()
void wifiHandler::begin() void wifiHandler::begin()
{ {
//this->display = display; //this->display = display;
mInstance = wifiHandler::getInstance();
WiFi.setHostname("OMOTE"); WiFi.setHostname("OMOTE");
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.onEvent([] (WiFiEvent_t event) {mInstance->WiFiEvent(event);}); WiFi.onEvent([] (WiFiEvent_t event) {mInstance->WiFiEvent(event);});
@ -137,6 +136,10 @@ void wifiHandler::turnOff()
WiFi.mode(WIFI_OFF); WiFi.mode(WIFI_OFF);
} }
void wifiHandler::disconnect(){
WiFi.disconnect();
}
bool wifiHandler::isConnected() bool wifiHandler::isConnected()
{ {
return WiFi.isConnected(); return WiFi.isConnected();
@ -147,7 +150,7 @@ char* wifiHandler::getSSID()
return this->SSID; return this->SSID;
} }
String wifiHandler::getIP() std::string wifiHandler::getIP()
{ {
return WiFi.localIP().toString(); return std::string(WiFi.localIP().toString().c_str());
} }

View file

@ -6,7 +6,7 @@
class wifiHandler: public wifiHandlerInterface { class wifiHandler: public wifiHandlerInterface {
public: public:
static wifiHandler* getInstance(); static std::shared_ptr<wifiHandler> getInstance();
/** /**
* @brief Function to initialize the wifi handler * @brief Function to initialize the wifi handler
* *
@ -78,7 +78,7 @@ class wifiHandler: public wifiHandlerInterface {
* @param temporary_ssid * @param temporary_ssid
* @param temporary_password * @param temporary_password
*/ */
static void update_credentials(const char* temporary_ssid, const char* temporary_password); void update_credentials(const char* temporary_ssid, const char* temporary_password);
void WiFiEvent(WiFiEvent_t event); void WiFiEvent(WiFiEvent_t event);
@ -87,27 +87,25 @@ class wifiHandler: public wifiHandlerInterface {
* *
* @return String IP Address of the device * @return String IP Address of the device
*/ */
String getIP(); std::string getIP();
private: private:
static wifiHandler* instance;
static char temporary_password[STRING_SIZE];
static char temporary_ssid[STRING_SIZE];
wifiHandler(); wifiHandler();
static std::shared_ptr<wifiHandler> mInstance;
char temporary_password[STRING_SIZE];
char temporary_ssid[STRING_SIZE];
/** /**
* @brief Internal variable to store the wifi password * @brief Internal variable to store the wifi password
* *
*/ */
static char password[STRING_SIZE]; char password[STRING_SIZE];
/** /**
* @brief Internal variable to store the wifi SSID * @brief Internal variable to store the wifi SSID
* *
*/ */
static char SSID[STRING_SIZE]; char SSID[STRING_SIZE];
}; };