improvements to apps and drivers

This commit is contained in:
Ken Van Hoeylandt 2023-12-25 18:43:48 +01:00
parent e6525364c6
commit 0cf7829a2d
16 changed files with 108 additions and 114 deletions

View File

@ -10,7 +10,7 @@
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/semphr.h> #include <freertos/semphr.h>
static const char* TAG = "ili9341"; static const char* TAG = "2432s024_ili9341";
static SemaphoreHandle_t refresh_finish = NULL; static SemaphoreHandle_t refresh_finish = NULL;
@ -32,8 +32,8 @@ IRAM_ATTR static bool prv_on_color_trans_done(esp_lcd_panel_io_handle_t io_handl
return (need_yield == pdTRUE); return (need_yield == pdTRUE);
} }
static esp_err_t prv_create_display(nb_display_t* display) { static bool prv_create_display(nb_display_t* display) {
ESP_LOGI(TAG, "init started"); ESP_LOGI(TAG, "creating display");
gpio_config_t io_conf = { gpio_config_t io_conf = {
.pin_bit_mask = BIT64(PIN_BACKLIGHT), .pin_bit_mask = BIT64(PIN_BACKLIGHT),
@ -50,11 +50,10 @@ static esp_err_t prv_create_display(nb_display_t* display) {
HORIZONTAL_RESOLUTION * DRAW_BUFFER_HEIGHT * (BITS_PER_PIXEL / 8) HORIZONTAL_RESOLUTION * DRAW_BUFFER_HEIGHT * (BITS_PER_PIXEL / 8)
); );
ESP_RETURN_ON_ERROR( if (spi_bus_initialize(SELECTED_SPI_HOST, &bus_config, SPI_DMA_CH_AUTO) != ESP_OK) {
spi_bus_initialize(SELECTED_SPI_HOST, &bus_config, SPI_DMA_CH_AUTO), ESP_LOGD(TAG, "spi bus init failed");
TAG, return false;
"spi bus init failed" }
);
const esp_lcd_panel_io_spi_config_t panel_io_config = ILI9341_PANEL_IO_SPI_CONFIG( const esp_lcd_panel_io_spi_config_t panel_io_config = ILI9341_PANEL_IO_SPI_CONFIG(
PIN_CS, PIN_CS,
@ -63,11 +62,10 @@ static esp_err_t prv_create_display(nb_display_t* display) {
NULL NULL
); );
ESP_RETURN_ON_ERROR( if (esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)SELECTED_SPI_HOST, &panel_io_config, &display->io_handle) != ESP_OK) {
esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)SELECTED_SPI_HOST, &panel_io_config, &display->io_handle), ESP_LOGD(TAG, "failed to create panel");
TAG, return false;
"failed to create panel" }
);
ESP_LOGI(TAG, "install driver"); ESP_LOGI(TAG, "install driver");
const esp_lcd_panel_dev_config_t panel_config = { const esp_lcd_panel_dev_config_t panel_config = {
@ -75,43 +73,43 @@ static esp_err_t prv_create_display(nb_display_t* display) {
.rgb_endian = LCD_RGB_ENDIAN_RGB, .rgb_endian = LCD_RGB_ENDIAN_RGB,
.bits_per_pixel = BITS_PER_PIXEL, .bits_per_pixel = BITS_PER_PIXEL,
}; };
ESP_RETURN_ON_ERROR( if (esp_lcd_new_panel_ili9341(display->io_handle, &panel_config, &display->display_handle) != ESP_OK) {
esp_lcd_new_panel_ili9341(display->io_handle, &panel_config, &display->display_handle), ESP_LOGD(TAG, "failed to create ili9341");
TAG, return false;
"failed to create ili9341" }
);
ESP_RETURN_ON_ERROR(
esp_lcd_panel_reset(display->display_handle), if (esp_lcd_panel_reset(display->display_handle) != ESP_OK) {
TAG, ESP_LOGD(TAG, "failed to reset panel");
"failed to reset panel" return false;
); }
ESP_RETURN_ON_ERROR(
esp_lcd_panel_init(display->display_handle), if (esp_lcd_panel_init(display->display_handle) != ESP_OK) {
TAG, ESP_LOGD(TAG, "failed to init panel");
"failed to init panel" return false;
); }
ESP_RETURN_ON_ERROR(
esp_lcd_panel_mirror(display->display_handle, true, false), if (esp_lcd_panel_mirror(display->display_handle, true, false) != ESP_OK) {
TAG, ESP_LOGD(TAG, "failed to set panel to mirror");
"failed to set panel to mirror" return false;
); }
ESP_RETURN_ON_ERROR(
esp_lcd_panel_disp_on_off(display->display_handle, true), if (esp_lcd_panel_disp_on_off(display->display_handle, true) != ESP_OK) {
TAG, ESP_LOGD(TAG, "failed to turn display on");
"failed to turn display on" return false;
); }
ESP_RETURN_ON_ERROR(
gpio_set_level(PIN_BACKLIGHT, 1), if (gpio_set_level(PIN_BACKLIGHT, 1) != ESP_OK) {
TAG, ESP_LOGD(TAG, "failed to turn backlight on");
"failed to turn backlight on" return false;
); }
display->horizontal_resolution = HORIZONTAL_RESOLUTION; display->horizontal_resolution = HORIZONTAL_RESOLUTION;
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;
return ESP_OK; return true;
} }
nb_display_driver_t board_2432s024_create_display_driver() { nb_display_driver_t board_2432s024_create_display_driver() {

View File

@ -1,15 +1,17 @@
#include "board_2432s024_touch.h" #include "board_2432s024_touch.h"
#include <esp_lcd_touch_cst816s.h> #include <esp_lcd_touch_cst816s.h>
#include <esp_log.h>
#include <esp_err.h> #include <esp_err.h>
#include <driver/i2c.h> #include <driver/i2c.h>
#include <esp_check.h>
#define CST816_I2C_PORT (0) #define CST816_I2C_PORT (0)
const char* TAG = "cst816"; const char* TAG = "2432s024_cst816";
static bool prv_create_touch(esp_lcd_panel_io_handle_t* io_handle, esp_lcd_touch_handle_t* touch_handle) {
ESP_LOGI(TAG, "creating touch");
static esp_err_t prv_init_io(esp_lcd_panel_io_handle_t* io_handle) {
const i2c_config_t i2c_conf = { const i2c_config_t i2c_conf = {
.mode = I2C_MODE_MASTER, .mode = I2C_MODE_MASTER,
.sda_io_num = GPIO_NUM_33, .sda_io_num = GPIO_NUM_33,
@ -19,30 +21,24 @@ static esp_err_t prv_init_io(esp_lcd_panel_io_handle_t* io_handle) {
.master.clk_speed = 400000 .master.clk_speed = 400000
}; };
ESP_RETURN_ON_ERROR( if (i2c_param_config(CST816_I2C_PORT, &i2c_conf) != ESP_OK) {
i2c_param_config(CST816_I2C_PORT, &i2c_conf), ESP_LOGE(TAG, "i2c config failed");
TAG, return false;
"i2c config failed" }
);
ESP_RETURN_ON_ERROR( if (i2c_driver_install(CST816_I2C_PORT, i2c_conf.mode, 0, 0, 0) != ESP_OK) {
i2c_driver_install(CST816_I2C_PORT, i2c_conf.mode, 0, 0, 0), ESP_LOGE(TAG, "i2c driver install failed");
TAG, return false;
"i2c driver install failed" }
);
const esp_lcd_panel_io_i2c_config_t touch_io_config = ESP_LCD_TOUCH_IO_I2C_CST816S_CONFIG(); const esp_lcd_panel_io_i2c_config_t touch_io_config = ESP_LCD_TOUCH_IO_I2C_CST816S_CONFIG();
ESP_RETURN_ON_ERROR( if (esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)CST816_I2C_PORT, &touch_io_config, io_handle) != ESP_OK) {
esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)CST816_I2C_PORT, &touch_io_config, io_handle), ESP_LOGE(TAG, "esp_lcd_panel creation failed");
TAG, return false;
"esp_lcd_panel creation failed" }
);
return ESP_OK; ESP_LOGI(TAG, "create_touch");
}
static esp_err_t prv_create_touch(esp_lcd_panel_io_handle_t io_handle, esp_lcd_touch_handle_t* touch_handle) {
esp_lcd_touch_config_t config = { esp_lcd_touch_config_t config = {
.x_max = 240, .x_max = 240,
.y_max = 320, .y_max = 320,
@ -60,13 +56,17 @@ static esp_err_t prv_create_touch(esp_lcd_panel_io_handle_t io_handle, esp_lcd_t
.interrupt_callback = NULL, .interrupt_callback = NULL,
}; };
return esp_lcd_touch_new_i2c_cst816s(io_handle, &config, touch_handle); if (esp_lcd_touch_new_i2c_cst816s(*io_handle, &config, touch_handle) != ESP_OK) {
ESP_LOGE(TAG, "esp_lcd_touch_new_i2c_cst816s failed");
return false;
}
return true;
} }
nb_touch_driver_t board_2432s024_create_touch_driver() { nb_touch_driver_t board_2432s024_create_touch_driver() {
nb_touch_driver_t driver = { nb_touch_driver_t driver = {
.name = "cst816s_2432s024", .name = "cst816s_2432s024",
.init_io = prv_init_io,
.create_touch = &prv_create_touch .create_touch = &prv_create_touch
}; };
return driver; return driver;

View File

@ -21,13 +21,13 @@ enum nb_app_type {
USER USER
}; };
typedef struct nb_app_config nb_app_config_t; typedef struct nb_app nb_app_t;
typedef void (*nb_app_callback_on_create) (nb_platform_t* platform, lv_obj_t* lv_parent); typedef void (*nb_app_callback_on_create) (nb_platform_t* platform, lv_obj_t* lv_parent);
typedef void (*nb_app_callback_update) (nb_platform_t* platform, lv_obj_t* lv_parent); typedef void (*nb_app_callback_update) (nb_platform_t* platform, lv_obj_t* lv_parent);
typedef void (*nb_app_callback_on_destroy) (nb_platform_t* platform); typedef void (*nb_app_callback_on_destroy) (nb_platform_t* platform);
struct nb_app_config { struct nb_app {
char id[NB_APP_ID_LENGTH]; char id[NB_APP_ID_LENGTH];
char name[NB_APP_NAME_LENGTH]; char name[NB_APP_NAME_LENGTH];
nb_app_type_t type; nb_app_type_t type;
@ -38,12 +38,12 @@ struct nb_app_config {
uint32_t update_task_priority; uint32_t update_task_priority;
}; };
typedef struct nb_app nb_app_t; typedef struct nb_app_instance nb_app_instance_t;
struct nb_app { struct nb_app_instance {
nb_app_config_t config; nb_app_t config;
}; };
esp_err_t nb_app_config_validate(nb_app_config_t* _Nonnull app); esp_err_t nb_app_validate(nb_app_t* _Nonnull app);
#endif //NANOBAKE_NB_APP_H #endif //NANOBAKE_NB_APP_H

View File

@ -18,7 +18,7 @@ typedef struct nb_display_driver nb_display_driver_t;
struct nb_display_driver { struct nb_display_driver {
char name[32]; char name[32];
esp_err_t (*create_display)(nb_display_t* display); bool (*create_display)(nb_display_t* display);
}; };
/** /**

View File

@ -10,8 +10,6 @@
typedef nb_touch_driver_t (*create_touch_driver)(); typedef nb_touch_driver_t (*create_touch_driver)();
typedef nb_display_driver_t (*create_display_driver)(); typedef nb_display_driver_t (*create_display_driver)();
typedef nb_app_config_t (*create_app)();
typedef struct nb_platform_config nb_platform_config_t; typedef struct nb_platform_config nb_platform_config_t;
struct nb_platform_config { struct nb_platform_config {
// Required driver for display // Required driver for display
@ -19,7 +17,7 @@ struct nb_platform_config {
// Optional driver for touch input // Optional driver for touch input
create_touch_driver _Nullable touch_driver; create_touch_driver _Nullable touch_driver;
// List of user applications // List of user applications
create_app apps[]; nb_app_t* apps[];
}; };
typedef struct nb_lvgl nb_lvgl_t; typedef struct nb_lvgl nb_lvgl_t;

View File

@ -8,8 +8,7 @@ typedef struct nb_touch_driver nb_touch_driver_t;
struct nb_touch_driver { struct nb_touch_driver {
char name[32]; char name[32];
esp_err_t (*init_io)(esp_lcd_panel_io_handle_t* io_handle); bool (*create_touch)(esp_lcd_panel_io_handle_t* io_handle, esp_lcd_touch_handle_t* touch_handle);
esp_err_t (*create_touch)(esp_lcd_panel_io_handle_t io_handle, esp_lcd_touch_handle_t* touch_handle);
}; };
typedef struct nb_touch nb_touch_t; typedef struct nb_touch nb_touch_t;

View File

@ -23,14 +23,11 @@ static void prv_on_create(nb_platform_t _Nonnull* platform, lv_obj_t _Nonnull* l
lvgl_port_unlock(); lvgl_port_unlock();
} }
nb_app_config_t system_info_app_config() { nb_app_t system_info_app = {
nb_app_config_t config = { .id = "systeminfo",
.id = "systeminfo", .name = "System Info",
.name = "System Info", .type = SYSTEM,
.type = SYSTEM, .on_create = &prv_on_create,
.on_create = &prv_on_create, .on_update = NULL,
.on_update = NULL, .on_destroy = NULL
.on_destroy = NULL };
};
return config;
}

View File

@ -3,6 +3,6 @@
#include "nb_app.h" #include "nb_app.h"
nb_app_config_t system_info_app_config(); extern nb_app_t system_info_app;
#endif // NANOBAKE_SYSTEM_INFO_H #endif // NANOBAKE_SYSTEM_INFO_H

View File

@ -1,14 +1,15 @@
#include "nanobake.h" #include "nanobake.h"
#include "applications/main/system_info/system_info.h" #include "applications/main/system_info/system_info.h"
void nb_app_start(nb_platform_t _Nonnull* platform, nb_app_config_t _Nonnull* config) { void nb_app_start(nb_platform_t _Nonnull* platform, nb_app_t _Nonnull* config) {
lv_obj_t* scr = lv_scr_act(); lv_obj_t* scr = lv_scr_act();
ESP_ERROR_CHECK(nb_app_config_validate(config)); ESP_ERROR_CHECK(nb_app_validate(config));
config->on_create(platform, scr); config->on_create(platform, scr);
} }
extern void nanobake_run(nb_platform_config_t _Nonnull* config) { extern void nanobake_run(nb_platform_config_t _Nonnull* config) {
nb_platform_t _Nonnull* platform = nb_platform_create(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); nb_app_start(platform, config->apps[0]);
// nb_app_start(platform, &system_info_app);
} }

View File

@ -4,7 +4,7 @@
static const char* TAG = "nb_app"; static const char* TAG = "nb_app";
esp_err_t nb_app_config_validate(nb_app_config_t* _Nonnull app) { esp_err_t nb_app_validate(nb_app_t* _Nonnull app) {
ESP_RETURN_ON_FALSE( ESP_RETURN_ON_FALSE(
strlen(app->id) < NB_APP_ID_LENGTH, strlen(app->id) < NB_APP_ID_LENGTH,
ESP_FAIL, ESP_FAIL,

View File

@ -3,6 +3,6 @@
nb_display_t _Nonnull* nb_display_create(nb_display_driver_t _Nonnull* driver) { nb_display_t _Nonnull* nb_display_create(nb_display_driver_t _Nonnull* driver) {
nb_display_t _Nonnull* display = malloc(sizeof(nb_display_t)); nb_display_t _Nonnull* display = malloc(sizeof(nb_display_t));
NB_ASSERT(driver->create_display(display) == ESP_OK, "failed to create display"); NB_ASSERT(driver->create_display(display), "failed to create display");
return display; return display;
} }

View File

@ -75,7 +75,7 @@ nb_platform_t _Nonnull* nb_platform_create(nb_platform_config_t _Nonnull* config
platform->touch = NULL; platform->touch = NULL;
} }
ESP_ERROR_CHECK(prv_lvgl_init(platform)); NB_ASSERT(prv_lvgl_init(platform) == ESP_OK, "failed to init lvgl");
return platform; return platform;
} }

View File

@ -1,9 +1,13 @@
#include "nb_touch.h" #include "nb_touch.h"
#include "nb_assert.h"
#include <esp_check.h> #include <esp_check.h>
nb_touch_t _Nonnull* nb_touch_create(nb_touch_driver_t _Nonnull* driver) { nb_touch_t _Nonnull* nb_touch_create(nb_touch_driver_t _Nonnull* driver) {
nb_touch_t _Nonnull* touch = malloc(sizeof(nb_touch_t)); nb_touch_t _Nonnull* touch = malloc(sizeof(nb_touch_t));
assert(driver->init_io(&(touch->io_handle)) == ESP_OK); bool success = driver->create_touch(
driver->create_touch(touch->io_handle, &(touch->touch_handle)); &(touch->io_handle),
&(touch->touch_handle)
);
NB_ASSERT(success, "touch driver failed");
return touch; return touch;
} }

View File

@ -29,14 +29,11 @@ static void prv_on_create(nb_platform_t _Nonnull* platform, lv_obj_t _Nonnull* l
lvgl_port_unlock(); lvgl_port_unlock();
} }
nb_app_config_t hello_world_app_config() { nb_app_t hello_world_app = {
nb_app_config_t config = { .id = "helloworld",
.id = "helloworld", .name = "Hello World",
.name = "Hello World", .type = USER,
.type = USER, .on_create = &prv_on_create,
.on_create = &prv_on_create, .on_update = NULL,
.on_update = NULL, .on_destroy = NULL
.on_destroy = NULL };
};
return config;
}

View File

@ -3,6 +3,6 @@
#include "nb_app.h" #include "nb_app.h"
nb_app_config_t hello_world_app_config(); extern nb_app_t hello_world_app;
#endif //NANOBAKE_HELLO_WORLD_H #endif //NANOBAKE_HELLO_WORLD_H

View File

@ -12,7 +12,7 @@ void app_main(void) {
.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_config &hello_world_app
} }
}; };