Refactor LVGL code into kernel module (#472)

* **New Features**
  * Added a standalone LVGL module and enabled LVGL support in the simulator for richer local UI testing.

* **Refactor**
  * HAL and LVGL split into distinct modules; startup and device attach/detach flows reorganized for clearer lifecycle management.
  * Public APIs tightened with clearer nullability/documentation.

* **Bug Fixes**
  * More consistent LVGL start/stop and device attach/detach behavior for improved stability.
This commit is contained in:
Ken Van Hoeylandt
2026-02-01 22:57:45 +01:00
committed by GitHub
parent 3fe1dc0312
commit 9f721e6655
135 changed files with 1553 additions and 667 deletions
@@ -4,7 +4,7 @@
#include <PwmBacklight.h>
#include <RgbDisplay.h>
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable createTouch() {
std::shared_ptr<tt::hal::touch::TouchDevice> createTouch() {
// Note for future changes: Reset pin is 41 and interrupt pin is 40
auto configuration = std::make_unique<Gt911Touch::Configuration>(
I2C_NUM_0,
@@ -219,7 +219,7 @@ lvgl_port_display_rgb_cfg_t St7701Display::getLvglPortDisplayRgbConfig(esp_lcd_p
};
}
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable St7701Display::getTouchDevice() {
std::shared_ptr<tt::hal::touch::TouchDevice> St7701Display::getTouchDevice() {
if (touchDevice == nullptr) {
auto configuration = std::make_unique<Gt911Touch::Configuration>(
I2C_NUM_0,
@@ -7,7 +7,7 @@
class St7701Display final : public EspLcdDisplay {
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable touchDevice;
std::shared_ptr<tt::hal::touch::TouchDevice> touchDevice;
bool createIoHandle(esp_lcd_panel_io_handle_t& outHandle) override;
@@ -27,7 +27,7 @@ public:
std::string getDescription() const override { return "ST7701S RGB display"; }
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable getTouchDevice() override;
std::shared_ptr<tt::hal::touch::TouchDevice> getTouchDevice() override;
void setBacklightDuty(uint8_t backlightDuty) override;
@@ -4,7 +4,7 @@
#include <PwmBacklight.h>
#include <RgbDisplay.h>
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable createTouch() {
std::shared_ptr<tt::hal::touch::TouchDevice> createTouch() {
// Note for future changes: Reset pin is 38 and interrupt pin is 18
// or INT = NC, schematic and other info floating around is kinda conflicting...
auto configuration = std::make_unique<Gt911Touch::Configuration>(
@@ -3,7 +3,7 @@
#include <Gt911Touch.h>
#include <RgbDisplay.h>
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable createTouch() {
std::shared_ptr<tt::hal::touch::TouchDevice> createTouch() {
// Note for future changes: Reset pin is 38 and interrupt pin is 18
// or INT = NC, schematic and other info floating around is kinda conflicting...
auto configuration = std::make_unique<Gt911Touch::Configuration>(
@@ -4,7 +4,7 @@
#include <PwmBacklight.h>
#include <RgbDisplay.h>
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable createTouch() {
std::shared_ptr<tt::hal::touch::TouchDevice> createTouch() {
// Note for future changes: Reset pin is 38 and interrupt pin is 18
// or INT = NC, schematic and other info floating around is kinda conflicting...
auto configuration = std::make_unique<Gt911Touch::Configuration>(
@@ -72,23 +72,23 @@ public:
uint32_t bufferSize; // Size in pixel count. 0 means default, which is 1/10 of the screen size
lcd_rgb_element_order_t rgbElementOrder;
std::shared_ptr<tt::hal::touch::TouchDevice> touch;
std::function<void(uint8_t)> _Nullable backlightDutyFunction = nullptr;
std::function<void(uint8_t)> backlightDutyFunction = nullptr;
};
private:
esp_lcd_panel_io_handle_t _Nullable ioHandle = nullptr;
esp_lcd_panel_handle_t _Nullable panelHandle = nullptr;
lv_display_t* _Nullable lvglDisplay = nullptr;
uint16_t* _Nullable buffer1 = nullptr;
uint16_t* _Nullable buffer2 = nullptr;
uint16_t* _Nullable tempBuf = nullptr;
SemaphoreHandle_t _Nullable teSyncSemaphore = nullptr;
esp_lcd_panel_io_handle_t ioHandle = nullptr;
esp_lcd_panel_handle_t panelHandle = nullptr;
lv_display_t* lvglDisplay = nullptr;
uint16_t* buffer1 = nullptr;
uint16_t* buffer2 = nullptr;
uint16_t* tempBuf = nullptr;
SemaphoreHandle_t teSyncSemaphore = nullptr;
bool teIsrInstalled = false;
bool isrServiceInstalledByUs = false;
std::unique_ptr<Configuration> configuration;
std::shared_ptr<tt::hal::display::DisplayDriver> _Nullable displayDriver;
std::shared_ptr<tt::hal::display::DisplayDriver> displayDriver;
bool createIoHandle();
@@ -118,9 +118,9 @@ public:
bool stopLvgl() override;
lv_display_t* _Nullable getLvglDisplay() const override { return lvglDisplay; }
lv_display_t* getLvglDisplay() const override { return lvglDisplay; }
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable getTouchDevice() override { return configuration->touch; }
std::shared_ptr<tt::hal::touch::TouchDevice> getTouchDevice() override { return configuration->touch; }
void setBacklightDuty(uint8_t backlightDuty) override {
if (configuration->backlightDutyFunction != nullptr) {
@@ -132,7 +132,7 @@ public:
bool supportsDisplayDriver() const override { return true; }
std::shared_ptr<tt::hal::display::DisplayDriver> _Nullable getDisplayDriver() override;
std::shared_ptr<tt::hal::display::DisplayDriver> getDisplayDriver() override;
static void lvgl_port_flush_callback(lv_display_t *drv, const lv_area_t *area, uint8_t *color_map);
@@ -4,7 +4,7 @@
#include <PwmBacklight.h>
#include <RgbDisplay.h>
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable createTouch() {
std::shared_ptr<tt::hal::touch::TouchDevice> createTouch() {
// Note for future changes: Reset pin is 38 and interrupt pin is 18
// or INT = NC, schematic and other info floating around is kinda conflicting...
auto configuration = std::make_unique<Gt911Touch::Configuration>(
@@ -5,7 +5,7 @@
class TdeckKeyboard final : public tt::hal::keyboard::KeyboardDevice {
lv_indev_t* _Nullable deviceHandle = nullptr;
lv_indev_t* deviceHandle = nullptr;
public:
@@ -15,5 +15,5 @@ public:
bool startLvgl(lv_display_t* display) override;
bool stopLvgl() override;
bool isAttached() const override;
lv_indev_t* _Nullable getLvglIndev() override { return deviceHandle; }
lv_indev_t* getLvglIndev() override { return deviceHandle; }
};
@@ -5,7 +5,7 @@
class TpagerEncoder final : public tt::hal::encoder::EncoderDevice {
lv_indev_t* _Nullable encHandle = nullptr;
lv_indev_t* encHandle = nullptr;
pcnt_unit_handle_t encPcntUnit = nullptr;
bool initEncoder();
@@ -26,5 +26,5 @@ public:
int getEncoderPulses() const;
lv_indev_t* _Nullable getLvglIndev() override { return encHandle; }
lv_indev_t* getLvglIndev() override { return encHandle; }
};
@@ -10,7 +10,7 @@
class TpagerKeyboard final : public tt::hal::keyboard::KeyboardDevice {
lv_indev_t* _Nullable kbHandle = nullptr;
lv_indev_t* kbHandle = nullptr;
gpio_num_t backlightPin = GPIO_NUM_NC;
ledc_timer_t backlightTimer;
ledc_channel_t backlightChannel;
@@ -45,7 +45,7 @@ public:
bool stopLvgl() override;
bool isAttached() const override;
lv_indev_t* _Nullable getLvglIndev() override { return kbHandle; }
lv_indev_t* getLvglIndev() override { return kbHandle; }
bool setBacklightDuty(uint8_t duty);
void makeBacklightImpulse();
@@ -19,10 +19,10 @@ static int stop(Device* device) {
Driver tlora_pager_driver = {
.name = "T-Lora Pager",
.compatible = (const char*[]) { "lilygo,tlora-pager", nullptr },
.startDevice = start,
.stopDevice = stop,
.start_device = start,
.stop_device = stop,
.api = nullptr,
.deviceType = nullptr,
.device_type = nullptr,
.owner = &device_module,
.driver_private = nullptr
};
@@ -9,7 +9,7 @@
class CardputerKeyboard final : public tt::hal::keyboard::KeyboardDevice {
lv_indev_t* _Nullable kbHandle = nullptr;
lv_indev_t* kbHandle = nullptr;
QueueHandle_t queue = nullptr;
std::shared_ptr<Tca8418> keypad;
@@ -42,5 +42,5 @@ public:
bool stopLvgl() override;
bool isAttached() const override;
lv_indev_t* _Nullable getLvglIndev() override { return kbHandle; }
lv_indev_t* getLvglIndev() override { return kbHandle; }
};
@@ -13,7 +13,7 @@
class CardputerEncoder final : public tt::hal::encoder::EncoderDevice {
KEYBOARD::Keyboard keyboard;
int lastKeyNum = 0;
lv_indev_t* _Nullable lvglDevice = nullptr;
lv_indev_t* lvglDevice = nullptr;
static void readCallback(lv_indev_t* indev, lv_indev_data_t* data);
@@ -25,5 +25,5 @@ public:
bool startLvgl(lv_display_t* display) override;
bool stopLvgl() override;
lv_indev_t* _Nullable getLvglIndev() override { return lvglDevice; }
lv_indev_t* getLvglIndev() override { return lvglDevice; }
};
@@ -8,7 +8,7 @@
class CardputerKeyboard : public tt::hal::keyboard::KeyboardDevice {
KEYBOARD::Keyboard keyboard;
int lastKeyNum = 0;
lv_indev_t* _Nullable lvglDevice = nullptr;
lv_indev_t* lvglDevice = nullptr;
static void readCallback(lv_indev_t* indev, lv_indev_data_t* data);
@@ -22,5 +22,5 @@ public:
bool isAttached() const override { return true; }
lv_indev_t* _Nullable getLvglIndev() override { return lvglDevice; }
lv_indev_t* getLvglIndev() override { return lvglDevice; }
};
-3
View File
@@ -21,7 +21,4 @@ if (NOT DEFINED ENV{ESP_IDF_VERSION})
target_link_libraries(Simulator PRIVATE ${SDL2_LIBRARIES})
add_definitions(-D_Nullable=)
add_definitions(-D_Nonnull=)
endif()
-108
View File
@@ -1,108 +0,0 @@
#include "LvglTask.h"
#include <Tactility/Logger.h>
#include <Tactility/RecursiveMutex.h>
#include <Tactility/Thread.h>
#include <tactility/check.h>
#include <Tactility/lvgl/LvglSync.h>
#include <lvgl.h>
static const auto LOGGER = tt::Logger("LvglTask");
// Mutex for LVGL drawing
static tt::RecursiveMutex lvgl_mutex;
static tt::RecursiveMutex task_mutex;
static uint32_t task_max_sleep_ms = 10;
// Mutex for LVGL task state (to modify task_running state)
static bool task_running = false;
lv_disp_t* displayHandle = nullptr;
static void lvgl_task(void* arg);
static bool task_lock(TickType_t timeout) {
return task_mutex.lock(timeout);
}
static void task_unlock() {
task_mutex.unlock();
}
static void task_set_running(bool running) {
assert(task_lock(configTICK_RATE_HZ / 100));
task_running = running;
task_unlock();
}
bool lvgl_task_is_running() {
assert(task_lock(configTICK_RATE_HZ / 100));
bool result = task_running;
task_unlock();
return result;
}
static bool lvgl_lock(uint32_t timeoutMillis) {
return lvgl_mutex.lock(pdMS_TO_TICKS(timeoutMillis));
}
static void lvgl_unlock() {
lvgl_mutex.unlock();
}
void lvgl_task_interrupt() {
check(task_lock(portMAX_DELAY));
task_set_running(false); // interrupt task with boolean as flag
task_unlock();
}
void lvgl_task_start() {
LOGGER.info("LVGL task starting");
tt::lvgl::syncSet(&lvgl_lock, &lvgl_unlock);
// Create the main app loop, like ESP-IDF
BaseType_t task_result = xTaskCreate(
lvgl_task,
"lvgl",
8192,
nullptr,
static_cast<UBaseType_t>(tt::Thread::Priority::High), // Should be higher than main app task
nullptr
);
assert(task_result == pdTRUE);
}
static void lvgl_task(void* arg) {
LOGGER.info("LVGL task started");
/** Ideally. the display handle would be created during Simulator.start(),
* but somehow that doesn't work. Waiting here from a ThreadFlag when that happens
* also doesn't work. It seems that it must be called from this task. */
displayHandle = lv_sdl_window_create(320, 240);
lv_sdl_window_set_title(displayHandle, "Tactility");
uint32_t task_delay_ms = task_max_sleep_ms;
task_set_running(true);
while (lvgl_task_is_running()) {
if (lvgl_lock(10)) {
task_delay_ms = lv_timer_handler();
lvgl_unlock();
}
if ((task_delay_ms > task_max_sleep_ms) || (1 == task_delay_ms)) {
task_delay_ms = task_max_sleep_ms;
} else if (task_delay_ms < 1) {
task_delay_ms = 1;
}
vTaskDelay(pdMS_TO_TICKS(task_delay_ms));
}
lv_disp_remove(displayHandle);
displayHandle = nullptr;
vTaskDelete(nullptr);
}
-5
View File
@@ -1,5 +0,0 @@
#pragma once
void lvgl_task_start();
bool lvgl_task_is_running();
void lvgl_task_interrupt();
-18
View File
@@ -40,21 +40,3 @@ void freertosMain() {
}
} // namespace
/**
* Assert implementation as defined in the FreeRTOSConfig.h
* It allows you to set breakpoints and debug asserts.
*/
void vAssertCalled(unsigned long line, const char* const file) {
volatile uint32_t set_to_nonzero_in_debugger_to_continue = 0;
LOGGER.error("Assert triggered at {}:{}", file, line);
taskENTER_CRITICAL();
{
// Step out by attaching a debugger and setting set_to_nonzero_in_debugger_to_continue
while (set_to_nonzero_in_debugger_to_continue == 0) {
// NO-OP
}
}
taskEXIT_CRITICAL();
}
+1 -19
View File
@@ -1,4 +1,3 @@
#include "LvglTask.h"
#include "hal/SdlDisplay.h"
#include "hal/SdlKeyboard.h"
#include "hal/SimulatorPower.h"
@@ -11,23 +10,6 @@
using namespace tt::hal;
static bool initBoot() {
lv_init();
lvgl_task_start();
return true;
}
static void deinitPower() {
lvgl_task_interrupt();
while (lvgl_task_is_running()) {
tt::kernel::delayMillis(10);
}
#if LV_ENABLE_GC || !LV_MEM_CUSTOM
lv_deinit();
#endif
}
static std::vector<std::shared_ptr<tt::hal::Device>> createDevices() {
return {
std::make_shared<SdlDisplay>(),
@@ -38,7 +20,7 @@ static std::vector<std::shared_ptr<tt::hal::Device>> createDevices() {
}
extern const Configuration hardwareConfiguration = {
.initBoot = initBoot,
.initBoot = nullptr,
.createDevices = createDevices,
.uart = {
uart::Configuration {
+22 -9
View File
@@ -4,26 +4,39 @@
#include <tactility/check.h>
#include <Tactility/hal/display/DisplayDevice.h>
/** Hack: variable comes from LvglTask.cpp */
extern lv_disp_t* displayHandle;
class SdlDisplay final : public tt::hal::display::DisplayDevice {
lv_disp_t* displayHandle = nullptr;
public:
std::string getName() const override { return "SDL Display"; }
std::string getDescription() const override { return ""; }
bool start() override { return true; }
bool stop() override { check(false, "Not supported"); }
bool stop() override { return true; }
bool supportsLvgl() const override { return true; }
bool startLvgl() override { return displayHandle != nullptr; }
bool stopLvgl() override { check(false, "Not supported"); }
lv_display_t* _Nullable getLvglDisplay() const override { return displayHandle; }
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable getTouchDevice() override { return std::make_shared<SdlTouch>(); }
bool startLvgl() override {
if (displayHandle) return true; // already started
displayHandle = lv_sdl_window_create(320, 240);
lv_sdl_window_set_title(displayHandle, "Tactility");
return displayHandle != nullptr;
}
bool stopLvgl() override {
if (!displayHandle) return true;
lv_display_delete(displayHandle);
displayHandle = nullptr;
return true;
}
lv_display_t* getLvglDisplay() const override { return displayHandle; }
std::shared_ptr<tt::hal::touch::TouchDevice> getTouchDevice() override { return std::make_shared<SdlTouch>(); }
bool supportsDisplayDriver() const override { return false; }
std::shared_ptr<tt::hal::display::DisplayDriver> _Nullable getDisplayDriver() override { return nullptr; }
std::shared_ptr<tt::hal::display::DisplayDriver> getDisplayDriver() override { return nullptr; }
};
+2 -2
View File
@@ -6,7 +6,7 @@
class SdlKeyboard final : public tt::hal::keyboard::KeyboardDevice {
lv_indev_t* _Nullable handle = nullptr;
lv_indev_t* handle = nullptr;
public:
@@ -22,5 +22,5 @@ public:
bool isAttached() const override { return true; }
lv_indev_t* _Nullable getLvglIndev() override { return handle; }
lv_indev_t* getLvglIndev() override { return handle; }
};
+3 -3
View File
@@ -6,7 +6,7 @@
class SdlTouch final : public tt::hal::touch::TouchDevice {
lv_indev_t* _Nullable handle = nullptr;
lv_indev_t* handle = nullptr;
public:
@@ -27,10 +27,10 @@ public:
bool stopLvgl() override { check(false, "Not supported"); }
lv_indev_t* _Nullable getLvglIndev() override { return handle; }
lv_indev_t* getLvglIndev() override { return handle; }
bool supportsTouchDriver() override { return false; }
std::shared_ptr<tt::hal::touch::TouchDriver> _Nullable getTouchDriver() override { return nullptr; };
std::shared_ptr<tt::hal::touch::TouchDriver> getTouchDriver() override { return nullptr; };
};
@@ -99,7 +99,7 @@ bool Hx8357Display::stopLvgl() {
return true;
}
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable Hx8357Display::getTouchDevice() {
std::shared_ptr<tt::hal::touch::TouchDevice> Hx8357Display::getTouchDevice() {
if (touchDevice == nullptr) {
touchDevice = std::reinterpret_pointer_cast<tt::hal::touch::TouchDevice>(createTouch());
LOGGER.info("Created touch device");
@@ -21,10 +21,10 @@
class Hx8357Display : public tt::hal::display::DisplayDevice {
uint8_t* _Nullable buffer = nullptr;
lv_display_t* _Nullable lvglDisplay = nullptr;
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable touchDevice;
std::shared_ptr<tt::hal::display::DisplayDriver> _Nullable nativeDisplay;
uint8_t* buffer = nullptr;
lv_display_t* lvglDisplay = nullptr;
std::shared_ptr<tt::hal::touch::TouchDevice> touchDevice;
std::shared_ptr<tt::hal::display::DisplayDriver> nativeDisplay;
class Hx8357Driver : public tt::hal::display::DisplayDriver {
std::shared_ptr<tt::Lock> lock = tt::hal::spi::getLock(SPI2_HOST);
@@ -51,14 +51,14 @@ public:
bool stopLvgl() override;
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable getTouchDevice() override;
std::shared_ptr<tt::hal::touch::TouchDevice> getTouchDevice() override;
lv_display_t* _Nullable getLvglDisplay() const override { return lvglDisplay; }
lv_display_t* getLvglDisplay() const override { return lvglDisplay; }
// TODO: Set to true after fixing UnPhoneDisplayDriver
bool supportsDisplayDriver() const override { return false; }
std::shared_ptr<tt::hal::display::DisplayDriver> _Nullable getDisplayDriver() override {
std::shared_ptr<tt::hal::display::DisplayDriver> getDisplayDriver() override {
if (nativeDisplay == nullptr) {
nativeDisplay = std::make_shared<Hx8357Driver>();
}
@@ -4,7 +4,7 @@
#include <PwmBacklight.h>
#include <Gc9a01Display.h>
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable createTouch() {
std::shared_ptr<tt::hal::touch::TouchDevice> createTouch() {
auto configuration = std::make_unique<Cst816sTouch::Configuration>(
I2C_NUM_0,
240,
@@ -67,7 +67,7 @@ public:
uint32_t bufferSize; // Size in pixel count. 0 means default, which is 1/10 of the screen size
lcd_rgb_element_order_t rgbElementOrder;
std::shared_ptr<tt::hal::touch::TouchDevice> touch;
std::function<void(uint8_t)> _Nullable backlightDutyFunction = nullptr;
std::function<void(uint8_t)> backlightDutyFunction = nullptr;
};
private:
@@ -93,7 +93,7 @@ public:
std::string getDescription() const override { return "JD9853 display"; }
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable getTouchDevice() override { return configuration->touch; }
std::shared_ptr<tt::hal::touch::TouchDevice> getTouchDevice() override { return configuration->touch; }
void setBacklightDuty(uint8_t backlightDuty) override {
if (configuration->backlightDutyFunction != nullptr) {
@@ -3,7 +3,7 @@
#include <Gt911Touch.h>
#include <RgbDisplay.h>
std::shared_ptr<tt::hal::touch::TouchDevice> _Nullable createTouch() {
std::shared_ptr<tt::hal::touch::TouchDevice> createTouch() {
// Note for future changes: Reset pin is 38 and interrupt pin is 18
// or INT = NC, schematic and other info floating around is kinda conflicting...
auto configuration = std::make_unique<Gt911Touch::Configuration>(