Hal refactored (#99)

This commit is contained in:
Ken Van Hoeylandt
2024-12-02 00:32:39 +01:00
committed by GitHub
parent 0188ce721c
commit 33bb742dfb
103 changed files with 1222 additions and 1228 deletions
+88 -5
View File
@@ -1,20 +1,103 @@
#include "app/display/DisplayPreferences.h"
#include "lvgl.h"
#include "hal/Configuration.h"
#include "hal/Display.h"
#include "hal/Touch.h"
#include "hal/Keyboard.h"
#include "lvgl/LvglKeypad.h"
#include "lvgl/Lvgl.h"
namespace tt::lvgl {
void init(const hal::Configuration* config) {
hal::SetBacklightDuty set_backlight_duty = config->display.setBacklightDuty;
if (set_backlight_duty != nullptr) {
int32_t backlight_duty = app::display::preferences_get_backlight_duty();
set_backlight_duty(backlight_duty);
#define TAG "lvglinit"
bool initDisplay(const hal::Configuration& config) {
assert(config.createDisplay);
auto* display = config.createDisplay();
if (!display->start()) {
TT_LOG_E(TAG, "Display start failed");
return false;
}
lv_display_t* lvgl_display = display->getLvglDisplay();
tt_assert(lvgl_display);
if (display->supportsBacklightDuty()) {
display->setBacklightDuty(0);
}
void* existing_display_user_data = lv_display_get_user_data(lvgl_display);
// esp_lvgl_port users user_data by default, so we have to modify the source
// this is a check for when we upgrade esp_lvgl_port and forget to modify it again
tt_assert(existing_display_user_data == nullptr);
lv_display_set_user_data(lvgl_display, display);
lv_display_rotation_t rotation = app::display::preferences_get_rotation();
if (rotation != lv_disp_get_rotation(lv_disp_get_default())) {
lv_disp_set_rotation(lv_disp_get_default(), static_cast<lv_display_rotation_t>(rotation));
}
return true;
}
bool initTouch(hal::Display* display, hal::Touch* touch) {
TT_LOG_I(TAG, "Touch init");
tt_assert(display);
tt_assert(touch);
if (touch->start(display->getLvglDisplay())) {
return true;
} else {
TT_LOG_E(TAG, "Touch init failed");
return false;
}
}
bool initKeyboard(hal::Display* display, hal::Keyboard* keyboard) {
TT_LOG_I(TAG, "Keyboard init");
tt_assert(display);
tt_assert(keyboard);
if (keyboard->isAttached()) {
if (keyboard->start(display->getLvglDisplay())) {
lv_indev_t* keyboard_indev = keyboard->getLvglIndev();
lv_indev_set_user_data(keyboard_indev, keyboard);
tt::lvgl::keypad_set_indev(keyboard_indev);
TT_LOG_I(TAG, "Keyboard started");
return true;
} else {
TT_LOG_E(TAG, "Keyboard start failed");
return false;
}
} else {
TT_LOG_E(TAG, "Keyboard attach failed");
return false;
}
}
void init(const hal::Configuration& config) {
TT_LOG_I(TAG, "Starting");
if (config.initLvgl != nullptr && !config.initLvgl()) {
TT_LOG_E(TAG, "LVGL init failed");
return;
}
if (!initDisplay(config)) {
return;
}
hal::Display* display = getDisplay();
hal::Touch* touch = display->createTouch();
if (touch != nullptr) {
initTouch(display, touch);
}
if (config.createKeyboard) {
hal::Keyboard* keyboard = config.createKeyboard();
initKeyboard(display, keyboard);
}
TT_LOG_I(TAG, "Finished");
}
} // namespace
+14
View File
@@ -0,0 +1,14 @@
#include "lvgl/Lvgl.h"
#include "Check.h"
namespace tt::lvgl {
hal::Display* getDisplay() {
auto* lvgl_display = lv_display_get_default();
tt_assert(lvgl_display != nullptr);
auto* hal_display = (tt::hal::Display*)lv_display_get_user_data(lvgl_display);
tt_assert(hal_display != nullptr);
return hal_display;
}
}
+7
View File
@@ -0,0 +1,7 @@
#pragma once
#include "hal/Display.h"
namespace tt::lvgl {
hal::Display* getDisplay();
}
+13 -6
View File
@@ -1,11 +1,20 @@
#include <Mutex.h>
#include "LvglSync.h"
#include "Check.h"
namespace tt::lvgl {
static LvglLock lock_singleton = nullptr;
static LvglUnlock unlock_singleton = nullptr;
Mutex lockMutex;
static bool defaultLock(uint32_t timeoutTicks) {
return lockMutex.acquire(timeoutTicks) == TtStatusOk;
}
static void defaultUnlock() {
lockMutex.release();
}
static LvglLock lock_singleton = defaultLock;
static LvglUnlock unlock_singleton = defaultUnlock;
void syncSet(LvglLock lock, LvglUnlock unlock) {
lock_singleton = lock;
@@ -13,12 +22,10 @@ void syncSet(LvglLock lock, LvglUnlock unlock) {
}
bool lock(uint32_t timeout_ticks) {
tt_check(lock_singleton);
return lock_singleton(timeout_ticks);
}
void unlock() {
tt_check(unlock_singleton);
unlock_singleton();
}
+1
View File
@@ -8,6 +8,7 @@ typedef bool (*LvglLock)(uint32_t timeout_ticks);
typedef void (*LvglUnlock)();
void syncSet(LvglLock lock, LvglUnlock unlock);
bool isSyncSet();
bool lock(uint32_t timeout_ticks);
void unlock();