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:
parent
9f04900092
commit
ff554a472c
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
|
|
@ -5,39 +5,52 @@
|
||||||
#include "../system/sgl/sgl.h"
|
#include "../system/sgl/sgl.h"
|
||||||
|
|
||||||
static TaskHandle_t business_task_handle = NULL;
|
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_obj_delete(NULL);
|
||||||
sgl_page_set_color(sgl_screen_act(), sgl_rgb(0x00, 0x00, 0x00));
|
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_t *label = sgl_label_create(NULL);
|
||||||
sgl_obj_set_pos(label, 0, 0);
|
sgl_obj_set_pos(label, 0, 0);
|
||||||
sgl_obj_set_size(label, 240, 240);
|
sgl_obj_set_size(label, 240, 240);
|
||||||
sgl_label_set_text(label, "Hello World");
|
|
||||||
sgl_label_set_font(label, &song23);
|
sgl_label_set_font(label, &song23);
|
||||||
sgl_label_set_text_color(label, sgl_rgb(0xFF, 0xFF, 0xFF));
|
sgl_label_set_text_color(label, sgl_rgb(0xFF, 0xFF, 0xFF));
|
||||||
sgl_label_set_text_align(label, SGL_ALIGN_CENTER);
|
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)
|
void business_task(void *pvParameters)
|
||||||
{
|
{
|
||||||
/* 启动时显示 Hello World 2 秒 */
|
|
||||||
sgl_print_hello();
|
|
||||||
sgl_task_handle();
|
|
||||||
vTaskDelay(2000);
|
|
||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
rtc_update();
|
rtc_update();
|
||||||
|
|
||||||
|
sgl_print_hello();
|
||||||
|
vTaskDelay(1000); /* 显示 1 秒 */
|
||||||
|
|
||||||
lcd_rtc_test();
|
lcd_rtc_test();
|
||||||
sgl_task_handle();
|
|
||||||
led_toggle(LED_USER);
|
led_toggle(LED_USER);
|
||||||
vTaskDelay(1000);
|
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)
|
void app_init(void)
|
||||||
{
|
{
|
||||||
userShellInit(); // 初始化shell
|
userShellInit(); // 初始化shell
|
||||||
|
|
|
||||||
|
|
@ -149,12 +149,10 @@ BOOL rtc_init(void)
|
||||||
|
|
||||||
void lcd_rtc_test(void)
|
void lcd_rtc_test(void)
|
||||||
{
|
{
|
||||||
static sgl_obj_t *label_time = NULL;
|
char tmp[32];
|
||||||
static char buf[32];
|
|
||||||
|
|
||||||
if (label_time == NULL)
|
|
||||||
{
|
|
||||||
sgl_obj_delete(NULL);
|
sgl_obj_delete(NULL);
|
||||||
|
sgl_task_handle(); /* 刷新:屏幕清空 */
|
||||||
|
|
||||||
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_t *lbl = sgl_label_create(NULL);
|
||||||
|
|
@ -173,18 +171,20 @@ void lcd_rtc_test(void)
|
||||||
sgl_label_set_text_color(lbl, sgl_rgb(0xFF, 0xFF, 0x00));
|
sgl_label_set_text_color(lbl, sgl_rgb(0xFF, 0xFF, 0x00));
|
||||||
sgl_label_set_text_align(lbl, SGL_ALIGN_CENTER);
|
sgl_label_set_text_align(lbl, SGL_ALIGN_CENTER);
|
||||||
|
|
||||||
label_time = sgl_label_create(NULL);
|
lbl = sgl_label_create(NULL);
|
||||||
sgl_obj_set_pos(label_time, 0, 130);
|
sgl_obj_set_pos(lbl, 0, 130);
|
||||||
sgl_obj_set_size(label_time, 240, 28);
|
sgl_obj_set_size(lbl, 240, 28);
|
||||||
sgl_label_set_font(label_time, &song23);
|
/* 使用 sgl_label_set_text_dyn:文本复制到 label 自有堆内存,label 销毁时自动释放 */
|
||||||
sgl_label_set_text_color(label_time, sgl_rgb(0xFF, 0x40, 0x40));
|
sprintf(tmp, "%d-%02d-%02d %02d:%02d:%02d",
|
||||||
sgl_label_set_text_align(label_time, SGL_ALIGN_CENTER);
|
|
||||||
}
|
|
||||||
|
|
||||||
sprintf(buf, "%d-%02d-%02d %02d:%02d:%02d",
|
|
||||||
2000 + board.rtc_date.year, board.rtc_date.month, board.rtc_date.day,
|
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);
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ÇåÆÁ²âÊÔ
|
// ÇåÆÁ²âÊÔ
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,6 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "sgl_label.h"
|
#include "sgl_label.h"
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief construct the label object
|
* @brief construct the label object
|
||||||
* @param surf pointer to the surface
|
* @param surf pointer to the surface
|
||||||
|
|
@ -46,34 +45,49 @@ static void sgl_label_construct_cb(sgl_surf_t *surf, sgl_obj_t* obj, sgl_event_t
|
||||||
sgl_pos_t align_pos;
|
sgl_pos_t align_pos;
|
||||||
|
|
||||||
SGL_ASSERT(label->font != NULL);
|
SGL_ASSERT(label->font != NULL);
|
||||||
|
if (evt->type == SGL_EVENT_DESTROYED)
|
||||||
if (evt->type == SGL_EVENT_DRAW_MAIN) {
|
{
|
||||||
if (label->bg_flag) {
|
/* 释放动态文本缓冲 */
|
||||||
|
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);
|
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);
|
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 (CONFIG_SGL_LABEL_ROTATION)
|
||||||
if (label->rota == 0) {
|
if (label->rota == 0)
|
||||||
|
{
|
||||||
#endif
|
#endif
|
||||||
sgl_draw_string(surf, &obj->area, align_pos.x + label->transform.offset.offset_x,
|
sgl_draw_string(surf, &obj->area, align_pos.x + label->transform.offset.offset_x,
|
||||||
align_pos.y + label->transform.offset.offset_y,
|
align_pos.y + label->transform.offset.offset_y,
|
||||||
label->text, label->color, label->alpha, label->font);
|
label->text, label->color, label->alpha, label->font);
|
||||||
#if (CONFIG_SGL_LABEL_ROTATION)
|
#if (CONFIG_SGL_LABEL_ROTATION)
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
const int16_t width = obj->area.x2 - obj->area.x1 + 1;
|
const int16_t width = obj->area.x2 - obj->area.x1 + 1;
|
||||||
const int16_t height = obj->area.y2 - obj->area.y1 + 1;
|
const int16_t height = obj->area.y2 - obj->area.y1 + 1;
|
||||||
const uint32_t buf_size = width * height;
|
const uint32_t buf_size = width * height;
|
||||||
|
|
||||||
sgl_color_t *temp_buf = sgl_malloc(buf_size * sizeof(sgl_color_t));
|
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");
|
SGL_LOG_ERROR("sgl_label_construct_cb: malloc rotation temp buffer failed");
|
||||||
return;
|
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;
|
temp_buf[i] = label->bg_color;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -85,8 +99,7 @@ static void sgl_label_construct_cb(sgl_surf_t *surf, sgl_obj_t* obj, sgl_event_t
|
||||||
.buffer = temp_buf,
|
.buffer = temp_buf,
|
||||||
.w = width,
|
.w = width,
|
||||||
.h = height,
|
.h = height,
|
||||||
.dirty = NULL
|
.dirty = NULL};
|
||||||
};
|
|
||||||
|
|
||||||
sgl_draw_string(&temp_surf, &obj->area, align_pos.x, align_pos.y,
|
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);
|
||||||
|
|
@ -98,7 +111,6 @@ static void sgl_label_construct_cb(sgl_surf_t *surf, sgl_obj_t* obj, sgl_event_t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief create a label object
|
* @brief create a label object
|
||||||
* @param parent parent of the label
|
* @param parent parent of the label
|
||||||
|
|
@ -107,7 +119,8 @@ static void sgl_label_construct_cb(sgl_surf_t *surf, sgl_obj_t* obj, sgl_event_t
|
||||||
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));
|
sgl_label_t *label = sgl_malloc(sizeof(sgl_label_t));
|
||||||
if(label == NULL) {
|
if (label == NULL)
|
||||||
|
{
|
||||||
SGL_LOG_ERROR("sgl_label_create: malloc failed");
|
SGL_LOG_ERROR("sgl_label_create: malloc failed");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -121,6 +134,7 @@ sgl_obj_t* sgl_label_create(sgl_obj_t* parent)
|
||||||
|
|
||||||
label->alpha = SGL_ALPHA_MAX;
|
label->alpha = SGL_ALPHA_MAX;
|
||||||
label->bg_flag = 0;
|
label->bg_flag = 0;
|
||||||
|
label->text_buf = NULL;
|
||||||
label->color = SGL_THEME_TEXT_COLOR;
|
label->color = SGL_THEME_TEXT_COLOR;
|
||||||
label->text = "";
|
label->text = "";
|
||||||
label->transform.rotation = 0;
|
label->transform.rotation = 0;
|
||||||
|
|
|
||||||
|
|
@ -33,15 +33,16 @@
|
||||||
#include <sgl_cfgfix.h>
|
#include <sgl_cfgfix.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief sgl label object
|
* @brief sgl label object
|
||||||
* @obj: sgl general object
|
* @obj: sgl general object
|
||||||
* @desc: draw task descriptor
|
* @desc: draw task descriptor
|
||||||
*/
|
*/
|
||||||
typedef struct sgl_label {
|
typedef struct sgl_label
|
||||||
|
{
|
||||||
sgl_obj_t obj;
|
sgl_obj_t obj;
|
||||||
const char *text;
|
const char *text;
|
||||||
|
char *text_buf; /* 动态文本缓冲,由 label 持有,销毁时自动释放 */
|
||||||
const sgl_font_t *font;
|
const sgl_font_t *font;
|
||||||
sgl_color_t color;
|
sgl_color_t color;
|
||||||
sgl_color_t bg_color;
|
sgl_color_t bg_color;
|
||||||
|
|
@ -49,8 +50,10 @@ typedef struct sgl_label {
|
||||||
uint8_t align : 6;
|
uint8_t align : 6;
|
||||||
uint8_t bg_flag : 1;
|
uint8_t bg_flag : 1;
|
||||||
uint8_t rota : 1;
|
uint8_t rota : 1;
|
||||||
union {
|
union
|
||||||
struct {
|
{
|
||||||
|
struct
|
||||||
|
{
|
||||||
int8_t offset_x;
|
int8_t offset_x;
|
||||||
int8_t offset_y;
|
int8_t offset_y;
|
||||||
} offset;
|
} offset;
|
||||||
|
|
@ -58,7 +61,6 @@ typedef struct sgl_label {
|
||||||
} transform;
|
} transform;
|
||||||
} sgl_label_t;
|
} sgl_label_t;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief create a label object
|
* @brief create a label object
|
||||||
* @param parent parent of the label
|
* @param parent parent of the label
|
||||||
|
|
@ -66,7 +68,6 @@ typedef struct sgl_label {
|
||||||
*/
|
*/
|
||||||
sgl_obj_t *sgl_label_create(sgl_obj_t *parent);
|
sgl_obj_t *sgl_label_create(sgl_obj_t *parent);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief set label text
|
* @brief set label text
|
||||||
* @param obj pointer to the label object
|
* @param obj pointer to the label object
|
||||||
|
|
@ -80,6 +81,27 @@ static inline void sgl_label_set_text(sgl_obj_t *obj, const char *text)
|
||||||
sgl_obj_set_dirty(obj);
|
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
|
* @brief set label font
|
||||||
* @param obj pointer to the label object
|
* @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);
|
sgl_label_t *label = sgl_container_of(obj, sgl_label_t, obj);
|
||||||
label->transform.rotation = text_rotation % 360;
|
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;
|
label->rota = label->transform.rotation ? 1 : 0;
|
||||||
sgl_obj_set_dirty(obj);
|
sgl_obj_set_dirty(obj);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue