From 9645fbc278597e0827e948b9c81c24a17fe72269 Mon Sep 17 00:00:00 2001 From: Przemyslaw Brudny Date: Tue, 12 Jan 2021 18:05:24 +0100 Subject: [PATCH] [EDG-4717] Add screen for brightness control Window for manual and automatic screen brightness control. --- module-apps/Application.cpp | 43 ++-- .../ApplicationSettings.cpp | 60 ++++- .../ApplicationSettings.hpp | 25 +- .../application-settings-new/CMakeLists.txt | 3 +- .../widgets/OptionSetting.hpp | 30 +++ .../widgets/SpinBox.cpp | 93 ++++++++ .../widgets/SpinBox.hpp | 43 ++++ .../widgets/SpinBoxOptionSetting.cpp | 41 ++++ .../windows/BaseSettingsWindow.cpp | 1 - .../windows/BaseSettingsWindow.hpp | 1 + .../windows/DisplayLightWindow.cpp | 65 ++++-- .../windows/DisplayLightWindow.hpp | 18 +- .../windows/SimSelectWindow.hpp | 2 +- module-apps/options/type/OptionSetting.hpp | 2 +- module-apps/widgets/BarGraph.cpp | 159 +++++-------- module-apps/widgets/BarGraph.hpp | 26 +-- module-apps/windows/OptionWindow.cpp | 1 + module-apps/windows/OptionWindow.hpp | 1 - module-gui/gui/widgets/ProgressBar.cpp | 14 +- module-gui/gui/widgets/ProgressBar.hpp | 12 +- .../agents/settings/SystemSettings.hpp | 7 + module-services/service-evtmgr/CMakeLists.txt | 4 +- .../service-evtmgr/EventManager.cpp | 33 +-- .../service-evtmgr/WorkerEvent.cpp | 4 +- .../doc/screen_light_control.md | 3 +- .../screen-light-control/ControlFunctions.cpp | 4 +- .../screen-light-control/ControlFunctions.hpp | 4 +- .../ScreenLightControl.cpp | 218 ++++++++++++------ .../ScreenLightControl.hpp | 73 +++++- .../service-evtmgr/EVMessages.hpp | 13 -- .../service-evtmgr/EventManager.hpp | 3 + .../ScreenLightControlMessage.hpp | 55 +++++ .../test-ScreenLightControlFunctions.cpp | 7 +- source/MessageType.hpp | 9 +- 34 files changed, 775 insertions(+), 302 deletions(-) create mode 100644 module-apps/application-settings-new/widgets/OptionSetting.hpp create mode 100644 module-apps/application-settings-new/widgets/SpinBox.cpp create mode 100644 module-apps/application-settings-new/widgets/SpinBox.hpp create mode 100644 module-apps/application-settings-new/widgets/SpinBoxOptionSetting.cpp create mode 100644 module-services/service-evtmgr/service-evtmgr/ScreenLightControlMessage.hpp diff --git a/module-apps/Application.cpp b/module-apps/Application.cpp index fe303d9590322d4189d076175beb8392de9ed4fd..091fd9ef5e400ce4545429c3b46d5346b9ad84b9 100644 --- a/module-apps/Application.cpp +++ b/module-apps/Application.cpp @@ -2,31 +2,31 @@ // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "Application.hpp" -#include "Common.hpp" // for RefreshModes -#include "GuiTimer.hpp" // for GuiTimer -#include "Item.hpp" // for Item -#include "MessageType.hpp" // for MessageType -#include "Service/Timer.hpp" // for Timer -#include "Timer.hpp" // for Timer -#include "Translator.hpp" // for KeyInputSim... -#include "common_data/EventStore.hpp" // for Battery -#include "common_data/RawKey.hpp" // for RawKey, key... -#include "gui/input/InputEvent.hpp" // for InputEvent -#include "log/debug.hpp" // for DEBUG_APPLI... -#include "log/log.hpp" // for LOG_INFO -#include "messages/AppMessage.hpp" // for AppSwitchMe... -#include "service-appmgr/Controller.hpp" // for Controller +#include "Common.hpp" // for RefreshModes +#include "GuiTimer.hpp" // for GuiTimer +#include "Item.hpp" // for Item +#include "MessageType.hpp" // for MessageType +#include "Service/Timer.hpp" // for Timer +#include "Timer.hpp" // for Timer +#include "Translator.hpp" // for KeyInputSim... +#include "common_data/EventStore.hpp" // for Battery +#include "common_data/RawKey.hpp" // for RawKey, key... +#include "gui/input/InputEvent.hpp" // for InputEvent +#include "log/debug.hpp" // for DEBUG_APPLI... +#include "log/log.hpp" // for LOG_INFO +#include "messages/AppMessage.hpp" // for AppSwitchMe... +#include "service-appmgr/Controller.hpp" // for Controller #include #include #include #include -#include "service-gui/messages/DrawMessage.hpp" // for DrawMessage -#include "task.h" // for xTaskGetTic... -#include "windows/AppWindow.hpp" // for AppWindow -#include // for Text -#include // for find -#include // for distance, next -#include // for add_const<>... +#include "service-gui/messages/DrawMessage.hpp" // for DrawMessage +#include "task.h" // for xTaskGetTic... +#include "windows/AppWindow.hpp" // for AppWindow +#include // for Text +#include // for find +#include // for distance, next +#include // for add_const<>... #include #include #include @@ -104,7 +104,6 @@ namespace app state = st; } - void Application::longPressTimerCallback() { // TODO if(check widget type long press trigger) diff --git a/module-apps/application-settings-new/ApplicationSettings.cpp b/module-apps/application-settings-new/ApplicationSettings.cpp index 056a1dcd77cc6a0c2bf71bfb413e2fda1b779596..71a35cfbbe3a57983275054a08fcdb557879e797 100644 --- a/module-apps/application-settings-new/ApplicationSettings.cpp +++ b/module-apps/application-settings-new/ApplicationSettings.cpp @@ -30,16 +30,21 @@ #include "Dialog.hpp" #include -#include #include -#include -#include +#include +#include #include #include #include +#include #include #include #include +#include + +#include +#include +#include namespace app { @@ -188,7 +193,7 @@ namespace app return std::make_unique(app); }); windowsFactory.attach(gui::window::name::display_light, [](Application *app, const std::string &name) { - return std::make_unique(app); + return std::make_unique(app, static_cast(app)); }); windowsFactory.attach(gui::window::name::apps_and_tools, [](Application *app, const std::string &name) { return std::make_unique(app); @@ -292,4 +297,51 @@ namespace app lockPassHash = value; settings->setValue(::settings::SystemProperties::lockPassHash, std::to_string(value)); } + + auto ApplicationSettingsNew::getCurrentValues() -> settingsInterface::ScreenLightSettings::Values + { + constexpr int timeout = pdMS_TO_TICKS(1500); + + auto response = sys::Bus::SendUnicast( + std::make_shared(), service::name::evt_manager, this, timeout); + + if (response.first == sys::ReturnCodes::Success) { + auto msgState = dynamic_cast(response.second.get()); + if (msgState == nullptr) { + return {}; + } + + return {msgState->lightOn, msgState->mode, msgState->parameters}; + } + + return {}; + } + + void ApplicationSettingsNew::setBrightness(bsp::eink_frontlight::BrightnessPercentage value) + { + screen_light_control::Parameters parameters{value}; + sys::Bus::SendUnicast(std::make_shared( + screen_light_control::Action::setManualModeBrightness, parameters), + service::name::evt_manager, + this); + } + + void ApplicationSettingsNew::setMode(bool isAutoLightSwitchOn) + { + sys::Bus::SendUnicast(std::make_shared( + isAutoLightSwitchOn ? screen_light_control::Action::enableAutomaticMode + : screen_light_control::Action::disableAutomaticMode), + service::name::evt_manager, + this); + } + + void ApplicationSettingsNew::setStatus(bool isDisplayLightSwitchOn) + { + sys::Bus::SendUnicast( + std::make_shared( + isDisplayLightSwitchOn ? screen_light_control::Action::turnOn : screen_light_control::Action::turnOff), + service::name::evt_manager, + this); + } + } /* namespace app */ diff --git a/module-apps/application-settings-new/ApplicationSettings.hpp b/module-apps/application-settings-new/ApplicationSettings.hpp index 20ab914b3a7200c41874695de116b1b1cdbe0710..9aa536e9066afb799f217baf5f96bb006b927ace 100644 --- a/module-apps/application-settings-new/ApplicationSettings.hpp +++ b/module-apps/application-settings-new/ApplicationSettings.hpp @@ -6,6 +6,7 @@ #include "Application.hpp" #include +#include namespace gui::window::name { @@ -69,11 +70,28 @@ namespace app virtual void setVoLTEOn(bool value) = 0; [[nodiscard]] virtual bool getVoLTEOn() const noexcept = 0; }; + class ScreenLightSettings + { + public: + struct Values + { + bool lightOn; + screen_light_control::ScreenLightMode mode; + screen_light_control::Parameters parameters; + }; + + virtual ~ScreenLightSettings() = default; + virtual auto getCurrentValues() -> Values = 0; + virtual void setBrightness(float brigtnessValue) = 0; + virtual void setMode(bool isAutoLightSwitchOn) = 0; + virtual void setStatus(bool isDisplayLightSwitchOn) = 0; + }; }; // namespace settingsInterface class ApplicationSettingsNew : public app::Application, public settingsInterface::SimParams, - public settingsInterface::OperatorsSettings + public settingsInterface::OperatorsSettings, + public settingsInterface::ScreenLightSettings { public: ApplicationSettingsNew(std::string name = name_settings_new, @@ -106,6 +124,11 @@ namespace app } void setLockPassHash(unsigned int value); + ScreenLightSettings::Values getCurrentValues() override; + void setBrightness(float brigtnessValue) override; + void setMode(bool isAutoLightSwitchOn) override; + void setStatus(bool isDisplayLightSwitchOn) override; + private: Store::GSM::SIM selectedSim = Store::GSM::get()->selected; std::string selectedSimNumber = {}; diff --git a/module-apps/application-settings-new/CMakeLists.txt b/module-apps/application-settings-new/CMakeLists.txt index 01f6d4bc5f46207f0a8137af42b1d5ab11e40402..456332f6237bf696a78ab5b3436a9b6360164ffc 100644 --- a/module-apps/application-settings-new/CMakeLists.txt +++ b/module-apps/application-settings-new/CMakeLists.txt @@ -41,6 +41,8 @@ target_sources( ${PROJECT_NAME} windows/QuotesAddWindow.cpp windows/SecurityMainWindow.cpp windows/ChangePasscodeWindow.cpp + widgets/SpinBox.cpp + widgets/SpinBoxOptionSetting.cpp PUBLIC ApplicationSettings.hpp @@ -66,5 +68,4 @@ add_dependencies(${PROJECT_NAME} version) target_link_libraries(${PROJECT_NAME} PUBLIC service-bluetooth - service-evtmgr ) diff --git a/module-apps/application-settings-new/widgets/OptionSetting.hpp b/module-apps/application-settings-new/widgets/OptionSetting.hpp new file mode 100644 index 0000000000000000000000000000000000000000..0e2ec3f4ea48479efc8882a5cca0f34078455da8 --- /dev/null +++ b/module-apps/application-settings-new/widgets/OptionSetting.hpp @@ -0,0 +1,30 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#pragma once + +#include "SpinBox.hpp" +#include "OptionWindow.hpp" +#include "Application.hpp" + +#include + +namespace gui +{ + class SpinBoxOptionSettings : public option::OptionSettings + { + public: + SpinBoxOptionSettings(UTF8 text, + uint8_t value, + uint8_t maxValue, + std::function updateCallback, + std::function focusChangedCallback = nullptr); + + [[nodiscard]] auto build() const -> ListItem * override; + + private: + std::function updateCallback; + std::uint8_t maxValue; + std::uint8_t value; + }; +} // namespace gui diff --git a/module-apps/application-settings-new/widgets/SpinBox.cpp b/module-apps/application-settings-new/widgets/SpinBox.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5e8946da9b9730edab7665f82ed72c322448123f --- /dev/null +++ b/module-apps/application-settings-new/widgets/SpinBox.cpp @@ -0,0 +1,93 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "SpinBox.hpp" +#include "widgets/BarGraph.hpp" + +#include +#include +#include +#include +#include + +namespace gui +{ + SpinBox::SpinBox( + Item *parent, const std::string &title, UpdateCallback updateCallback, uint8_t maxValue, uint8_t startValue) + : HBox(parent, style::window::default_left_margin), updateBarCallback(std::move(updateCallback)) + { + setMinimumSize(style::window::default_body_width, style::window::label::big_h); + setPenWidth(style::window::default_border_no_focus_w); + setPenFocusWidth(style::window::default_border_focus_w); + setEdges(gui::RectangleEdge::Top | gui::RectangleEdge::Bottom); + + titleLabel = addTitle(this, title); + leftArrow = addArrow(this, "arrow_left", Alignment::Horizontal::Left, false); + rightArrow = addArrow(this, "arrow_right", Alignment::Horizontal::Right, false); + bar = addBarGraph(this, maxValue, startValue); + + focusChangedCallback = [this](Item &item) { + leftArrow->setVisible(item.focus); + rightArrow->setVisible(item.focus); + resizeItems(); + return true; + }; + + inputCallback = [this](gui::Item &item, const gui::InputEvent &event) { + if (!event.isShortPress()) { + return false; + } + + int update = 0; + if (event.is(KeyCode::KEY_LEFT)) { + update = -1; + } + else if (event.is(KeyCode::KEY_RIGHT)) { + update = 1; + } + + if (update != 0 && bar->update(update)) { + updateBarCallback(bar->getValue()); + return true; + } + + return false; + }; + } + + Image *SpinBox::addArrow(Item *parent, const std::string &arrowName, Alignment::Horizontal aligment, bool visible) + { + auto arrow = new Image(parent, 0, 0, 0, 0); + arrow->setAlignment(Alignment(aligment, Alignment::Vertical::Center)); + arrow->setMargins(Margins(0, 0, style::margins::big, 0)); + arrow->set(arrowName); + arrow->setVisible(visible); + + return arrow; + } + + Label *SpinBox::addTitle(Item *parent, const std::string &text) + { + auto label = new Label(parent); + label->setMinimumHeight(style::window::label::default_h); + label->setMaximumWidth(style::window::default_body_width); + + label->setEdges(RectangleEdge::None); + label->setAlignment(Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Center)); + label->setFont(style::window::font::big); + label->setText(text); + label->activeItem = false; + + return label; + } + + HBarGraph *SpinBox::addBarGraph(Item *parent, uint8_t maxValue, uint8_t startValue) + { + auto barGraph = new HBarGraph(parent, 0, 0, maxValue); + barGraph->setAlignment(Alignment(gui::Alignment::Horizontal::Right, gui::Alignment::Vertical::Center)); + barGraph->setValue(startValue); + barGraph->activeItem = false; + + return barGraph; + } +} // namespace gui diff --git a/module-apps/application-settings-new/widgets/SpinBox.hpp b/module-apps/application-settings-new/widgets/SpinBox.hpp new file mode 100644 index 0000000000000000000000000000000000000000..bd80a2fb67244b8a1c6e31d2bc2dd6c9b9350b67 --- /dev/null +++ b/module-apps/application-settings-new/widgets/SpinBox.hpp @@ -0,0 +1,43 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#pragma once +#include "widgets/BarGraph.hpp" + +namespace style::settings::brightness +{ + inline constexpr auto max_value = 6; +}; + +class UTF8; + +namespace gui +{ + class Image; + class SpinBox : public HBox + { + public: + using UpdateCallback = std::function; + + SpinBox(Item *parent, + const std::string &title, + UpdateCallback updateCallback, + std::uint8_t maxValue = style::settings::brightness::max_value, + std::uint8_t startValue = 0); + + private: + auto addArrow(Item *parent, const std::string &arrowName, Alignment::Horizontal aligment, bool visible) + -> Image *; + auto addBarGraph(Item *parent, uint8_t maxValue, uint8_t startValue) -> HBarGraph *; + auto addTitle(Item *parent, const std::string &text) -> Label *; + + HBarGraph *bar; + Label *titleLabel; + Image *leftArrow; + Image *rightArrow; + UpdateCallback updateBarCallback; + }; +} // namespace gui diff --git a/module-apps/application-settings-new/widgets/SpinBoxOptionSetting.cpp b/module-apps/application-settings-new/widgets/SpinBoxOptionSetting.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e5a90b52489175b7dccfba25b3ab53de1531806 --- /dev/null +++ b/module-apps/application-settings-new/widgets/SpinBoxOptionSetting.cpp @@ -0,0 +1,41 @@ +// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved. +// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md + +#include "OptionSetting.hpp" + +#include +#include "SpinBox.hpp" + +namespace gui +{ + SpinBoxOptionSettings::SpinBoxOptionSettings(UTF8 text, + std::uint8_t value, + std::uint8_t maxValue, + std::function updateCallback, + std::function focusChangedCallback) + : option::OptionSettings(text, nullptr, focusChangedCallback, nullptr), + updateCallback(std::move(updateCallback)), maxValue(maxValue), value(value) + {} + + auto SpinBoxOptionSettings::build() const -> ListItem * + { + auto spinBox = new SpinBox(nullptr, text, updateCallback, maxValue, value); + + auto optionItem = new gui::ListItem(); + optionItem->setMinimumSize(style::window::default_body_width, style::window::label::big_h); + optionItem->inputCallback = spinBox->inputCallback; + optionItem->focusChangedCallback = [spinBox, this](Item &item) { + spinBox->focusChangedCallback(item); + return focusChangedCallback(item); + }; + optionItem->dimensionChangedCallback = [spinBox](gui::Item &, const BoundingBox &newDim) -> bool { + spinBox->setPosition(0, 0); + spinBox->setSize(newDim.w, newDim.h); + return true; + }; + + optionItem->addWidget(spinBox); + + return optionItem; + } +} // namespace gui diff --git a/module-apps/application-settings-new/windows/BaseSettingsWindow.cpp b/module-apps/application-settings-new/windows/BaseSettingsWindow.cpp index 28527afaead9836c78337eb5ebb61d8122697829..bab60d6d98fcaed9adc189bc8635bfc3dacf82eb 100644 --- a/module-apps/application-settings-new/windows/BaseSettingsWindow.cpp +++ b/module-apps/application-settings-new/windows/BaseSettingsWindow.cpp @@ -2,7 +2,6 @@ // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "BaseSettingsWindow.hpp" - #include namespace gui diff --git a/module-apps/application-settings-new/windows/BaseSettingsWindow.hpp b/module-apps/application-settings-new/windows/BaseSettingsWindow.hpp index 210b77c1f80c4709a67fb40e108f485e26ffb316..bfa65d0202c0a2bc374d81f19797e866494cd9f4 100644 --- a/module-apps/application-settings-new/windows/BaseSettingsWindow.hpp +++ b/module-apps/application-settings-new/windows/BaseSettingsWindow.hpp @@ -6,6 +6,7 @@ #include "Application.hpp" #include "windows/AppWindow.hpp" #include "windows/OptionWindow.hpp" +#include namespace gui { diff --git a/module-apps/application-settings-new/windows/DisplayLightWindow.cpp b/module-apps/application-settings-new/windows/DisplayLightWindow.cpp index b8be429642ce9afdc97b09b157349fab8a52988d..370e8819787b66f060ac3a6d464c47a343ac97a3 100644 --- a/module-apps/application-settings-new/windows/DisplayLightWindow.cpp +++ b/module-apps/application-settings-new/windows/DisplayLightWindow.cpp @@ -2,23 +2,38 @@ // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md #include "DisplayLightWindow.hpp" - +#include "application-settings-new/widgets/OptionSetting.hpp" +#include "OptionSetting.hpp" #include "application-settings-new/ApplicationSettings.hpp" #include "OptionSetting.hpp" +#include #include +#include namespace gui { - DisplayLightWindow::DisplayLightWindow(app::Application *app) : BaseSettingsWindow(app, window::name::display_light) + + DisplayLightWindow::DisplayLightWindow(app::Application *app, app::settingsInterface::ScreenLightSettings *settings) + : BaseSettingsWindow(app, window::name::display_light), screenLightSettings(settings) { + auto values = screenLightSettings->getCurrentValues(); + + isDisplayLightSwitchOn = values.lightOn; + isAutoLightSwitchOn = values.mode; + brightnessValue = values.parameters.manualModeBrightness; + setTitle(utils::localize.get("app_settings_display_display_light")); } void DisplayLightWindow::switchHandler(bool &onOffSwitch) { onOffSwitch = !onOffSwitch; + rebuildOptionList(); + + screenLightSettings->setStatus(isDisplayLightSwitchOn); + screenLightSettings->setMode(isAutoLightSwitchOn); } auto DisplayLightWindow::buildOptionsList() -> std::list @@ -49,21 +64,43 @@ namespace gui } if (isDisplayLightSwitchOn && !isAutoLightSwitchOn) { - optionsList.emplace_back(std::make_unique( - utils::translateI18("app_settings_display_light_brightness"), - [=](gui::Item &item) { return true; }, - [=](gui::Item &item) { - if (item.focus) { - this->setBottomBarText(utils::translateI18(style::strings::common::set), - BottomBar::Side::CENTER); - } - return true; - }, - this, - gui::option::SettingRightItem::ArrowWhite)); + addBrightnessOption(optionsList); } return optionsList; } + auto DisplayLightWindow::createBrightnessOption(int brightnessStep) -> std::unique_ptr + { + auto setBrightness = [this, brightnessStep](uint8_t value) { + screenLightSettings->setBrightness(value * brightnessStep); + return true; + }; + + auto setBottomBarOnSpinnerFocus = [&](gui::Item &item) { + if (item.focus) { + setBottomBarText(utils::translateI18(style::strings::common::set), BottomBar::Side::CENTER); + } + return true; + }; + + auto spinner = std::make_unique( + utils::translateI18("app_settings_display_light_brightness") + " " + std::to_string(brightnessStep), + brightnessValue * brightnessStep, + std::ceil(screen_light_control::Parameters::MAX_BRIGHTNESS / brightnessStep), + setBrightness, + setBottomBarOnSpinnerFocus); + + return spinner; + } + + void DisplayLightWindow::addBrightnessOption(std::list &options) + { + /* + * We are adding 4 brightness widgets to easily check what is the best step for setting screen brightness. + */ + for (auto step : {10, 15, 20, 25}) { + options.emplace_back(createBrightnessOption(step)); + } + } } // namespace gui diff --git a/module-apps/application-settings-new/windows/DisplayLightWindow.hpp b/module-apps/application-settings-new/windows/DisplayLightWindow.hpp index 64451acad655be210ea89bc454b4850bcc5ed691..5ec6bd70c1be7a6635ee0fc7bbbbb2d8382576e1 100644 --- a/module-apps/application-settings-new/windows/DisplayLightWindow.hpp +++ b/module-apps/application-settings-new/windows/DisplayLightWindow.hpp @@ -3,21 +3,29 @@ #pragma once +#include +#include +#include +#include #include "BaseSettingsWindow.hpp" namespace gui { - class DisplayLightWindow : public BaseSettingsWindow { public: - DisplayLightWindow(app::Application *app); + DisplayLightWindow(app::Application *app, app::settingsInterface::ScreenLightSettings *screenLightSettings); private: - void switchHandler(bool &onOffSwitch); auto buildOptionsList() -> std::list