cleanup and improvements

improved driver creation
fixed compile warnings in check.h
hello_world example is now working again with lvgl
This commit is contained in:
Ken Van Hoeylandt 2023-12-26 23:04:40 +01:00
parent 25b0aa09e2
commit 88c5c55be3
20 changed files with 138 additions and 108 deletions

View File

@ -108,14 +108,15 @@ static bool prv_create_display(nb_display_t* display) {
display->vertical_resolution = VERTICAL_RESOLUTION; display->vertical_resolution = VERTICAL_RESOLUTION;
display->draw_buffer_height = DRAW_BUFFER_HEIGHT; display->draw_buffer_height = DRAW_BUFFER_HEIGHT;
display->bits_per_pixel = BITS_PER_PIXEL; display->bits_per_pixel = BITS_PER_PIXEL;
display->mirror_x = true;
display->mirror_y = false;
return true; return true;
} }
nb_display_driver_t board_2432s024_create_display_driver() { nb_display_driver_t board_2432s024_create_display_driver() {
nb_display_driver_t driver = { return (nb_display_driver_t) {
.name = "ili9341_2432s024", .name = "ili9341_2432s024",
.create_display = &prv_create_display .create_display = &prv_create_display
}; };
return driver;
} }

View File

@ -65,9 +65,8 @@ static bool prv_create_touch(esp_lcd_panel_io_handle_t* io_handle, esp_lcd_touch
} }
nb_touch_driver_t board_2432s024_create_touch_driver() { nb_touch_driver_t board_2432s024_create_touch_driver() {
nb_touch_driver_t driver = { return (nb_touch_driver_t) {
.name = "cst816s_2432s024", .name = "cst816s_2432s024",
.create_touch = &prv_create_touch .create_touch = &prv_create_touch
}; };
return driver;
} }

View File

