Files
tactility/Drivers/lilygo-module/source/tdeck_keyboard.cpp
Ken Van Hoeylandt 50c0a14a93 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
2026-07-12 00:29:12 +02:00

102 lines
2.9 KiB
C++

// 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
};