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;
|
||||
};
|
||||
|
||||
void wifiHandler::WiFiEvent(WiFiEvent_t event) {
|
||||
void wifiHandler::WiFiEvent(WiFiEvent_t event, WiFiEventInfo_t aEventInfo) {
|
||||
int no_networks = 0;
|
||||
switch (event) {
|
||||
case ARDUINO_EVENT_WIFI_SCAN_DONE: {
|
||||
|
@ -30,12 +30,14 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event) {
|
|||
}
|
||||
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_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_STOP:
|
||||
UpdateStatus();
|
||||
|
@ -43,16 +45,14 @@ void wifiHandler::WiFiEvent(WiFiEvent_t event) {
|
|||
default:
|
||||
break;
|
||||
}
|
||||
if (WiFi.status() == WL_CONNECT_FAILED) {
|
||||
WiFi.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
void wifiHandler::UpdateStatus() {
|
||||
Serial.println("UpdateStatus");
|
||||
mCurrentStatus.isConnected = WiFi.isConnected();
|
||||
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);
|
||||
}
|
||||
|
@ -78,14 +78,16 @@ void wifiHandler::StoreCredentials() {
|
|||
|
||||
void wifiHandler::scan() {
|
||||
Serial.println("scan called");
|
||||
|
||||
WiFi.setAutoReconnect(false);
|
||||
WiFi.scanNetworks(true);
|
||||
}
|
||||
|
||||
void wifiHandler::begin() {
|
||||
WiFi.setHostname("OMOTE");
|
||||
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.begin("wifiSettings", false);
|
||||
|
@ -95,7 +97,7 @@ void wifiHandler::begin() {
|
|||
|
||||
// Attempt Connection with stored Credentials
|
||||
if (!ssid.isEmpty()) {
|
||||
connect(mSSID, mPassword);
|
||||
connect(ssid.c_str(), password.c_str());
|
||||
} else {
|
||||
Serial.println("no SSID or password stored");
|
||||
WiFi.disconnect();
|
||||
|
@ -105,9 +107,10 @@ void wifiHandler::begin() {
|
|||
}
|
||||
|
||||
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;
|
||||
mConnectionAttemptPassword = password;
|
||||
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
|
||||
* @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
|
||||
|
|
|
@ -33,7 +33,9 @@ WifiSettings::WifiSettings(std::shared_ptr<wifiHandlerInterface> aWifi)
|
|||
mScanningText->SetText("Attempting Connection to " +
|
||||
wifiInfo.ssid);
|
||||
mPasswordGetter->AnimateOut();
|
||||
});
|
||||
StartHandlingStatusUpdates();
|
||||
},
|
||||
"Password:");
|
||||
keyboard->OnKeyboardAnimatedOut([this] {
|
||||
// Once keyboard is done animating out remove it and null the ref to
|
||||
// it.
|
||||
|
@ -46,21 +48,24 @@ WifiSettings::WifiSettings(std::shared_ptr<wifiHandlerInterface> aWifi)
|
|||
}
|
||||
};
|
||||
|
||||
mWifi->scan();
|
||||
}
|
||||
|
||||
void WifiSettings::StartHandlingStatusUpdates() {
|
||||
mScanStatusHandler = [this](auto aWifiStatus) {
|
||||
if (aWifiStatus.isConnected) {
|
||||
mScanningText->SetText("Connected to " + aWifiStatus.ssid);
|
||||
} 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) {
|
||||
Base::SetHeight(aHeight);
|
||||
mScanningText->AlignTo(this, LV_ALIGN_TOP_MID);
|
||||
mScanningText->SetHeight(20);
|
||||
mScanningText->SetLongMode(LV_LABEL_LONG_SCROLL);
|
||||
const auto padding = 10;
|
||||
mWifiNetworks->AlignTo(mScanningText, LV_ALIGN_OUT_BOTTOM_MID, 0, padding);
|
||||
mWifiNetworks->SetHeight(GetContentHeight() - mScanningText->GetBottom() -
|
||||
|
|
|
@ -17,6 +17,9 @@ public:
|
|||
|
||||
void SetHeight(lv_coord_t aHeight) override;
|
||||
|
||||
protected:
|
||||
void StartHandlingStatusUpdates();
|
||||
|
||||
private:
|
||||
std::shared_ptr<wifiHandlerInterface> mWifi;
|
||||
Handler<wifiHandlerInterface::ScanDoneDataTy> mScanCompleteHandler;
|
||||
|
|
|
@ -5,7 +5,8 @@
|
|||
using namespace UI;
|
||||
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),
|
||||
mKeyboard(AddElement<Base>(std::make_unique<Base>(
|
||||
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))),
|
||||
mOnUserCompleteTextEntry(aOnUserCompletedTextEntry) {
|
||||
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) {
|
||||
if (aEvent->code == LV_EVENT_READY) {
|
||||
|
|
|
@ -8,7 +8,8 @@ namespace UI::Widget {
|
|||
|
||||
class Keyboard : public Base {
|
||||
public:
|
||||
Keyboard(std::function<void(std::string)> aOnUserCompletedTextEntry);
|
||||
Keyboard(std::function<void(std::string)> aOnUserCompletedTextEntry,
|
||||
std::string aPrompt = "");
|
||||
|
||||
void OnAdded(UIElement *aNewParent) override;
|
||||
|
||||
|
|
|
@ -14,7 +14,6 @@ Label::Label(std::string aText)
|
|||
void Label::SetText(std::string aText) {
|
||||
auto lock = LvglResourceManager::GetInstance().scopeLock();
|
||||
lv_label_set_text(LvglSelf(), aText.c_str());
|
||||
SetLongMode(LV_LABEL_LONG_SCROLL);
|
||||
}
|
||||
|
||||
void Label::SetLongMode(lv_label_long_mode_t aLongMode) {
|
||||
|
|
Loading…
Add table
Reference in a new issue