Fine Tune Wifi connection experience
This commit is contained in:
parent
cf19cfd1c1
commit
1ee9dcf43f
7 changed files with 37 additions and 22 deletions
|
@ -13,7 +13,7 @@ std::shared_ptr<wifiHandler> wifiHandler::getInstance() {
|
||||||
return mInstance;
|
return mInstance;
|
||||||
};
|
};
|
||||||
|
|
||||||
void wifiHandler::WiFiEvent(WiFiEvent_t event) {
|
void wifiHandler::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t aEventInfo) {
|
||||||
int no_networks = 0;
|
int no_networks = 0;
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case ARDUINO_EVENT_WIFI_SCAN_DONE: {
|
case ARDUINO_EVENT_WIFI_SCAN_DONE: {
|
||||||
|
@ -30,12 +30,14 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event) {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
|
||||||
|
StoreCredentials();
|
||||||
|
WiFi.setAutoConnect(true);
|
||||||
|
UpdateStatus();
|
||||||
|
break;
|
||||||
|
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
|
||||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
|
||||||
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
|
case ARDUINO_EVENT_WIFI_STA_GOT_IP6:
|
||||||
this->StoreCredentials();
|
|
||||||
break;
|
|
||||||
case ARDUINO_EVENT_WIFI_STA_CONNECTED:
|
|
||||||
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
|
|
||||||
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
|
case ARDUINO_EVENT_WIFI_STA_LOST_IP:
|
||||||
case ARDUINO_EVENT_WIFI_STA_STOP:
|
case ARDUINO_EVENT_WIFI_STA_STOP:
|
||||||
UpdateStatus();
|
UpdateStatus();
|
||||||
|
@ -43,16 +45,14 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event) {
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (WiFi.status() == WL_CONNECT_FAILED) {
|
|
||||||
WiFi.disconnect();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiHandler::UpdateStatus() {
|
void wifiHandler::UpdateStatus() {
|
||||||
Serial.println("UpdateStatus");
|
Serial.println("UpdateStatus");
|
||||||
mCurrentStatus.isConnected = WiFi.isConnected();
|
mCurrentStatus.isConnected = WiFi.isConnected();
|
||||||
mCurrentStatus.IP = WiFi.localIP().toString().c_str();
|
mCurrentStatus.IP = WiFi.localIP().toString().c_str();
|
||||||
mCurrentStatus.ssid = WiFi.SSID().c_str();
|
mCurrentStatus.ssid =
|
||||||
|
mCurrentStatus.isConnected ? WiFi.SSID().c_str() : mConnectionAttemptSSID;
|
||||||
|
|
||||||
mStatusUpdate->notify(mCurrentStatus);
|
mStatusUpdate->notify(mCurrentStatus);
|
||||||
}
|
}
|
||||||
|
@ -78,14 +78,16 @@ void wifiHandler::StoreCredentials() {
|
||||||
|
|
||||||
void wifiHandler::scan() {
|
void wifiHandler::scan() {
|
||||||
Serial.println("scan called");
|
Serial.println("scan called");
|
||||||
|
WiFi.setAutoReconnect(false);
|
||||||
WiFi.scanNetworks(true);
|
WiFi.scanNetworks(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiHandler::begin() {
|
void wifiHandler::begin() {
|
||||||
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, WiFiEventInfo_t aEventInfo) {
|
||||||
|
mInstance->WiFiEvent(event, aEventInfo);
|
||||||
|
});
|
||||||
|
|
||||||
Preferences preferences;
|
Preferences preferences;
|
||||||
preferences.begin("wifiSettings", false);
|
preferences.begin("wifiSettings", false);
|
||||||
|
@ -95,7 +97,7 @@ void wifiHandler::begin() {
|
||||||
|
|
||||||
// Attempt Connection with stored Credentials
|
// Attempt Connection with stored Credentials
|
||||||
if (!ssid.isEmpty()) {
|
if (!ssid.isEmpty()) {
|
||||||
connect(mSSID, mPassword);
|
connect(ssid.c_str(), password.c_str());
|
||||||
} else {
|
} else {
|
||||||
Serial.println("no SSID or password stored");
|
Serial.println("no SSID or password stored");
|
||||||
WiFi.disconnect();
|
WiFi.disconnect();
|
||||||
|
@ -105,9 +107,10 @@ void wifiHandler::begin() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void wifiHandler::connect(std::string ssid, std::string password) {
|
void wifiHandler::connect(std::string ssid, std::string password) {
|
||||||
Serial.printf("Attempting Wifi Connection To %s \n", mSSID.c_str());
|
Serial.printf("Attempting Wifi Connection To %s \n", ssid.c_str());
|
||||||
mIsConnectionAttempt = true;
|
mIsConnectionAttempt = true;
|
||||||
mConnectionAttemptPassword = password;
|
mConnectionAttemptPassword = password;
|
||||||
mConnectionAttemptSSID = ssid;
|
mConnectionAttemptSSID = ssid;
|
||||||
WiFi.begin(ssid.c_str(), password.c_str());
|
auto status = WiFi.begin(mConnectionAttemptSSID.c_str(),
|
||||||
|
mConnectionAttemptPassword.c_str());
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ private:
|
||||||
* @brief Handler for incoming arduino wifi events
|
* @brief Handler for incoming arduino wifi events
|
||||||
* @param event - a Wifi event
|
* @param event - a Wifi event
|
||||||
*/
|
*/
|
||||||
void WiFiEvent(WiFiEvent_t event);
|
void WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t aEventInfo);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Update Internal status and send out a notification
|
* @brief Update Internal status and send out a notification
|
||||||
|
|
|
@ -33,7 +33,9 @@ WifiSettings::WifiSettings(std::shared_ptr<wifiHandlerInterface> aWifi)
|
||||||
mScanningText->SetText("Attempting Connection to " +
|
mScanningText->SetText("Attempting Connection to " +
|
||||||
wifiInfo.ssid);
|
wifiInfo.ssid);
|
||||||
mPasswordGetter->AnimateOut();
|
mPasswordGetter->AnimateOut();
|
||||||
});
|
StartHandlingStatusUpdates();
|
||||||
|
},
|
||||||
|
"Password:");
|
||||||
keyboard->OnKeyboardAnimatedOut([this] {
|
keyboard->OnKeyboardAnimatedOut([this] {
|
||||||
// Once keyboard is done animating out remove it and null the ref to
|
// Once keyboard is done animating out remove it and null the ref to
|
||||||
// it.
|
// it.
|
||||||
|
@ -46,21 +48,24 @@ WifiSettings::WifiSettings(std::shared_ptr<wifiHandlerInterface> aWifi)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
mWifi->scan();
|
||||||
|
}
|
||||||
|
|
||||||
|
void WifiSettings::StartHandlingStatusUpdates() {
|
||||||
mScanStatusHandler = [this](auto aWifiStatus) {
|
mScanStatusHandler = [this](auto aWifiStatus) {
|
||||||
if (aWifiStatus.isConnected) {
|
if (aWifiStatus.isConnected) {
|
||||||
mScanningText->SetText("Connected to " + aWifiStatus.ssid);
|
mScanningText->SetText("Connected to " + aWifiStatus.ssid);
|
||||||
} else {
|
} else {
|
||||||
mScanningText->SetText("Failed To Connect To" + aWifiStatus.ssid);
|
mScanningText->SetText("Failed To Connect To " + aWifiStatus.ssid);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
mWifi->scan();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void WifiSettings::SetHeight(lv_coord_t aHeight) {
|
void WifiSettings::SetHeight(lv_coord_t aHeight) {
|
||||||
Base::SetHeight(aHeight);
|
Base::SetHeight(aHeight);
|
||||||
mScanningText->AlignTo(this, LV_ALIGN_TOP_MID);
|
mScanningText->AlignTo(this, LV_ALIGN_TOP_MID);
|
||||||
mScanningText->SetHeight(20);
|
mScanningText->SetHeight(20);
|
||||||
|
mScanningText->SetLongMode(LV_LABEL_LONG_SCROLL);
|
||||||
const auto padding = 10;
|
const auto padding = 10;
|
||||||
mWifiNetworks->AlignTo(mScanningText, LV_ALIGN_OUT_BOTTOM_MID, 0, padding);
|
mWifiNetworks->AlignTo(mScanningText, LV_ALIGN_OUT_BOTTOM_MID, 0, padding);
|
||||||
mWifiNetworks->SetHeight(GetContentHeight() - mScanningText->GetBottom() -
|
mWifiNetworks->SetHeight(GetContentHeight() - mScanningText->GetBottom() -
|
||||||
|
|
|
@ -17,6 +17,9 @@ public:
|
||||||
|
|
||||||
void SetHeight(lv_coord_t aHeight) override;
|
void SetHeight(lv_coord_t aHeight) override;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void StartHandlingStatusUpdates();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<wifiHandlerInterface> mWifi;
|
std::shared_ptr<wifiHandlerInterface> mWifi;
|
||||||
Handler<wifiHandlerInterface::ScanDoneDataTy> mScanCompleteHandler;
|
Handler<wifiHandlerInterface::ScanDoneDataTy> mScanCompleteHandler;
|
||||||
|
|
|
@ -5,7 +5,8 @@
|
||||||
using namespace UI;
|
using namespace UI;
|
||||||
using namespace UI::Widget;
|
using namespace UI::Widget;
|
||||||
|
|
||||||
Keyboard::Keyboard(std::function<void(std::string)> aOnUserCompletedTextEntry)
|
Keyboard::Keyboard(std::function<void(std::string)> aOnUserCompletedTextEntry,
|
||||||
|
std::string aPrompt)
|
||||||
: Base(ID::Widgets::Keyboard),
|
: Base(ID::Widgets::Keyboard),
|
||||||
mKeyboard(AddElement<Base>(std::make_unique<Base>(
|
mKeyboard(AddElement<Base>(std::make_unique<Base>(
|
||||||
lv_keyboard_create(LvglSelf()), ID::Widgets::INVALID_WIDGET_ID))),
|
lv_keyboard_create(LvglSelf()), ID::Widgets::INVALID_WIDGET_ID))),
|
||||||
|
@ -13,6 +14,9 @@ Keyboard::Keyboard(std::function<void(std::string)> aOnUserCompletedTextEntry)
|
||||||
lv_textarea_create(LvglSelf()), ID::Widgets::INVALID_WIDGET_ID))),
|
lv_textarea_create(LvglSelf()), ID::Widgets::INVALID_WIDGET_ID))),
|
||||||
mOnUserCompleteTextEntry(aOnUserCompletedTextEntry) {
|
mOnUserCompleteTextEntry(aOnUserCompletedTextEntry) {
|
||||||
lv_keyboard_set_textarea(mKeyboard->LvglSelf(), mTextArea->LvglSelf());
|
lv_keyboard_set_textarea(mKeyboard->LvglSelf(), mTextArea->LvglSelf());
|
||||||
|
if (!aPrompt.empty()) {
|
||||||
|
lv_textarea_set_placeholder_text(mTextArea->LvglSelf(), aPrompt.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
mKeyboard->OnLvglEvent([this](auto aEvent) {
|
mKeyboard->OnLvglEvent([this](auto aEvent) {
|
||||||
if (aEvent->code == LV_EVENT_READY) {
|
if (aEvent->code == LV_EVENT_READY) {
|
||||||
|
|
|
@ -8,7 +8,8 @@ namespace UI::Widget {
|
||||||
|
|
||||||
class Keyboard : public Base {
|
class Keyboard : public Base {
|
||||||
public:
|
public:
|
||||||
Keyboard(std::function<void(std::string)> aOnUserCompletedTextEntry);
|
Keyboard(std::function<void(std::string)> aOnUserCompletedTextEntry,
|
||||||
|
std::string aPrompt = "");
|
||||||
|
|
||||||
void OnAdded(UIElement *aNewParent) override;
|
void OnAdded(UIElement *aNewParent) override;
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,6 @@ Label::Label(std::string aText)
|
||||||
void Label::SetText(std::string aText) {
|
void Label::SetText(std::string aText) {
|
||||||
auto lock = LvglResourceManager::GetInstance().scopeLock();
|
auto lock = LvglResourceManager::GetInstance().scopeLock();
|
||||||
lv_label_set_text(LvglSelf(), aText.c_str());
|
lv_label_set_text(LvglSelf(), aText.c_str());
|
||||||
SetLongMode(LV_LABEL_LONG_SCROLL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Label::SetLongMode(lv_label_long_mode_t aLongMode) {
|
void Label::SetLongMode(lv_label_long_mode_t aLongMode) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue