summaryrefslogtreecommitdiff
path: root/src/ui/encoder_input.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-10-03 10:37:29 +1100
committerjacqueline <me@jacqueline.id.au>2023-10-03 10:37:29 +1100
commit7d5536e2abca61f503ed68521603bd30700a7e5e (patch)
treefc8b191a5d248a86de08a00d814359f335423e95 /src/ui/encoder_input.cpp
parentf2bad894cdac88b94a358cebdb062f426b191d1b (diff)
downloadtangara-fw-7d5536e2abca61f503ed68521603bd30700a7e5e.tar.gz
Generalise the lvgl input driver in preparation for more input methods
Diffstat (limited to 'src/ui/encoder_input.cpp')
-rw-r--r--src/ui/encoder_input.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/ui/encoder_input.cpp b/src/ui/encoder_input.cpp
new file mode 100644
index 00000000..62cb4c8b
--- /dev/null
+++ b/src/ui/encoder_input.cpp
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2023 jacqueline <me@jacqueline.id.au>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#include "encoder_input.hpp"
+
+#include <sys/_stdint.h>
+#include <memory>
+
+#include "core/lv_group.h"
+#include "gpios.hpp"
+#include "hal/lv_hal_indev.h"
+#include "relative_wheel.hpp"
+#include "touchwheel.hpp"
+
+namespace ui {
+
+static void encoder_read(lv_indev_drv_t* drv, lv_indev_data_t* data) {
+ EncoderInput* instance = reinterpret_cast<EncoderInput*>(drv->user_data);
+ instance->Read(data);
+}
+
+EncoderInput::EncoderInput(drivers::IGpios& gpios, drivers::TouchWheel& wheel)
+ : gpios_(gpios),
+ raw_wheel_(wheel),
+ relative_wheel_(std::make_unique<drivers::RelativeWheel>(wheel)) {
+ lv_indev_drv_init(&driver_);
+ driver_.type = LV_INDEV_TYPE_ENCODER;
+ driver_.read_cb = encoder_read;
+ driver_.user_data = this;
+
+ registration_ = lv_indev_drv_register(&driver_);
+}
+
+auto EncoderInput::Read(lv_indev_data_t* data) -> void {
+ raw_wheel_.Update();
+ relative_wheel_->Update();
+
+ data->enc_diff = relative_wheel_->ticks();
+ data->state = relative_wheel_->is_clicking() ? LV_INDEV_STATE_PRESSED
+ : LV_INDEV_STATE_RELEASED;
+}
+
+} // namespace ui