firmware: Initial working touch control

This commit is contained in:
Jack O'Sullivan 2024-12-08 14:53:51 +00:00
parent 1d5853e91c
commit db683dbc71
6 changed files with 133 additions and 162 deletions

View File

@ -1,4 +1,29 @@
dependencies:
espressif/esp_lcd_touch:
component_hash: 779b4ba2464a3ae85681e4b860caa5fdc35801458c23f3039ee761bae7f442a4
dependencies:
- name: idf
registry_url: https://components.espressif.com
require: private
version: '>=4.4.2'
source:
registry_url: https://components.espressif.com
type: service
version: 1.1.2
espressif/esp_lcd_touch_gt911:
component_hash: 145801c24c7ecfa2c43eea0dd5e87fd5bde5653eb9a48d619c7bad60b20a1acf
dependencies:
- name: espressif/esp_lcd_touch
registry_url: https://components.espressif.com
require: public
version: ^1.1.0
- name: idf
require: private
version: '>=4.4.2'
source:
registry_url: https://components.espressif.com/
type: service
version: 1.1.1~2
idf:
source:
type: idf
@ -11,8 +36,9 @@ dependencies:
type: service
version: 9.2.2
direct_dependencies:
- espressif/esp_lcd_touch_gt911
- idf
- lvgl/lvgl
manifest_hash: 5572feed6f42d829a5cb8a4d05d14bdccf26a3eea1ab283866a22e031aea011b
manifest_hash: fdff5bb68ec2efda1d9ba20223b9e2213f1e52139964d15c1177212d5b447ce9
target: esp32s3
version: 2.0.0

View File

@ -1,3 +1,3 @@
idf_component_register(
SRCS "valconomy.c" "lvgl_demo_ui.c"
SRCS "valconomy.c" "ui.c"
INCLUDE_DIRS ".")

View File

@ -1,6 +1,7 @@
## IDF Component Manager Manifest File
dependencies:
lvgl/lvgl: "^9.2.2"
esp_lcd_touch_gt911: "^1.1.1"
## Required IDF version
idf:
version: ">=4.1.0"

View File

@ -1,152 +0,0 @@
#include "lvgl.h"
static lv_style_t style_bullet;
static lv_obj_t *scale1;
static const lv_font_t *font_normal = &lv_font_montserrat_14;
static lv_obj_t *create_scale_box(lv_obj_t *parent, const char *text1, const char *text2, const char *text3)
{
lv_obj_t *scale = lv_scale_create(parent);
lv_obj_center(scale);
lv_obj_set_size(scale, 300, 300);
lv_scale_set_mode(scale, LV_SCALE_MODE_ROUND_OUTER);
lv_scale_set_label_show(scale, false);
lv_scale_set_post_draw(scale, true);
lv_obj_set_width(scale, LV_PCT(100));
lv_obj_set_style_pad_all(scale, 30, 0);
lv_obj_t *bullet1 = lv_obj_create(parent);
lv_obj_set_size(bullet1, 13, 13);
lv_obj_remove_style(bullet1, NULL, LV_PART_SCROLLBAR);
lv_obj_add_style(bullet1, &style_bullet, 0);
lv_obj_set_style_bg_color(bullet1, lv_palette_main(LV_PALETTE_RED), 0);
lv_obj_t *label1 = lv_label_create(parent);
lv_label_set_text(label1, text1);
lv_obj_t *bullet2 = lv_obj_create(parent);
lv_obj_set_size(bullet2, 13, 13);
lv_obj_remove_style(bullet2, NULL, LV_PART_SCROLLBAR);
lv_obj_add_style(bullet2, &style_bullet, 0);
lv_obj_set_style_bg_color(bullet2, lv_palette_main(LV_PALETTE_BLUE), 0);
lv_obj_t *label2 = lv_label_create(parent);
lv_label_set_text(label2, text2);
lv_obj_t *bullet3 = lv_obj_create(parent);
lv_obj_set_size(bullet3, 13, 13);
lv_obj_remove_style(bullet3, NULL, LV_PART_SCROLLBAR);
lv_obj_add_style(bullet3, &style_bullet, 0);
lv_obj_set_style_bg_color(bullet3, lv_palette_main(LV_PALETTE_GREEN), 0);
lv_obj_t *label3 = lv_label_create(parent);
lv_label_set_text(label3, text3);
static int32_t grid_col_dsc[] = {LV_GRID_CONTENT, LV_GRID_FR(1), LV_GRID_TEMPLATE_LAST};
static int32_t grid_row_dsc[] = {LV_GRID_CONTENT, LV_GRID_CONTENT, LV_GRID_CONTENT, LV_GRID_CONTENT, LV_GRID_CONTENT, LV_GRID_TEMPLATE_LAST};
lv_obj_set_grid_dsc_array(parent, grid_col_dsc, grid_row_dsc);
lv_obj_set_grid_cell(scale, LV_GRID_ALIGN_START, 0, 2, LV_GRID_ALIGN_START, 1, 1);
lv_obj_set_grid_cell(bullet1, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_START, 2, 1);
lv_obj_set_grid_cell(bullet2, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_START, 3, 1);
lv_obj_set_grid_cell(bullet3, LV_GRID_ALIGN_START, 0, 1, LV_GRID_ALIGN_START, 4, 1);
lv_obj_set_grid_cell(label1, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_START, 2, 1);
lv_obj_set_grid_cell(label2, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_START, 3, 1);
lv_obj_set_grid_cell(label3, LV_GRID_ALIGN_STRETCH, 1, 1, LV_GRID_ALIGN_START, 4, 1);
return scale;
}
static void scale1_indic1_anim_cb(void *var, int32_t v)
{
lv_arc_set_value(var, v);
lv_obj_t *card = lv_obj_get_parent(scale1);
lv_obj_t *label = lv_obj_get_child(card, -5);
lv_label_set_text_fmt(label, "Revenue: %"LV_PRId32" %%", v);
}
static void scale1_indic2_anim_cb(void *var, int32_t v)
{
lv_arc_set_value(var, v);
lv_obj_t *card = lv_obj_get_parent(scale1);
lv_obj_t *label = lv_obj_get_child(card, -3);
lv_label_set_text_fmt(label, "Sales: %"LV_PRId32" %%", v);
}
static void scale1_indic3_anim_cb(void *var, int32_t v)
{
lv_arc_set_value(var, v);
lv_obj_t *card = lv_obj_get_parent(scale1);
lv_obj_t *label = lv_obj_get_child(card, -1);
lv_label_set_text_fmt(label, "Costs: %"LV_PRId32" %%", v);
}
void example_lvgl_demo_ui(lv_display_t *disp)
{
// init default theme
lv_theme_default_init(disp, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED), LV_THEME_DEFAULT_DARK,
font_normal);
// lv_sysmon_hide_performance(disp);
// bullet style
lv_style_init(&style_bullet);
lv_style_set_border_width(&style_bullet, 0);
lv_style_set_radius(&style_bullet, LV_RADIUS_CIRCLE);
lv_obj_t *parent = lv_display_get_screen_active(disp);
// create scale widget
scale1 = create_scale_box(parent, "Revenue", "Sales", "Costs");
// create arc indicators
lv_obj_t *arc;
arc = lv_arc_create(scale1);
lv_obj_remove_style(arc, NULL, LV_PART_KNOB);
lv_obj_remove_style(arc, NULL, LV_PART_MAIN);
lv_obj_set_size(arc, lv_pct(100), lv_pct(100));
lv_obj_set_style_arc_opa(arc, 0, 0);
lv_obj_set_style_arc_width(arc, 15, LV_PART_INDICATOR);
lv_obj_set_style_arc_color(arc, lv_palette_main(LV_PALETTE_BLUE), LV_PART_INDICATOR);
lv_obj_remove_flag(arc, LV_OBJ_FLAG_CLICKABLE);
// animation
lv_anim_t a;
lv_anim_init(&a);
lv_anim_set_values(&a, 20, 100);
lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
lv_anim_set_exec_cb(&a, scale1_indic1_anim_cb);
lv_anim_set_var(&a, arc);
lv_anim_set_duration(&a, 4100);
lv_anim_set_playback_duration(&a, 2700);
lv_anim_start(&a);
arc = lv_arc_create(scale1);
lv_obj_remove_style(arc, NULL, LV_PART_KNOB);
lv_obj_set_size(arc, lv_pct(100), lv_pct(100));
lv_obj_set_style_margin_all(arc, 20, 0);
lv_obj_set_style_arc_opa(arc, 0, 0);
lv_obj_set_style_arc_width(arc, 15, LV_PART_INDICATOR);
lv_obj_set_style_arc_color(arc, lv_palette_main(LV_PALETTE_RED), LV_PART_INDICATOR);
lv_obj_remove_flag(arc, LV_OBJ_FLAG_CLICKABLE);
lv_obj_center(arc);
lv_anim_set_exec_cb(&a, scale1_indic2_anim_cb);
lv_anim_set_var(&a, arc);
lv_anim_set_duration(&a, 2600);
lv_anim_set_playback_duration(&a, 3200);
lv_anim_start(&a);
arc = lv_arc_create(scale1);
lv_obj_remove_style(arc, NULL, LV_PART_KNOB);
lv_obj_set_size(arc, lv_pct(100), lv_pct(100));
lv_obj_set_style_margin_all(arc, 40, 0);
lv_obj_set_style_arc_opa(arc, 0, 0);
lv_obj_set_style_arc_width(arc, 15, LV_PART_INDICATOR);
lv_obj_set_style_arc_color(arc, lv_palette_main(LV_PALETTE_GREEN), LV_PART_INDICATOR);
lv_obj_remove_flag(arc, LV_OBJ_FLAG_CLICKABLE);
lv_obj_center(arc);
lv_anim_set_exec_cb(&a, scale1_indic3_anim_cb);
lv_anim_set_var(&a, arc);
lv_anim_set_duration(&a, 2800);
lv_anim_set_playback_duration(&a, 1800);
lv_anim_start(&a);
}

