Add a brightness slider to settings

custom
jacqueline 2 years ago
parent 8ee5e781e7
commit f2bb2e2528
  1. 12
      src/system_fsm/booting.cpp
  2. 23
      src/ui/include/screen_settings.hpp
  3. 11
      src/ui/include/ui_events.hpp
  4. 3
      src/ui/include/ui_fsm.hpp
  5. 4
      src/ui/screen_menu.cpp
  6. 68
      src/ui/screen_settings.cpp
  7. 35
      src/ui/ui_fsm.cpp

@ -49,29 +49,23 @@ auto Booting::entry() -> void {
sSamd.reset(drivers::Samd::Create()); sSamd.reset(drivers::Samd::Create());
sAdc.reset(drivers::AdcBattery::Create()); sAdc.reset(drivers::AdcBattery::Create());
assert(sSamd.get() && sAdc.get()); sNvs.reset(drivers::NvsStorage::Open());
assert(sSamd.get() && sAdc.get() && sNvs.get());
sBattery.reset(new battery::Battery(sSamd.get(), sAdc.get())); sBattery.reset(new battery::Battery(sSamd.get(), sAdc.get()));
// Start bringing up LVGL now, since we have all of its prerequisites. // Start bringing up LVGL now, since we have all of its prerequisites.
sTrackQueue.reset(new audio::TrackQueue()); sTrackQueue.reset(new audio::TrackQueue());
ESP_LOGI(kTag, "starting ui"); ESP_LOGI(kTag, "starting ui");
if (!ui::UiState::Init(sGpios.get(), sTrackQueue.get(), sBattery)) { if (!ui::UiState::Init(sGpios.get(), sNvs, sTrackQueue.get(), sBattery)) {
events::System().Dispatch(FatalError{}); events::System().Dispatch(FatalError{});
return; return;
} }
// Install everything else that is certain to be needed. // Install everything else that is certain to be needed.
ESP_LOGI(kTag, "installing remaining drivers"); ESP_LOGI(kTag, "installing remaining drivers");
sNvs.reset(drivers::NvsStorage::Open());
sTagParser.reset(new database::TagParserImpl()); sTagParser.reset(new database::TagParserImpl());
if (!sNvs) {
events::System().Dispatch(FatalError{});
events::Ui().Dispatch(FatalError{});
return;
}
// ESP_LOGI(kTag, "starting bluetooth"); // ESP_LOGI(kTag, "starting bluetooth");
// sBluetooth.reset(new drivers::Bluetooth(sNvs.get())); // sBluetooth.reset(new drivers::Bluetooth(sNvs.get()));
// sBluetooth->Enable(); // sBluetooth->Enable();

@ -6,12 +6,15 @@
#pragma once #pragma once
#include <cstdint>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "display.hpp"
#include "index.hpp" #include "index.hpp"
#include "lvgl.h" #include "lvgl.h"
#include "nvs.hpp"
#include "screen.hpp" #include "screen.hpp"
namespace ui { namespace ui {
@ -20,16 +23,6 @@ namespace screens {
class Settings : public MenuScreen { class Settings : public MenuScreen {
public: public:
Settings(); Settings();
~Settings();
private:
std::shared_ptr<Screen> bluetooth_;
std::shared_ptr<Screen> headphones_;
std::shared_ptr<Screen> appearance_;
std::shared_ptr<Screen> input_method_;
std::shared_ptr<Screen> storage_;
std::shared_ptr<Screen> firmware_update_;
std::shared_ptr<Screen> about_;
}; };
class Bluetooth : public MenuScreen { class Bluetooth : public MenuScreen {
@ -44,7 +37,15 @@ class Headphones : public MenuScreen {
class Appearance : public MenuScreen { class Appearance : public MenuScreen {
public: public:
Appearance(); Appearance(drivers::NvsStorage* nvs, drivers::Display* display);
auto ChangeBrightness(uint_fast8_t) -> void;
private:
drivers::NvsStorage* nvs_;
drivers::Display* display_;
lv_obj_t* current_brightness_label_;
}; };
class InputMethod : public MenuScreen { class InputMethod : public MenuScreen {

@ -39,7 +39,16 @@ struct IndexSelected : tinyfsm::Event {
struct BackPressed : tinyfsm::Event {}; struct BackPressed : tinyfsm::Event {};
struct ShowNowPlaying : tinyfsm::Event {}; struct ShowNowPlaying : tinyfsm::Event {};
struct ShowSettingsPage : tinyfsm::Event { struct ShowSettingsPage : tinyfsm::Event {
std::shared_ptr<Screen> screen; enum class Page {
kRoot,
kBluetooth,
kHeadphones,
kAppearance,
kInput,
kStorage,
kFirmwareUpdate,
kAbout,
} page;
}; };
struct ModalConfirmPressed : tinyfsm::Event {}; struct ModalConfirmPressed : tinyfsm::Event {};

@ -11,6 +11,7 @@
#include "audio_events.hpp" #include "audio_events.hpp"
#include "battery.hpp" #include "battery.hpp"
#include "nvs.hpp"
#include "relative_wheel.hpp" #include "relative_wheel.hpp"
#include "screen_playing.hpp" #include "screen_playing.hpp"
#include "tinyfsm.hpp" #include "tinyfsm.hpp"
@ -29,6 +30,7 @@ namespace ui {
class UiState : public tinyfsm::Fsm<UiState> { class UiState : public tinyfsm::Fsm<UiState> {
public: public:
static auto Init(drivers::IGpios*, static auto Init(drivers::IGpios*,
std::shared_ptr<drivers::NvsStorage>,
audio::TrackQueue*, audio::TrackQueue*,
std::shared_ptr<battery::Battery>) -> bool; std::shared_ptr<battery::Battery>) -> bool;
@ -81,6 +83,7 @@ class UiState : public tinyfsm::Fsm<UiState> {
static std::shared_ptr<drivers::RelativeWheel> sRelativeWheel; static std::shared_ptr<drivers::RelativeWheel> sRelativeWheel;
static std::shared_ptr<drivers::Display> sDisplay; static std::shared_ptr<drivers::Display> sDisplay;
static std::shared_ptr<battery::Battery> sBattery; static std::shared_ptr<battery::Battery> sBattery;
static std::shared_ptr<drivers::NvsStorage> sNvs;
static std::weak_ptr<database::Database> sDb; static std::weak_ptr<database::Database> sDb;
static std::stack<std::shared_ptr<Screen>> sScreens; static std::stack<std::shared_ptr<Screen>> sScreens;

@ -31,8 +31,8 @@ static void now_playing_click_cb(lv_event_t* ev) {
} }
static void settings_click_callback(lv_event_t* ev) { static void settings_click_callback(lv_event_t* ev) {
std::shared_ptr<Screen> settings{new Settings()}; events::Ui().Dispatch(internal::ShowSettingsPage{
events::Ui().Dispatch(internal::ShowSettingsPage{.screen = settings}); .page = internal::ShowSettingsPage::Page::kRoot});
} }
static void index_click_cb(lv_event_t* ev) { static void index_click_cb(lv_event_t* ev) {

@ -8,6 +8,7 @@
#include "core/lv_event.h" #include "core/lv_event.h"
#include "core/lv_obj.h" #include "core/lv_obj.h"
#include "display.hpp"
#include "esp_log.h" #include "esp_log.h"
#include "core/lv_group.h" #include "core/lv_group.h"
@ -21,6 +22,7 @@
#include "index.hpp" #include "index.hpp"
#include "misc/lv_anim.h" #include "misc/lv_anim.h"
#include "misc/lv_area.h" #include "misc/lv_area.h"
#include "nvs.hpp"
#include "screen.hpp" #include "screen.hpp"
#include "ui_events.hpp" #include "ui_events.hpp"
#include "ui_fsm.hpp" #include "ui_fsm.hpp"
@ -35,51 +37,43 @@
namespace ui { namespace ui {
namespace screens { namespace screens {
using Page = internal::ShowSettingsPage::Page;
static void open_sub_menu_cb(lv_event_t* e) { static void open_sub_menu_cb(lv_event_t* e) {
std::shared_ptr<Screen>* next_screen = Page next_page = static_cast<Page>(reinterpret_cast<uintptr_t>(e->user_data));
reinterpret_cast<std::shared_ptr<Screen>*>(e->user_data);
events::Ui().Dispatch(internal::ShowSettingsPage{ events::Ui().Dispatch(internal::ShowSettingsPage{
.screen = *next_screen, .page = next_page,
}); });
} }
static void sub_menu(lv_obj_t* list, static void sub_menu(lv_obj_t* list,
lv_group_t* group, lv_group_t* group,
const std::string& text, const std::string& text,
std::shared_ptr<Screen>* screen) { Page page) {
lv_obj_t* item = lv_list_add_btn(list, NULL, text.c_str()); lv_obj_t* item = lv_list_add_btn(list, NULL, text.c_str());
lv_group_add_obj(group, item); lv_group_add_obj(group, item);
lv_obj_add_event_cb(item, open_sub_menu_cb, LV_EVENT_CLICKED, screen); lv_obj_add_event_cb(item, open_sub_menu_cb, LV_EVENT_CLICKED,
reinterpret_cast<void*>(static_cast<uintptr_t>(page)));
} }
Settings::Settings() Settings::Settings() : MenuScreen("Settings") {
: MenuScreen("Settings"),
bluetooth_(new Bluetooth()),
headphones_(new Headphones()),
appearance_(new Appearance()),
input_method_(new InputMethod()),
storage_(new Storage()),
firmware_update_(new FirmwareUpdate()),
about_(new About()) {
lv_obj_t* list = lv_list_create(content_); lv_obj_t* list = lv_list_create(content_);
lv_obj_set_size(list, lv_pct(100), lv_pct(100)); lv_obj_set_size(list, lv_pct(100), lv_pct(100));
lv_list_add_text(list, "Audio"); lv_list_add_text(list, "Audio");
sub_menu(list, group_, "Bluetooth", &bluetooth_); sub_menu(list, group_, "Bluetooth", Page::kBluetooth);
sub_menu(list, group_, "Headphones", &headphones_); sub_menu(list, group_, "Headphones", Page::kBluetooth);
lv_list_add_text(list, "Interface"); lv_list_add_text(list, "Interface");
sub_menu(list, group_, "Appearance", &appearance_); sub_menu(list, group_, "Appearance", Page::kAppearance);
sub_menu(list, group_, "Input Method", &input_method_); sub_menu(list, group_, "Input Method", Page::kInput);
lv_list_add_text(list, "System"); lv_list_add_text(list, "System");
sub_menu(list, group_, "Storage", &storage_); sub_menu(list, group_, "Storage", Page::kStorage);
sub_menu(list, group_, "Firmware Update", &firmware_update_); sub_menu(list, group_, "Firmware Update", Page::kFirmwareUpdate);
sub_menu(list, group_, "About", &about_); sub_menu(list, group_, "About", Page::kAbout);
} }
Settings::~Settings() {}
static auto settings_container(lv_obj_t* parent) -> lv_obj_t* { static auto settings_container(lv_obj_t* parent) -> lv_obj_t* {
lv_obj_t* res = lv_obj_create(parent); lv_obj_t* res = lv_obj_create(parent);
lv_obj_set_layout(res, LV_LAYOUT_FLEX); lv_obj_set_layout(res, LV_LAYOUT_FLEX);
@ -152,13 +146,39 @@ Headphones::Headphones() : MenuScreen("Headphones") {
lv_obj_set_size(current_balance_label, lv_pct(100), LV_SIZE_CONTENT); lv_obj_set_size(current_balance_label, lv_pct(100), LV_SIZE_CONTENT);
} }
Appearance::Appearance() : MenuScreen("Appearance") { static void change_brightness_cb(lv_event_t* ev) {
Appearance* instance = reinterpret_cast<Appearance*>(ev->user_data);
instance->ChangeBrightness(lv_slider_get_value(ev->target));
}
Appearance::Appearance(drivers::NvsStorage* nvs, drivers::Display* display)
: MenuScreen("Appearance"), nvs_(nvs), display_(display) {
lv_obj_t* toggle_container = settings_container(content_); lv_obj_t* toggle_container = settings_container(content_);
lv_obj_t* toggle_label = lv_label_create(toggle_container); lv_obj_t* toggle_label = lv_label_create(toggle_container);
lv_obj_set_flex_grow(toggle_label, 1); lv_obj_set_flex_grow(toggle_label, 1);
lv_label_set_text(toggle_label, "Dark Mode"); lv_label_set_text(toggle_label, "Dark Mode");
lv_obj_t* toggle = lv_switch_create(toggle_container); lv_obj_t* toggle = lv_switch_create(toggle_container);
lv_group_add_obj(group_, toggle); lv_group_add_obj(group_, toggle);
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_group_add_obj(group_, brightness);
current_brightness_label_ = lv_label_create(content_);
lv_label_set_text(current_brightness_label_, "50%");
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);
}
auto Appearance::ChangeBrightness(uint_fast8_t new_level) -> void {
display_->SetBrightness(new_level);
std::string new_str = std::to_string(new_level) + "%";
lv_label_set_text(current_brightness_label_, new_str.c_str());
} }
InputMethod::InputMethod() : MenuScreen("Input Method") { InputMethod::InputMethod() : MenuScreen("Input Method") {

@ -19,6 +19,7 @@
#include "gpios.hpp" #include "gpios.hpp"
#include "lvgl_task.hpp" #include "lvgl_task.hpp"
#include "modal_confirm.hpp" #include "modal_confirm.hpp"
#include "nvs.hpp"
#include "relative_wheel.hpp" #include "relative_wheel.hpp"
#include "screen.hpp" #include "screen.hpp"
#include "screen_menu.hpp" #include "screen_menu.hpp"
@ -46,6 +47,7 @@ std::shared_ptr<drivers::TouchWheel> UiState::sTouchWheel;
std::shared_ptr<drivers::RelativeWheel> UiState::sRelativeWheel; std::shared_ptr<drivers::RelativeWheel> UiState::sRelativeWheel;
std::shared_ptr<drivers::Display> UiState::sDisplay; std::shared_ptr<drivers::Display> UiState::sDisplay;
std::shared_ptr<battery::Battery> UiState::sBattery; std::shared_ptr<battery::Battery> UiState::sBattery;
std::shared_ptr<drivers::NvsStorage> UiState::sNvs;
std::weak_ptr<database::Database> UiState::sDb; std::weak_ptr<database::Database> UiState::sDb;
std::stack<std::shared_ptr<Screen>> UiState::sScreens; std::stack<std::shared_ptr<Screen>> UiState::sScreens;
@ -53,9 +55,11 @@ std::shared_ptr<Screen> UiState::sCurrentScreen;
std::shared_ptr<Modal> UiState::sCurrentModal; std::shared_ptr<Modal> UiState::sCurrentModal;
auto UiState::Init(drivers::IGpios* gpio_expander, auto UiState::Init(drivers::IGpios* gpio_expander,
std::shared_ptr<drivers::NvsStorage> nvs,
audio::TrackQueue* queue, audio::TrackQueue* queue,
std::shared_ptr<battery::Battery> battery) -> bool { std::shared_ptr<battery::Battery> battery) -> bool {
sIGpios = gpio_expander; sIGpios = gpio_expander;
sNvs = nvs;
sQueue = queue; sQueue = queue;
sBattery = battery; sBattery = battery;
@ -160,7 +164,36 @@ void Browse::react(const internal::ShowNowPlaying& ev) {
} }
void Browse::react(const internal::ShowSettingsPage& ev) { void Browse::react(const internal::ShowSettingsPage& ev) {
PushScreen(ev.screen); std::shared_ptr<Screen> screen;
switch (ev.page) {
case internal::ShowSettingsPage::Page::kRoot:
screen.reset(new screens::Settings());
break;
case internal::ShowSettingsPage::Page::kBluetooth:
screen.reset(new screens::Bluetooth());
break;
case internal::ShowSettingsPage::Page::kHeadphones:
screen.reset(new screens::Headphones());
break;
case internal::ShowSettingsPage::Page::kAppearance:
screen.reset(new screens::Appearance(sNvs.get(), sDisplay.get()));
break;
case internal::ShowSettingsPage::Page::kInput:
screen.reset(new screens::InputMethod());
break;
case internal::ShowSettingsPage::Page::kStorage:
screen.reset(new screens::Storage());
break;
case internal::ShowSettingsPage::Page::kFirmwareUpdate:
screen.reset(new screens::FirmwareUpdate());
break;
case internal::ShowSettingsPage::Page::kAbout:
screen.reset(new screens::About());
break;
}
if (screen) {
PushScreen(screen);
}
} }
void Browse::react(const internal::RecordSelected& ev) { void Browse::react(const internal::RecordSelected& ev) {

Loading…
Cancel
Save