summaryrefslogtreecommitdiff
path: root/src/drivers/relative_wheel.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-06-01 15:28:32 +1000
committerjacqueline <me@jacqueline.id.au>2023-06-01 15:28:54 +1000
commit6fd588e970470b15936187980829916d0dbe77bb (patch)
tree1b1e73ef52bef2e41499ee5ceadc45efd408050b /src/drivers/relative_wheel.cpp
parentdb2e29a72d9b934e7b58f1d20ac3768eae484ab5 (diff)
downloadtangara-fw-6fd588e970470b15936187980829916d0dbe77bb.tar.gz
Add touchwheel -> encoder adapter
Diffstat (limited to 'src/drivers/relative_wheel.cpp')
-rw-r--r--src/drivers/relative_wheel.cpp78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/drivers/relative_wheel.cpp b/src/drivers/relative_wheel.cpp
new file mode 100644
index 00000000..f64d213d
--- /dev/null
+++ b/src/drivers/relative_wheel.cpp
@@ -0,0 +1,78 @@
+/*
+ * 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_pressed_(false),
+ is_first_read_(true),
+ ticks_(0),
+ last_angle_(0) {}
+
+auto RelativeWheel::Update() -> void {
+ touch_->Update();
+ TouchWheelData d = touch_->GetTouchWheelData();
+ is_pressed_ = d.is_touched;
+
+ uint8_t new_angle = d.wheel_position;
+ if (is_first_read_) {
+ is_first_read_ = false;
+ last_angle_ = new_angle;
+ 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);
+
+ last_angle_ = new_angle;
+
+ // Round to eliminate noise.
+ if (change <= 2) {
+ 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_pressed() -> bool {
+ return is_pressed_;
+}
+
+auto RelativeWheel::ticks() -> std::int_fast16_t {
+ int_fast16_t t = ticks_;
+ if (t != 0) {
+ ESP_LOGI("teeks", "ticks %d", t);
+ }
+ ticks_ = 0;
+ return t;
+}
+
+
+} // namespace drivers