Refactor app.c and board.c for improved display handling and memory management

- Updated sgl_print_hello function to clear the screen and render "Hello World" with improved task handling.
- Modified business_task to repeatedly display "Hello World" every second.
- Enhanced lcd_rtc_test to create and manage labels more efficiently, ensuring proper screen updates.
- Introduced dynamic text handling in sgl_label to manage memory for label text, allowing for automatic cleanup on destruction.
- Added cmd_mem_info function to display SGL memory usage via shell command.
This commit is contained in:
许晟昊 2026-03-23 18:01:14 +08:00
parent 9f04900092
commit ff554a472c
6 changed files with 6931 additions and 3239 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@ -5,39 +5,52 @@
#include "../system/sgl/sgl.h"
static TaskHandle_t business_task_handle = NULL;
extern Shell shell; /* defined in shell_port.c */
void sgl_print_hello(void)
static void sgl_print_hello(void)
{
/* 设置页面背景为黑色,使白色文字可见 */
sgl_page_set_color(sgl_screen_act(), sgl_rgb(0x00, 0x00, 0x00));
sgl_obj_delete(NULL);
sgl_task_handle(); /* 刷新:屏幕清空 */
/* 全屏 label居中显示 Hello World */
sgl_page_set_color(sgl_screen_act(), sgl_rgb(0x00, 0x00, 0x00));
sgl_obj_t *label = sgl_label_create(NULL);
sgl_obj_set_pos(label, 0, 0);
sgl_obj_set_size(label, 240, 240);
sgl_label_set_text(label, "Hello World");
sgl_label_set_font(label, &song23);
sgl_label_set_text_color(label, sgl_rgb(0xFF, 0xFF, 0xFF));
sgl_label_set_text_align(label, SGL_ALIGN_CENTER);
sgl_label_set_text(label, "Hello World");
sgl_task_handle(); /* 渲染Hello World 出现在屏幕 */
}
void business_task(void *pvParameters)
{
/* 启动时显示 Hello World 2 秒 */
sgl_print_hello();
sgl_task_handle();
vTaskDelay(2000);
for (;;)
{
rtc_update();
sgl_print_hello();
vTaskDelay(1000); /* 显示 1 秒 */
lcd_rtc_test();
sgl_task_handle();
led_toggle(LED_USER);
vTaskDelay(1000);
}
}
static void cmd_mem_info(void)
{
sgl_mm_monitor_t m = sgl_mm_get_monitor();
shellPrint(&shell, "SGL mem: total=%u used=%u free=%u rate=%u.%02u%%\r\n",
m.total_size, m.used_size, m.free_size,
(m.used_rate >> 8) & 0xFF,
m.used_rate & 0xFF);
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC),
meminfo, cmd_mem_info, "show sgl memory usage");
void app_init(void)
{
userShellInit(); // 初始化shell

View File

@ -149,42 +149,42 @@ BOOL rtc_init(void)
void lcd_rtc_test(void)
{
static sgl_obj_t *label_time = NULL;
static char buf[32];
char tmp[32];
sgl_obj_delete(NULL);
sgl_task_handle(); /* 刷新:屏幕清空 */
if (label_time == NULL)
{
sgl_obj_delete(NULL);
sgl_page_set_color(sgl_screen_act(), sgl_rgb(0x00, 0x00, 0x00));
sgl_page_set_color(sgl_screen_act(), sgl_rgb(0x00, 0x00, 0x00));
sgl_obj_t *lbl = sgl_label_create(NULL);
sgl_obj_set_pos(lbl, 0, 70);
sgl_obj_set_size(lbl, 240, 28);
sgl_label_set_text(lbl, "STM32 RTC Test");
sgl_label_set_font(lbl, &song23);
sgl_label_set_text_color(lbl, sgl_rgb(0x00, 0xAA, 0xFF));
sgl_label_set_text_align(lbl, SGL_ALIGN_CENTER);
sgl_obj_t *lbl = sgl_label_create(NULL);
sgl_obj_set_pos(lbl, 0, 70);
sgl_obj_set_size(lbl, 240, 28);
sgl_label_set_text(lbl, "STM32 RTC Test");
sgl_label_set_font(lbl, &song23);
sgl_label_set_text_color(lbl, sgl_rgb(0x00, 0xAA, 0xFF));
sgl_label_set_text_align(lbl, SGL_ALIGN_CENTER);
lbl = sgl_label_create(NULL);
sgl_obj_set_pos(lbl, 0, 100);
sgl_obj_set_size(lbl, 240, 28);
sgl_label_set_text(lbl, "Resolution: 240x240");
sgl_label_set_font(lbl, &song23);
sgl_label_set_text_color(lbl, sgl_rgb(0xFF, 0xFF, 0x00));
sgl_label_set_text_align(lbl, SGL_ALIGN_CENTER);
lbl = sgl_label_create(NULL);
sgl_obj_set_pos(lbl, 0, 100);
sgl_obj_set_size(lbl, 240, 28);
sgl_label_set_text(lbl, "Resolution: 240x240");
sgl_label_set_font(lbl, &song23);
sgl_label_set_text_color(lbl, sgl_rgb(0xFF, 0xFF, 0x00));
sgl_label_set_text_align(lbl, SGL_ALIGN_CENTER);
label_time = sgl_label_create(NULL);
sgl_obj_set_pos(label_time, 0, 130);
sgl_obj_set_size(label_time, 240, 28);
sgl_label_set_font(label_time, &song23);
sgl_label_set_text_color(label_time, sgl_rgb(0xFF, 0x40, 0x40));
sgl_label_set_text_align(label_time, SGL_ALIGN_CENTER);
}
sprintf(buf, "%d-%02d-%02d %02d:%02d:%02d",
lbl = sgl_label_create(NULL);
sgl_obj_set_pos(lbl, 0, 130);
sgl_obj_set_size(lbl, 240, 28);
/* 使用 sgl_label_set_text_dyn文本复制到 label 自有堆内存label 销毁时自动释放 */
sprintf(tmp, "%d-%02d-%02d %02d:%02d:%02d",
2000 + board.rtc_date.year, board.rtc_date.month, board.rtc_date.day,
board.rtc_time.hour, board.rtc_time.minute, board.rtc_time.second);
sgl_label_set_text(label_time, buf);
// sgl_label_set_text_dyn(lbl, tmp);
sgl_label_set_text(lbl, tmp);
sgl_label_set_font(lbl, &song23);
sgl_label_set_text_color(lbl, sgl_rgb(0xFF, 0x40, 0x40));
sgl_label_set_text_align(lbl, SGL_ALIGN_CENTER);
sgl_task_handle();
}
// 헌팁꿎桿

View File

@ -32,7 +32,6 @@
#include <string.h>
#include "sgl_label.h"
/**
* @brief construct the label object
* @param surf pointer to the surface
@ -40,40 +39,55 @@
* @param evt pointer to the event
* @return none
*/
static void sgl_label_construct_cb(sgl_surf_t *surf, sgl_obj_t* obj, sgl_event_t *evt)
static void sgl_label_construct_cb(sgl_surf_t *surf, sgl_obj_t *obj, sgl_event_t *evt)
{
sgl_label_t *label = sgl_container_of(obj, sgl_label_t, obj);
sgl_pos_t align_pos;
SGL_ASSERT(label->font != NULL);
if (evt->type == SGL_EVENT_DRAW_MAIN) {
if (label->bg_flag) {
if (evt->type == SGL_EVENT_DESTROYED)
{
/* 释放动态文本缓冲 */
if (label->text_buf != NULL)
{
sgl_free(label->text_buf);
label->text_buf = NULL;
}
return;
}
if (evt->type == SGL_EVENT_DRAW_MAIN)
{
if (label->bg_flag)
{
sgl_draw_fill_rect(surf, &obj->area, &obj->coords, obj->radius, label->bg_color, label->alpha);
}
align_pos = sgl_get_text_pos(&obj->coords, label->font, label->text, 0, (sgl_align_type_t)label->align);
#if (CONFIG_SGL_LABEL_ROTATION)
if (label->rota == 0) {
if (label->rota == 0)
{
#endif
sgl_draw_string(surf, &obj->area, align_pos.x + label->transform.offset.offset_x,
align_pos.y + label->transform.offset.offset_y,
label->text, label->color, label->alpha, label->font);
align_pos.y + label->transform.offset.offset_y,
label->text, label->color, label->alpha, label->font);
#if (CONFIG_SGL_LABEL_ROTATION)
}
else {
else
{
const int16_t width = obj->area.x2 - obj->area.x1 + 1;
const int16_t height = obj->area.y2 - obj->area.y1 + 1;
const uint32_t buf_size = width * height;
sgl_color_t *temp_buf = sgl_malloc(buf_size * sizeof(sgl_color_t));
if (temp_buf == NULL) {
if (temp_buf == NULL)
{
SGL_LOG_ERROR("sgl_label_construct_cb: malloc rotation temp buffer failed");
return;
}
for (uint32_t i = 0; i < buf_size; i++) {
for (uint32_t i = 0; i < buf_size; i++)
{
temp_buf[i] = label->bg_color;
}
@ -85,11 +99,10 @@ static void sgl_label_construct_cb(sgl_surf_t *surf, sgl_obj_t* obj, sgl_event_t
.buffer = temp_buf,
.w = width,
.h = height,
.dirty = NULL
};
.dirty = NULL};
sgl_draw_string(&temp_surf, &obj->area, align_pos.x, align_pos.y,
label->text, label->color, label->alpha, label->font);
label->text, label->color, label->alpha, label->font);
sgl_draw_xform_surf(surf, &temp_surf, &obj->area, obj->coords.x1, obj->coords.y1, label->transform.rotation);
sgl_free(temp_buf);
@ -98,16 +111,16 @@ static void sgl_label_construct_cb(sgl_surf_t *surf, sgl_obj_t* obj, sgl_event_t
}
}
/**
* @brief create a label object
* @param parent parent of the label
* @return pointer to the label object
*/
sgl_obj_t* sgl_label_create(sgl_obj_t* parent)
sgl_obj_t *sgl_label_create(sgl_obj_t *parent)
{
sgl_label_t *label = sgl_malloc(sizeof(sgl_label_t));
if(label == NULL) {
if (label == NULL)
{
SGL_LOG_ERROR("sgl_label_create: malloc failed");
return NULL;
}
@ -121,6 +134,7 @@ sgl_obj_t* sgl_label_create(sgl_obj_t* parent)
label->alpha = SGL_ALPHA_MAX;
label->bg_flag = 0;
label->text_buf = NULL;
label->color = SGL_THEME_TEXT_COLOR;
label->text = "";
label->transform.rotation = 0;

View File

@ -33,39 +33,40 @@
#include <sgl_cfgfix.h>
#include <string.h>
/**
* @brief sgl label object
* @obj: sgl general object
* @desc: draw task descriptor
*/
typedef struct sgl_label {
sgl_obj_t obj;
const char *text;
typedef struct sgl_label
{
sgl_obj_t obj;
const char *text;
char *text_buf; /* 动态文本缓冲,由 label 持有,销毁时自动释放 */
const sgl_font_t *font;
sgl_color_t color;
sgl_color_t bg_color;
uint8_t alpha;
uint8_t align: 6;
uint8_t bg_flag : 1;
uint8_t rota : 1;
union {
struct {
sgl_color_t color;
sgl_color_t bg_color;
uint8_t alpha;
uint8_t align : 6;
uint8_t bg_flag : 1;
uint8_t rota : 1;
union
{
struct
{
int8_t offset_x;
int8_t offset_y;
} offset;
int16_t rotation;
} transform;
}sgl_label_t;
} sgl_label_t;
/**
* @brief create a label object
* @param parent parent of the label
* @return pointer to the label object
*/
sgl_obj_t* sgl_label_create(sgl_obj_t* parent);
sgl_obj_t *sgl_label_create(sgl_obj_t *parent);
/**
* @brief set label text
@ -80,6 +81,27 @@ static inline void sgl_label_set_text(sgl_obj_t *obj, const char *text)
sgl_obj_set_dirty(obj);
}
/**
* @brief text label label
* @note 使 sprintf buf buf
*/
static inline void sgl_label_set_text_dyn(sgl_obj_t *obj, const char *text)
{
sgl_label_t *label = sgl_container_of(obj, sgl_label_t, obj);
size_t len = strlen(text) + 1;
if (label->text_buf != NULL)
{
sgl_free(label->text_buf);
}
label->text_buf = (char *)sgl_malloc(len);
if (label->text_buf != NULL)
{
memcpy(label->text_buf, text, len);
label->text = label->text_buf;
}
sgl_obj_set_dirty(obj);
}
/**
* @brief set label font
* @param obj pointer to the label object
@ -183,7 +205,8 @@ static inline void sgl_label_set_text_rotation(sgl_obj_t *obj, int16_t text_rota
{
sgl_label_t *label = sgl_container_of(obj, sgl_label_t, obj);
label->transform.rotation = text_rotation % 360;
if (label->transform.rotation < 0) label->transform.rotation += 360;
if (label->transform.rotation < 0)
label->transform.rotation += 360;
label->rota = label->transform.rotation ? 1 : 0;
sgl_obj_set_dirty(obj);
}