Skip to content

Commit

Permalink
Add start stop event handling in UIElement
Browse files Browse the repository at this point in the history
Add demo page to get play logic out of settings page.

Add a demo page and a Settings page to the home screen.

Change-Id: I22274df5d64fae9b3212a59aa7da64fbefa1f222
  • Loading branch information
Matthew Colvin authored and Matthew Colvin committed Oct 12, 2023
1 parent 7d5a1e0 commit c02bac3
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 61 deletions.
54 changes: 54 additions & 0 deletions Platformio/OmoteUI/UIs/BasicRefactored/page/Demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "Demo.hpp"
#include "Slider.hpp"

using namespace UI::Page;

Demo::Demo(std::shared_ptr<HardwareAbstract> aHardware): Base(ID::Pages::Demo), mHardware(aHardware){


}

void Demo::AddSlider() {
auto fakeSlider = std::make_unique<Widget::Slider>([](auto data){});
fakeSlider->SetHeight(lv_pct(10));
fakeSlider->SetWidth(GetContentWidth());
if (sliders.empty()) {
fakeSlider->AlignTo(this,LV_ALIGN_TOP_MID);
} else {
auto nextY = sliders.back()->GetY() + sliders.back()->GetHeight();
fakeSlider->SetY(nextY + 10);
}

sliders.push_back(AddElement(std::move(fakeSlider)));
}

bool Demo::OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) {
using id = KeyPressAbstract::KeyId;
using eventType = KeyPressAbstract::KeyEvent::Type;
bool used = true;
switch (aKeyEvent.mId) {
case id::Aux1:
if (aKeyEvent.mType == eventType::Press) {
AddSlider();
}
break;
case id::Aux2:
if (aKeyEvent.mType == eventType::Release) {
if (sliders.size() > 0) {
RemoveElement(sliders[0]);
sliders.erase(
sliders.begin()); // sliders is non owning so after removing delete
// it from non owning array
}
}
break;
case id::Aux3:
break;
case id::Aux4:
break;
default:
used = Page::Base::OnKeyEvent(aKeyEvent);
break;
}
return used;
}
22 changes: 22 additions & 0 deletions Platformio/OmoteUI/UIs/BasicRefactored/page/Demo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include "PageBase.hpp"
#include "HardwareAbstract.hpp"

namespace UI::Page{

class Demo : public Base{
public:
Demo(std::shared_ptr<HardwareAbstract> aHardware);

void AddSlider();

void OnShow()override{};
void OnHide()override{};
bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent);

private:
std::shared_ptr<HardwareAbstract> mHardware;
std::vector<UIElement *> sliders;
};

}
61 changes: 6 additions & 55 deletions Platformio/OmoteUI/UIs/BasicRefactored/page/SettingsPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "BackgroundScreen.hpp"
#include "Button.hpp"
#include "Slider.hpp"
#include "List.hpp"
#include "Colors.hpp"
#include "DisplaySettings.hpp"
#include "PopUpScreen.hpp"
Expand All @@ -11,66 +12,16 @@ using namespace UI::Page;
using namespace UI::Color;

