Merge pull request 'scroll-sensitivity' (#36) from scroll-sensitivity into main

Reviewed-on: https://codeberg.org/cool-tech-zone/tangara-fw/pulls/36
Reviewed-by: cooljqln <cooljqln@noreply.codeberg.org>
custom
cooljqln 1 year ago
commit e466522c25
  1. 15
      lua/settings.lua
  2. 4
      src/drivers/include/nvs.hpp
  3. 5
      src/drivers/include/relative_wheel.hpp
  4. 14
      src/drivers/nvs.cpp
  5. 19
      src/drivers/relative_wheel.cpp
  6. 6
      src/ui/encoder_input.cpp
  7. 2
      src/ui/include/encoder_input.hpp
  8. 2
      src/ui/include/ui_fsm.hpp
  9. 24
      src/ui/ui_fsm.cpp

@ -256,6 +256,21 @@ function settings.input()
controls.scheme:set(scheme) controls.scheme:set(scheme)
end) end)
menu.content:Label {
text = "Scroll Sensitivity",
}:add_style(theme.settings_title)
local slider_scale = 4; -- Power steering
local sensitivity = menu.content:Slider {
w = lvgl.PCT(90),
h = 5,
range = { min = 0, max = 255/slider_scale },
value = controls.scroll_sensitivity:get()/slider_scale,
}
sensitivity:onevent(lvgl.EVENT.VALUE_CHANGED, function()
controls.scroll_sensitivity:set(sensitivity:value()*slider_scale)
end)
return menu return menu
end end

