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
@@ -1,25 +0,0 @@
description: LilyGO T-Deck 5-way GPIO trackball (4 directions + click button)
compatible: "lilygo,tdeck-trackball"
properties:
pin-right:
type: phandles
required: true
description: Right-direction GPIO pin
pin-up:
type: phandles
required: true
description: Up-direction GPIO pin
pin-left:
type: phandles
required: true
description: Left-direction GPIO pin
pin-down:
type: phandles
required: true
description: Down-direction GPIO pin
pin-click:
type: phandles
required: true
description: Click button GPIO pin
@@ -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
};
@@ -1,193 +0,0 @@
// SPDX-License-Identifier: Apache-2.0
#include <lilygo/drivers/tdeck_trackball.h>
#include <tactility/device.h>
#include <tactility/driver.h>
#include <tactility/drivers/gpio_controller.h>
#include <tactility/drivers/gpio_descriptor.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)))
struct TdeckTrackballInternal {
GpioDescriptor* pin_right = nullptr;
GpioDescriptor* pin_up = nullptr;
GpioDescriptor* pin_left = nullptr;
GpioDescriptor* pin_down = nullptr;
GpioDescriptor* pin_click = nullptr;
std::atomic<int32_t> dx {0};
std::atomic<int32_t> dy {0};
std::atomic<bool> button_pressed {false};
};
// region ISR callbacks
static void on_right(void* arg) {
static_cast<TdeckTrackballInternal*>(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 void on_down(void* arg) {
static_cast<TdeckTrackballInternal*>(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 void on_click(void* arg) {
auto* internal = static_cast<TdeckTrackballInternal*>(arg);
bool high = true;
gpio_descriptor_get_level(internal->pin_click, &high);
// Active low: pressed when level is low
internal->button_pressed.store(!high, std::memory_order_relaxed);
}
// endregion
// region Pin acquisition
static error_t acquire_pin(const GpioPinSpec& spec, GpioInterruptType interrupt_type, void (*callback)(void*), void* arg, GpioDescriptor** out_descriptor) {
gpio_flags_t flags = GPIO_FLAG_DIRECTION_INPUT | GPIO_FLAG_PULL_UP;
flags = GPIO_FLAG_INTERRUPT_TO_OPTIONS(flags, interrupt_type);
auto* descriptor = gpio_descriptor_acquire(spec.gpio_controller, spec.pin, flags, GPIO_OWNER_GPIO);
if (descriptor == nullptr) {
return ERROR_RESOURCE;
}
error_t error = gpio_descriptor_add_callback(descriptor, callback, arg);
if (error == ERROR_NONE) {
error = gpio_descriptor_enable_interrupt(descriptor);
}
if (error != ERROR_NONE) {
gpio_descriptor_remove_callback(descriptor);
gpio_descriptor_release(descriptor);
return error;
}
*out_descriptor = descriptor;
return ERROR_NONE;
}
static void release_pin(GpioDescriptor*& descriptor) {
if (descriptor == nullptr) {
return;
}
gpio_descriptor_disable_interrupt(descriptor);
gpio_descriptor_remove_callback(descriptor);
gpio_descriptor_release(descriptor);
descriptor = nullptr;
}
static void release_all_pins(TdeckTrackballInternal* internal) {
release_pin(internal->pin_right);
release_pin(internal->pin_up);
release_pin(internal->pin_left);
release_pin(internal->pin_down);
release_pin(internal->pin_click);
}
// endregion
extern "C" {
static error_t read_delta(Device* device, int32_t* out_dx, int32_t* out_dy) {
auto* internal = GET_INTERNAL(device);
*out_dx = internal->dx.exchange(0, std::memory_order_relaxed);
*out_dy = internal->dy.exchange(0, std::memory_order_relaxed);
return ERROR_NONE;
}
static error_t get_button_pressed(Device* device, bool* out_pressed) {
*out_pressed = GET_INTERNAL(device)->button_pressed.load(std::memory_order_relaxed);
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();
if (internal == nullptr) {
return ERROR_OUT_OF_MEMORY;
}
error_t error = acquire_pin(config->pin_right, GPIO_INTERRUPT_NEG_EDGE, on_right, internal, &internal->pin_right);
if (error == ERROR_NONE) {
error = acquire_pin(config->pin_up, GPIO_INTERRUPT_NEG_EDGE, on_up, internal, &internal->pin_up);
}
if (error == ERROR_NONE) {
error = acquire_pin(config->pin_left, GPIO_INTERRUPT_NEG_EDGE, on_left, internal, &internal->pin_left);
}
if (error == ERROR_NONE) {
error = acquire_pin(config->pin_down, GPIO_INTERRUPT_NEG_EDGE, on_down, internal, &internal->pin_down);
}
if (error == ERROR_NONE) {
error = acquire_pin(config->pin_click, GPIO_INTERRUPT_ANY_EDGE, on_click, internal, &internal->pin_click);
}
if (error != ERROR_NONE) {
LOG_E(TAG, "Failed to acquire trackball pins: %s", error_to_string(error));
release_all_pins(internal);
delete internal;
return error;
}
// Read the click pin's initial level now that the descriptor is acquired.
on_click(internal);
device_set_driver_data(device, internal);
return ERROR_NONE;
}
static error_t stop(Device* device) {
LOG_I(TAG, "stop %s", device->name);
auto* internal = GET_INTERNAL(device);
release_all_pins(internal);
device_set_driver_data(device, nullptr);
delete internal;
return ERROR_NONE;
}
static constexpr TdeckTrackballApi TDECK_TRACKBALL_API = {
.read_delta = read_delta,
.get_button_pressed = get_button_pressed,
};
const struct DeviceType TDECK_TRACKBALL_TYPE {
.name = "tdeck-trackball"
};
extern Module lilygo_module;
Driver tdeck_trackball_driver = {
.name = "tdeck_trackball",
.compatible = (const char*[]) { "lilygo,tdeck-trackball", nullptr },
.start_device = start,
.stop_device = stop,
.api = &TDECK_TRACKBALL_API,
.device_type = &TDECK_TRACKBALL_TYPE,
.owner = &lilygo_module,
.internal = 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;
}
}