Trackball rewrite (#600)

- Implement generic trackball settings
- Refactor T-Deck trackball driver into generic driver at `Drivers/gpio-trackball-module`
- Automatically bind/unbind trackball devices with LVGL
- Automatically load settings for trackball devices on boot
This commit is contained in:
Ken Van Hoeylandt
2026-07-30 13:38:19 +02:00
committed by GitHub
parent de6fc3b346
commit c729e8340f
39 changed files with 1194 additions and 666 deletions
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.20)
include("${CMAKE_CURRENT_LIST_DIR}/../../Buildscripts/module.cmake")
file(GLOB_RECURSE SOURCE_FILES "source/*.c*")
tactility_add_module(gpio-trackball-module
SRCS ${SOURCE_FILES}
INCLUDE_DIRS include/
REQUIRES TactilityKernel
)
@@ -0,0 +1,195 @@
Apache License
==============
_Version 2.0, January 2004_
_&lt;<http://www.apache.org/licenses/>&gt;_
### Terms and Conditions for use, reproduction, and distribution
#### 1. Definitions
“License” shall mean the terms and conditions for use, reproduction, and
distribution as defined by Sections 1 through 9 of this document.
“Licensor” shall mean the copyright owner or entity authorized by the copyright
owner that is granting the License.
“Legal Entity” shall mean the union of the acting entity and all other entities
that control, are controlled by, or are under common control with that entity.
For the purposes of this definition, “control” means **(i)** the power, direct or
indirect, to cause the direction or management of such entity, whether by
contract or otherwise, or **(ii)** ownership of fifty percent (50%) or more of the
outstanding shares, or **(iii)** beneficial ownership of such entity.
“You” (or “Your”) shall mean an individual or Legal Entity exercising
permissions granted by this License.
“Source” form shall mean the preferred form for making modifications, including
but not limited to software source code, documentation source, and configuration
files.
“Object” form shall mean any form resulting from mechanical transformation or
translation of a Source form, including but not limited to compiled object code,
generated documentation, and conversions to other media types.
“Work” shall mean the work of authorship, whether in Source or Object form, made
available under the License, as indicated by a copyright notice that is included
in or attached to the work (an example is provided in the Appendix below).
“Derivative Works” shall mean any work, whether in Source or Object form, that
is based on (or derived from) the Work and for which the editorial revisions,
annotations, elaborations, or other modifications represent, as a whole, an
original work of authorship. For the purposes of this License, Derivative Works
shall not include works that remain separable from, or merely link (or bind by
name) to the interfaces of, the Work and Derivative Works thereof.
“Contribution” shall mean any work of authorship, including the original version
of the Work and any modifications or additions to that Work or Derivative Works
thereof, that is intentionally submitted to Licensor for inclusion in the Work
by the copyright owner or by an individual or Legal Entity authorized to submit
on behalf of the copyright owner. For the purposes of this definition,
“submitted” means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems, and
issue tracking systems that are managed by, or on behalf of, the Licensor for
the purpose of discussing and improving the Work, but excluding communication
that is conspicuously marked or otherwise designated in writing by the copyright
owner as “Not a Contribution.”
“Contributor” shall mean Licensor and any individual or Legal Entity on behalf
of whom a Contribution has been received by Licensor and subsequently
incorporated within the Work.
#### 2. Grant of Copyright License
Subject to the terms and conditions of this License, each Contributor hereby
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
irrevocable copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the Work and such
Derivative Works in Source or Object form.
#### 3. Grant of Patent License
Subject to the terms and conditions of this License, each Contributor hereby
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
irrevocable (except as stated in this section) patent license to make, have
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
such license applies only to those patent claims licensable by such Contributor
that are necessarily infringed by their Contribution(s) alone or by combination
of their Contribution(s) with the Work to which such Contribution(s) was
submitted. If You institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
Contribution incorporated within the Work constitutes direct or contributory
patent infringement, then any patent licenses granted to You under this License
for that Work shall terminate as of the date such litigation is filed.
#### 4. Redistribution
You may reproduce and distribute copies of the Work or Derivative Works thereof
in any medium, with or without modifications, and in Source or Object form,
provided that You meet the following conditions:
* **(a)** You must give any other recipients of the Work or Derivative Works a copy of
this License; and
* **(b)** You must cause any modified files to carry prominent notices stating that You
changed the files; and
* **(c)** You must retain, in the Source form of any Derivative Works that You distribute,
all copyright, patent, trademark, and attribution notices from the Source form
of the Work, excluding those notices that do not pertain to any part of the
Derivative Works; and
* **(d)** If the Work includes a “NOTICE” text file as part of its distribution, then any
Derivative Works that You distribute must include a readable copy of the
attribution notices contained within such NOTICE file, excluding those notices
that do not pertain to any part of the Derivative Works, in at least one of the
following places: within a NOTICE text file distributed as part of the
Derivative Works; within the Source form or documentation, if provided along
with the Derivative Works; or, within a display generated by the Derivative
Works, if and wherever such third-party notices normally appear. The contents of
the NOTICE file are for informational purposes only and do not modify the
License. You may add Your own attribution notices within Derivative Works that
You distribute, alongside or as an addendum to the NOTICE text from the Work,
provided that such additional attribution notices cannot be construed as
modifying the License.
You may add Your own copyright statement to Your modifications and may provide
additional or different license terms and conditions for use, reproduction, or
distribution of Your modifications, or for any such Derivative Works as a whole,
provided Your use, reproduction, and distribution of the Work otherwise complies
with the conditions stated in this License.
#### 5. Submission of Contributions
Unless You explicitly state otherwise, any Contribution intentionally submitted
for inclusion in the Work by You to the Licensor shall be under the terms and
conditions of this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify the terms of
any separate license agreement you may have executed with Licensor regarding
such Contributions.
#### 6. Trademarks
This License does not grant permission to use the trade names, trademarks,
service marks, or product names of the Licensor, except as required for
reasonable and customary use in describing the origin of the Work and
reproducing the content of the NOTICE file.
#### 7. Disclaimer of Warranty
Unless required by applicable law or agreed to in writing, Licensor provides the
Work (and each Contributor provides its Contributions) on an “AS IS” BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
including, without limitation, any warranties or conditions of TITLE,
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
solely responsible for determining the appropriateness of using or
redistributing the Work and assume any risks associated with Your exercise of
permissions under this License.
#### 8. Limitation of Liability
In no event and under no legal theory, whether in tort (including negligence),
contract, or otherwise, unless required by applicable law (such as deliberate
and grossly negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special, incidental,
or consequential damages of any character arising as a result of this License or
out of the use or inability to use the Work (including but not limited to
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
any and all other commercial damages or losses), even if such Contributor has
been advised of the possibility of such damages.
#### 9. Accepting Warranty or Additional Liability
While redistributing the Work or Derivative Works thereof, You may choose to
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
other liability obligations and/or rights consistent with this License. However,
in accepting such obligations, You may act only on Your own behalf and on Your
sole responsibility, not on behalf of any other Contributor, and only if You
agree to indemnify, defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason of your
accepting any such warranty or additional liability.
_END OF TERMS AND CONDITIONS_
### APPENDIX: How to apply the Apache License to your work
To apply the Apache License to your work, attach the following boilerplate
notice, with the fields enclosed by brackets `[]` replaced with your own
identifying information. (Don't include the brackets!) The text should be
enclosed in the appropriate comment syntax for the file format. We also
recommend that a file or class name and description of purpose be included on
the same “printed page” as the copyright notice for easier identification within
third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+27
View File
@@ -0,0 +1,27 @@
# GPIO Trackball
Kernel driver for a 5-way GPIO trackball (4 direction pins + a click button), exposing a
`TRACKBALL_TYPE` device (`tactility/drivers/trackball.h`).
Devicetree binding: `tactility,gpio-trackball` (see `bindings/tactility,gpio-trackball.yaml`).
Each direction pin is wired to a falling-edge interrupt that accumulates a signed delta;
the click pin is active-low and read on any edge. `read_delta()` drains and resets the
accumulated delta on each call.
## Example
```dts
trackball {
compatible = "tactility,gpio-trackball";
pin-right = <&gpio0 1 GPIO_FLAG_NONE>;
pin-up = <&gpio0 2 GPIO_FLAG_NONE>;
pin-left = <&gpio0 3 GPIO_FLAG_NONE>;
pin-down = <&gpio0 4 GPIO_FLAG_NONE>;
pin-click = <&gpio0 5 GPIO_FLAG_NONE>;
};
```
## License
[Apache License Version 2.0](LICENSE-Apache-2.0.md)
@@ -1,6 +1,6 @@
description: LilyGO T-Deck 5-way GPIO trackball (4 directions + click button)
description: 5-way GPIO trackball (4 directions + click button)
compatible: "lilygo,tdeck-trackball"
compatible: "tactility,gpio-trackball"
properties:
pin-right:
@@ -0,0 +1,3 @@
dependencies:
- TactilityKernel
bindings: bindings
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <drivers/gpio_trackball.h>
DEFINE_DEVICETREE(gpio_trackball, struct GpioTrackballConfig)
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <tactility/drivers/gpio.h>
#include <tactility/error.h>
#include <stdbool.h>
#include <stdint.h>
struct Device;
struct GpioTrackballConfig {
struct GpioPinSpec pin_right;
struct GpioPinSpec pin_up;
struct GpioPinSpec pin_left;
struct GpioPinSpec pin_down;
struct GpioPinSpec pin_click;
};
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/module.h>
#ifdef __cplusplus
extern "C" {
#endif
extern struct Module gpio_trackball_module;
#ifdef __cplusplus
}
#endif
@@ -1,20 +1,22 @@
// SPDX-License-Identifier: Apache-2.0
#include <lilygo/drivers/tdeck_trackball.h>
#include <drivers/gpio_trackball.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#include <tactility/drivers/gpio_controller.h>
#include <tactility/drivers/gpio_descriptor.h>
#include <tactility/drivers/trackball.h>
#include <tactility/log.h>
#include <atomic>
#include <new>
#define TAG "tdeck_trackball"
#define GET_CONFIG(device) (static_cast<const TdeckTrackballConfig*>((device)->config))
#define GET_INTERNAL(device) (static_cast<TdeckTrackballInternal*>(device_get_driver_data(device)))
constexpr auto* TAG = "gpio_trackball";
struct TdeckTrackballInternal {
#define GET_CONFIG(device) (static_cast<const GpioTrackballConfig*>((device)->config))
#define GET_INTERNAL(device) (static_cast<GpioTrackballInternal*>(device_get_driver_data(device)))
struct GpioTrackballInternal {
GpioDescriptor* pin_right = nullptr;
GpioDescriptor* pin_up = nullptr;
GpioDescriptor* pin_left = nullptr;
@@ -29,23 +31,23 @@ struct TdeckTrackballInternal {
// region ISR callbacks
static void on_right(void* arg) {
static_cast<TdeckTrackballInternal*>(arg)->dx.fetch_add(1, std::memory_order_relaxed);
static_cast<GpioTrackballInternal*>(arg)->dx.fetch_add(1, std::memory_order_relaxed);
}
static void on_left(void* arg) {
static_cast<TdeckTrackballInternal*>(arg)->dx.fetch_sub(1, std::memory_order_relaxed);
static_cast<GpioTrackballInternal*>(arg)->dx.fetch_sub(1, std::memory_order_relaxed);
}
static void on_down(void* arg) {
static_cast<TdeckTrackballInternal*>(arg)->dy.fetch_add(1, std::memory_order_relaxed);
static_cast<GpioTrackballInternal*>(arg)->dy.fetch_add(1, std::memory_order_relaxed);
}
static void on_up(void* arg) {
static_cast<TdeckTrackballInternal*>(arg)->dy.fetch_sub(1, std::memory_order_relaxed);
static_cast<GpioTrackballInternal*>(arg)->dy.fetch_sub(1, std::memory_order_relaxed);
}
static void on_click(void* arg) {
auto* internal = static_cast<TdeckTrackballInternal*>(arg);
auto* internal = static_cast<GpioTrackballInternal*>(arg);
bool high = true;
gpio_descriptor_get_level(internal->pin_click, &high);
// Active low: pressed when level is low
@@ -90,7 +92,7 @@ static void release_pin(GpioDescriptor*& descriptor) {
descriptor = nullptr;
}
static void release_all_pins(TdeckTrackballInternal* internal) {
static void release_all_pins(GpioTrackballInternal* internal) {
release_pin(internal->pin_right);
release_pin(internal->pin_up);
release_pin(internal->pin_left);
@@ -114,19 +116,11 @@ static error_t get_button_pressed(Device* device, bool* out_pressed) {
return ERROR_NONE;
}
error_t tdeck_trackball_read_delta(Device* device, int32_t* out_dx, int32_t* out_dy) {
return read_delta(device, out_dx, out_dy);
}
error_t tdeck_trackball_get_button_pressed(Device* device, bool* out_pressed) {
return get_button_pressed(device, out_pressed);
}
static error_t start(Device* device) {
LOG_I(TAG, "start %s", device->name);
auto* config = GET_CONFIG(device);
auto* internal = new(std::nothrow) TdeckTrackballInternal();
auto* internal = new(std::nothrow) GpioTrackballInternal();
if (internal == nullptr) {
return ERROR_OUT_OF_MEMORY;
}
@@ -168,25 +162,21 @@ static error_t stop(Device* device) {
return ERROR_NONE;
}
static constexpr TdeckTrackballApi TDECK_TRACKBALL_API = {
static constexpr TrackballApi GPIO_TRACKBALL_API = {
.read_delta = read_delta,
.get_button_pressed = get_button_pressed,
};
const struct DeviceType TDECK_TRACKBALL_TYPE {
.name = "tdeck-trackball"
};
extern Module gpio_trackball_module;
extern Module lilygo_module;
Driver tdeck_trackball_driver = {
.name = "tdeck_trackball",
.compatible = (const char*[]) { "lilygo,tdeck-trackball", nullptr },
Driver gpio_trackball_driver = {
.name = "gpio_trackball",
.compatible = (const char*[]) { "tactility,gpio-trackball", nullptr },
.start_device = start,
.stop_device = stop,
.api = &TDECK_TRACKBALL_API,
.device_type = &TDECK_TRACKBALL_TYPE,
.owner = &lilygo_module,
.api = &GPIO_TRACKBALL_API,
.device_type = &TRACKBALL_TYPE,
.owner = &gpio_trackball_module,
.internal = nullptr
};
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: Apache-2.0
#include <tactility/driver.h>
#include <tactility/module.h>
extern "C" {
extern Driver gpio_trackball_driver;
static Driver* const gpio_trackball_drivers[] = {
&gpio_trackball_driver,
nullptr
};
Module gpio_trackball_module = {
.name = "gpio-trackball",
.drivers = gpio_trackball_drivers
};
} // extern "C"
@@ -1,7 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#include <tactility/bindings/bindings.h>
#include <lilygo/drivers/tdeck_trackball.h>
DEFINE_DEVICETREE(tdeck_trackball, struct TdeckTrackballConfig)
@@ -1,62 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <tactility/drivers/gpio.h>
#include <tactility/error.h>
#include <stdbool.h>
#include <stdint.h>
struct Device;
struct DeviceType;
struct TdeckTrackballConfig {
struct GpioPinSpec pin_right;
struct GpioPinSpec pin_up;
struct GpioPinSpec pin_left;
struct GpioPinSpec pin_down;
struct GpioPinSpec pin_click;
};
/**
* @brief API for the T-Deck 5-way trackball driver.
* Reports raw, unscaled movement: sensitivity and mode (encoder vs. pointer) are UI concerns
* layered on top by the consumer, not something this driver knows about.
*/
struct TdeckTrackballApi {
/**
* @brief Reads the accumulated movement since the last read, then resets it to zero.
* @param[in] device the trackball device
* @param[out] out_dx horizontal movement (right positive), in raw pulses
* @param[out] out_dy vertical movement (down positive), in raw pulses
* @retval ERROR_NONE when the operation was successful
*/
error_t (*read_delta)(struct Device* device, int32_t* out_dx, int32_t* out_dy);
/**
* @brief Gets whether the click button is currently pressed.
* @param[in] device the trackball device
* @param[out] out_pressed true when pressed
* @retval ERROR_NONE when the operation was successful
*/
error_t (*get_button_pressed)(struct Device* device, bool* out_pressed);
};
/**
* @brief Reads the accumulated movement using the specified trackball device.
*/
error_t tdeck_trackball_read_delta(struct Device* device, int32_t* out_dx, int32_t* out_dy);
/**
* @brief Gets whether the click button is currently pressed on the specified trackball device.
*/
error_t tdeck_trackball_get_button_pressed(struct Device* device, bool* out_pressed);
extern const struct DeviceType TDECK_TRACKBALL_TYPE;
#ifdef __cplusplus
}
#endif
@@ -1,56 +0,0 @@
#pragma once
#include <lvgl.h>
namespace trackball {
/**
* @brief Trackball operating mode
*/
enum class Mode {
Encoder, // Navigation via enc_diff (scroll wheel behavior)
Pointer // Mouse cursor via point.x/y
};
/**
* @brief Initialize trackball as an LVGL input device, backed by the kernel tdeck_trackball driver.
* @return LVGL input device pointer, or nullptr if the kernel device isn't found/started
*/
lv_indev_t* init();
/**
* @brief Deinitialize trackball
*/
void deinit();
/**
* @brief Set encoder mode sensitivity
* @param sensitivity Steps per trackball tick (1-10, default: 1)
*/
void setEncoderSensitivity(uint8_t sensitivity);
/**
* @brief Set pointer mode sensitivity
* @param sensitivity Pixels per trackball tick (1-10, default: 10)
*/
void setPointerSensitivity(uint8_t sensitivity);
/**
* @brief Enable or disable trackball input processing
* @param enabled Boolean value to enable or disable
*/
void setEnabled(bool enabled);
/**
* @brief Set trackball operating mode
* @param mode Encoder or Pointer mode
*/
void setMode(Mode mode);
/**
* @brief Get current trackball operating mode
* @return Current mode
*/
Mode getMode();
}
-2
View File
@@ -6,13 +6,11 @@ extern "C" {
extern Driver tdeck_keyboard_driver;
extern Driver tdeck_keyboard_backlight_driver;
extern Driver tdeck_trackball_driver;
extern Driver tpager_encoder_driver;
static Driver* const lilygo_drivers[] = {
&tdeck_keyboard_driver,
&tdeck_keyboard_backlight_driver,
&tdeck_trackball_driver,
&tpager_encoder_driver,
nullptr
};
-201
View File
@@ -1,201 +0,0 @@
#include <lilygo/drivers/trackball.h>
#include <lilygo/drivers/tdeck_trackball.h>
#include <Tactility/Assets.h>
#include <tactility/device.h>
#include <tactility/log.h>
constexpr auto* TAG = "Trackball";
namespace trackball {
static lv_indev_t* g_indev = nullptr;
static Device* g_device = nullptr;
static bool g_enabled = true;
static Mode g_mode = Mode::Encoder;
static uint8_t g_encoderSensitivity = 1;
static uint8_t g_pointerSensitivity = 10;
// Pointer mode cursor position (screen-relative)
static int32_t g_cursorX = 160;
static int32_t g_cursorY = 120;
static lv_obj_t* g_cursor = nullptr;
// Screen dimensions (T-Deck: 320x240)
static constexpr int32_t SCREEN_WIDTH = 320;
static constexpr int32_t SCREEN_HEIGHT = 240;
static constexpr int32_t CURSOR_SIZE = 16;
static inline int32_t clamp(int32_t val, int32_t minVal, int32_t maxVal) {
if (val < minVal) return minVal;
if (val > maxVal) return maxVal;
return val;
}
// Note: must be called from the LVGL thread (main thread), same as the setters below.
static void read_cb(lv_indev_t* /*indev*/, lv_indev_data_t* data) {
// Always drain accumulated movement so it doesn't jump on re-enable, but discard it while disabled.
int32_t dx = 0;
int32_t dy = 0;
tdeck_trackball_read_delta(g_device, &dx, &dy);
if (!g_enabled) {
dx = 0;
dy = 0;
}
if (g_mode == Mode::Encoder) {
int32_t ticks = (dx + dy) * static_cast<int32_t>(g_encoderSensitivity);
data->enc_diff = static_cast<int16_t>(clamp(ticks, INT16_MIN, INT16_MAX));
if (ticks != 0) {
lv_display_trigger_activity(nullptr);
}
} else {
g_cursorX = clamp(g_cursorX + dx * static_cast<int32_t>(g_pointerSensitivity), 0, SCREEN_WIDTH - CURSOR_SIZE - 1);
g_cursorY = clamp(g_cursorY + dy * static_cast<int32_t>(g_pointerSensitivity), 0, SCREEN_HEIGHT - CURSOR_SIZE - 1);
data->point.x = static_cast<int16_t>(g_cursorX);
data->point.y = static_cast<int16_t>(g_cursorY);
}
bool pressed = false;
if (g_enabled) {
tdeck_trackball_get_button_pressed(g_device, &pressed);
}
data->state = pressed ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED;
if (pressed) {
lv_display_trigger_activity(nullptr);
}
}
lv_indev_t* init() {
if (g_indev != nullptr) {
LOG_W(TAG, "Already initialized");
return g_indev;
}
if (device_get_first_active_by_type(&TDECK_TRACKBALL_TYPE, &g_device) != ERROR_NONE) {
LOG_E(TAG, "tdeck_trackball kernel device not found or not started");
return nullptr;
}
g_cursorX = SCREEN_WIDTH / 2;
g_cursorY = SCREEN_HEIGHT / 2;
g_indev = lv_indev_create();
if (g_indev == nullptr) {
LOG_E(TAG, "Failed to register LVGL input device");
device_put(g_device);
g_device = nullptr;
return nullptr;
}
lv_indev_set_type(g_indev, LV_INDEV_TYPE_ENCODER);
lv_indev_set_read_cb(g_indev, read_cb);
LOG_I(TAG, "Initialized");
return g_indev;
}
// Create cursor for pointer mode
static void createCursor() {
if (g_cursor != nullptr || g_indev == nullptr) return;
g_cursor = lv_image_create(lv_layer_sys());
if (g_cursor != nullptr) {
lv_obj_remove_flag(g_cursor, LV_OBJ_FLAG_CLICKABLE);
lv_image_set_src(g_cursor, TT_ASSETS_UI_CURSOR);
lv_indev_set_cursor(g_indev, g_cursor);
LOG_D(TAG, "Cursor created");
}
}
// Destroy cursor when switching back to encoder mode
static void destroyCursor() {
if (g_cursor == nullptr) return;
// Delete the cursor object - this automatically detaches it from the indev
lv_obj_delete(g_cursor);
g_cursor = nullptr;
LOG_D(TAG, "Cursor destroyed");
}
void deinit() {
if (g_indev == nullptr) return;
destroyCursor();
lv_indev_delete(g_indev);
g_indev = nullptr;
device_put(g_device);
g_device = nullptr;
g_mode = Mode::Encoder;
g_enabled = true;
LOG_I(TAG, "Deinitialized");
}
void setEncoderSensitivity(uint8_t sensitivity) {
if (sensitivity > 0) {
g_encoderSensitivity = sensitivity;
LOG_D(TAG, "Encoder sensitivity set to %d", sensitivity);
}
}
void setPointerSensitivity(uint8_t sensitivity) {
if (sensitivity > 0) {
g_pointerSensitivity = sensitivity;
LOG_D(TAG, "Pointer sensitivity set to %d", sensitivity);
}
}
void setEnabled(bool enabled) {
g_enabled = enabled;
if (g_cursor != nullptr) {
if (enabled) {
lv_obj_clear_flag(g_cursor, LV_OBJ_FLAG_HIDDEN);
} else {
lv_obj_add_flag(g_cursor, LV_OBJ_FLAG_HIDDEN);
}
}
LOG_I(TAG, "%s", enabled ? "Enabled" : "Disabled");
}
void setMode(Mode mode) {
if (g_indev == nullptr) {
LOG_W(TAG, "Cannot set mode - not initialized");
return;
}
if (g_mode == mode) {
return;
}
g_mode = mode;
if (mode == Mode::Pointer) {
lv_indev_set_type(g_indev, LV_INDEV_TYPE_POINTER);
createCursor();
if (!g_enabled && g_cursor != nullptr) {
lv_obj_add_flag(g_cursor, LV_OBJ_FLAG_HIDDEN);
}
g_cursorX = SCREEN_WIDTH / 2;
g_cursorY = SCREEN_HEIGHT / 2;
LOG_I(TAG, "Switched to Pointer mode");
} else {
destroyCursor();
lv_indev_set_type(g_indev, LV_INDEV_TYPE_ENCODER);
LOG_I(TAG, "Switched to Encoder mode");
}
}
Mode getMode() {
return g_mode;
}
}