New kernel drivers and more (#560)
- Added kernel base drivers for: display, pointer (touch, mouse, etc.), keyboard, adc, power supply and backlight - Implement new kernel driver modules: `st7789-module` and `gt911-module` - Implement ESP32 ADC "oneshot" kernel driver - Implement ESP32 backlight kernel driver with ledc API - Implemented `battery-sense` driver that allows for voltage measurement and creates a power supply child device - Updated github actions - Updated flash scripts - Fix for esp32 legacy driver conflict with ADC - Created separate `lilygo-tdeck` and `lilygo-tdeck-plus` devices - Created `lilygo-module` with LilyGO kernel drivers - Fix for intermittent errors in build related to code generation of `devicetree.c` - `lvgl-module` can now map kernel drivers onto LVGL devices - Created `KernelDisplayApp` as a mirror of `HalDisplayApp` (formerly `DisplayApp`) - Removed `struct` and `enum` prefix in a lot of kernel driver cpp source files - `lilygo-tdeck` and `lilygo-tdeck-plus` are now fully relying on kernel drivers and don't use any of the old HAL
This commit is contained in:
committed by
GitHub
parent
c4406b24ba
commit
50c0a14a93
@@ -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(lilygo-module
|
||||
SRCS ${SOURCE_FILES}
|
||||
INCLUDE_DIRS include/
|
||||
REQUIRES TactilityKernel Tactility lvgl
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
description: LilyGO T-Deck onboard keyboard backlight controller
|
||||
|
||||
include: ["i2c-device.yaml"]
|
||||
|
||||
compatible: "lilygo,tdeck-keyboard-backlight"
|
||||
|
||||
bus: i2c
|
||||
|
||||
properties:
|
||||
brightness-default:
|
||||
type: int
|
||||
default: 127
|
||||
description: Default brightness level, applied on start and by set_brightness_default(). Range 0-255.
|
||||
@@ -0,0 +1,7 @@
|
||||
description: LilyGO T-Deck onboard keyboard controller
|
||||
|
||||
include: ["i2c-device.yaml"]
|
||||
|
||||
compatible: "lilygo,tdeck-keyboard"
|
||||
|
||||
bus: i2c
|
||||
@@ -0,0 +1,25 @@
|
||||
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
|
||||
@@ -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 <lilygo/drivers/tdeck_keyboard.h>
|
||||
|
||||
DEFINE_DEVICETREE(tdeck_keyboard, struct TdeckKeyboardConfig)
|
||||
@@ -0,0 +1,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <tactility/bindings/bindings.h>
|
||||
#include <lilygo/drivers/tdeck_keyboard_backlight.h>
|
||||
|
||||
DEFINE_DEVICETREE(tdeck_keyboard_backlight, struct TdeckKeyboardBacklightConfig)
|
||||
@@ -0,0 +1,7 @@
|
||||
// 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)
|
||||
@@ -0,0 +1,16 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct TdeckKeyboardConfig {
|
||||
uint8_t address;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,17 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct TdeckKeyboardBacklightConfig {
|
||||
uint8_t address;
|
||||
uint8_t brightness_default;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,14 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
bool tdeck_power_on();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,62 @@
|
||||
// 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
|
||||
@@ -0,0 +1,56 @@
|
||||
#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();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <tactility/module.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern struct Module lilygo_module;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,38 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <tactility/check.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/module.h>
|
||||
|
||||
extern "C" {
|
||||
|
||||
extern Driver tdeck_keyboard_driver;
|
||||
extern Driver tdeck_keyboard_backlight_driver;
|
||||
extern Driver tdeck_trackball_driver;
|
||||
|
||||
static error_t start() {
|
||||
/* We crash when construct fails, because if a single driver fails to construct,
|
||||
* there is no guarantee that the previously constructed drivers can be destroyed */
|
||||
check(driver_construct_add(&tdeck_keyboard_driver) == ERROR_NONE);
|
||||
check(driver_construct_add(&tdeck_keyboard_backlight_driver) == ERROR_NONE);
|
||||
check(driver_construct_add(&tdeck_trackball_driver) == ERROR_NONE);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop() {
|
||||
/* We crash when destruct fails, because if a single driver fails to destruct,
|
||||
* there is no guarantee that the previously destroyed drivers can be recovered */
|
||||
check(driver_remove_destruct(&tdeck_keyboard_driver) == ERROR_NONE);
|
||||
check(driver_remove_destruct(&tdeck_keyboard_backlight_driver) == ERROR_NONE);
|
||||
check(driver_remove_destruct(&tdeck_trackball_driver) == ERROR_NONE);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
Module lilygo_module = {
|
||||
.name = "lilygo",
|
||||
.start = start,
|
||||
.stop = stop,
|
||||
.symbols = nullptr,
|
||||
.internal = nullptr
|
||||
};
|
||||
|
||||
} // extern "C"
|
||||
@@ -0,0 +1,101 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <lilygo/drivers/tdeck_keyboard.h>
|
||||
|
||||
#include <tactility/check.h>
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/drivers/i2c_controller.h>
|
||||
#include <tactility/drivers/keyboard.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#define TAG "TdeckKeyboard"
|
||||
#define GET_CONFIG(device) (static_cast<const TdeckKeyboardConfig*>((device)->config))
|
||||
|
||||
static constexpr TickType_t I2C_TIMEOUT = pdMS_TO_TICKS(100);
|
||||
|
||||
struct TdeckKeyboardInternal {
|
||||
// The T-Deck keyboard controller only ever reports the currently pressed key (0x00 = none) and
|
||||
// never a release event, so release events are synthesized by comparing against this.
|
||||
uint8_t last_key;
|
||||
};
|
||||
|
||||
// region Driver lifecycle
|
||||
|
||||
static error_t start(Device* device) {
|
||||
auto* parent = device_get_parent(device);
|
||||
check(device_get_type(parent) == &I2C_CONTROLLER_TYPE);
|
||||
|
||||
auto address = GET_CONFIG(device)->address;
|
||||
if (i2c_controller_has_device_at_address(parent, address, I2C_TIMEOUT) != ERROR_NONE) {
|
||||
LOG_E(TAG, "No device found on I2C bus at address 0x%02X", address);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
auto* internal = static_cast<TdeckKeyboardInternal*>(malloc(sizeof(TdeckKeyboardInternal)));
|
||||
if (internal == nullptr) {
|
||||
return ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
internal->last_key = 0;
|
||||
|
||||
device_set_driver_data(device, internal);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop(Device* device) {
|
||||
auto* internal = static_cast<TdeckKeyboardInternal*>(device_get_driver_data(device));
|
||||
free(internal);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region KeyboardApi
|
||||
|
||||
static error_t tdeck_keyboard_read_key(Device* device, KeyboardKeyData* data) {
|
||||
auto* parent = device_get_parent(device);
|
||||
auto address = GET_CONFIG(device)->address;
|
||||
auto* internal = static_cast<TdeckKeyboardInternal*>(device_get_driver_data(device));
|
||||
|
||||
uint8_t read_buffer = 0;
|
||||
if (i2c_controller_read(parent, address, &read_buffer, 1, I2C_TIMEOUT) != ERROR_NONE) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
// This controller never buffers more than one key event.
|
||||
data->continue_reading = false;
|
||||
|
||||
if (read_buffer == 0 && internal->last_key != 0) {
|
||||
data->key = internal->last_key;
|
||||
data->pressed = false;
|
||||
} else if (read_buffer != 0) {
|
||||
data->key = read_buffer;
|
||||
data->pressed = true;
|
||||
} else {
|
||||
data->key = 0;
|
||||
data->pressed = false;
|
||||
}
|
||||
|
||||
internal->last_key = read_buffer;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
static const KeyboardApi tdeck_keyboard_api = {
|
||||
.read_key = tdeck_keyboard_read_key,
|
||||
};
|
||||
|
||||
extern Module lilygo_module;
|
||||
|
||||
Driver tdeck_keyboard_driver = {
|
||||
.name = "tdeck_keyboard",
|
||||
.compatible = (const char*[]) { "lilygo,tdeck-keyboard", nullptr },
|
||||
.start_device = start,
|
||||
.stop_device = stop,
|
||||
.api = &tdeck_keyboard_api,
|
||||
.device_type = &KEYBOARD_TYPE,
|
||||
.owner = &lilygo_module,
|
||||
.internal = nullptr
|
||||
};
|
||||
@@ -0,0 +1,122 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <lilygo/drivers/tdeck_keyboard_backlight.h>
|
||||
|
||||
#include <tactility/check.h>
|
||||
#include <tactility/device.h>
|
||||
#include <tactility/driver.h>
|
||||
#include <tactility/drivers/backlight.h>
|
||||
#include <tactility/drivers/i2c_controller.h>
|
||||
#include <tactility/log.h>
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#define TAG "TdeckKeyboardBacklight"
|
||||
#define GET_CONFIG(device) (static_cast<const TdeckKeyboardBacklightConfig*>((device)->config))
|
||||
|
||||
static constexpr TickType_t I2C_TIMEOUT = pdMS_TO_TICKS(100);
|
||||
static constexpr uint8_t CMD_BRIGHTNESS = 0x01;
|
||||
static constexpr uint8_t CMD_DEFAULT_BRIGHTNESS = 0x02;
|
||||
|
||||
struct TdeckKeyboardBacklightInternal {
|
||||
// The controller is write-only for brightness, so the current value is cached here.
|
||||
uint8_t current_brightness;
|
||||
};
|
||||
|
||||
// region Driver lifecycle
|
||||
|
||||
static error_t start(Device* device) {
|
||||
auto* parent = device_get_parent(device);
|
||||
check(device_get_type(parent) == &I2C_CONTROLLER_TYPE);
|
||||
|
||||
auto address = GET_CONFIG(device)->address;
|
||||
if (i2c_controller_has_device_at_address(parent, address, I2C_TIMEOUT) != ERROR_NONE) {
|
||||
LOG_E(TAG, "No device found on I2C bus at address 0x%02X", address);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
auto* internal = static_cast<TdeckKeyboardBacklightInternal*>(malloc(sizeof(TdeckKeyboardBacklightInternal)));
|
||||
if (internal == nullptr) {
|
||||
return ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
internal->current_brightness = 0;
|
||||
device_set_driver_data(device, internal);
|
||||
|
||||
auto brightness_default = GET_CONFIG(device)->brightness_default;
|
||||
|
||||
// Configures the keyboard controller's own persisted default, used by its onboard ALT+B toggle.
|
||||
if (i2c_controller_write_register(parent, address, CMD_DEFAULT_BRIGHTNESS, &brightness_default, 1, I2C_TIMEOUT) != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to set default brightness");
|
||||
}
|
||||
|
||||
if (i2c_controller_write_register(parent, address, CMD_BRIGHTNESS, &brightness_default, 1, I2C_TIMEOUT) != ERROR_NONE) {
|
||||
LOG_E(TAG, "Failed to set initial brightness");
|
||||
} else {
|
||||
internal->current_brightness = brightness_default;
|
||||
}
|
||||
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t stop(Device* device) {
|
||||
auto* internal = static_cast<TdeckKeyboardBacklightInternal*>(device_get_driver_data(device));
|
||||
free(internal);
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region BacklightApi
|
||||
|
||||
static error_t tdeck_keyboard_backlight_set_brightness(Device* device, uint8_t brightness) {
|
||||
auto* parent = device_get_parent(device);
|
||||
auto address = GET_CONFIG(device)->address;
|
||||
auto* internal = static_cast<TdeckKeyboardBacklightInternal*>(device_get_driver_data(device));
|
||||
|
||||
if (i2c_controller_write_register(parent, address, CMD_BRIGHTNESS, &brightness, 1, I2C_TIMEOUT) != ERROR_NONE) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
internal->current_brightness = brightness;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static error_t tdeck_keyboard_backlight_set_brightness_default(Device* device) {
|
||||
return tdeck_keyboard_backlight_set_brightness(device, GET_CONFIG(device)->brightness_default);
|
||||
}
|
||||
|
||||
static error_t tdeck_keyboard_backlight_get_brightness(Device* device, uint8_t* out_brightness) {
|
||||
auto* internal = static_cast<TdeckKeyboardBacklightInternal*>(device_get_driver_data(device));
|
||||
*out_brightness = internal->current_brightness;
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
static uint8_t tdeck_keyboard_backlight_get_min_brightness(Device*) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint8_t tdeck_keyboard_backlight_get_max_brightness(Device*) {
|
||||
return 255;
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
static const BacklightApi tdeck_keyboard_backlight_api = {
|
||||
.set_brightness = tdeck_keyboard_backlight_set_brightness,
|
||||
.set_brightness_default = tdeck_keyboard_backlight_set_brightness_default,
|
||||
.get_brightness = tdeck_keyboard_backlight_get_brightness,
|
||||
.get_min_brightness = tdeck_keyboard_backlight_get_min_brightness,
|
||||
.get_max_brightness = tdeck_keyboard_backlight_get_max_brightness,
|
||||
};
|
||||
|
||||
extern struct Module lilygo_module;
|
||||
|
||||
Driver tdeck_keyboard_backlight_driver = {
|
||||
.name = "tdeck_keyboard_backlight",
|
||||
.compatible = (const char*[]) { "lilygo,tdeck-keyboard-backlight", nullptr },
|
||||
.start_device = start,
|
||||
.stop_device = stop,
|
||||
.api = &tdeck_keyboard_backlight_api,
|
||||
.device_type = &BACKLIGHT_TYPE,
|
||||
.owner = &lilygo_module,
|
||||
.internal = nullptr
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <driver/gpio.h>
|
||||
|
||||
#define TDECK_POWERON_GPIO GPIO_NUM_10
|
||||
|
||||
bool tdeck_power_on() {
|
||||
gpio_config_t device_power_signal_config = {
|
||||
.pin_bit_mask = BIT64(TDECK_POWERON_GPIO),
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
|
||||
if (gpio_config(&device_power_signal_config) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (gpio_set_level(TDECK_POWERON_GPIO, 1) != ESP_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
// 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) {
|
||||
auto* descriptor = gpio_descriptor_acquire(spec.gpio_controller, spec.pin, GPIO_OWNER_GPIO);
|
||||
if (descriptor == nullptr) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
gpio_flags_t flags = GPIO_FLAG_DIRECTION_INPUT | GPIO_FLAG_PULL_UP;
|
||||
flags = GPIO_FLAG_INTERRUPT_TO_OPTIONS(flags, interrupt_type);
|
||||
|
||||
error_t error = gpio_descriptor_set_flags(descriptor, flags);
|
||||
if (error == ERROR_NONE) {
|
||||
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
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
g_device = device_find_first_active_by_type(&TDECK_TRACKBALL_TYPE);
|
||||
if (g_device == nullptr) {
|
||||
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");
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user