31
firmware/main/ui.c Normal file
View File

@ -0,0 +1,31 @@
#include "lvgl.h"
static const lv_font_t *font_normal = &lv_font_montserrat_14;
static void b_cfg_cb(lv_event_t *e) {
lv_obj_t *box = lv_msgbox_create(NULL);
lv_msgbox_add_title(box, "Hello");
lv_msgbox_add_text(box, "test message");
lv_msgbox_add_close_button(box);
}
static void ui_home() {
lv_obj_t *b_cfg = lv_button_create(lv_screen_active());
lv_obj_align(b_cfg, LV_ALIGN_BOTTOM_RIGHT, -10, -10);
lv_obj_add_event_cb(b_cfg, b_cfg_cb, LV_EVENT_CLICKED, NULL);
lv_obj_t *l_cfg = lv_label_create(b_cfg);
lv_label_set_text(l_cfg, LV_SYMBOL_SETTINGS);
lv_obj_center(l_cfg);
}
void val_lvgl_ui(lv_display_t *disp) {
// init default theme
lv_theme_default_init(
disp, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED),
true, // dark theme
font_normal);
// lv_sysmon_hide_performance(disp);
ui_home();
}

View File

@ -8,12 +8,16 @@
#include "esp_err.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "driver/i2c_master.h"
#include "esp_lcd_panel_ops.h"
#include "esp_lcd_panel_rgb.h"
#include "driver/i2c_master.h"
#include "esp_lcd_touch_gt911.h"
#include "lvgl.h"
#define SCL_SPEED 100000
#define LCD_HRES 800
#define LCD_VRES 480
#define LCD_PIXEL_SIZE 2
@ -33,7 +37,7 @@ static i2c_master_dev_handle_t exio_cfg_handle, exio_o_handle;
// LVGL library is not thread-safe, we will call LVGL APIs from different tasks, so use a mutex to protect it
static SemaphoreHandle_t lvgl_mtx = NULL;
extern void example_lvgl_demo_ui(lv_display_t *disp);
extern void val_lvgl_ui(lv_display_t *disp);
bool val_lvgl_lock(int timeout_ms) {
// Convert timeout in milliseconds to FreeRTOS ticks
@ -82,6 +86,27 @@ static void val_lvgl_port_task(void *arg) {
}
}
static void val_lvgl_touch_read(lv_indev_t *indev, lv_indev_data_t *data) {
uint16_t touchpad_x[1] = {0};
uint16_t touchpad_y[1] = {0};
uint8_t touchpad_cnt = 0;
// Read touch controller data
esp_lcd_touch_handle_t touch_panel = lv_indev_get_user_data(indev);
esp_lcd_touch_read_data(touch_panel);
// Get coordinates
bool touchpad_pressed = esp_lcd_touch_get_coordinates(touch_panel, touchpad_x, touchpad_y, NULL, &touchpad_cnt, 1);
if (touchpad_pressed && touchpad_cnt > 0) {
data->point.x = touchpad_x[0];
data->point.y = touchpad_y[0];
data->state = LV_INDEV_STATE_PR;
} else {
data->state = LV_INDEV_STATE_REL;
}
}
static void val_i2c_master_init(void) {
ESP_LOGI(TAG, "Init I2C");
i2c_master_bus_config_t i2c_mst_config = {
@ -98,7 +123,7 @@ static void val_i2c_master_init(void) {
i2c_device_config_t exio_dev_cfg = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = 0x24, // CH422G config register
.scl_speed_hz = 100000,
.scl_speed_hz = SCL_SPEED,
};
ESP_ERROR_CHECK(i2c_master_bus_add_device(i2c_bus_handle, &exio_dev_cfg, &exio_cfg_handle));
@ -106,6 +131,39 @@ static void val_i2c_master_init(void) {
ESP_ERROR_CHECK(i2c_master_bus_add_device(i2c_bus_handle, &exio_dev_cfg, &exio_o_handle));
}
static esp_lcd_touch_handle_t val_setup_touch(void) {
ESP_LOGI(TAG, "Install touch panel driver");
esp_lcd_panel_io_i2c_config_t io_config = ESP_LCD_TOUCH_IO_I2C_GT911_CONFIG();
io_config.scl_speed_hz = SCL_SPEED;
esp_lcd_panel_io_handle_t io_handle = NULL;
ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c(i2c_bus_handle, &io_config, &io_handle));
esp_lcd_touch_io_gt911_config_t gt911_config = {
.dev_addr = io_config.dev_addr,
};
esp_lcd_touch_config_t tp_cfg = {
.x_max = LCD_HRES,
.y_max = LCD_VRES,
.rst_gpio_num = -1,
.int_gpio_num = 4,
.levels = {
.reset = 0,
.interrupt = 0,
},
.flags = {
.swap_xy = 0,
.mirror_x = 0,
.mirror_y = 0,
},
.driver_data = &gt911_config,
};
esp_lcd_touch_handle_t tp;
ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_gt911(io_handle, &tp_cfg, &tp));
return tp;
}
static esp_lcd_panel_handle_t val_setup_lcd(void) {
uint8_t i2c_b;
i2c_b = 0x01; // Output enable
@ -114,7 +172,7 @@ static esp_lcd_panel_handle_t val_setup_lcd(void) {
i2c_b = 0x00; // Reset everything
ESP_ERROR_CHECK(i2c_master_transmit(exio_o_handle, &i2c_b, 1, -1));
vTaskDelay(pdMS_TO_TICKS(10));
i2c_b = 0x08; // Pull LCD out of reset
i2c_b = 0x0c; // Pull LCD + touch out of reset
ESP_ERROR_CHECK(i2c_master_transmit(exio_o_handle, &i2c_b, 1, -1));
vTaskDelay(pdMS_TO_TICKS(100));
@ -172,13 +230,13 @@ static esp_lcd_panel_handle_t val_setup_lcd(void) {
ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
ESP_LOGI(TAG, "Turn on LCD backlight");
i2c_b = 0x0e; // LCD out of reset, backlight on
i2c_b = 0x0e; // LCD + touch out of reset, backlight on
ESP_ERROR_CHECK(i2c_master_transmit(exio_o_handle, &i2c_b, 1, -1));
return panel_handle;
}
void val_setup_lvgl(esp_lcd_panel_handle_t lcd_panel) {
void val_setup_lvgl(esp_lcd_panel_handle_t lcd_panel, esp_lcd_touch_handle_t touch_panel) {
ESP_LOGI(TAG, "Initialize LVGL library");
lv_init();
// create a lvgl display
@ -205,6 +263,11 @@ void val_setup_lvgl(esp_lcd_panel_handle_t lcd_panel) {
};
ESP_ERROR_CHECK(esp_lcd_rgb_panel_register_event_callbacks(lcd_panel, &cbs, display));
lv_indev_t *indev = lv_indev_create();
lv_indev_set_user_data(indev, touch_panel);
lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
lv_indev_set_read_cb(indev, val_lvgl_touch_read);
ESP_LOGI(TAG, "Install LVGL tick timer");
// Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
const esp_timer_create_args_t lvgl_tick_timer_args = {
@ -223,7 +286,7 @@ void val_setup_lvgl(esp_lcd_panel_handle_t lcd_panel) {
ESP_LOGI(TAG, "Display LVGL UI");
// Lock the mutex due to the LVGL APIs are not thread-safe
assert(val_lvgl_lock(-1));
example_lvgl_demo_ui(display);
val_lvgl_ui(display);
val_lvgl_unlock();
}
@ -232,6 +295,8 @@ void app_main(void) {
esp_lcd_panel_handle_t lcd_panel = val_setup_lcd();
assert(lcd_panel);
esp_lcd_touch_handle_t touch_panel = val_setup_touch();
assert(touch_panel);
val_setup_lvgl(lcd_panel);
val_setup_lvgl(lcd_panel, touch_panel);
}