diff options
| author | jacqueline <me@jacqueline.id.au> | 2024-04-11 15:16:35 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2024-04-11 15:16:35 +1000 |
| commit | 33919e9e3f419e13318fa6b8217d8c8dcd86c1eb (patch) | |
| tree | 9e3a1209c8f17f9a6d57249fae7067cbb81e0227 /src/input/input_touch_wheel.cpp | |
| parent | ed82063af5f83530afa5cfb5bf5bd516f3d05f2a (diff) | |
| download | tangara-fw-33919e9e3f419e13318fa6b8217d8c8dcd86c1eb.tar.gz | |
Migrate all existing control schemes to the cool new world
Diffstat (limited to 'src/input/input_touch_wheel.cpp')
| -rw-r--r-- | src/input/input_touch_wheel.cpp | 76 |
1 files changed, 69 insertions, 7 deletions
diff --git a/src/input/input_touch_wheel.cpp b/src/input/input_touch_wheel.cpp index 302a17aa..7670e342 100644 --- a/src/input/input_touch_wheel.cpp +++ b/src/input/input_touch_wheel.cpp @@ -8,27 +8,46 @@ #include <stdint.h> #include <cstdint> +#include <variant> +#include "event_queue.hpp" #include "hal/lv_hal_indev.h" #include "haptics.hpp" #include "input_device.hpp" +#include "input_trigger.hpp" +#include "nvs.hpp" +#include "property.hpp" #include "touchwheel.hpp" +#include "ui_events.hpp" namespace input { -TouchWheel::TouchWheel(drivers::TouchWheel& wheel) - : wheel_(wheel), +TouchWheel::TouchWheel(drivers::NvsStorage& nvs, drivers::TouchWheel& wheel) + : nvs_(nvs), + wheel_(wheel), + sensitivity_(static_cast<int>(nvs.ScrollSensitivity()), + [&](const lua::LuaValue& val) { + if (!std::holds_alternative<int>(val)) { + return false; + } + int int_val = std::get<int>(val); + if (int_val < 0 || int_val > UINT8_MAX) { + return false; + } + nvs.ScrollSensitivity(int_val); + threshold_ = calculateThreshold(int_val); + return true; + }), is_scrolling_(false), - sensitivity_(128), - threshold_(10), + threshold_(calculateThreshold(nvs.ScrollSensitivity())), is_first_read_(true), last_angle_(0) {} auto TouchWheel::read(lv_indev_data_t* data) -> void { wheel_.Update(); auto wheel_data = wheel_.GetTouchWheelData(); - int8_t ticks = calculate_ticks(wheel_data); + int8_t ticks = calculateTicks(wheel_data); if (!wheel_data.is_wheel_touched) { // User has released the wheel. @@ -49,10 +68,47 @@ auto TouchWheel::read(lv_indev_data_t* data) -> void { } else { data->state = LV_INDEV_STATE_RELEASED; } + + // If the user is touching the wheel but not scrolling, then they may be + // clicking on one of the wheel's cardinal directions. + bool pressing = wheel_data.is_wheel_touched && !is_scrolling_; + + switch (up_.update(pressing && drivers::TouchWheel::isAngleWithin( + wheel_data.wheel_position, 0, 32))) { + case Trigger::State::kLongPress: + data->enc_diff = INT16_MIN; + break; + default: + break; + } + switch (right_.update(pressing && drivers::TouchWheel::isAngleWithin( + wheel_data.wheel_position, 192, 32))) { + default: + break; + } + switch (down_.update(pressing && drivers::TouchWheel::isAngleWithin( + wheel_data.wheel_position, 128, 32))) { + case Trigger::State::kLongPress: + data->enc_diff = INT16_MAX; + break; + default: + break; + } + switch (left_.update(pressing && drivers::TouchWheel::isAngleWithin( + wheel_data.wheel_position, 64, 32))) { + case Trigger::State::kLongPress: + events::Ui().Dispatch(ui::internal::BackPressed{}); + break; + default: + break; + } +} + +auto TouchWheel::sensitivity() -> lua::Property& { + return sensitivity_; } -auto TouchWheel::calculate_ticks(const drivers::TouchWheelData& data) - -> int8_t { +auto TouchWheel::calculateTicks(const drivers::TouchWheelData& data) -> int8_t { if (!data.is_wheel_touched) { is_first_read_ = true; return 0; @@ -78,4 +134,10 @@ auto TouchWheel::calculate_ticks(const drivers::TouchWheelData& data) } } +auto TouchWheel::calculateThreshold(uint8_t sensitivity) -> uint8_t { + int tmax = 35; + int tmin = 5; + return (((255. - sensitivity) / 255.) * (tmax - tmin) + tmin); +} + } // namespace input |