@ -146,7 +146,6 @@ FURI_NORETURN void __furi_crash_implementation() {
// Check if debug enabled by DAP // Check if debug enabled by DAP
// https://developer.arm.com/documentation/ddi0403/d/Debug-Architecture/ARMv7-M-Debug/Debug-register-support-in-the-SCS/Debug-Halting-Control-and-Status-Register--DHCSR?lang=en // https://developer.arm.com/documentation/ddi0403/d/Debug-Architecture/ARMv7-M-Debug/Debug-register-support-in-the-SCS/Debug-Halting-Control-and-Status-Register--DHCSR?lang=en
// bool debug = CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk; // bool debug = CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk;
bool debug = true;
#ifdef FURI_NDEBUG #ifdef FURI_NDEBUG
if(debug) { if(debug) {
#endif #endif

View File

@ -37,7 +37,7 @@ FURI_NORETURN void __furi_halt_implementation();
/** Crash system with message. */ /** Crash system with message. */
#define __furi_crash(message) \ #define __furi_crash(message) \
do { \ do { \
ESP_LOGE("crash", "%s\n\tat %s:%d", (message) ? (message) : "", __FILE__, __LINE__); \ ESP_LOGE("crash", "%s\n\tat %s:%d", ((message) ? ((const char*)message) : ""), __FILE__, __LINE__); \
__furi_crash_implementation(); \ __furi_crash_implementation(); \
} while(0) } while(0)
@ -50,7 +50,7 @@ FURI_NORETURN void __furi_halt_implementation();
/** Halt system with message. */ /** Halt system with message. */
#define __furi_halt(message) \ #define __furi_halt(message) \
do { \ do { \
ESP_LOGE("halt", "%s\n\tat %s:%d", (message) ? (message) : "", __FILE__, __LINE__); \ ESP_LOGE("halt", "%s\n\tat %s:%d", ((message) ? ((const char*)message) : ""), __FILE__, __LINE__); \
__furi_halt_implementation(); \ __furi_halt_implementation(); \
} while(0) } while(0)

View File

@ -81,7 +81,7 @@ static void furi_thread_body(void* context) {
furi_assert(thread->state == FuriThreadStateStarting); furi_assert(thread->state == FuriThreadStateStarting);
furi_thread_set_state(thread, FuriThreadStateRunning); furi_thread_set_state(thread, FuriThreadStateRunning);
TaskHandle_t task_handle = xTaskGetCurrentTaskHandle(); // TaskHandle_t task_handle = xTaskGetCurrentTaskHandle();
// if(thread->heap_trace_enabled == true) { // if(thread->heap_trace_enabled == true) {
// memmgr_heap_enable_thread_trace((FuriThreadId)task_handle); // memmgr_heap_enable_thread_trace((FuriThreadId)task_handle);
// } // }

View File

@ -2,14 +2,17 @@
#include "nb_hardware.h" #include "nb_hardware.h"
#include "nb_app.h" #include "nb_app.h"
#include "nb_config.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
extern void nanobake_start(nb_config_t _Nonnull * config); // Forward declarations
typedef void* FuriThreadId; typedef void* FuriThreadId;
typedef struct nb_lvgl nb_lvgl_t;
extern void nanobake_start(nb_config_t _Nonnull * config);
extern FuriThreadId nanobake_get_app_thread_id(size_t index); extern FuriThreadId nanobake_get_app_thread_id(size_t index);
extern size_t nanobake_get_app_thread_count(); extern size_t nanobake_get_app_thread_count();

View File

@ -18,7 +18,8 @@ struct nb_config {
// Optional driver for touch input // Optional driver for touch input
const create_touch_driver _Nullable touch_driver; const create_touch_driver _Nullable touch_driver;
// List of user applications // List of user applications
const nb_app_t* apps[]; const size_t apps_count;
const nb_app_t* const apps[];
}; };
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -15,6 +15,8 @@ struct nb_display {
uint16_t bits_per_pixel; uint16_t bits_per_pixel;
esp_lcd_panel_io_handle_t _Nonnull io_handle; esp_lcd_panel_io_handle_t _Nonnull io_handle;
esp_lcd_panel_handle_t _Nonnull display_handle; esp_lcd_panel_handle_t _Nonnull display_handle;
bool mirror_x;
bool mirror_y;
}; };
typedef struct nb_display_driver nb_display_driver_t; typedef struct nb_display_driver nb_display_driver_t;

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "nb_config.h" #include "nb_display.h"
#include <lvgl.h> #include "nb_touch.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -13,12 +13,6 @@ struct nb_hardware {
nb_touch_t* _Nullable touch; nb_touch_t* _Nullable touch;
}; };
/**
* @param[in] config
* @return a newly allocated platform instance (caller takes ownership)
*/
nb_hardware_t _Nonnull* nb_hardware_alloc(nb_config_t _Nonnull* config);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <esp_lvgl_port.h> #include <lvgl.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -12,10 +12,6 @@ struct nb_lvgl {
lv_indev_t* _Nullable touch_indev; lv_indev_t* _Nullable touch_indev;
}; };
typedef struct nb_hardware nb_hardware_t;
extern nb_lvgl_t nb_lvgl_init(nb_hardware_t* platform);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -22,7 +22,7 @@ const nb_app_t* const FLIPPER_SYSTEM_APPS[] = {
const size_t FLIPPER_SYSTEM_APPS_COUNT = sizeof(FLIPPER_SYSTEM_APPS) / sizeof(nb_app_t*); const size_t FLIPPER_SYSTEM_APPS_COUNT = sizeof(FLIPPER_SYSTEM_APPS) / sizeof(nb_app_t*);
const FlipperInternalOnStartHook FLIPPER_ON_SYSTEM_START[] = { const nb_on_system_start_ FLIPPER_ON_SYSTEM_START[] = {
}; };
const size_t FLIPPER_ON_SYSTEM_START_COUNT = sizeof(FLIPPER_ON_SYSTEM_START) / sizeof(FlipperInternalOnStartHook); const size_t FLIPPER_ON_SYSTEM_START_COUNT = sizeof(FLIPPER_ON_SYSTEM_START) / sizeof(nb_on_system_start_);

View File

@ -6,7 +6,10 @@
extern "C" { extern "C" {
#endif #endif
typedef void (*FlipperInternalOnStartHook)(void); // Forward declaration
typedef struct nb_hardware nb_hardware_t;
typedef void (*nb_on_system_start_)(nb_hardware_t* hardware);
extern const nb_app_t* const FLIPPER_SERVICES[]; extern const nb_app_t* const FLIPPER_SERVICES[];
extern const size_t FLIPPER_SERVICES_COUNT; extern const size_t FLIPPER_SERVICES_COUNT;
@ -14,7 +17,7 @@ extern const size_t FLIPPER_SERVICES_COUNT;
extern const nb_app_t* const FLIPPER_SYSTEM_APPS[]; extern const nb_app_t* const FLIPPER_SYSTEM_APPS[];
extern const size_t FLIPPER_SYSTEM_APPS_COUNT; extern const size_t FLIPPER_SYSTEM_APPS_COUNT;
extern const FlipperInternalOnStartHook FLIPPER_ON_SYSTEM_START[]; extern const nb_on_system_start_ FLIPPER_ON_SYSTEM_START[];
extern const size_t FLIPPER_ON_SYSTEM_START_COUNT; extern const size_t FLIPPER_ON_SYSTEM_START_COUNT;
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -16,7 +16,7 @@ static int32_t prv_desktop_main(void* param) {
// //
// lv_obj_t* label = lv_label_create(lv_parent); // lv_obj_t* label = lv_label_create(lv_parent);
// lv_label_set_recolor(label, true); // lv_label_set_recolor(label, true);
// lv_obj_set_width(label, (lv_coord_t)platform->display->horizontal_resolution); // lv_obj_set_width(label, (lv_coord_t)hardware->display->horizontal_resolution);
// lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_LEFT, 0); // lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_LEFT, 0);
// lv_label_set_text(label, "Desktop app"); // lv_label_set_text(label, "Desktop app");
// lv_obj_align(label, LV_ALIGN_TOP_LEFT, 0, 0); // lv_obj_align(label, LV_ALIGN_TOP_LEFT, 0, 0);

View File

@ -1,6 +1,6 @@
#include "nanobake.h" #include "nanobake.h"
#include "nb_hardware.h" #include "nb_hardwarei.h"
#include "nb_lvgl.h" #include "nb_lvgli.h"
#include "applications/nb_applications.h" #include "applications/nb_applications.h"
#include <esp_log.h> #include <esp_log.h>
#include <m-list.h> #include <m-list.h>
@ -10,10 +10,11 @@
#include <record.h> #include <record.h>
#include <check.h> #include <check.h>
static const char* TAG = "nanobake";
M_LIST_DEF(thread_ids, FuriThreadId); M_LIST_DEF(thread_ids, FuriThreadId);
static const char* TAG = "nanobake";
thread_ids_t prv_thread_ids;
static void prv_furi_init() { static void prv_furi_init() {
// TODO: can we remove the suspend-resume logic? // TODO: can we remove the suspend-resume logic?
if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) { if (xTaskGetSchedulerState() == taskSCHEDULER_RUNNING) {
@ -25,8 +26,6 @@ static void prv_furi_init() {
xTaskResumeAll(); xTaskResumeAll();
} }
thread_ids_t prv_thread_ids;
FuriThreadId nanobake_get_app_thread_id(size_t index) { FuriThreadId nanobake_get_app_thread_id(size_t index) {
return *thread_ids_get(prv_thread_ids, index); return *thread_ids_get(prv_thread_ids, index);
} }
@ -38,8 +37,8 @@ size_t nanobake_get_app_thread_count() {
extern void nanobake_start(nb_config_t _Nonnull* config) { extern void nanobake_start(nb_config_t _Nonnull* config) {
prv_furi_init(); prv_furi_init();
nb_hardware_t _Nonnull* hardware = nb_hardware_alloc(config); nb_hardware_t hardware = nb_hardware_create(config);
nb_lvgl_init(hardware); nb_lvgl_t lvgl = nb_lvgl_init(&hardware);
thread_ids_init(prv_thread_ids); thread_ids_init(prv_thread_ids);
@ -81,24 +80,24 @@ extern void nanobake_start(nb_config_t _Nonnull* config) {
thread_ids_push_back(prv_thread_ids, thread_id); thread_ids_push_back(prv_thread_ids, thread_id);
} }
// ESP_LOGI(TAG, "Starting external apps"); ESP_LOGI(TAG, "Starting external apps");
//
// size_t external_apps_count = sizeof(*config->apps); for(size_t i = 0; i < config->apps_count; i++) {
// for(size_t i = 0; i < FLIPPER_SERVICES_COUNT; i++) { const nb_app_t* app = config->apps[i];
// ESP_LOGI(TAG, "Starting external app \"%s\"", FLIPPER_[i]->name); ESP_LOGI(TAG, "Starting external app \"%s\"", app->name);
//
// FuriThread* thread = furi_thread_alloc_ex( FuriThread* thread = furi_thread_alloc_ex(
// FLIPPER_SERVICES[i]->name, app->name,
// FLIPPER_SERVICES[i]->stack_size, app->stack_size,
// FLIPPER_SERVICES[i]->entry_point, app->entry_point,
// NULL NULL
// ); );
// furi_thread_set_appid(thread, FLIPPER_SERVICES[i]->id); furi_thread_set_appid(thread, app->id);
// furi_thread_start(thread); furi_thread_start(thread);
//
// FuriThreadId thread_id = furi_thread_get_id(thread); FuriThreadId thread_id = furi_thread_get_id(thread);
// thread_ids_push_back(prv_thread_ids, thread_id); thread_ids_push_back(prv_thread_ids, thread_id);
// } }
ESP_LOGI(TAG, "Startup complete"); ESP_LOGI(TAG, "Startup complete");
} }

View File

@ -1,32 +1,29 @@
#include "nb_hardware.h" #include "nb_hardwarei.h"
#include "nb_display.h"
#include "nb_touch.h"
#include <esp_check.h> #include <esp_check.h>
#include <esp_err.h> #include <esp_err.h>
#include <esp_lvgl_port.h>
#include <check.h> #include <check.h>
static const char* TAG = "nb_hardware"; static const char* TAG = "nb_hardware";
nb_hardware_t _Nonnull* nb_hardware_alloc(nb_config_t _Nonnull* config) { nb_hardware_t nb_hardware_create(nb_config_t _Nonnull* config) {
nb_hardware_t* platform = malloc(sizeof(nb_hardware_t));
furi_check(config->display_driver != NULL, "no display driver configured"); furi_check(config->display_driver != NULL, "no display driver configured");
nb_display_driver_t display_driver = config->display_driver(); nb_display_driver_t display_driver = config->display_driver();
ESP_LOGI(TAG, "display with driver %s", display_driver.name); ESP_LOGI(TAG, "display with driver %s", display_driver.name);
platform->display = nb_display_alloc(&display_driver); nb_display_t* display = nb_display_alloc(&display_driver);
nb_touch_t* touch = NULL;
if (config->touch_driver != NULL) { if (config->touch_driver != NULL) {
nb_touch_driver_t touch_driver = config->touch_driver(); nb_touch_driver_t touch_driver = config->touch_driver();
ESP_LOGI(TAG, "touch with driver %s", touch_driver.name); ESP_LOGI(TAG, "touch with driver %s", touch_driver.name);
platform->touch = nb_touch_alloc(&touch_driver); touch = nb_touch_alloc(&touch_driver);
} else { } else {
ESP_LOGI(TAG, "no touch configured"); ESP_LOGI(TAG, "no touch configured");
platform->touch = NULL; touch = NULL;
} }
return (nb_hardware_t) {
return platform; .display = display,
.touch = touch
};
} }

View File

@ -0,0 +1,14 @@
#pragma once
#include "nb_hardware.h"
#include "nb_config.h"
#ifdef __cplusplus
extern "C" {
#endif
extern nb_hardware_t nb_hardware_create(nb_config_t _Nonnull* config);
#ifdef __cplusplus
}
#endif

View File

@ -1,13 +1,11 @@
#include "nb_lvgl.h" #include "nb_lvgli.h"
#include "nb_hardware.h" #include "nb_hardwarei.h"
#include <esp_check.h> #include <esp_lvgl_port.h>
#include <check.h> #include <check.h>
static const char* TAG = "nb_lvgl"; static const char* TAG = "nb_lvgl";
nb_lvgl_t nb_lvgl_init(nb_hardware_t* platform) { nb_lvgl_t nb_lvgl_init(nb_hardware_t _Nonnull* hardware) {
nb_lvgl_t lvgl;
const lvgl_port_cfg_t lvgl_cfg = { const lvgl_port_cfg_t lvgl_cfg = {
.task_priority = 4, .task_priority = 4,
.task_stack = 4096, .task_stack = 4096,
@ -15,8 +13,10 @@ nb_lvgl_t nb_lvgl_init(nb_hardware_t* platform) {
.task_max_sleep_ms = 500, .task_max_sleep_ms = 500,
.timer_period_ms = 5 .timer_period_ms = 5
}; };
furi_check(lvgl_port_init(&lvgl_cfg) == ESP_OK, "lvgl port init failed"); furi_check(lvgl_port_init(&lvgl_cfg) == ESP_OK, "lvgl port init failed");
nb_display_t _Nonnull* display = platform->display; nb_display_t _Nonnull* display = hardware->display;
// Add display // Add display
ESP_LOGD(TAG, "lvgl add display"); ESP_LOGD(TAG, "lvgl add display");
const lvgl_port_display_cfg_t disp_cfg = { const lvgl_port_display_cfg_t disp_cfg = {
@ -27,28 +27,31 @@ nb_lvgl_t nb_lvgl_init(nb_hardware_t* platform) {
.hres = display->horizontal_resolution, .hres = display->horizontal_resolution,
.vres = display->vertical_resolution, .vres = display->vertical_resolution,
.monochrome = false, .monochrome = false,
/* Rotation values must be same as defined in driver */
// TODO: expose data from driver
.rotation = { .rotation = {
.swap_xy = false, .swap_xy = false,
.mirror_x = true, .mirror_x = display->mirror_x,
.mirror_y = false, .mirror_y = display->mirror_y,
}, },
.flags = { .flags = {
.buff_dma = true, .buff_dma = true,
} }
}; };
lvgl.disp = lvgl_port_add_disp(&disp_cfg);
lv_disp_t _Nonnull* disp = lvgl_port_add_disp(&disp_cfg);
lv_indev_t _Nullable* touch_indev = NULL;
// Add touch // Add touch
if (platform->touch != NULL) { if (hardware->touch != NULL) {
const lvgl_port_touch_cfg_t touch_cfg = { const lvgl_port_touch_cfg_t touch_cfg = {
.disp = lvgl.disp, .disp = disp,
.handle = platform->touch->touch_handle, .handle = hardware->touch->touch_handle,
}; };
lvgl.touch_indev = lvgl_port_add_touch(&touch_cfg); touch_indev = lvgl_port_add_touch(&touch_cfg);
furi_check(lvgl.touch_indev != NULL, "failed to add touch to lvgl"); furi_check(touch_indev != NULL, "failed to add touch to lvgl");
} }
return lvgl; return (nb_lvgl_t) {
.disp = disp,
.touch_indev = touch_indev
};
} }

View File

@ -0,0 +1,15 @@
#pragma once
#include "nb_lvgl.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct nb_hardware nb_hardware_t;
extern nb_lvgl_t nb_lvgl_init(nb_hardware_t _Nonnull* hardware);
#ifdef __cplusplus
}
#endif

View File

@ -1,9 +1,10 @@
#include "hello_world.h" #include "hello_world.h"
#include "core_defines.h" #include <core_defines.h>
#include "esp_lvgl_port.h" #include <nb_lvgl.h>
#include <esp_lvgl_port.h>
#include <esp_log.h> #include <esp_log.h>
#include "nb_hardware.h" #include <nb_hardware.h>
static const char* TAG = "app_helloworld"; static const char* TAG = "app_helloworld";
@ -13,22 +14,24 @@ static void prv_on_button_click(lv_event_t _Nonnull* event) {
static int32_t prv_on_create(void* param) { static int32_t prv_on_create(void* param) {
UNUSED(param); UNUSED(param);
// lvgl_port_lock(0); lvgl_port_lock(0);
//
// lv_obj_t* label = lv_label_create(lv_parent); lv_obj_t* lv_parent = lv_scr_act();
// lv_label_set_recolor(label, true);
// lv_obj_set_width(label, (lv_coord_t)platform->display->horizontal_resolution); lv_obj_t* label = lv_label_create(lv_parent);
// lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0); lv_label_set_recolor(label, true);
// lv_label_set_text(label, "Hello, world!"); lv_obj_set_width(label, 200);
// lv_obj_align(label, LV_ALIGN_CENTER, 0, -30); lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
// lv_label_set_text(label, "Hello, world!");
// lv_obj_t* btn = lv_btn_create(lv_parent); lv_obj_align(label, LV_ALIGN_CENTER, 0, -30);
// label = lv_label_create(btn);
// lv_label_set_text_static(label, "Button"); lv_obj_t* btn = lv_btn_create(lv_parent);
// lv_obj_align(btn, LV_ALIGN_CENTER, 0, 30); label = lv_label_create(btn);
// lv_obj_add_event_cb(btn, prv_on_button_click, LV_EVENT_CLICKED, NULL); lv_label_set_text_static(label, "Button");
// lv_obj_align(btn, LV_ALIGN_CENTER, 0, 30);
// lvgl_port_unlock(); lv_obj_add_event_cb(btn, prv_on_button_click, LV_EVENT_CLICKED, NULL);
lvgl_port_unlock();
return 0; return 0;
} }

View File

@ -8,13 +8,14 @@
#include "hello_world/hello_world.h" #include "hello_world/hello_world.h"
void app_main(void) { void app_main(void) {
static nb_config_t platform_config = { static nb_config_t config = {
.display_driver = &board_2432s024_create_display_driver, .display_driver = &board_2432s024_create_display_driver,
.touch_driver = &board_2432s024_create_touch_driver, .touch_driver = &board_2432s024_create_touch_driver,
.apps = { .apps = {
&hello_world_app &hello_world_app
} },
.apps_count = 1
}; };
nanobake_start(&platform_config); nanobake_start(&config);
} }