summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorailurux <ailuruxx@gmail.com>2023-07-08 12:57:55 +1000
committerailurux <ailuruxx@gmail.com>2023-07-08 12:57:55 +1000
commitdaa3013836d619d920db3a9dc1f9cc988047a4b4 (patch)
treee020874816d9fbe5d2f7bb859501297f37b020e3 /src
parenta48516221fe7764b8191695f7710af0b8a3829c5 (diff)
downloadtangara-fw-daa3013836d619d920db3a9dc1f9cc988047a4b4.tar.gz
Touchwheel ticks :)
Diffstat (limited to 'src')
-rw-r--r--src/drivers/relative_wheel.cpp40
-rw-r--r--src/ui/wheel_encoder.cpp18
2 files changed, 21 insertions, 37 deletions
diff --git a/src/drivers/relative_wheel.cpp b/src/drivers/relative_wheel.cpp
index a631cd2e..de982d5a 100644
--- a/src/drivers/relative_wheel.cpp
+++ b/src/drivers/relative_wheel.cpp
@@ -27,8 +27,15 @@ auto RelativeWheel::Update() -> void {
is_clicking_ = d.is_button_touched;
+ if (is_clicking_) {
+ ticks_ = 0;
+ return;
+ }
+
if (!d.is_wheel_touched) {
+ ticks_ = 0;
is_first_read_ = true;
+ return;
}
uint8_t new_angle = d.wheel_position;
@@ -38,33 +45,20 @@ auto RelativeWheel::Update() -> void {
return;
}
- // Work out the magnitude of travel.
- uint8_t change_cw = last_angle_ - new_angle;
- uint8_t change_ccw = new_angle - last_angle_;
- int change = std::min(change_cw, change_ccw);
+ int delta = 128 - last_angle_;
+ uint8_t rotated_angle = new_angle + delta;
+ int threshold = 20;
- last_angle_ = new_angle;
-
- // Round to eliminate noise.
- if (change <= 2) {
+ if (rotated_angle < 128 - threshold) {
+ ticks_ = 1;
+ last_angle_ = new_angle;
+ } else if (rotated_angle > 128 + threshold) {
+ ticks_ = -1;
+ last_angle_ = new_angle;
+ } else {
ticks_ = 0;
- return;
- }
-
- // Quantize into ticks.
- change /= 4;
-
- // Clamp to reliminate more noise.
- if (change > 10) {
- change = 0;
- }
-
- // Work out the direction of travel.
- if (change_cw > change_ccw) {
- change *= -1;
}
- ticks_ = change;
}
auto RelativeWheel::is_clicking() -> bool {
diff --git a/src/ui/wheel_encoder.cpp b/src/ui/wheel_encoder.cpp
index 243772d7..a0e12b7f 100644
--- a/src/ui/wheel_encoder.cpp
+++ b/src/ui/wheel_encoder.cpp
@@ -25,7 +25,7 @@ TouchWheelEncoder::TouchWheelEncoder(
std::weak_ptr<drivers::RelativeWheel> wheel)
: last_key_(0), wheel_(wheel) {
lv_indev_drv_init(&driver_);
- driver_.type = LV_INDEV_TYPE_KEYPAD;
+ driver_.type = LV_INDEV_TYPE_ENCODER;
driver_.read_cb = encoder_read;
// driver_.feedback_cb = encoder_feedback;
driver_.user_data = this;
@@ -41,19 +41,9 @@ auto TouchWheelEncoder::Read(lv_indev_data_t* data) -> void {
lock->Update();
- auto ticks = lock->ticks();
- if (ticks > 0) {
- data->key = LV_KEY_PREV;
- data->state = LV_INDEV_STATE_PRESSED;
- } else if (ticks < 0) {
- data->key = LV_KEY_NEXT;
- data->state = LV_INDEV_STATE_PRESSED;
- } else if (lock->is_clicking()) {
- data->key = LV_KEY_ENTER;
- data->state = LV_INDEV_STATE_PRESSED;
- } else {
- data->state = LV_INDEV_STATE_RELEASED;
- }
+ data->enc_diff = lock->ticks();
+ data->state =
+ lock->is_clicking() ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
}
} // namespace ui