@ -87,6 +87,9 @@ class NvsStorage {
auto ScreenBrightness() -> uint_fast8_t; auto ScreenBrightness() -> uint_fast8_t;
auto ScreenBrightness(uint_fast8_t) -> void; auto ScreenBrightness(uint_fast8_t) -> void;
auto ScrollSensitivity() -> uint_fast8_t;
auto ScrollSensitivity(uint_fast8_t) -> void;
auto AmpMaxVolume() -> uint16_t; auto AmpMaxVolume() -> uint16_t;
auto AmpMaxVolume(uint16_t) -> void; auto AmpMaxVolume(uint16_t) -> void;
@ -118,6 +121,7 @@ class NvsStorage {
Setting<uint8_t> lock_polarity_; Setting<uint8_t> lock_polarity_;
Setting<uint8_t> brightness_; Setting<uint8_t> brightness_;
Setting<uint8_t> sensitivity_;
Setting<uint16_t> amp_max_vol_; Setting<uint16_t> amp_max_vol_;
Setting<uint16_t> amp_cur_vol_; Setting<uint16_t> amp_cur_vol_;
Setting<int8_t> amp_left_bias_; Setting<int8_t> amp_left_bias_;

@ -25,6 +25,9 @@ class RelativeWheel {
auto Update() -> void; auto Update() -> void;
auto SetEnabled(bool) -> void; auto SetEnabled(bool) -> void;
auto SetSensitivity(uint8_t) -> void;
auto GetSensitivity() -> uint8_t;
auto is_clicking() const -> bool; auto is_clicking() const -> bool;
auto ticks() const -> std::int_fast16_t; auto ticks() const -> std::int_fast16_t;
@ -36,6 +39,8 @@ class RelativeWheel {
TouchWheel& touch_; TouchWheel& touch_;
bool is_enabled_; bool is_enabled_;
uint8_t sensitivity_;
uint8_t threshold_;
bool is_clicking_; bool is_clicking_;
bool was_clicking_; bool was_clicking_;

@ -35,6 +35,7 @@ static constexpr char kKeyAmpMaxVolume[] = "hp_vol_max";
static constexpr char kKeyAmpCurrentVolume[] = "hp_vol"; static constexpr char kKeyAmpCurrentVolume[] = "hp_vol";
static constexpr char kKeyAmpLeftBias[] = "hp_bias"; static constexpr char kKeyAmpLeftBias[] = "hp_bias";
static constexpr char kKeyPrimaryInput[] = "in_pri"; static constexpr char kKeyPrimaryInput[] = "in_pri";
static constexpr char kKeyScrollSensitivity[] = "scroll";
static constexpr char kKeyLockPolarity[] = "lockpol"; static constexpr char kKeyLockPolarity[] = "lockpol";
static auto nvs_get_string(nvs_handle_t nvs, const char* key) static auto nvs_get_string(nvs_handle_t nvs, const char* key)
@ -161,6 +162,7 @@ NvsStorage::NvsStorage(nvs_handle_t handle)
: handle_(handle), : handle_(handle),
lock_polarity_(kKeyLockPolarity), lock_polarity_(kKeyLockPolarity),
brightness_(kKeyBrightness), brightness_(kKeyBrightness),
sensitivity_(kKeyScrollSensitivity),
amp_max_vol_(kKeyAmpMaxVolume), amp_max_vol_(kKeyAmpMaxVolume),
amp_cur_vol_(kKeyAmpCurrentVolume), amp_cur_vol_(kKeyAmpCurrentVolume),
amp_left_bias_(kKeyAmpLeftBias), amp_left_bias_(kKeyAmpLeftBias),
@ -179,6 +181,7 @@ auto NvsStorage::Read() -> void {
std::lock_guard<std::mutex> lock{mutex_}; std::lock_guard<std::mutex> lock{mutex_};
lock_polarity_.read(handle_); lock_polarity_.read(handle_);
brightness_.read(handle_); brightness_.read(handle_);
sensitivity_.read(handle_);
amp_max_vol_.read(handle_); amp_max_vol_.read(handle_);
amp_cur_vol_.read(handle_); amp_cur_vol_.read(handle_);
amp_left_bias_.read(handle_); amp_left_bias_.read(handle_);
@ -192,6 +195,7 @@ auto NvsStorage::Write() -> bool {
std::lock_guard<std::mutex> lock{mutex_}; std::lock_guard<std::mutex> lock{mutex_};
lock_polarity_.write(handle_); lock_polarity_.write(handle_);
brightness_.write(handle_); brightness_.write(handle_);
sensitivity_.write(handle_);
amp_max_vol_.write(handle_); amp_max_vol_.write(handle_);
amp_cur_vol_.write(handle_); amp_cur_vol_.write(handle_);
amp_left_bias_.write(handle_); amp_left_bias_.write(handle_);
@ -284,6 +288,16 @@ auto NvsStorage::ScreenBrightness(uint_fast8_t val) -> void {
brightness_.set(val); brightness_.set(val);
} }
auto NvsStorage::ScrollSensitivity() -> uint_fast8_t {
std::lock_guard<std::mutex> lock{mutex_};
return std::clamp<uint8_t>(sensitivity_.get().value_or(128), 0, 255);
}
auto NvsStorage::ScrollSensitivity(uint_fast8_t val) -> void {
std::lock_guard<std::mutex> lock{mutex_};
sensitivity_.set(val);
}
auto NvsStorage::AmpMaxVolume() -> uint16_t { auto NvsStorage::AmpMaxVolume() -> uint16_t {
std::lock_guard<std::mutex> lock{mutex_}; std::lock_guard<std::mutex> lock{mutex_};
return amp_max_vol_.get().value_or(wm8523::kDefaultMaxVolume); return amp_max_vol_.get().value_or(wm8523::kDefaultMaxVolume);

@ -16,6 +16,8 @@ namespace drivers {
RelativeWheel::RelativeWheel(TouchWheel& touch) RelativeWheel::RelativeWheel(TouchWheel& touch)
: touch_(touch), : touch_(touch),
is_enabled_(true), is_enabled_(true),
sensitivity_(128),
threshold_(10),
is_clicking_(false), is_clicking_(false),
was_clicking_(false), was_clicking_(false),
is_first_read_(true), is_first_read_(true),
@ -47,12 +49,10 @@ auto RelativeWheel::Update() -> void {
int delta = 128 - last_angle_; int delta = 128 - last_angle_;
uint8_t rotated_angle = new_angle + delta; uint8_t rotated_angle = new_angle + delta;
int threshold = 10; if (rotated_angle < 128 - threshold_) {
if (rotated_angle < 128 - threshold) {
ticks_ = 1; ticks_ = 1;
last_angle_ = new_angle; last_angle_ = new_angle;
} else if (rotated_angle > 128 + threshold) { } else if (rotated_angle > 128 + threshold_) {
ticks_ = -1; ticks_ = -1;
last_angle_ = new_angle; last_angle_ = new_angle;
} else { } else {
@ -64,6 +64,17 @@ auto RelativeWheel::SetEnabled(bool en) -> void {
is_enabled_ = en; is_enabled_ = en;
} }
auto RelativeWheel::SetSensitivity(uint8_t val) -> void {
sensitivity_ = val;
int tmax = 35;
int tmin = 5;
threshold_ = (((255. - sensitivity_)/255.)*(tmax - tmin) + tmin);
}
auto RelativeWheel::GetSensitivity() -> uint8_t {
return sensitivity_;
}
auto RelativeWheel::is_clicking() const -> bool { auto RelativeWheel::is_clicking() const -> bool {
if (!is_enabled_) { if (!is_enabled_) {
return false; return false;

@ -50,6 +50,7 @@ EncoderInput::EncoderInput(drivers::IGpios& gpios, drivers::TouchWheel& wheel)
scroller_(std::make_unique<Scroller>()), scroller_(std::make_unique<Scroller>()),
mode_(drivers::NvsStorage::InputModes::kRotatingWheel), mode_(drivers::NvsStorage::InputModes::kRotatingWheel),
is_locked_(false), is_locked_(false),
scroll_sensitivity_(10),
is_scrolling_wheel_(false) { is_scrolling_wheel_(false) {
lv_indev_drv_init(&driver_); lv_indev_drv_init(&driver_);
driver_.type = LV_INDEV_TYPE_ENCODER; driver_.type = LV_INDEV_TYPE_ENCODER;
@ -272,6 +273,11 @@ auto EncoderInput::Read(lv_indev_data_t* data) -> void {
} }
} }
auto EncoderInput::scroll_sensitivity(uint8_t val) -> void {
scroll_sensitivity_ = val;
relative_wheel_->SetSensitivity(scroll_sensitivity_);
}
auto EncoderInput::UpdateKeyState(Keys key, uint64_t ms, bool clicked) -> void { auto EncoderInput::UpdateKeyState(Keys key, uint64_t ms, bool clicked) -> void {
if (clicked) { if (clicked) {
if (!touch_time_ms_.contains(key)) { if (!touch_time_ms_.contains(key)) {

@ -38,6 +38,7 @@ class EncoderInput {
auto registration() -> lv_indev_t* { return registration_; } auto registration() -> lv_indev_t* { return registration_; }
auto mode(drivers::NvsStorage::InputModes mode) { mode_ = mode; } auto mode(drivers::NvsStorage::InputModes mode) { mode_ = mode; }
auto scroll_sensitivity(uint8_t val) -> void; // Value between 0-255, used to scale the threshold
auto lock(bool l) -> void { is_locked_ = l; } auto lock(bool l) -> void { is_locked_ = l; }
private: private:
@ -51,6 +52,7 @@ class EncoderInput {
drivers::NvsStorage::InputModes mode_; drivers::NvsStorage::InputModes mode_;
bool is_locked_; bool is_locked_;
uint8_t scroll_sensitivity_;
// Every kind of distinct input that we could map to an action. // Every kind of distinct input that we could map to an action.
enum class Keys { enum class Keys {

@ -128,7 +128,7 @@ class UiState : public tinyfsm::Fsm<UiState> {
static lua::Property sDisplayBrightness; static lua::Property sDisplayBrightness;
static lua::Property sControlsScheme; static lua::Property sControlsScheme;
static lua::Property sControlSensitivity; static lua::Property sScrollSensitivity;
static lua::Property sDatabaseUpdating; static lua::Property sDatabaseUpdating;
}; };

@ -242,6 +242,25 @@ lua::Property UiState::sControlsScheme{
return true; return true;
}}; }};
lua::Property UiState::sScrollSensitivity{
0, [](const lua::LuaValue& val) {
std::optional<int> sensitivity = 0;
std::visit(
[&](auto&& v) {
using T = std::decay_t<decltype(v)>;
if constexpr (std::is_same_v<T, int>) {
sensitivity = v;
}
},
val);
if (!sensitivity) {
return false;
}
sInput->scroll_sensitivity(*sensitivity);
sServices->nvs().ScrollSensitivity(*sensitivity);
return true;
}};
lua::Property UiState::sDatabaseUpdating{false}; lua::Property UiState::sDatabaseUpdating{false};
auto UiState::InitBootSplash(drivers::IGpios& gpios) -> bool { auto UiState::InitBootSplash(drivers::IGpios& gpios) -> bool {
@ -403,6 +422,10 @@ void Splash::react(const system_fsm::BootComplete& ev) {
sInput->mode(mode); sInput->mode(mode);
sControlsScheme.Update(static_cast<int>(mode)); sControlsScheme.Update(static_cast<int>(mode));
auto sensitivity = sServices->nvs().ScrollSensitivity();
sInput->scroll_sensitivity(sensitivity);
sScrollSensitivity.Update(static_cast<int>(sensitivity));
sTask->input(sInput); sTask->input(sInput);
} else { } else {
ESP_LOGE(kTag, "no input devices initialised!"); ESP_LOGE(kTag, "no input devices initialised!");
@ -466,6 +489,7 @@ void Lua::entry() {
sLua->bridge().AddPropertyModule("controls", sLua->bridge().AddPropertyModule("controls",
{ {
{"scheme", &sControlsScheme}, {"scheme", &sControlsScheme},
{"scroll_sensitivity", &sScrollSensitivity},
}); });
sLua->bridge().AddPropertyModule( sLua->bridge().AddPropertyModule(

Loading…
Cancel
Save