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