SettingsPage::SettingsPage(std::shared_ptr<HardwareAbstract> aHardware)
: Base(ID::Pages::Settings), mHardware(aHardware) {
SetBgColor(RED);
auto button =
std::make_unique<UI::Widget::Button>([this] { PushDisplaySettings(); });
button->SetBorder(button->GetBorder().Color(BLUE).Width(2));
button->SetY(0);
button->SetHeight(lv_pct(10));
button->SetWidth(lv_pct(10));
: Base(ID::Pages::Settings),
mSettingsList(AddElement<Widget::List>(std::make_unique<Widget::List>())),
mHardware(aHardware) {

mButton = AddElement<Widget::Button>(std::move(button));
mSettingsList->AddItem("Display",LV_SYMBOL_EYE_OPEN,[this] { PushDisplaySettings(); });
mSettingsList->AddItem("Wifi",LV_SYMBOL_WIFI,[]{});
}

void SettingsPage::OnShow() {}

void SettingsPage::PushDisplaySettings() {
UI::Screen::Manager::getInstance().pushPopUp(
std::make_unique<DisplaySettings>(mHardware->display()));
}

void SettingsPage::AddSlider() {
auto fakeSlider = std::make_unique<Widget::Slider>([](auto data){});
fakeSlider->SetHeight(lv_pct(10));
fakeSlider->SetWidth(GetContentWidth());
if (sliders.empty()) {
fakeSlider->SetY(mButton->GetBottom());
} else {
auto nextY = sliders.back()->GetY() + sliders.back()->GetHeight();
fakeSlider->SetY(nextY + 10);
}

sliders.push_back(AddElement(std::move(fakeSlider)));
}

bool SettingsPage::OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) {
using id = KeyPressAbstract::KeyId;
using eventType = KeyPressAbstract::KeyEvent::Type;
bool used = true;
switch (aKeyEvent.mId) {
case id::Aux1:
if (aKeyEvent.mType == eventType::Press) {
AddSlider();
}
break;
case id::Aux2:
if (aKeyEvent.mType == eventType::Release) {
if (sliders.size() > 0) {
RemoveElement(sliders[0]);
sliders.erase(
sliders.begin()); // sliders is non owning so after removing delete
// it from non owning array
}
}
break;
case id::Aux3:
break;
case id::Aux4:
break;
default:
used = Page::Base::OnKeyEvent(aKeyEvent);
break;
}
return used;
}
8 changes: 4 additions & 4 deletions Platformio/OmoteUI/UIs/BasicRefactored/page/SettingsPage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@

namespace UI::Widget{
class Button;
class List;
}
namespace UI::Page {
class SettingsPage : public Base {
public:
SettingsPage(std::shared_ptr<HardwareAbstract> aHardware = nullptr);

bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override;
bool OnKeyEvent(KeyPressAbstract::KeyEvent aKeyEvent) override{return false;};

void AddSlider();
void PushDisplaySettings();

protected:
void OnShow() override;
void OnShow() override{};
void OnHide() override{};

std::vector<UIElement *> sliders;
Widget::Button *mButton;
Widget::List *mSettingsList;
std::shared_ptr<HardwareAbstract> mHardware;
};
} // namespace UI::Page
3 changes: 2 additions & 1 deletion Platformio/OmoteUI/UIs/BasicRefactored/screen/HomeScreen.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "HomeScreen.hpp"
#include "Colors.hpp"
#include "SettingsPage.hpp"
#include "Demo.hpp"

using namespace UI::Screen;

Expand All @@ -15,7 +16,7 @@ HomeScreen::HomeScreen(std::shared_ptr<HardwareAbstract> aHardware)

// Adds pages to the Tab view
mTabView->AddTab(std::make_unique<Page::SettingsPage>(aHardware));
mTabView->AddTab(std::make_unique<Page::SettingsPage>(aHardware));
mTabView->AddTab(std::make_unique<Page::Demo>(aHardware));
}

void HomeScreen::SetBgColor(lv_color_t value, lv_style_selector_t selector) {
Expand Down
13 changes: 13 additions & 0 deletions Platformio/OmoteUI/core/UIElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,19 @@ void UIElement::SetBgOpacity(lv_opa_t aOpacity, lv_style_selector_t aStyle) {
});
}

void UIElement::StartLvglEventHandler(){
if(mIsHandlingLvglEvents){ return; }
lv_obj_add_event_cb(mLvglSelf, UIElement::LvglEventHandler, LV_EVENT_ALL,
this);
mIsHandlingLvglEvents = true;
}
void UIElement::StopLvglEventHandler(){
if(!mIsHandlingLvglEvents){return;}
lv_obj_remove_event_cb_with_user_data(mLvglSelf,UIElement::LvglEventHandler,
this);
mIsHandlingLvglEvents = false;
}

void UIElement::Show() {
if (IsVisible()) {
return;
Expand Down
7 changes: 6 additions & 1 deletion Platformio/OmoteUI/core/UIElement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,16 @@ class UIElement {
/// @brief There are use cases in which objects
/// need to stay alive in LVGL but can die
/// in terms of our usage this is a helper for these
/// use Sparengly!!!
/// use Sparingly!!!
/// @param aTimeToKeepLvglObj
void SetKeepAliveTime(uint32_t aTimeToKeepLvglObj) {
mLvglKeepAliveTime = aTimeToKeepLvglObj;
}


void StartLvglEventHandler();
void StopLvglEventHandler();

protected:
/// @brief get Lvgl object refernce to use in LVGL APIs
/// @return lvgl object a
Expand Down Expand Up @@ -118,6 +122,7 @@ class UIElement {
lv_obj_t *mLvglSelf;
const ID mId;
uint32_t mLvglKeepAliveTime = 0;
bool mIsHandlingLvglEvents = true;

/// @brief Elements that are currently in this element
std::vector<UIElement::Ptr> mContainedElements;
Expand Down
2 changes: 2 additions & 0 deletions Platformio/OmoteUI/core/UIElementIds.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ class ID {
Slider = static_cast<int>(Screens::INVALID_SCREEN_ID) + 1,
Button,
Label,
List,
BrightnessSlider,
INVALID_WIDGET_ID
};

enum class Pages {
Settings = static_cast<int>(Widgets::INVALID_WIDGET_ID) + 1,
DisplaySettings,
Demo,
INVALID_PAGE_ID
};

Expand Down
32 changes: 32 additions & 0 deletions Platformio/OmoteUI/core/widget/List.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "List.hpp"
#include "BackgroundScreen.hpp"
using namespace UI;
using namespace UI::Widget;

ListItem::ListItem(lv_obj_t *aListItem, std::function<void()> onItemSelected):
UIElement(aListItem,ID()),
mSelectedHandler(std::move(onItemSelected)){}

void ListItem::OnLvglEvent(lv_event_t* anEvent){
if(anEvent->code == LV_EVENT_CLICKED){
if(mSelectedHandler){
mSelectedHandler();
}
}
}

List::List():Base(lv_list_create(Screen::BackgroundScreen::getLvInstance()),ID::Widgets::List){
StopLvglEventHandler();
}

void List::AddItem(std::string aTitle, const char* aSymbol, std::function<void()> onItemSelected){
auto lvListItem = lv_list_add_btn(LvglSelf(),aSymbol,aTitle.c_str());
mListItems.push_back(std::make_unique<ListItem>(lvListItem, std::move(onItemSelected)));
mListItems.back()->SetHeight(lv_pct(20));
}

void List::OnLvglEvent(lv_event_t* anEvent){
if(anEvent->code == LV_EVENT_CLICKED){
auto tgt = anEvent->target;
}
}
30 changes: 30 additions & 0 deletions Platformio/OmoteUI/core/widget/List.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include "WidgetBase.hpp"

namespace UI::Widget{

class ListItem : public UIElement{
public:
ListItem(lv_obj_t* aListItem, std::function<void()> onItemSelected);
protected:
void OnLvglEvent(lv_event_t* anEvent) override;
bool OnKeyEvent(KeyPressAbstract::KeyEvent anEvent)override{return false;};
void OnShow()override{};
void OnHide()override{};
private:
std::function<void()> mSelectedHandler;
};

class List : public Base{
public:
List();
void AddItem(std::string aTitle, const char* aSymbol, std::function<void()> onItemSelected);

protected:
void OnLvglEvent(lv_event_t* anEvent)override;

private:
std::vector<UIElement::Ptr> mListItems;
};

}

0 comments on commit c02bac3

Please sign in to comment.