Add Wifi Settings Ability to enter password and attempt connection

This commit is contained in:
MatthewColvin 2023-10-17 17:05:16 -05:00
parent 73b9e7dbc7
commit cf646f21db
7 changed files with 138 additions and 13 deletions

View file

@ -1,8 +1,8 @@
#include "WifiSettings.hpp"
#include "Keyboard.hpp"
#include "Label.hpp"
#include "List.hpp"
#include "LvglResourceManager.hpp"
//#include <Arduino.h>
using namespace UI;
using namespace UI::Page;
@ -12,22 +12,36 @@ WifiSettings::WifiSettings(std::shared_ptr<wifiHandlerInterface> aWifi)
mScanCompleteHandler(mWifi->ScanCompleteNotification()),
mScanningText(AddElement<Widget::Label>(
std::make_unique<Widget::Label>("Scanning..."))),
mWifiNetworks(
AddElement<Widget::List>(std::make_unique<Widget::List>())) {
mWifiNetworks(AddElement<Widget::List>(std::make_unique<Widget::List>())),
mPasswordGetter(nullptr) {
// Set Handler for when the wifi scan is done
mScanCompleteHandler = [this](auto aWifiInfos) {
// Serial.println("populating UI");
mScanningText->SetText("Networks Found");
// Create List of wifi infos
for (WifiInfo wifiInfo : aWifiInfos) {
mWifiNetworks->AddItem(wifiInfo.ssid, LV_SYMBOL_WIFI, [] {});
mWifiNetworks->AddItem(wifiInfo.ssid, LV_SYMBOL_WIFI, [this, wifiInfo] {
if (!mPasswordGetter) {
// Launch a Keyboard if we dont already have one when user selects
// list item
auto keyboard = std::make_unique<Widget::Keyboard>(
[this, wifiInfo](auto aUserEnteredPassword) {
// Attempt Connection when user finishes up with keyboard input
mWifi->connect(wifiInfo.ssid, aUserEnteredPassword);
mPasswordGetter->AnimateOut();
});
keyboard->OnKeyboardAnimatedOut([this] {
// Once keyboard is done animating out remove it and null the ref to
// it.
RemoveElement(mPasswordGetter);
mPasswordGetter = nullptr;
});
mPasswordGetter = AddElement<Widget::Keyboard>(std::move(keyboard));
}
});
}
};
mWifi->scan();
// mWifi->onScanDone([this](auto aWifiInfos) {
//
// });
}
void WifiSettings::SetHeight(lv_coord_t aHeight) {

View file

@ -5,6 +5,7 @@
namespace UI::Widget {
class List;
class Label;
class Keyboard;
} // namespace UI::Widget
namespace UI::Page {
@ -22,6 +23,7 @@ private:
UI::Widget::Label *mScanningText;
UI::Widget::List *mWifiNetworks;
UI::Widget::Keyboard *mPasswordGetter;
};
} // namespace UI::Page

View file

@ -4,21 +4,37 @@ using namespace UI;
Animation::Animation(std::function<void(int32_t)> aAnimator,
int32_t aAnimationTime, int32_t aStart, int32_t aEnd)
: mAnimator(aAnimator) {
: mAnimator(aAnimator), mStart(aStart), mEnd(aEnd) {
lv_anim_init(&mAnimation);
mAnimation.user_data = this;
lv_anim_set_custom_exec_cb(&mAnimation, AnimatorImpl);
lv_anim_set_values(&mAnimation, aStart, aEnd);
lv_anim_set_time(&mAnimation, aAnimationTime);
}
Animation::~Animation() { lv_anim_custom_del(&mAnimation, AnimatorImpl); }
void Animation::Start() { lv_anim_start(&mAnimation); }
void Animation::HandleAnimationComplete(
std::function<void()> onAnimationComplete) {
mOnComplete = onAnimationComplete;
}
void Animation::Start() {
lv_anim_set_values(&mAnimation, mStart, mEnd);
lv_anim_start(&mAnimation);
}
void Animation::Reverse() {
std::swap(mStart, mEnd);
Start();
}
//////////////////////// Statics /////////////////////////////////////////
void Animation::AnimatorImpl(lv_anim_t *aSelf, int32_t aNextValue) {
auto self = reinterpret_cast<Animation *>(aSelf->user_data);
self->mAnimator(aNextValue);
// TODO This probably will not support Overshoot animations.
if (self->mOnComplete && aNextValue == self->mEnd) {
self->mOnComplete();
}
}

View file

@ -11,11 +11,19 @@ public:
virtual ~Animation();
void HandleAnimationComplete(std::function<void()> onAnimationComplete);
void Start();
void Reverse();
private:
lv_anim_t mAnimation;
std::function<void(int32_t)> mAnimator = nullptr;
std::function<void()> mOnComplete = nullptr;
bool onCompleteCalled = false;
int32_t mStart = 0;
int32_t mEnd = 0;
static void AnimatorImpl(lv_anim_t *aSelf, int32_t aNextValue);
};

View file

@ -19,6 +19,7 @@ public:
Label,
List,
DropDown,
Keyboard,
BrightnessSlider,
INVALID_WIDGET_ID
};

