LVGL使用随记(界面使用GUI Guider绘制)

1、实体按键绑定keypad
删除文件lv_port_indev.c跟keypad无关的所有代码,并修改keypad_get_key()绑定实体按键,keypad_read会在程序中定时调用,定时时间可在lv_conf.h中修改,默认一般为30ms

/*Will be called by the library to read the mouse*/
static void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    static uint32_t last_key = 0;

    /*Get whether the a key is pressed and save the pressed key*/
    uint32_t act_key = keypad_get_key();
    if(act_key != 0) {
        data->state = LV_INDEV_STATE_PR;

        /*Translate the keys to LVGL control characters according to your key definitions*/
        switch(act_key) {
            case 1:
                act_key = LV_KEY_NEXT;
                break;
            case 2:
                act_key = LV_KEY_PREV;
                break;
            case 3:
                act_key = LV_KEY_LEFT;
                break;
            case 4:
                act_key = LV_KEY_RIGHT;
                break;
            case 5:
                act_key = LV_KEY_ENTER;
                break;
        }

        last_key = act_key;
    }
    else {
        data->state = LV_INDEV_STATE_REL;
    }

    data->key = last_key;
}

/*Get the currently being pressed key.  0 if no key is pressed*/
static uint32_t keypad_get_key(void)
{
    /*Your code comes here*/
	if(!HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1))
		return 1;
	if(!HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0))
		return 5;
    return 0;
}

预定义的键具有特殊含义:

LV_KEY_NEXT 专注于下一个对象
LV_KEY_PREV 专注于上一个对象
LV_KEY_ENTER 触发器 LV_EVENT_PRESSED/CLICKED/LONG_PRESSED 等事件
LV_KEY_UP 增加值或向上移动
LV_KEY_DOWN 减小值或向下移动
LV_KEY_RIGHT 增加值或向右移动
LV_KEY_LEFT 减小值或向左移动
LV_KEY_ESC 关闭或退出(例如,关闭下拉列表)
LV_KEY_DEL 删除(例如,“ 文本”区域中右侧的字符)
LV_KEY_BACKSPACE 删除左侧的字符(例如,在文本区域中)
LV_KEY_HOME 转到开头/顶部(例如,在“ 文本”区域中)
LV_KEY_END 转到末尾(例如,在“ 文本”区域中)

在相关界面初始化时将输入设备与组关联

	extern lv_indev_t * indev_keypad;
	lv_group_t *group=lv_group_create();
    lv_indev_set_group(indev_keypad, group);	//将组绑定到输入设备
 
    lv_group_set_editing(group, false);   //导航模式
	lv_group_add_obj(group ,guider_ui.main_screen_test_btn);
	lv_group_add_obj(group ,guider_ui.main_screen_chart_btn);
	lv_group_add_obj(group ,guider_ui.main_screen_history_btn);

2、屏幕数据定时更新
可在界面加载完成时创建定时任务

static lv_timer_t *updata_data_task;
static void main_screen_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_SCREEN_LOADED:
	{
		screen_id = 1;
		updata_data_task = lv_timer_create(main_screen_updata_data_task_timer_cb,100,&guider_ui);
		break;
	}
	default:
		break;
	}
}
void events_init_main_screen(lv_ui *ui)
{
	lv_obj_add_event_cb(ui->main_screen, main_screen_event_handler, LV_EVENT_ALL, ui);
}
void main_screen_updata_data_task_timer_cb(lv_timer_t *t)
{
    lv_label_set_text_fmt(guider_ui.main_screen_CurrV_label,"%dmv",ads1220_data);
	if(finish_flag)
	{
		finish_flag = 0;
		lv_label_set_text_fmt(guider_ui.main_screen_MaxV_label,"%dmv",maxv);
		lv_label_set_text_fmt(guider_ui.main_screen_MinV_label,"%dmv",minv);
		lv_label_set_text_fmt(guider_ui.main_screen_Linear_label,"%.2f",(linearity * 100.0));
	}

	lv_bar_set_value(guider_ui.main_screen_power_bar, (ADC_ConvertedValue - 1030.0) / 3.0, LV_ANIM_OFF);
}

注:切换界面时如果选择删除上个界面记得同时删除上个界面的定时任务

lv_timer_del(updata_data_task);

3、对象style样式的使用
如在按钮被聚焦时改变按钮样式,可在组件初始化后加入以下代码

	static lv_style_t btn_focused_stytle;
	lv_style_init(&btn_focused_stytle);

	lv_style_set_border_width(&btn_focused_stytle, 4);

	lv_obj_add_style(ui->main_screen_history_btn,&btn_focused_stytle,LV_STATE_FOCUSED);
	lv_obj_add_style(ui->main_screen_chart_btn,&btn_focused_stytle,LV_STATE_FOCUSED);
	lv_obj_add_style(ui->main_screen_test_btn,&btn_focused_stytle,LV_STATE_FOCUSED);

对象可以处于以下状态的组合:

LV_STATE_DEFAULT (0x0000) 正常,释放状态
LV_STATE_CHECKED (0x0001) 切换或检查状态
LV_STATE_FOCUSED (0x0002) 通过键盘或编码器聚焦或通过触摸板/鼠标点击
LV_STATE_FOCUS_KEY (0x0004) 通过键盘或编码器聚焦,但不通过触摸板/鼠标聚焦
LV_STATE_EDITED (0x0008) 由编码器编辑
LV_STATE_HOVERED (0x0010) 鼠标悬停(现在不支持)
LV_STATE_PRESSED (0x0020) 被按下
LV_STATE_SCROLLED (0x0040) 正在滚动
LV_STATE_DISABLED (0x0080) 禁用状态
LV_STATE_USER_1 (0x1000) 自定义状态
LV_STATE_USER_2 (0x2000) 自定义状态
LV_STATE_USER_3 (0x4000) 自定义状态
LV_STATE_USER_4 (0x8000) 自定义状态

3、chart组件的使用
折线图数据添加

		lv_chart_set_point_count(guider_ui.chart_screen_chart_1,point_cnt);//设定显示的点数
		lv_chart_set_range(guider_ui.chart_screen_chart_1, LV_CHART_AXIS_PRIMARY_Y, point_v_min, point_v_max);//y轴范围
		lv_chart_set_range(guider_ui.chart_screen_chart_1, LV_CHART_AXIS_PRIMARY_X, point_l_min, point_l_max);//x轴范围
		for(int i = 0;i < point_cnt;i++)
		{
			lv_chart_set_next_value2(guider_ui.chart_screen_chart_1, ser1, point_l[i], point_v[i]);//添加数据点
		}

4、lv_conf.h
内存修改

/*=========================
   MEMORY SETTINGS
 *=========================*/

/*1: use custom malloc/free, 0: use the built-in `lv_mem_alloc()` and `lv_mem_free()`*/
#define LV_MEM_CUSTOM 0
#if LV_MEM_CUSTOM == 0
    /*Size of the memory available for `lv_mem_alloc()` in bytes (>= 2kB)*/
    #define LV_MEM_SIZE (48U * 1024U)          /*[bytes]*///默认48kb

    /*Set an address for the memory pool instead of allocating it as a normal array. Can be in external SRAM too.*/
    #define LV_MEM_ADR 0     /*0: unused*/
    /*Instead of an address give a memory allocator that will be called to get a memory pool for LVGL. E.g. my_malloc*/
    #if LV_MEM_ADR == 0
        #undef LV_MEM_POOL_INCLUDE
        #undef LV_MEM_POOL_ALLOC
    #endif

#else       /*LV_MEM_CUSTOM*/
    #define LV_MEM_CUSTOM_INCLUDE <stdlib.h>   /*Header for the dynamic memory function*/
    #define LV_MEM_CUSTOM_ALLOC   malloc
    #define LV_MEM_CUSTOM_FREE    free
    #define LV_MEM_CUSTOM_REALLOC realloc
#endif     /*LV_MEM_CUSTOM*/

刷新周期

/*====================
   HAL SETTINGS
 *====================*/

/*Default display refresh period. LVG will redraw changed areas with this period time*/
#define LV_DISP_DEF_REFR_PERIOD 30      /*[ms]屏幕刷新频率*/

/*Input device read period in milliseconds*/
#define LV_INDEV_DEF_READ_PERIOD 5     /*[ms]输入设备刷新频率*/

/*Use a custom tick source that tells the elapsed time in milliseconds.
 *It removes the need to manually update the tick with `lv_tick_inc()`)*/
#define LV_TICK_CUSTOM 0
#if LV_TICK_CUSTOM
    #define LV_TICK_CUSTOM_INCLUDE "Arduino.h"         /*Header for the system time function*/
    #define LV_TICK_CUSTOM_SYS_TIME_EXPR (millis())    /*Expression evaluating to current system time in ms*/
    /*If using lvgl as ESP32 component*/
    // #define LV_TICK_CUSTOM_INCLUDE "esp_timer.h"
    // #define LV_TICK_CUSTOM_SYS_TIME_EXPR ((esp_timer_get_time() / 1000LL))
#endif   /*LV_TICK_CUSTOM*/

/*Default Dot Per Inch. Used to initialize default sizes such as widgets sized, style paddings.
 *(Not so important, you can adjust it to modify default sizes and spaces)*/
#define LV_DPI_DEF 130     /*[px/inch]*/

显示浮点数,LVGL默认设置中,如果使用%f显示浮点数会只显示f

/*Change the built in (v)snprintf functions*/
#define LV_SPRINTF_CUSTOM 1   //默认为0,修改为1可显示浮点数
#if LV_SPRINTF_CUSTOM
    #define LV_SPRINTF_INCLUDE <stdio.h>
    #define lv_snprintf  snprintf
    #define lv_vsnprintf vsnprintf
#else   /*LV_SPRINTF_CUSTOM*/
    #define LV_SPRINTF_USE_FLOAT 0
#endif  /*LV_SPRINTF_CUSTOM*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值