blob: 2b8c9b2017d56269322b875f08b1b200e39aa84a (
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
|
/*
* Copyright 2023 jacqueline <me@jacqueline.id.au>
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#include "relative_wheel.hpp"
#include <stdint.h>
#include <cstdint>
#include "esp_log.h"
namespace drivers {
RelativeWheel::RelativeWheel(TouchWheel& touch)
: touch_(touch),
is_enabled_(true),
is_clicking_(false),
was_clicking_(false),
is_first_read_(true),
ticks_(0),
last_angle_(0) {}
auto RelativeWheel::Update() -> void {
TouchWheelData d = touch_.GetTouchWheelData();
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;
if (is_first_read_) {
is_first_read_ = false;
last_angle_ = new_angle;
return;
}
int delta = 128 - last_angle_;
uint8_t rotated_angle = new_angle + delta;
int threshold = 10;
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;
}
}
auto RelativeWheel::SetEnabled(bool en) -> void {
is_enabled_ = en;
}
auto RelativeWheel::is_clicking() const -> bool {
if (!is_enabled_) {
return false;
}
return is_clicking_;
}
auto RelativeWheel::ticks() const -> std::int_fast16_t {
if (!is_enabled_) {
return 0;
}
return ticks_;
}
} // namespace drivers
|