View file

@ -0,0 +1,53 @@
#include "Keyboard.hpp"
#include "BackgroundScreen.hpp"
#include <cmath>
using namespace UI;
using namespace UI::Widget;
Keyboard::Keyboard(std::function<void(std::string)> aOnUserCompletedTextEntry)
: Base(ID::Widgets::Keyboard),
mKeyboard(AddElement<Base>(std::make_unique<Base>(
lv_keyboard_create(LvglSelf()), ID::Widgets::INVALID_WIDGET_ID))),
mTextArea(AddElement<Base>(std::make_unique<Base>(
lv_textarea_create(LvglSelf()), ID::Widgets::INVALID_WIDGET_ID))),
mOnUserCompleteTextEntry(aOnUserCompletedTextEntry) {
lv_keyboard_set_textarea(mKeyboard->LvglSelf(), mTextArea->LvglSelf());
mKeyboard->HandleLvglEvent([this](auto aEvent) {
if (aEvent->code == LV_EVENT_READY) {
std::string userEnteredText =
std::string(lv_textarea_get_text(mTextArea->LvglSelf()));
mOnUserCompleteTextEntry(userEnteredText);
}
});
}
void Keyboard::OnAdded(UIElement *aNewParent) {
auto selfHeight = ceil(aNewParent->GetContentHeight() * 0.60f);
// Align to final position and get Y for end of animation
SetHeight(selfHeight);
AlignTo(aNewParent, LV_ALIGN_BOTTOM_MID);
auto endAnimationY = GetY();
auto startAnimationY = aNewParent->GetBottom();
mAnimateIn = std::make_unique<Animation>([this](auto aY) { SetY(aY); }, 500,
startAnimationY, endAnimationY);
mAnimateIn->Start();
}
void Keyboard::AnimateOut() {
if (mOnKeyboardAnimatedOut) {
mAnimateIn->HandleAnimationComplete(mOnKeyboardAnimatedOut);
}
mAnimateIn->Reverse();
};
void Keyboard::SetHeight(lv_coord_t aHeight) {
Base::SetHeight(aHeight);
auto txtAreaHight = 33;
mTextArea->SetHeight(txtAreaHight);
mKeyboard->SetHeight(GetContentHeight() - txtAreaHight);
mTextArea->AlignTo(this, LV_ALIGN_TOP_MID);
mKeyboard->AlignTo(mTextArea, LV_ALIGN_OUT_BOTTOM_MID);
}

View file

@ -0,0 +1,31 @@
#pragma once
#include "Animation.hpp"
#include "WidgetBase.hpp"
#include <memory>
#include <string>
namespace UI::Widget {
class Keyboard : public Base {
public:
Keyboard(std::function<void(std::string)> aOnUserCompletedTextEntry);
void OnAdded(UIElement *aNewParent) override;
void SetHeight(lv_coord_t aHeight) override;
void AnimateOut();
void OnKeyboardAnimatedOut(std::function<void()> aOnKeyboardAnimatedOut) {
mOnKeyboardAnimatedOut = aOnKeyboardAnimatedOut;
}
private:
std::function<void()> mOnKeyboardAnimatedOut;
std::function<void(std::string)> mOnUserCompleteTextEntry;
std::unique_ptr<Animation> mAnimateIn;
Base *mKeyboard;
Base *mTextArea;
};
} // namespace UI::Widget