basic application support

This commit is contained in:
Ken Van Hoeylandt
2023-12-25 17:53:58 +01:00
parent 7886d5c2f9
commit e6525364c6
21 changed files with 341 additions and 122 deletions
@@ -0,0 +1,36 @@
#include "system_info.h"
#include <esp_lvgl_port.h>
#include <nb_platform.h>
static void prv_on_create(nb_platform_t _Nonnull* platform, lv_obj_t _Nonnull* lv_parent) {
lvgl_port_lock(0);
lv_obj_t* cpu_label = lv_label_create(lv_parent);
lv_label_set_recolor(cpu_label, true);
lv_obj_set_width(cpu_label, (lv_coord_t)platform->display->horizontal_resolution);
lv_obj_set_style_text_align(cpu_label, LV_TEXT_ALIGN_LEFT, 0);
lv_label_set_text(cpu_label, "CPU usage: ?");
lv_obj_align(cpu_label, LV_ALIGN_TOP_LEFT, 0, 0);
lv_obj_t* mem_free_label = lv_label_create(lv_parent);
lv_label_set_recolor(mem_free_label, true);
lv_obj_set_width(mem_free_label, (lv_coord_t)platform->display->horizontal_resolution);
lv_obj_set_style_text_align(mem_free_label, LV_TEXT_ALIGN_LEFT, 0);
lv_label_set_text(mem_free_label, "Memory: ?");
lv_obj_align(mem_free_label, LV_ALIGN_TOP_LEFT, 0, 15);
lvgl_port_unlock();
}
nb_app_config_t system_info_app_config() {
nb_app_config_t config = {
.id = "systeminfo",
.name = "System Info",
.type = SYSTEM,
.on_create = &prv_on_create,
.on_update = NULL,
.on_destroy = NULL
};
return config;
}
@@ -0,0 +1,8 @@
#ifndef NANOBAKE_SYSTEM_INFO_H
#define NANOBAKE_SYSTEM_INFO_H
#include "nb_app.h"
nb_app_config_t system_info_app_config();
#endif // NANOBAKE_SYSTEM_INFO_H
+14
View File
@@ -0,0 +1,14 @@
#include "nanobake.h"
#include "applications/main/system_info/system_info.h"
void nb_app_start(nb_platform_t _Nonnull* platform, nb_app_config_t _Nonnull* config) {
lv_obj_t* scr = lv_scr_act();
ESP_ERROR_CHECK(nb_app_config_validate(config));
config->on_create(platform, scr);
}
extern void nanobake_run(nb_platform_config_t _Nonnull* config) {
nb_platform_t _Nonnull* platform = nb_platform_create(config);
nb_app_config_t app_config = system_info_app_config();
nb_app_start(platform, &app_config);
}
+32
View File
@@ -0,0 +1,32 @@
#include "nb_app.h"
#include <esp_check.h>
#include <string.h>
static const char* TAG = "nb_app";
esp_err_t nb_app_config_validate(nb_app_config_t* _Nonnull app) {
ESP_RETURN_ON_FALSE(
strlen(app->id) < NB_APP_ID_LENGTH,
ESP_FAIL,
TAG,
"app id cannot be larger than %d characters",
NB_APP_ID_LENGTH - 1
);
ESP_RETURN_ON_FALSE(
strlen(app->name) < NB_APP_NAME_LENGTH,
ESP_FAIL,
TAG,
"app name cannot be larger than %d characters",
NB_APP_NAME_LENGTH - 1
);
ESP_RETURN_ON_FALSE(
(app->on_update == NULL) == (app->update_task_priority == 0 && app->update_task_priority == 0),
ESP_FAIL,
TAG,
"app update is inconsistently configured"
);
return ESP_OK;
}
+20
View File
@@ -0,0 +1,20 @@
#ifndef NANOBAKE_NB_ASSERT_H
#define NANOBAKE_NB_ASSERT_H
#include <assert.h>
#include <esp_log.h>
#define NB_ASSERT(x, message) do { \
if (!(x)) { \
ESP_LOGE("assert", message); \
_esp_error_check_failed( \
x, \
__FILE__, \
__LINE__, \
__ASSERT_FUNC, \
#x \
); \
} \
} while(0)
#endif //NANOBAKE_NB_ASSERT_H
+5 -6
View File
@@ -1,9 +1,8 @@
#include "nb_display.h"
#include "nb_internal.h"
#include <esp_check.h>
#include <esp_log.h>
#include "nb_assert.h"
esp_err_t nb_display_create(nb_display_driver_t driver, nb_display_t* display) {
ESP_RETURN_ON_ERROR(driver.create_display(display), nbi_tag, "failed to create driver");
return ESP_OK;
nb_display_t _Nonnull* nb_display_create(nb_display_driver_t _Nonnull* driver) {
nb_display_t _Nonnull* display = malloc(sizeof(nb_display_t));
NB_ASSERT(driver->create_display(display) == ESP_OK, "failed to create display");
return display;
}
+35 -34
View File
@@ -1,11 +1,13 @@
#include "nb_platform.h"
#include <esp_check.h>
#include "nb_display.h"
#include "nb_touch.h"
#include "nb_internal.h"
#include <esp_check.h>
#include <esp_err.h>
#include <esp_lvgl_port.h>
#include <nb_assert.h>
static const char* TAG = "nb_platform";
static esp_err_t prv_lvgl_init(
@@ -19,18 +21,19 @@ static esp_err_t prv_lvgl_init(
.timer_period_ms = 5
};
ESP_RETURN_ON_ERROR(lvgl_port_init(&lvgl_cfg), TAG, "lvgl port init failed");
nb_display_t _Nonnull* display = platform->display;
// Add display
ESP_LOGD(TAG, "lvgl add display");
const lvgl_port_display_cfg_t disp_cfg = {
.io_handle = platform->display.io_handle,
.panel_handle = platform->display.display_handle,
.buffer_size = platform->display.horizontal_resolution * platform->display.draw_buffer_height * (platform->display.bits_per_pixel / 8),
.io_handle = display->io_handle,
.panel_handle = display->display_handle,
.buffer_size = display->horizontal_resolution * display->draw_buffer_height * (display->bits_per_pixel / 8),
.double_buffer = 1,
.hres = platform->display.horizontal_resolution,
.vres = platform->display.vertical_resolution,
.hres = display->horizontal_resolution,
.vres = display->vertical_resolution,
.monochrome = false,
/* Rotation values must be same as used in esp_lcd for initial settings of the screen */
/* Rotation values must be same as defined in driver */
// TODO: expose data from driver
.rotation = {
.swap_xy = false,
.mirror_x = true,
@@ -40,41 +43,39 @@ static esp_err_t prv_lvgl_init(
.buff_dma = true,
}
};
platform->lvgl.disp = lvgl_port_add_disp(&disp_cfg);
platform->lvgl->disp = lvgl_port_add_disp(&disp_cfg);
// Add touch
if (platform->touch.io_handle != NULL && platform->touch.touch_handle != NULL) {
if (platform->touch != NULL) {
const lvgl_port_touch_cfg_t touch_cfg = {
.disp = platform->lvgl.disp,
.handle = platform->touch.touch_handle,
.disp = platform->lvgl->disp,
.handle = platform->touch->touch_handle,
};
platform->lvgl.touch_indev = lvgl_port_add_touch(&touch_cfg);
ESP_RETURN_ON_FALSE(platform->lvgl.touch_indev != NULL, ESP_FAIL, TAG, "failed to add touch to lvgl");
platform->lvgl->touch_indev = lvgl_port_add_touch(&touch_cfg);
ESP_RETURN_ON_FALSE(platform->lvgl->touch_indev != NULL, ESP_FAIL, TAG, "failed to add touch to lvgl");
}
return ESP_OK;
}
esp_err_t nb_platform_create(nb_platform_config_t config, nb_platform_t* platform) {
ESP_LOGI(TAG, "display with driver %s", config.display_driver.name);
ESP_RETURN_ON_ERROR(
nb_display_create(config.display_driver, &(platform->display)),
nbi_tag,
"display driver init failed"
);
nb_platform_t _Nonnull* nb_platform_create(nb_platform_config_t _Nonnull* config) {
nb_platform_t* platform = malloc(sizeof(nb_platform_t));
ESP_LOGI(TAG, "touch with driver %s", config.touch_driver.name);
ESP_RETURN_ON_ERROR(
nb_touch_create(config.touch_driver, &(platform->touch)),
nbi_tag,
"touch driver init failed"
);
NB_ASSERT(config->display_driver != NULL, "no display driver configured");
nb_display_driver_t display_driver = config->display_driver();
ESP_LOGI(TAG, "display with driver %s", display_driver.name);
platform->display = nb_display_create(&display_driver);
ESP_RETURN_ON_ERROR(
prv_lvgl_init(platform),
nbi_tag,
"lvgl init failed"
);
if (config->touch_driver != NULL) {
nb_touch_driver_t touch_driver = config->touch_driver();
ESP_LOGI(TAG, "touch with driver %s", touch_driver.name);
platform->touch = nb_touch_create(&touch_driver);
} else {
ESP_LOGI(TAG, "no touch configured");
platform->touch = NULL;
}
return ESP_OK;
ESP_ERROR_CHECK(prv_lvgl_init(platform));
return platform;
}
+5 -5
View File
@@ -1,9 +1,9 @@
#include "nb_touch.h"
#include <esp_check.h>
#include "nb_internal.h"
esp_err_t nb_touch_create(nb_touch_driver_t driver, nb_touch_t* touch) {
ESP_RETURN_ON_ERROR(driver.init_io(&(touch->io_handle)), nbi_tag, "failed to init io");
ESP_RETURN_ON_ERROR(driver.create_touch(touch->io_handle, &(touch->touch_handle)), nbi_tag, "failed to create driver");
return ESP_OK;
nb_touch_t _Nonnull* nb_touch_create(nb_touch_driver_t _Nonnull* driver) {
nb_touch_t _Nonnull* touch = malloc(sizeof(nb_touch_t));
assert(driver->init_io(&(touch->io_handle)) == ESP_OK);
driver->create_touch(touch->io_handle, &(touch->touch_handle));
return touch;
}