diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-08-29 16:07:56 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-08-29 16:07:56 +1000 |
| commit | 4247c9fe7d25c921fbfc73fc50e849c8780e7ad6 (patch) | |
| tree | b8288c7772010edfb4a398596bef8fd35b771a95 /src/ui | |
| parent | 773f2857678727f416a67a3a5ae71bd5b6761078 (diff) | |
| download | tangara-fw-4247c9fe7d25c921fbfc73fc50e849c8780e7ad6.tar.gz | |
store the screen brightness in nvs
Diffstat (limited to 'src/ui')
| -rw-r--r-- | src/ui/include/screen_settings.hpp | 2 | ||||
| -rw-r--r-- | src/ui/include/themes.hpp | 6 | ||||
| -rw-r--r-- | src/ui/screen_settings.cpp | 28 | ||||
| -rw-r--r-- | src/ui/themes.cpp | 14 | ||||
| -rw-r--r-- | src/ui/ui_fsm.cpp | 1 |
5 files changed, 37 insertions, 14 deletions
diff --git a/src/ui/include/screen_settings.hpp b/src/ui/include/screen_settings.hpp index 53d9277b..61375fa9 100644 --- a/src/ui/include/screen_settings.hpp +++ b/src/ui/include/screen_settings.hpp @@ -40,12 +40,14 @@ class Appearance : public MenuScreen { Appearance(drivers::NvsStorage* nvs, drivers::Display* display); auto ChangeBrightness(uint_fast8_t) -> void; + auto CommitBrightness() -> void; private: drivers::NvsStorage* nvs_; drivers::Display* display_; lv_obj_t* current_brightness_label_; + uint_fast8_t current_brightness_; }; class InputMethod : public MenuScreen { diff --git a/src/ui/include/themes.hpp b/src/ui/include/themes.hpp index ef0e719c..ee4bb05d 100644 --- a/src/ui/include/themes.hpp +++ b/src/ui/include/themes.hpp @@ -5,13 +5,9 @@ namespace ui { namespace themes { - enum class Style { - kMenuItem, - kTopBar - }; +enum class Style { kMenuItem, kTopBar }; class Theme { public: - void Apply(void); void Callback(lv_obj_t* obj); void ApplyStyle(lv_obj_t* obj, Style style); diff --git a/src/ui/screen_settings.cpp b/src/ui/screen_settings.cpp index c496ce00..6bafb9a8 100644 --- a/src/ui/screen_settings.cpp +++ b/src/ui/screen_settings.cpp @@ -5,6 +5,7 @@ */ #include "screen_settings.hpp" +#include <string> #include "core/lv_event.h" #include "core/lv_obj.h" @@ -151,6 +152,15 @@ static void change_brightness_cb(lv_event_t* ev) { instance->ChangeBrightness(lv_slider_get_value(ev->target)); } +static void release_brightness_cb(lv_event_t* ev) { + Appearance* instance = reinterpret_cast<Appearance*>(ev->user_data); + instance->CommitBrightness(); +} + +static auto brightness_str(uint_fast8_t percent) -> std::string { + return std::to_string(percent) + "%"; +} + Appearance::Appearance(drivers::NvsStorage* nvs, drivers::Display* display) : MenuScreen("Appearance"), nvs_(nvs), display_(display) { lv_obj_t* toggle_container = settings_container(content_); @@ -160,25 +170,35 @@ Appearance::Appearance(drivers::NvsStorage* nvs, drivers::Display* display) lv_obj_t* toggle = lv_switch_create(toggle_container); lv_group_add_obj(group_, toggle); + uint_fast8_t initial_brightness = nvs_->ScreenBrightness().get(); + lv_obj_t* brightness_label = lv_label_create(content_); lv_label_set_text(brightness_label, "Brightness"); lv_obj_t* brightness = lv_slider_create(content_); lv_obj_set_width(brightness, lv_pct(100)); lv_slider_set_range(brightness, 10, 100); - lv_slider_set_value(brightness, 50, LV_ANIM_OFF); + lv_slider_set_value(brightness, initial_brightness, LV_ANIM_OFF); lv_group_add_obj(group_, brightness); current_brightness_label_ = lv_label_create(content_); - lv_label_set_text(current_brightness_label_, "50%"); + lv_label_set_text(current_brightness_label_, + brightness_str(initial_brightness).c_str()); lv_obj_set_size(current_brightness_label_, lv_pct(100), LV_SIZE_CONTENT); lv_obj_add_event_cb(brightness, change_brightness_cb, LV_EVENT_VALUE_CHANGED, this); + lv_obj_add_event_cb(brightness, release_brightness_cb, LV_EVENT_RELEASED, + this); } auto Appearance::ChangeBrightness(uint_fast8_t new_level) -> void { + current_brightness_ = new_level; display_->SetBrightness(new_level); - std::string new_str = std::to_string(new_level) + "%"; - lv_label_set_text(current_brightness_label_, new_str.c_str()); + lv_label_set_text(current_brightness_label_, + brightness_str(new_level).c_str()); +} + +auto Appearance::CommitBrightness() -> void { + nvs_->ScreenBrightness(current_brightness_); } InputMethod::InputMethod() : MenuScreen("Input Method") { diff --git a/src/ui/themes.cpp b/src/ui/themes.cpp index dd9ef423..38bcd9fd 100644 --- a/src/ui/themes.cpp +++ b/src/ui/themes.cpp @@ -1,7 +1,7 @@ #include "themes.hpp" #include "core/lv_obj.h" -#include "misc/lv_color.h" #include "esp_log.h" +#include "misc/lv_color.h" LV_FONT_DECLARE(font_fusion); @@ -19,7 +19,8 @@ Theme::Theme() { lv_style_set_bg_color(&button_style_, lv_color_white()); lv_style_init(&button_style_focused_); - lv_style_set_bg_color(&button_style_focused_, lv_palette_lighten(LV_PALETTE_BLUE_GREY, 2)); + lv_style_set_bg_color(&button_style_focused_, + lv_palette_lighten(LV_PALETTE_BLUE_GREY, 2)); lv_theme_t* parent_theme = lv_disp_get_theme(NULL); theme_ = *parent_theme; @@ -41,15 +42,18 @@ void Theme::Callback(lv_obj_t* obj) { if (lv_obj_check_type(obj, &lv_btn_class) || lv_obj_check_type(obj, &lv_list_btn_class)) { lv_obj_add_style(obj, &button_style_, LV_PART_MAIN); - lv_obj_add_style(obj, &button_style_focused_, LV_PART_MAIN | LV_STATE_FOCUSED); + lv_obj_add_style(obj, &button_style_focused_, + LV_PART_MAIN | LV_STATE_FOCUSED); } } void Theme::ApplyStyle(lv_obj_t* obj, Style style) { if (style == Style::kTopBar) { - lv_obj_set_style_border_color(obj, lv_palette_darken(LV_PALETTE_BLUE_GREY, 2), LV_PART_MAIN); + lv_obj_set_style_border_color( + obj, lv_palette_darken(LV_PALETTE_BLUE_GREY, 2), LV_PART_MAIN); lv_obj_set_style_border_width(obj, 1, LV_PART_MAIN); - lv_obj_set_style_border_side(obj, LV_BORDER_SIDE_BOTTOM|LV_BORDER_SIDE_TOP, LV_PART_MAIN); + lv_obj_set_style_border_side( + obj, LV_BORDER_SIDE_BOTTOM | LV_BORDER_SIDE_TOP, LV_PART_MAIN); lv_obj_set_style_pad_top(obj, 2, LV_PART_MAIN); lv_obj_set_style_pad_bottom(obj, 2, LV_PART_MAIN); } diff --git a/src/ui/ui_fsm.cpp b/src/ui/ui_fsm.cpp index 733b6bee..1febd1c7 100644 --- a/src/ui/ui_fsm.cpp +++ b/src/ui/ui_fsm.cpp @@ -69,6 +69,7 @@ auto UiState::Init(drivers::IGpios* gpio_expander, if (sDisplay == nullptr) { return false; } + sDisplay->SetBrightness(nvs->ScreenBrightness().get()); sTouchWheel.reset(drivers::TouchWheel::Create()); if (sTouchWheel != nullptr) { |
