summaryrefslogtreecommitdiff
path: root/src/tangara/input/input_touch_wheel.cpp
blob: ae192e3125179ad350c1bff0def3915606113c43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * Copyright 2024 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "input/input_touch_wheel.hpp"

#include <cstdint>
#include <variant>

#include "indev/lv_indev.h"

#include "drivers/haptics.hpp"
#include "drivers/nvs.hpp"
#include "drivers/touchwheel.hpp"
#include "events/event_queue.hpp"
#include "input/input_device.hpp"
#include "input/input_hook_actions.hpp"
#include "input/input_trigger.hpp"
#include "lua/property.hpp"
#include "ui/ui_events.hpp"

#include "esp_timer.h"

namespace input {

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;
                   }),
      centre_("centre", actions::select(), {}, {}, {}),
      up_("up", {}, {}, actions::scrollToTop(), actions::scrollToTop()),
      right_("right", {}),
      down_("down",
            {},
            {},
            actions::scrollToBottom(),
            actions::scrollToBottom()),
      left_("left", {}, {}, actions::goBack(), {}),
      locked_(false),
      is_scrolling_(false),
      threshold_(calculateThreshold(nvs.ScrollSensitivity())),
      is_first_read_(true),
      last_angle_(0),
      last_wheel_touch_time_(0) {}

auto TouchWheel::read(lv_indev_data_t* data, std::vector<InputEvent>& events) -> void {
  if (locked_) {
    return;
  }

  wheel_.Update();
  auto wheel_data = wheel_.GetTouchWheelData();
  int8_t ticks = calculateTicks(wheel_data);

  if (!wheel_data.is_wheel_touched) {
    // User has released the wheel.
    is_scrolling_ = false;
    data->enc_diff = 0;
  } else if (ticks != 0) {
    // User is touching the wheel, and has just passed the sensitivity
    // threshold for a scroll tick.
    is_scrolling_ = true;
    data->enc_diff = ticks;
  } else {
    // User is touching the wheel, but hasn't moved.
    data->enc_diff = 0;
  }

  // Prevent accidental center button touches while scrolling
  if (wheel_data.is_wheel_touched) {
    last_wheel_touch_time_ = esp_timer_get_time();
  }

  bool wheel_touch_timed_out =
      esp_timer_get_time() - last_wheel_touch_time_ > SCROLL_TIMEOUT_US;

  auto centre_state = centre_.update(wheel_touch_timed_out && wheel_data.is_button_touched &&
                     !wheel_data.is_wheel_touched,
                 data);
  if (centre_state == input::Trigger::State::kPress) {
    events.push_back(InputEvent::kOnPress);
  } else if (centre_state == input::Trigger::State::kLongPress) {
    events.push_back(InputEvent::kOnLongPress);
  }

  // If the user is touching the wheel but not scrolling, then they may be
  // clicking on one of the wheel's cardinal directions.
  if (is_scrolling_) {
    up_.cancel();
    right_.cancel();
    down_.cancel();
    left_.cancel();
  } else {
    bool pressing = wheel_data.is_wheel_touched;
    up_.update(pressing && drivers::TouchWheel::isAngleWithin(
                               wheel_data.wheel_position, 0, 32),
               data);
    right_.update(pressing && drivers::TouchWheel::isAngleWithin(
                                  wheel_data.wheel_position, 192, 32),
                  data);
    down_.update(pressing && drivers::TouchWheel::isAngleWithin(
                                 wheel_data.wheel_position, 128, 32),
                 data);
    left_.update(pressing && drivers::TouchWheel::isAngleWithin(
                                 wheel_data.wheel_position, 64, 32),
                 data);
  }
}

auto TouchWheel::name() -> std::string {
  return "wheel";
}

auto TouchWheel::triggers()
    -> std::vector<std::reference_wrapper<TriggerHooks>> {
  return {centre_, up_, right_, down_, left_};
}

auto TouchWheel::onLock() -> void {
  wheel_.LowPowerMode(true);
  locked_ = true;
}

auto TouchWheel::onUnlock() -> void {
  wheel_.LowPowerMode(false);
  wheel_.Recalibrate();
  locked_ = false;
}

auto TouchWheel::sensitivity() -> lua::Property& {
  return sensitivity_;
}

auto TouchWheel::calculateTicks(const drivers::TouchWheelData& data) -> int8_t {
  if (!data.is_wheel_touched) {
    is_first_read_ = true;
    return 0;
  }

  uint8_t new_angle = data.wheel_position;
  if (is_first_read_) {
    is_first_read_ = false;
    last_angle_ = new_angle;
    return 0;
  }

  int delta = 128 - last_angle_;
  uint8_t rotated_angle = new_angle + delta;
  if (rotated_angle < 128 - threshold_) {
    last_angle_ = new_angle;
    return 1;
  } else if (rotated_angle > 128 + threshold_) {
    last_angle_ = new_angle;
    return -1;
  } else {
    return 0;
  }
}

auto TouchWheel::calculateThreshold(uint8_t sensitivity) -> uint8_t {
  int tmax = 35;
  int tmin = 5;
  return (((255. - sensitivity) / 255.) * (tmax - tmin) + tmin);
}

}  // namespace input