Files
tactility/Drivers/lilygo-module/source/tdeck_keyboard_backlight.cpp
T
Ken Van Hoeylandt 643cbc3806 Implement posix app loading (#643)
- Added POSIX desktop support for SDK builds, application packaging, and integration testing.
- Added POSIX filesystem partitions and improved application path handling.
- Added simulator support for loading and running applications dynamically.
- Improved simulator task stack handling and display startup reliability.
- Improved simulator display scaling, resizing, high-DPI support, and pointer accuracy.
- Fixed LVGL timers and input polling on POSIX. (fixes simulator with Linux on some Intel graphics platforms)
- Standardized data paths across platforms.
- Logging now works via separate task: this allows apps to write to log without it affecting their stdout (for apps that output text as relevant date for other apps, like the File Selection app)
- Fixes for app stdio
2026-08-31 22:09:04 +02:00

121 lines
4.1 KiB
C++

// 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);
// We don't check whether the device is present, because it doesn't respond reliably at boot
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;
auto address = GET_CONFIG(device)->address;
// 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
};