成功移植lvgl

This commit is contained in:
2026-02-19 02:53:47 +08:00
parent ee80965d58
commit 452ec672b1
11 changed files with 1987 additions and 79 deletions
Submodule code/esp32c3_espidf/components/lvgl added at 73fbfceeed
+8
View File
@@ -0,0 +1,8 @@
dependencies:
idf:
source:
type: idf
version: 5.3.1
manifest_hash: a52a8cabe7f10f1636effa39e0be0f2d62531803ed5f518e7921b728e64c8752
target: esp32c3
version: 2.0.0
+7 -2
View File
@@ -2,5 +2,10 @@
file(GLOB_RECURSE SRC_LIST "*.c") file(GLOB_RECURSE SRC_LIST "*.c")
idf_component_register(SRCS ${SRC_LIST} idf_component_register(SRCS ${SRC_LIST}
PRIV_REQUIRES spi_flash esp_driver_spi esp_driver_gpio PRIV_REQUIRES spi_flash esp_driver_spi esp_driver_gpio esp_timer
INCLUDE_DIRS "") INCLUDE_DIRS "."
REQUIRES
lvgl
PRIV_REQUIRES
)
+83 -35
View File
@@ -4,6 +4,12 @@
* SPDX-License-Identifier: CC0-1.0 * SPDX-License-Identifier: CC0-1.0
*/ */
#define LV_CONF_INCLUDE_SIMPLE 1
#include "lv_conf.h"
/* 然后包含 lvgl.h */
#include "lvgl.h"
#include <stdio.h> #include <stdio.h>
#include <inttypes.h> #include <inttypes.h>
#include "sdkconfig.h" #include "sdkconfig.h"
@@ -20,27 +26,76 @@
#include "lcd.h" #include "lcd.h"
TimerHandle_t periodic_timer = NULL; #include "esp_timer.h"
#include "lv_port_disp.h"
#include "lv_apps/helloworld/lv_helloworld.h"
static const char *TAG = "SYS";
// TimerHandle_t periodic_timer = NULL;
// 定时器回调函数(在定时器服务任务中执行) // 定时器回调函数(在定时器服务任务中执行)
void periodic_timer_callback(TimerHandle_t xTimer) // void periodic_timer_callback(TimerHandle_t xTimer)
{ // {
//printf("每秒执行一次的函数\n"); // // printf("每秒执行一次的函数\n");
// 在这里执行你的周期性任务 // // 在这里执行你的周期性任务
// 注意:此回调函数应尽快返回,避免阻塞定时器服务任务 // // 注意:此回调函数应尽快返回,避免阻塞定时器服务任务
lcd_one_second_task(); // lcd_one_second_task();
// }
// 自定义 tick 获取函数
static uint32_t custom_tick_get(void)
{
// 返回从启动到现在的毫秒数
return (uint32_t)(esp_timer_get_time() / 1000);
} }
/**
* Basic example to create a "Hello world" label
*/
// static void btn_event_cb(lv_event_t * e)
// {
// lv_event_code_t code = lv_event_get_code(e);
// lv_obj_t * btn = lv_event_get_target(e);
// if(code == LV_EVENT_CLICKED) {
// static uint8_t cnt = 0;
// cnt++;
// /*Get the first child of the button which is the label and change its text*/
// lv_obj_t * label = lv_obj_get_child(btn, 0);
// lv_label_set_text_fmt(label, "Button: %d", cnt);
// }
// }
// /**
// * Create a button with a label and react on click event.
// */
// void lv_example_get_started_2(void)
// {
// lv_obj_t * btn = lv_button_create(lv_screen_active()); /*Add a button the current screen*/
// lv_obj_set_pos(btn, 10, 10); /*Set its position*/
// lv_obj_set_size(btn, 120, 50); /*Set its size*/
// lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_ALL, NULL); /*Assign a callback to the button*/
// lv_obj_t * label = lv_label_create(btn); /*Add a label to the button*/
// lv_label_set_text(label, "Button"); /*Set the labels text*/
// lv_obj_center(label);
// }
void app_main(void) void app_main(void)
{ {
printf("Hello world!\n"); ESP_LOGI(TAG,"Hello world!");
/* Print chip information */ /* Print chip information */
esp_chip_info_t chip_info; esp_chip_info_t chip_info;
uint32_t flash_size; uint32_t flash_size;
esp_chip_info(&chip_info); esp_chip_info(&chip_info);
printf("This is %s chip with %d CPU core(s), %s%s%s%s, ", ESP_LOGI(TAG,"This is %s chip with %d CPU core(s), %s%s%s%s, ",
CONFIG_IDF_TARGET, CONFIG_IDF_TARGET,
chip_info.cores, chip_info.cores,
(chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "", (chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "",
@@ -50,48 +105,41 @@ void app_main(void)
unsigned major_rev = chip_info.revision / 100; unsigned major_rev = chip_info.revision / 100;
unsigned minor_rev = chip_info.revision % 100; unsigned minor_rev = chip_info.revision % 100;
printf("silicon revision v%d.%d, ", major_rev, minor_rev); ESP_LOGI(TAG,"silicon revision v%d.%d, ", major_rev, minor_rev);
if (esp_flash_get_size(NULL, &flash_size) != ESP_OK) if (esp_flash_get_size(NULL, &flash_size) != ESP_OK)
{ {
printf("Get flash size failed"); ESP_LOGI(TAG,"Get flash size failed");
return; return;
} }
printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024), ESP_LOGI(TAG,"%" PRIu32 "MB %s flash", flash_size / (uint32_t)(1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external"); (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size()); ESP_LOGI(TAG,"Minimum free heap size: %" PRIu32 " bytes", esp_get_minimum_free_heap_size());
printf("sizeof(int) ==%d\n", sizeof(int)); ESP_LOGI(TAG,"sizeof(int) ==%d", sizeof(int));
ESP_LOGI(TAG,"LVGL version: %s", lv_version_info());
ESP_LOGI(TAG,"LVGL memory size: %u bytes", LV_MEM_SIZE);
ESP_LOGI(TAG,"LVGL color depth: %d bits", LV_COLOR_DEPTH);
spi_init(); spi_init();
lcd_init(); lcd_init();
// 配置一个每隔一秒运行一次的函数 用于计算fps等 lv_init();
// 创建周期性软件定时器 lv_tick_set_cb(custom_tick_get);
// 参数: 定时器名称, 周期(单位: Tick), 自动重载, 回调参数, 回调函数 lv_port_disp_init();
periodic_timer = xTimerCreate(
"PeriodicTimer", // 定时器名称
pdMS_TO_TICKS(1000), // 周期:1000毫秒 (转换为Tick)
pdTRUE, // 自动重载 (pdTRUE为周期性,pdFALSE为单次)
(void *)0, // 传递给回调函数的参数
periodic_timer_callback // 回调函数
);
if (periodic_timer != NULL) lv_example_get_started_1();
{
// 启动定时器 (0 ticks后启动)
xTimerStart(periodic_timer, 0);
}
else
{
printf("创建定时器失败!\n");
}
while (1) while (1)
{ {
lcd_clear_buf(rand());
lcd_send_full_buf(); // lcd_clear_buf(rand());
// lcd_send_full_buf();
// t = custom_tick_get();
// ESP_LOGI(TAG, "NOW is%lu", t);
lv_task_handler();
vTaskDelay(1); // 必须让出CPU vTaskDelay(1); // 必须让出CPU
} }
} }
+41 -31
View File
@@ -3,7 +3,7 @@
static const char *TAG = "LCD"; static const char *TAG = "LCD";
// uint16_t dis_buff[LCD_WW][LCD_HH]; // uint16_t dis_buff[LCD_WW][LCD_HH];
lcd_t lcd_main; // lcd_t lcd_main;
// SPI 写数据 // SPI 写数据
esp_err_t lcd_spi_send_data_8(uint8_t data) esp_err_t lcd_spi_send_data_8(uint8_t data)
@@ -33,7 +33,7 @@ esp_err_t lcd_spi_send_data_16(uint16_t data)
return err; return err;
} }
esp_err_t lcd_spi_send_data_any(uint8_t *data, uint16_t len) esp_err_t lcd_spi_send_data_any(uint8_t *data, uint32_t len)
{ {
spi_transaction_t trans = { spi_transaction_t trans = {
.length = len * 8, .length = len * 8,
@@ -102,40 +102,49 @@ void lcd_set_window(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
#endif #endif
} }
void lcd_one_second_task() // void lcd_one_second_task()
{ // {
lcd_main.fps=lcd_main.fps_count; // lcd_main.fps=lcd_main.fps_count;
lcd_main.fps_count=0; // lcd_main.fps_count=0;
ESP_LOGI(TAG, "FPS:%d",lcd_main.fps); // ESP_LOGI(TAG, "FPS:%d",lcd_main.fps);
} // }
void lcd_send_full_buf() // void lcd_send_full_buf()
// {
// gpio_set_level(LCD_CS, 0);
// lcd_set_window(0, 0, LCD_WW - 1, LCD_HH - 1);
// // 分包发送 单次9600 分16次发送
// spi_transaction_t trans = {
// .length = 9600 * 8, // 数据位数
// };
// uint8_t *d_buf = (uint8_t *)(lcd_main.buf);
// for (uint16_t i = 0; i < 16; i++)
// {
// // trans.length=9600*8;
// trans.tx_buffer = d_buf + (i * 9600);
// spi_device_transmit(lcd_spi, &trans);
// }
// gpio_set_level(LCD_CS, 1);
// //发送成功 帧计数+1
// lcd_main.fps_count+=1;
// }
// void lcd_clear_buf(uint16_t color)
// {
// for (uint32_t i = 0; i < (LCD_WW * LCD_HH); i++)
// {
// lcd_main.buf[i] = color;
// }
// }
void lcd_full_dis(uint16_t color)
{ {
gpio_set_level(LCD_CS, 0);
lcd_set_window(0, 0, LCD_WW - 1, LCD_HH - 1); lcd_set_window(0, 0, LCD_WW - 1, LCD_HH - 1);
// 分包发送 单次9600 分16次发送
spi_transaction_t trans = {
.length = 9600 * 8, // 数据位数
};
uint8_t *d_buf = (uint8_t *)(lcd_main.buf);
for (uint16_t i = 0; i < 16; i++)
{
// trans.length=9600*8;
trans.tx_buffer = d_buf + (i * 9600);
spi_device_transmit(lcd_spi, &trans);
}
gpio_set_level(LCD_CS, 1);
//发送成功 帧计数+1
lcd_main.fps_count+=1;
}
void lcd_clear_buf(uint16_t color)
{
for (uint32_t i = 0; i < (LCD_WW * LCD_HH); i++) for (uint32_t i = 0; i < (LCD_WW * LCD_HH); i++)
{ {
lcd_main.buf[i] = color; lcd_spi_send_data_16(color);
} }
} }
@@ -256,7 +265,8 @@ void lcd_init()
lcd_spi_send_cmd(0x29); lcd_spi_send_cmd(0x29);
lcd_clear_buf(0xffff);
//lcd_full_dis(0xffff);
gpio_set_level(LCD_CS, 1); gpio_set_level(LCD_CS, 1);
ESP_LOGI(TAG, "初始化LCD完成"); ESP_LOGI(TAG, "初始化LCD完成");
+11 -10
View File
@@ -13,21 +13,22 @@
#define LCD_WW 320 #define LCD_WW 320
#define LCD_HH 240 #define LCD_HH 240
typedef struct{ // typedef struct{
uint16_t buf[LCD_WW*LCD_HH]; // uint16_t buf[LCD_WW*LCD_HH];
uint16_t fps_count; // uint16_t fps_count;
uint16_t fps; // uint16_t fps;
}lcd_t; // }lcd_t;
extern lcd_t lcd_main; // extern lcd_t lcd_main;
void LCD_Address_Set(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2); void lcd_set_window(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2);
esp_err_t lcd_spi_send_data_any(uint8_t *data, uint32_t len);
void lcd_init(); void lcd_init();
void lcd_clear_buf(uint16_t Color); // void lcd_clear_buf(uint16_t Color);
void lcd_send_full_buf(); // void lcd_send_full_buf();
void lcd_one_second_task(); // void lcd_one_second_task();
#endif #endif
@@ -0,0 +1,81 @@
#include "lv_helloworld.h"
static lv_style_t style_btn;
static lv_style_t style_button_pressed;
static lv_style_t style_button_red;
static lv_color_t darken(const lv_color_filter_dsc_t * dsc, lv_color_t color, lv_opa_t opa)
{
LV_UNUSED(dsc);
return lv_color_darken(color, opa);
}
static void style_init(void)
{
/*Create a simple button style*/
lv_style_init(&style_btn);
lv_style_set_radius(&style_btn, 10);
lv_style_set_bg_opa(&style_btn, LV_OPA_COVER);
lv_style_set_bg_color(&style_btn, lv_palette_lighten(LV_PALETTE_GREY, 3));
lv_style_set_bg_grad_color(&style_btn, lv_palette_main(LV_PALETTE_GREY));
lv_style_set_bg_grad_dir(&style_btn, LV_GRAD_DIR_VER);
lv_style_set_border_color(&style_btn, lv_color_black());
lv_style_set_border_opa(&style_btn, LV_OPA_20);
lv_style_set_border_width(&style_btn, 2);
lv_style_set_text_color(&style_btn, lv_color_black());
/*Create a style for the pressed state.
*Use a color filter to simply modify all colors in this state*/
static lv_color_filter_dsc_t color_filter;
lv_color_filter_dsc_init(&color_filter, darken);
lv_style_init(&style_button_pressed);
lv_style_set_color_filter_dsc(&style_button_pressed, &color_filter);
lv_style_set_color_filter_opa(&style_button_pressed, LV_OPA_20);
/*Create a red style. Change only some colors.*/
lv_style_init(&style_button_red);
lv_style_set_bg_color(&style_button_red, lv_palette_main(LV_PALETTE_RED));
lv_style_set_bg_grad_color(&style_button_red, lv_palette_lighten(LV_PALETTE_RED, 3));
}
/**
* Create styles from scratch for buttons.
*/
void lv_example_get_started_1(void)
{
/*Initialize the style*/
style_init();
/*Create a button and use the new styles*/
lv_obj_t * btn = lv_button_create(lv_screen_active());
/* Remove the styles coming from the theme
* Note that size and position are also stored as style properties
* so lv_obj_remove_style_all will remove the set size and position too */
lv_obj_remove_style_all(btn);
lv_obj_set_pos(btn, 10, 10);
lv_obj_set_size(btn, 120, 50);
lv_obj_add_style(btn, &style_btn, 0);
lv_obj_add_style(btn, &style_button_pressed, LV_STATE_PRESSED);
/*Add a label to the button*/
lv_obj_t * label = lv_label_create(btn);
lv_label_set_text(label, "Button");
lv_obj_center(label);
/*Create another button and use the red style too*/
lv_obj_t * btn2 = lv_button_create(lv_screen_active());
lv_obj_remove_style_all(btn2); /*Remove the styles coming from the theme*/
lv_obj_set_pos(btn2, 10, 80);
lv_obj_set_size(btn2, 120, 50);
lv_obj_add_style(btn2, &style_btn, 0);
lv_obj_add_style(btn2, &style_button_red, 0);
lv_obj_add_style(btn2, &style_button_pressed, LV_STATE_PRESSED);
lv_obj_set_style_radius(btn2, LV_RADIUS_CIRCLE, 0); /*Add a local style too*/
label = lv_label_create(btn2);
lv_label_set_text(label, "Button 2");
lv_obj_center(label);
}
@@ -0,0 +1,8 @@
#ifndef LV_HELLOWORLD_H
#define LV_HELLOWORLD_H
#include "lvgl.h"
void lv_example_get_started_1(void);
#endif
File diff suppressed because it is too large Load Diff
+163
View File
@@ -0,0 +1,163 @@
/**
* @file lv_port_disp_template.c
*
*/
/*Copy this file as "lv_port_disp.c" and set this value to "1" to enable content*/
#if 1
/*********************
* INCLUDES
*********************/
#include "lv_port_disp.h"
#include <stdbool.h>
#include "esp_log.h"
#include "lcd.h"
#define MY_DISP_HOR_RES 320
#define MY_DISP_VER_RES 240
static const char *TAG = "LVGL_disp";
/*********************
* DEFINES
*********************/
#ifndef MY_DISP_HOR_RES
#warning Please define or replace the macro MY_DISP_HOR_RES with the actual screen width, default value 320 is used for now.
#define MY_DISP_HOR_RES 320
#endif
#ifndef MY_DISP_VER_RES
#warning Please define or replace the macro MY_DISP_VER_RES with the actual screen height, default value 240 is used for now.
#define MY_DISP_VER_RES 240
#endif
#define BYTE_PER_PIXEL (LV_COLOR_FORMAT_GET_SIZE(LV_COLOR_FORMAT_RGB565_SWAPPED)) /*will be 2 for RGB565 */
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static void disp_init(void);
static void disp_flush(lv_display_t *disp, const lv_area_t *area, uint8_t *px_map);
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
void lv_port_disp_init(void)
{
/*-------------------------
* Initialize your display
* -----------------------*/
disp_init();
/*------------------------------------
* Create a display and set a flush_cb
* -----------------------------------*/
lv_display_t *disp = lv_display_create(MY_DISP_HOR_RES, MY_DISP_VER_RES);
lv_display_set_color_format(disp, LV_COLOR_FORMAT_RGB565_SWAPPED);
lv_display_set_flush_cb(disp, disp_flush);
/* Example 1
* One buffer for partial rendering*/
// LV_ATTRIBUTE_MEM_ALIGN
// static uint8_t buf_1_1[MY_DISP_HOR_RES * 10 * BYTE_PER_PIXEL]; /*A buffer for 10 rows*/
// lv_display_set_buffers(disp, buf_1_1, NULL, sizeof(buf_1_1), LV_DISPLAY_RENDER_MODE_PARTIAL);
/* Example 2
* Two buffers for partial rendering
* In flush_cb DMA or similar hardware should be used to update the display in the background.*/
LV_ATTRIBUTE_MEM_ALIGN
static uint8_t buf_2_1[MY_DISP_HOR_RES * 10 * BYTE_PER_PIXEL];
LV_ATTRIBUTE_MEM_ALIGN
static uint8_t buf_2_2[MY_DISP_HOR_RES * 10 * BYTE_PER_PIXEL];
lv_display_set_buffers(disp, buf_2_1, buf_2_2, sizeof(buf_2_1), LV_DISPLAY_RENDER_MODE_PARTIAL);
/* Example 3
* Two buffers screen sized buffer for double buffering.
* Both LV_DISPLAY_RENDER_MODE_DIRECT and LV_DISPLAY_RENDER_MODE_FULL works, see their comments*/
// LV_ATTRIBUTE_MEM_ALIGN
// static uint8_t buf_3_1[MY_DISP_HOR_RES * MY_DISP_VER_RES * BYTE_PER_PIXEL];
// LV_ATTRIBUTE_MEM_ALIGN
// static uint8_t buf_3_2[MY_DISP_HOR_RES * MY_DISP_VER_RES * BYTE_PER_PIXEL];
// lv_display_set_buffers(disp, buf_3_1, buf_3_2, sizeof(buf_3_1), LV_DISPLAY_RENDER_MODE_DIRECT);
}
/**********************
* STATIC FUNCTIONS
**********************/
/*Initialize your display and the required peripherals.*/
static void disp_init(void)
{
/*You code here*/
}
volatile bool disp_flush_enabled = true;
/* Enable updating the screen (the flushing process) when disp_flush() is called by LVGL
*/
void disp_enable_update(void)
{
disp_flush_enabled = true;
}
/* Disable updating the screen (the flushing process) when disp_flush() is called by LVGL
*/
void disp_disable_update(void)
{
disp_flush_enabled = false;
}
/*Flush the content of the internal buffer the specific area on the display.
*`px_map` contains the rendered image as raw pixel map and it should be copied to `area` on the display.
*You can use DMA or any hardware acceleration to do this operation in the background but
*'lv_display_flush_ready()' has to be called when it's finished.*/
static void disp_flush(lv_display_t *disp_drv, const lv_area_t *area, uint8_t *px_map)
{
if (disp_flush_enabled)
{
/*The most simple case (but also the slowest) to put all pixels to the screen one-by-one*/
// 获取显示驱动的颜色格式
lv_color_format_t color_format = lv_display_get_color_format(disp_drv);
// 获取像素字节数
uint8_t px_size = lv_color_format_get_size(color_format);
gpio_set_level(LCD_CS, 0);
lcd_set_window(area->x1, area->y1, area->x2+1, area->y2+1);
esp_err_t err = lcd_spi_send_data_any(px_map, (area->x2+1 - area->x1) * (area->y2+1 - area->y1) * px_size);
gpio_set_level(LCD_CS, 1);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "失败:%x", err);
}
//ESP_LOGI(TAG, "flush: x1:%lu y1:%lu x2:%lu y2:%lu len:%d", area->x1, area->y1, area->x2, area->y2, px_size);
}
/*IMPORTANT!!!
*Inform the graphics library that you are ready with the flushing*/
lv_display_flush_ready(disp_drv);
}
#else /*Enable this file at the top*/
/*This dummy typedef exists purely to silence -Wpedantic.*/
typedef int keep_pedantic_happy;
#endif
+57
View File
@@ -0,0 +1,57 @@
/**
* @file lv_port_disp_templ.h
*
*/
/*Copy this file as "lv_port_disp.h" and set this value to "1" to enable content*/
#if 1
#ifndef LV_PORT_DISP_TEMPL_H
#define LV_PORT_DISP_TEMPL_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************
* INCLUDES
*********************/
#if defined(LV_LVGL_H_INCLUDE_SIMPLE)
#include "lvgl.h"
#else
#include "lvgl.h"
#endif
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* GLOBAL PROTOTYPES
**********************/
/* Initialize low level display driver */
void lv_port_disp_init(void);
/* Enable updating the screen (the flushing process) when disp_flush() is called by LVGL
*/
void disp_enable_update(void);
/* Disable updating the screen (the flushing process) when disp_flush() is called by LVGL
*/
void disp_disable_update(void);
/**********************
* MACROS
**********************/
#ifdef __cplusplus
} /*extern "C"*/
#endif
#endif /*LV_PORT_DISP_TEMPL_H*/
#endif /*Disable/Enable content*/