summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcooljqln <cooljqln@noreply.codeberg.org>2024-02-13 00:47:53 +0000
committercooljqln <cooljqln@noreply.codeberg.org>2024-02-13 00:47:53 +0000
commite466522c25758670da335195d60a5d599ed56177 (patch)
tree8a7d29a07c3045b3e6a666468bcad63a7c0ba697 /src
parentcb379f4bc3c51eacf80b786566ab3c2675191164 (diff)
parentffa0894e380874774fb13986b3ff1a48696d304e (diff)
downloadtangara-fw-e466522c25758670da335195d60a5d599ed56177.tar.gz
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>
Diffstat (limited to 'src')
-rw-r--r--src/drivers/include/nvs.hpp4
-rw-r--r--src/drivers/include/relative_wheel.hpp5
-rw-r--r--src/drivers/nvs.cpp14
-rw-r--r--src/drivers/relative_wheel.cpp19
-rw-r--r--src/ui/encoder_input.cpp6
-rw-r--r--src/ui/include/encoder_input.hpp2
-rw-r--r--src/ui/include/ui_fsm.hpp2
-rw-r--r--src/ui/ui_fsm.cpp24
8 files changed, 71 insertions, 5 deletions
diff --git a/src/drivers/include/nvs.hpp b/src/drivers/include/nvs.hpp
index 197591d5..5bd825e5 100644
--- a/src/drivers/include/nvs.hpp
+++ b/src/drivers/include/nvs.hpp
@@ -87,6 +87,9 @@ class NvsStorage {
auto ScreenBrightness() -> uint_fast8_t;
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) -> void;
@@ -118,6 +121,7 @@ class NvsStorage {
Setting<uint8_t> lock_polarity_;
Setting<uint8_t> brightness_;
+ Setting<uint8_t> sensitivity_;
Setting<uint16_t> amp_max_vol_;
Setting<uint16_t> amp_cur_vol_;
Setting<int8_t> amp_left_bias_;
diff --git a/src/drivers/include/relative_wheel.hpp b/src/drivers/include/relative_wheel.hpp
index 88077d08..e1106143 100644
--- a/src/drivers/include/relative_wheel.hpp
+++ b/src/drivers/include/relative_wheel.hpp
@@ -25,6 +25,9 @@ class RelativeWheel {
auto Update() -> void;
auto SetEnabled(bool) -> void;
+ auto SetSensitivity(uint8_t) -> void;
+ auto GetSensitivity() -> uint8_t;
+
auto is_clicking() const -> bool;
auto ticks() const -> std::int_fast16_t;
@@ -36,6 +39,8 @@ class RelativeWheel {
TouchWheel& touch_;
bool is_enabled_;
+ uint8_t sensitivity_;
+ uint8_t threshold_;
bool is_clicking_;
bool was_clicking_;
diff --git a/src/drivers/nvs.cpp b/src/drivers/nvs.cpp
index a304c149..875059be 100644
--- a/src/drivers/nvs.cpp
+++ b/src/drivers/nvs.cpp
@@ -35,6 +35,7 @@ static constexpr char kKeyAmpMaxVolume[] = "hp_vol_max";
static constexpr char kKeyAmpCurrentVolume[] = "hp_vol";
static constexpr char kKeyAmpLeftBias[] = "hp_bias";
static constexpr char kKeyPrimaryInput[] = "in_pri";
+static constexpr char kKeyScrollSensitivity[] = "scroll";
static constexpr char kKeyLockPolarity[] = "lockpol";
static auto nvs_get_string(nvs_handle_t nvs, const char* key)
@@ -161,6 +162,7 @@ NvsStorage::NvsStorage(nvs_handle_t handle)
: handle_(handle),
lock_polarity_(kKeyLockPolarity),
brightness_(kKeyBrightness),
+ sensitivity_(kKeyScrollSensitivity),
amp_max_vol_(kKeyAmpMaxVolume),
amp_cur_vol_(kKeyAmpCurrentVolume),
amp_left_bias_(kKeyAmpLeftBias),
@@ -179,6 +181,7 @@ auto NvsStorage::Read() -> void {
std::lock_guard<std::mutex> lock{mutex_};
lock_polarity_.read(handle_);
brightness_.read(handle_);
+ sensitivity_.read(handle_);
amp_max_vol_.read(handle_);
amp_cur_vol_.read(handle_);
amp_left_bias_.read(handle_);
@@ -192,6 +195,7 @@ auto NvsStorage::Write() -> bool {
std::lock_guard<std::mutex> lock{mutex_};
lock_polarity_.write(handle_);
brightness_.write(handle_);
+ sensitivity_.write(handle_);
amp_max_vol_.write(handle_);
amp_cur_vol_.write(handle_);
amp_left_bias_.write(handle_);
@@ -284,6 +288,16 @@ auto NvsStorage::ScreenBrightness(uint_fast8_t val) -> void {
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 {
std::lock_guard<std::mutex> lock{mutex_};
return amp_max_vol_.get().value_or(wm8523::kDefaultMaxVolume);
diff --git a/src/drivers/relative_wheel.cpp b/src/drivers/relative_wheel.cpp
index 2b8c9b20..e90143ae 100644
--- a/src/drivers/relative_wheel.cpp
+++ b/src/drivers/relative_wheel.cpp
@@ -16,6 +16,8 @@ namespace drivers {
RelativeWheel::RelativeWheel(TouchWheel& touch)
: touch_(touch),
is_enabled_(true),
+ sensitivity_(128),
+ threshold_(10),
is_clicking_(false),
was_clicking_(false),
is_first_read_(true),
@@ -47,12 +49,10 @@ auto RelativeWheel::Update() -> void {
int delta = 128 - last_angle_;
uint8_t rotated_angle = new_angle + delta;
- int threshold = 10;
-
- if (rotated_angle < 128 - threshold) {
+ if (rotated_angle < 128 - threshold_) {
ticks_ = 1;
last_angle_ = new_angle;
- } else if (rotated_angle > 128 + threshold) {
+ } else if (rotated_angle > 128 + threshold_) {
ticks_ = -1;
last_angle_ = new_angle;
} else {
@@ -64,6 +64,17 @@ auto RelativeWheel::SetEnabled(bool en) -> void {
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 {
if (!is_enabled_) {
return false;
diff --git a/src/ui/encoder_input.cpp b/src/ui/encoder_input.cpp
index 5a46cd05..3b5af2c1 100644
--- a/src/ui/encoder_input.cpp
+++ b/src/ui/encoder_input.cpp
@@ -50,6 +50,7 @@ EncoderInput::EncoderInput(drivers::IGpios& gpios, drivers::TouchWheel& wheel)
scroller_(std::make_unique<Scroller>()),
mode_(drivers::NvsStorage::InputModes::kRotatingWheel),
is_locked_(false),
+ scroll_sensitivity_(10),
is_scrolling_wheel_(false) {
lv_indev_drv_init(&driver_);
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 {
if (clicked) {
if (!touch_time_ms_.contains(key)) {
diff --git a/src/ui/include/encoder_input.hpp b/src/ui/include/encoder_input.hpp
index fbd57f32..7dfac071 100644
--- a/src/ui/include/encoder_input.hpp
+++ b/src/ui/include/encoder_input.hpp
@@ -38,6 +38,7 @@ class EncoderInput {
auto registration() -> lv_indev_t* { return registration_; }
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; }
private:
@@ -51,6 +52,7 @@ class EncoderInput {
drivers::NvsStorage::InputModes mode_;
bool is_locked_;
+ uint8_t scroll_sensitivity_;
// Every kind of distinct input that we could map to an action.
enum class Keys {
diff --git a/src/ui/include/ui_fsm.hpp b/src/ui/include/ui_fsm.hpp
index 52ab77a5..ffaff0bb 100644
--- a/src/ui/include/ui_fsm.hpp
+++ b/src/ui/include/ui_fsm.hpp
@@ -128,7 +128,7 @@ class UiState : public tinyfsm::Fsm<UiState> {
static lua::Property sDisplayBrightness;
static lua::Property sControlsScheme;
- static lua::Property sControlSensitivity;
+ static lua::Property sScrollSensitivity;
static lua::Property sDatabaseUpdating;
};
diff --git a/src/ui/ui_fsm.cpp b/src/ui/ui_fsm.cpp
index 728c9756..24145ead 100644
--- a/src/ui/ui_fsm.cpp
+++ b/src/ui/ui_fsm.cpp
@@ -242,6 +242,25 @@ lua::Property UiState::sControlsScheme{
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};
auto UiState::InitBootSplash(drivers::IGpios& gpios) -> bool {
@@ -403,6 +422,10 @@ void Splash::react(const system_fsm::BootComplete& ev) {
sInput->mode(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);
} else {
ESP_LOGE(kTag, "no input devices initialised!");
@@ -466,6 +489,7 @@ void Lua::entry() {
sLua->bridge().AddPropertyModule("controls",
{
{"scheme", &sControlsScheme},
+ {"scroll_sensitivity", &sScrollSensitivity},
});
sLua->bridge().AddPropertyModule(