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
-42
View File
@@ -1,42 +0,0 @@
#ifdef ESP_PLATFORM
#include <Tactility/Thread.h>
#include <Tactility/CpuAffinity.h>
#include <Tactility/Logger.h>
#include <Tactility/Mutex.h>
#include <Tactility/lvgl/LvglSync.h>
#include <esp_lvgl_port.h>
namespace tt::lvgl {
// LVGL
// The minimum task stack seems to be about 3500, but that crashes the wifi app in some scenarios
// At 8192, it sometimes crashes when wifi-auto enables and is busy connecting and then you open WifiManage
constexpr auto LVGL_TASK_STACK_DEPTH = 9216;
static const auto LOGGER = Logger("EspLvglPort");
bool initEspLvglPort() {
LOGGER.debug("Init");
const lvgl_port_cfg_t lvgl_cfg = {
.task_priority = static_cast<UBaseType_t>(Thread::Priority::Critical),
.task_stack = LVGL_TASK_STACK_DEPTH,
.task_affinity = getCpuAffinityConfiguration().graphics,
.task_max_sleep_ms = 500,
.timer_period_ms = 5
};
if (lvgl_port_init(&lvgl_cfg) != ESP_OK) {
LOGGER.error("Init failed");
return false;
}
syncSet(&lvgl_port_lock, &lvgl_port_unlock);
return true;
}
} // namespace tt::lvgl
#endif
+22 -54
View File
@@ -7,47 +7,24 @@
#include <Tactility/lvgl/Keyboard.h>
#include <Tactility/lvgl/Lvgl.h>
#include <Tactility/lvgl/LvglSync.h>
#include <Tactility/kernel/SystemEvents.h>
#include <Tactility/service/ServiceRegistration.h>
#include <Tactility/settings/DisplaySettings.h>
#ifdef ESP_PLATFORM
#include <Tactility/lvgl/EspLvglPort.h>
#endif
#include <tactility/lvgl_module.h>
#include <tactility/module.h>
#include <lvgl.h>
namespace tt::lvgl {
static const auto LOGGER = Logger("Lvgl");
static bool started = false;
void init(const hal::Configuration& config) {
LOGGER.info("Init started");
#ifdef ESP_PLATFORM
if (config.lvglInit == hal::LvglInit::Default && !initEspLvglPort()) {
return;
}
#endif
start();
LOGGER.info("Init finished");
}
static const auto LOGGER = Logger("LVGL");
bool isStarted() {
return started;
return module_is_started(&lvgl_module);
}
void start() {
LOGGER.info("Start LVGL");
if (started) {
LOGGER.warn("Can't start LVGL twice");
return;
}
void attachDevices() {
LOGGER.info("Adding devices");
auto lock = getSyncLock()->asScopedLock();
lock.lock();
@@ -56,7 +33,7 @@ void start() {
LOGGER.info("Start displays");
auto displays = hal::findDevices<hal::display::DisplayDevice>(hal::Device::Type::Display);
for (const auto& display : displays) {
for (const auto& display: displays) {
if (display->supportsLvgl()) {
if (display->startLvgl()) {
LOGGER.info("Started {}", display->getName());
@@ -82,7 +59,7 @@ void start() {
if (primary_display != nullptr) {
LOGGER.info("Start touch devices");
auto touch_devices = hal::findDevices<hal::touch::TouchDevice>(hal::Device::Type::Touch);
for (const auto& touch_device : touch_devices) {
for (const auto& touch_device: touch_devices) {
// Start any touch devices that haven't been started yet
if (touch_device->supportsLvgl() && touch_device->getLvglIndev() == nullptr) {
if (touch_device->startLvgl(primary_display->getLvglDisplay())) {
@@ -96,7 +73,7 @@ void start() {
// Start keyboards
LOGGER.info("Start keyboards");
auto keyboards = hal::findDevices<hal::keyboard::KeyboardDevice>(hal::Device::Type::Keyboard);
for (const auto& keyboard : keyboards) {
for (const auto& keyboard: keyboards) {
if (keyboard->isAttached()) {
if (keyboard->startLvgl(primary_display->getLvglDisplay())) {
lv_indev_t* keyboard_indev = keyboard->getLvglIndev();
@@ -111,7 +88,7 @@ void start() {
// Start encoders
LOGGER.info("Start encoders");
auto encoders = hal::findDevices<hal::encoder::EncoderDevice>(hal::Device::Type::Encoder);
for (const auto& encoder : encoders) {
for (const auto& encoder: encoders) {
if (encoder->startLvgl(primary_display->getLvglDisplay())) {
LOGGER.info("Started {}", encoder->getName());
} else {
@@ -141,21 +118,10 @@ void start() {
LOGGER.error("Statusbar service is not in Stopped state");
}
}
// Finalize
kernel::publishSystemEvent(kernel::SystemEvent::LvglStarted);
started = true;
}
void stop() {
LOGGER.info("Stopping LVGL");
if (!started) {
LOGGER.warn("Can't stop LVGL: not started");
return;
}
void detachDevices() {
LOGGER.info("Removing devices");
auto lock = getSyncLock()->asScopedLock();
lock.lock();
@@ -169,7 +135,7 @@ void stop() {
LOGGER.info("Stopping keyboards");
auto keyboards = hal::findDevices<hal::keyboard::KeyboardDevice>(hal::Device::Type::Keyboard);
for (auto keyboard : keyboards) {
for (auto keyboard: keyboards) {
if (keyboard->getLvglIndev() != nullptr) {
keyboard->stopLvgl();
}
@@ -180,7 +146,7 @@ void stop() {
LOGGER.info("Stopping touch");
// The display generally stops their own touch devices, but we'll clean up anything that didn't
auto touch_devices = hal::findDevices<hal::touch::TouchDevice>(hal::Device::Type::Touch);
for (auto touch_device : touch_devices) {
for (auto touch_device: touch_devices) {
if (touch_device->getLvglIndev() != nullptr) {
touch_device->stopLvgl();
}
@@ -191,7 +157,7 @@ void stop() {
LOGGER.info("Stopping encoders");
// The display generally stops their own touch devices, but we'll clean up anything that didn't
auto encoder_devices = hal::findDevices<hal::encoder::EncoderDevice>(hal::Device::Type::Encoder);
for (auto encoder_device : encoder_devices) {
for (auto encoder_device: encoder_devices) {
if (encoder_device->getLvglIndev() != nullptr) {
encoder_device->stopLvgl();
}
@@ -200,17 +166,19 @@ void stop() {
LOGGER.info("Stopping displays");
auto displays = hal::findDevices<hal::display::DisplayDevice>(hal::Device::Type::Display);
for (auto display : displays) {
for (auto display: displays) {
if (display->supportsLvgl() && display->getLvglDisplay() != nullptr && !display->stopLvgl()) {
LOGGER.error("Failed to detach display from LVGL");
}
}
}
started = false;
void start() {
check(module_start(&lvgl_module) == ERROR_NONE);
}
kernel::publishSystemEvent(kernel::SystemEvent::LvglStopped);
LOGGER.info("Stopped LVGL");
void stop() {
check(module_stop(&lvgl_module) == ERROR_NONE);
}
} // namespace
+5 -28
View File
@@ -1,39 +1,16 @@
#include "Tactility/lvgl/LvglSync.h"
#include <Tactility/Mutex.h>
#include <tactility/lvgl_module.h>
namespace tt::lvgl {
static Mutex lockMutex;
static bool defaultLock(uint32_t timeoutMillis) {
return lockMutex.lock(timeoutMillis);
}
static void defaultUnlock() {
lockMutex.unlock();
}
static LvglLock lock_singleton = defaultLock;
static LvglUnlock unlock_singleton = defaultUnlock;
void syncSet(LvglLock lock, LvglUnlock unlock) {
auto old_lock = lock_singleton;
auto old_unlock = unlock_singleton;
// Ensure the old lock is not engaged when changing locks
old_lock((uint32_t)kernel::MAX_TICKS);
lock_singleton = lock;
unlock_singleton = unlock;
old_unlock();
}
bool lock(TickType_t timeout) {
return lock_singleton(pdMS_TO_TICKS(timeout == 0 ? kernel::MAX_TICKS : timeout));
return lvgl_try_lock_timed(timeout);
}
void unlock() {
unlock_singleton();
lvgl_unlock();
}
class LvglSync : public Lock {
@@ -41,11 +18,11 @@ public:
~LvglSync() override = default;
bool lock(TickType_t timeoutTicks) const override {
return lvgl::lock(timeoutTicks);
return lvgl_try_lock_timed(timeoutTicks);
}
void unlock() const override {
lvgl::unlock();
lvgl_unlock();
}
};