added full ability to connect to wifi

This commit is contained in:
MatthewColvin 2023-10-17 17:30:41 -05:00
parent cf646f21db
commit ae7bc0da54
7 changed files with 41 additions and 27 deletions

View file

@ -3,25 +3,25 @@
#include <memory>
#include <string>
struct WifiInfo {
class wifiHandlerInterface {
public:
struct WifiInfo {
WifiInfo(){};
WifiInfo(std::string aSsid, int aRssi) : ssid(aSsid), rssi(aRssi) {}
std::string ssid = "";
int rssi = 0;
};
};
struct wifiStatus {
struct wifiStatus {
wifiStatus(bool aConnected, std::string aIp, std::string aSsid)
: isConnected(aConnected), IP(aIp), ssid(aSsid){};
bool isConnected;
std::string IP = "";
std::string ssid = "";
};
};
class wifiHandlerInterface {
public:
typedef std::vector<WifiInfo> ScanDoneDataTy;
typedef Notification<ScanDoneDataTy> ScanNotificationTy;

View file

@ -107,7 +107,6 @@ void wifiHandler::update_credentials() {
void wifiHandler::scan() {
Serial.println("scan called");
WiFi.disconnect();
WiFi.scanNetworks(true);
}

View file

@ -1,5 +1,7 @@
#include "wifiHandlerSim.hpp"
using WifiInfo = wifiHandlerInterface::WifiInfo;
wifiHandlerSim::wifiHandlerSim() {}
void wifiHandlerSim::begin() {}

View file

@ -6,10 +6,12 @@
using namespace UI;
using namespace UI::Page;
using WifiInfo = wifiHandlerInterface::WifiInfo;
WifiSettings::WifiSettings(std::shared_ptr<wifiHandlerInterface> aWifi)
: Base(ID::Pages::WifiSettings), mWifi(aWifi),
mScanCompleteHandler(mWifi->ScanCompleteNotification()),
mScanStatusHandler(mWifi->WifiStatusNotification()),
mScanningText(AddElement<Widget::Label>(
std::make_unique<Widget::Label>("Scanning..."))),
mWifiNetworks(AddElement<Widget::List>(std::make_unique<Widget::List>())),
@ -27,6 +29,8 @@ WifiSettings::WifiSettings(std::shared_ptr<wifiHandlerInterface> aWifi)
[this, wifiInfo](auto aUserEnteredPassword) {
// Attempt Connection when user finishes up with keyboard input
mWifi->connect(wifiInfo.ssid, aUserEnteredPassword);
mScanningText->SetText("Attempting Connection to " +
wifiInfo.ssid);
mPasswordGetter->AnimateOut();
});
keyboard->OnKeyboardAnimatedOut([this] {
@ -41,6 +45,14 @@ WifiSettings::WifiSettings(std::shared_ptr<wifiHandlerInterface> aWifi)
}
};
mScanStatusHandler = [this](auto aWifiStatus) {
if (aWifiStatus.isConnected) {
mScanningText->SetText("Connected to " + aWifiStatus.ssid);
} else {
mScanningText->SetText("Failed To Connect To" + aWifiStatus.ssid);
}
};
mWifi->scan();
}

View file

@ -20,6 +20,7 @@ public:
private:
std::shared_ptr<wifiHandlerInterface> mWifi;
Handler<wifiHandlerInterface::ScanDoneDataTy> mScanCompleteHandler;
Handler<wifiHandlerInterface::wifiStatus> mScanStatusHandler;
UI::Widget::Label *mScanningText;
UI::Widget::List *mWifiNetworks;

View file

@ -91,11 +91,17 @@ public:
void StartLvglEventHandler();
void StopLvglEventHandler();
protected:
/// @brief get Lvgl object refernce to use in LVGL APIs
/// @brief Register a callback to run for Lvgl Events for objects that
/// are created from base classes.
void OnLvglEvent(std::function<void(lv_event_t *anEvent)> aLvglEventHandler) {
mLvglEventHandler = aLvglEventHandler;
}
/// @brief get Lvgl object reference to use in LVGL APIs
/// @return lvgl object a
lv_obj_t *LvglSelf() { return mLvglSelf; }
protected:
/// @brief Show Element
virtual void Show();
/// @brief Hide Element
@ -116,12 +122,6 @@ protected:
}
};
/// @brief Register a callback to run for Lvgl Events for objects that
/// are created from base classes.
void OnLvglEvent(std::function<void(lv_event_t *anEvent)> aLvglEventHandler) {
mLvglEventHandler = aLvglEventHandler;
}
/// @brief Set KeyEvent to the UI element to see if it wants to handle it
virtual bool KeyEvent(KeyPressAbstract::KeyEvent aKeyEvent);

View file

@ -14,7 +14,7 @@ Keyboard::Keyboard(std::function<void(std::string)> aOnUserCompletedTextEntry)
mOnUserCompleteTextEntry(aOnUserCompletedTextEntry) {
lv_keyboard_set_textarea(mKeyboard->LvglSelf(), mTextArea->LvglSelf());
mKeyboard->HandleLvglEvent([this](auto aEvent) {
mKeyboard->OnLvglEvent([this](auto aEvent) {
if (aEvent->code == LV_EVENT_READY) {
std::string userEnteredText =
std::string(lv_textarea_get_text(mTextArea->LvglSelf()));