From 1d485c97b0c03577a40b34fb762c76e98f417fa4 Mon Sep 17 00:00:00 2001 From: Tess Eisenberger Date: Sat, 1 Feb 2025 16:15:17 -0800 Subject: [PATCH] Add optional support for changing volume while locked This adds a new Controls setting for adjusting the behavior when locked, and an option for allowing volume control. --- lua/settings.lua | 78 +++++++++++++--------- src/drivers/include/drivers/nvs.hpp | 1 + src/drivers/nvs.cpp | 4 +- src/tangara/input/input_volume_buttons.cpp | 10 +-- src/tangara/input/input_volume_buttons.hpp | 3 +- src/tangara/input/lvgl_input_driver.cpp | 2 + src/tangara/lua/lua_controls.cpp | 17 +++++ src/tangara/ui/ui_fsm.cpp | 1 + 8 files changed, 78 insertions(+), 38 deletions(-) diff --git a/lua/settings.lua b/lua/settings.lua index c0e7c23e..6386b468 100644 --- a/lua/settings.lua +++ b/lua/settings.lua @@ -380,44 +380,58 @@ settings.InputSettings = SettingsScreen:new { create_ui = function(self) SettingsScreen.create_ui(self) - theme.set_subject(self.content:Label { - text = "Control scheme", - }, "settings_title") + -- Use the control scheme enum lists to generate the relevant dropdowns + local make_scheme_control = function(self, scheme_list, control_scheme) + local option_to_scheme = {} + local scheme_to_option = {} + local option_idx = 0 + local options = "" + + -- Sort the keys to order the dropdowns the same as the enums + keys = {} + for i in pairs(scheme_list) do table.insert(keys, i) end + table.sort(keys) + + for i, k in pairs(keys) do + v = scheme_list[k] + + option_to_scheme[option_idx] = k + scheme_to_option[k] = option_idx + if option_idx > 0 then + options = options .. "\n" + end + options = options .. v + option_idx = option_idx + 1 + end - local schemes = controls.schemes() - local option_to_scheme = {} - local scheme_to_option = {} + local controls_chooser = self.content:Dropdown { + options = options, + symbol = img.chevron, + } - local option_idx = 0 - local options = "" + self.bindings = self.bindings + { + control_scheme:bind(function(s) + local option = scheme_to_option[s] + controls_chooser:set({ selected = option }) + end) + } - for i, v in pairs(schemes) do - option_to_scheme[option_idx] = i - scheme_to_option[i] = option_idx - if option_idx > 0 then - options = options .. "\n" - end - options = options .. v - option_idx = option_idx + 1 + controls_chooser:onevent(lvgl.EVENT.VALUE_CHANGED, function() + local option = controls_chooser:get('selected') + local scheme = option_to_scheme[option] + control_scheme:set(scheme) + end) end - local controls_chooser = self.content:Dropdown { - options = options, - symbol = img.chevron, - } - - self.bindings = self.bindings + { - controls.scheme:bind(function(s) - local option = scheme_to_option[s] - controls_chooser:set({ selected = option }) - end) - } + theme.set_subject(self.content:Label { + text = "Control scheme", + }, "settings_title") + make_scheme_control(self, controls.schemes(), controls.scheme) - controls_chooser:onevent(lvgl.EVENT.VALUE_CHANGED, function() - local option = controls_chooser:get('selected') - local scheme = option_to_scheme[option] - controls.scheme:set(scheme) - end) + theme.set_subject(self.content:Label { + text = "Control scheme when locked", + }, "settings_title") + make_scheme_control(self, controls.locked_schemes(), controls.locked_scheme) theme.set_subject(self.content:Label { text = "Scroll Sensitivity", diff --git a/src/drivers/include/drivers/nvs.hpp b/src/drivers/include/drivers/nvs.hpp index b6192ab5..18bc5de6 100644 --- a/src/drivers/include/drivers/nvs.hpp +++ b/src/drivers/include/drivers/nvs.hpp @@ -140,6 +140,7 @@ class NvsStorage { enum class LockedInputModes : uint8_t { kDisabled = 0, + kVolumeOnly = 1, }; auto LockedInput() -> LockedInputModes; diff --git a/src/drivers/nvs.cpp b/src/drivers/nvs.cpp index a9f30042..f46049ad 100644 --- a/src/drivers/nvs.cpp +++ b/src/drivers/nvs.cpp @@ -574,9 +574,11 @@ auto NvsStorage::PrimaryInput(InputModes mode) -> void { auto NvsStorage::LockedInput() -> LockedInputModes { std::lock_guard lock{mutex_}; - switch (input_mode_.get().value_or(static_cast(LockedInputModes::kDisabled))) { + switch (locked_input_mode_.get().value_or(static_cast(LockedInputModes::kDisabled))) { case static_cast(LockedInputModes::kDisabled): return LockedInputModes::kDisabled; + case static_cast(LockedInputModes::kVolumeOnly): + return LockedInputModes::kVolumeOnly; default: return LockedInputModes::kDisabled; } diff --git a/src/tangara/input/input_volume_buttons.cpp b/src/tangara/input/input_volume_buttons.cpp index 6720afae..5c814ffa 100644 --- a/src/tangara/input/input_volume_buttons.cpp +++ b/src/tangara/input/input_volume_buttons.cpp @@ -15,13 +15,15 @@ VolumeButtons::VolumeButtons(drivers::IGpios& gpios) : gpios_(gpios), up_("upper", actions::volumeUp()), down_("lower", actions::volumeDown()), - locked_(false) {} + locked_() {} auto VolumeButtons::read(lv_indev_data_t* data) -> void { bool up = !gpios_.Get(drivers::IGpios::Pin::kKeyUp); bool down = !gpios_.Get(drivers::IGpios::Pin::kKeyDown); - if ((up && down) || locked_) { + bool input_disabled = locked_.has_value() && (locked_ != drivers::NvsStorage::LockedInputModes::kVolumeOnly); + + if ((up && down) || input_disabled) { up = false; down = false; } @@ -40,11 +42,11 @@ auto VolumeButtons::triggers() } auto VolumeButtons::onLock(drivers::NvsStorage::LockedInputModes mode) -> void { - locked_ = true; + locked_ = mode; } auto VolumeButtons::onUnlock() -> void { - locked_ = false; + locked_ = {}; } } // namespace input diff --git a/src/tangara/input/input_volume_buttons.hpp b/src/tangara/input/input_volume_buttons.hpp index 008252cd..35a44390 100644 --- a/src/tangara/input/input_volume_buttons.hpp +++ b/src/tangara/input/input_volume_buttons.hpp @@ -36,7 +36,8 @@ class VolumeButtons : public IInputDevice { TriggerHooks up_; TriggerHooks down_; - bool locked_; + // When locked, this contains the active mode + std::optional locked_; }; } // namespace input diff --git a/src/tangara/input/lvgl_input_driver.cpp b/src/tangara/input/lvgl_input_driver.cpp index 9177f92f..2859c6a8 100644 --- a/src/tangara/input/lvgl_input_driver.cpp +++ b/src/tangara/input/lvgl_input_driver.cpp @@ -74,6 +74,8 @@ auto intToLockedMode(int raw) -> std::optional int { return 1; } +static auto locked_controls_schemes(lua_State* L) -> int { + lua_newtable(L); + + lua_pushliteral(L, "Disabled"); + lua_rawseti( + L, -2, + static_cast(drivers::NvsStorage::LockedInputModes::kDisabled)); + + lua_pushliteral(L, "Volume Only"); + lua_rawseti( + L, -2, + static_cast(drivers::NvsStorage::LockedInputModes::kVolumeOnly)); + + return 1; +} + static const struct luaL_Reg kControlsFuncs[] = {{"schemes", controls_schemes}, + {"locked_schemes", locked_controls_schemes}, {NULL, NULL}}; static auto lua_controls(lua_State* state) -> int { diff --git a/src/tangara/ui/ui_fsm.cpp b/src/tangara/ui/ui_fsm.cpp index 4a54d974..1823f780 100644 --- a/src/tangara/ui/ui_fsm.cpp +++ b/src/tangara/ui/ui_fsm.cpp @@ -667,6 +667,7 @@ void Lua::entry() { "controls", { {"scheme", &sInput->mode()}, + {"locked_scheme", &sInput->lockedMode()}, {"lock_switch", &sLockSwitch}, {"hooks", [&](lua_State* L) { return sInput->pushHooks(L); }}, });