Skip to content

Commit

Permalink
Add Wifi Settings Ability to enter password and attempt connection
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewColvin committed Oct 17, 2023
1 parent 73b9e7d commit cf646f2
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 13 deletions.
34 changes: 24 additions & 10 deletions Platformio/OmoteUI/UIs/BasicRefactored/page/WifiSettings.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions Platformio/OmoteUI/UIs/BasicRefactored/page/WifiSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace UI::Widget {
class List;
class Label;
class Keyboard;
} // namespace UI::Widget

namespace UI::Page {
Expand All @@ -22,6 +23,7 @@ class WifiSettings : public Base {

UI::Widget::Label *mScanningText;
UI::Widget::List *mWifiNetworks;
UI::Widget::Keyboard *mPasswordGetter;
};

} // namespace UI::Page
22 changes: 19 additions & 3 deletions Platformio/OmoteUI/core/Animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
8 changes: 8 additions & 0 deletions Platformio/OmoteUI/core/Animation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,19 @@ class Animation {

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);
};
Expand Down
1 change: 1 addition & 0 deletions Platformio/OmoteUI/core/UIElementIds.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ID {
Label,
List,
DropDown,
Keyboard,
BrightnessSlider,
INVALID_WIDGET_ID
};
Expand Down
53 changes: 53 additions & 0 deletions Platformio/OmoteUI/core/widget/Keyboard.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
31 changes: 31 additions & 0 deletions Platformio/OmoteUI/core/widget/Keyboard.hpp
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit cf646f2

Please sign in to comment.