概述
lVGL 按键的移植支持
参考官方的移植文档,tp keypad 等的移植适配都是在这里的lv_port_indev.c里面修改。直接上代码,我只显示我修改的地方,多余代码删除
//移植中添加keypad相关的初始化以及正确填充 indev_drv 这个结构体。
void lv_port_indev_init(void)
{
/*------------------
* Keypad
* -----------------*/
/*Initialize your keypad or keyboard if you have*/
keypad_init(); //初始化操作,对应你的平台 io 或 adc 检测
/*Register a keypad input device*/
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_KEYPAD;
indev_drv.read_cb = keypad_read;
indev_keypad = lv_indev_drv_register(&indev_drv);
}
/* Will be called by the library to read the mouse */
static bool keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
static uint32_t last_key = 0;
/*Get the current x and y coordinates*/
mouse_get_xy(&data->point.x, &data->point.y);
/*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;
//这个是我自己添加 我想在某个控件监听返home键,官方默认的只有1-5 ,我自己加了 6 == 》 home 键
case 6:
act_key = LV_KEY_HOME;
break;
}
last_key = act_key;
} else {
data->state = LV_INDEV_STATE_REL;
}
data->key = last_key;
/*Return `false` because we are not buffering and no more data to read*/
return false;
}
/*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(GPIOE,GPIO_PIN_4) == 0){
return 1; //和 LV_KEY_NEXT 对应
}else if( HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_3) == 0){
return 6; //自己添加和 LV_KEY_HOME 对应
}else if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_2) == 0){
return 5; //和 LV_KEY_ENTER 对应
}
return 0;
}
应用测试
首先必须创建一个group 和 indev 进行绑定lv_indev_set_group,并且把控件添加到group 中lv_group_add_obj,这样才能监听到。
extern lv_indev_t * indev_keypad;
static void event_callback(struct _lv_obj_t * obj, lv_event_t event)
{
if(event == LV_EVENT_KEY){
const u32 *key = lv_event_get_data();
if(*key == LV_KEY_HOME){
printf("rnevent = %d *key=%d",event,*key);
}else if(*key == LV_KEY_ENTER){
printf("rnevent = %d *key=%d",event,*key);
}
}
}
static void roller_callback(struct _lv_obj_t * obj, lv_event_t event)
{
static int i = 0;
if(event == LV_EVENT_CLICKED){
printf("rnclick = %d",event);
if(i++ >=6)
i=0;
lv_roller_set_selected(obj, i, LV_ANIM_OFF);
}
}
int main(void)
{
bsp_init();
lv_init();
lv_port_disp_init();
lv_port_indev_init();
lv_obj_t *scr = lv_scr_act();
lv_group_t *group = lv_group_create();
lv_indev_set_group(indev_keypad, group);
lv_obj_t *cont = lv_cont_create(scr,NULL);
lv_obj_set_size(cont,240,240);
lv_obj_t *btn1 = lv_btn_create(cont, NULL);
lv_obj_set_size(btn1,50,50);
lv_obj_set_pos(btn1,0,0);
lv_obj_set_event_cb(btn1, event_callback);
lv_obj_t *btn2 = lv_btn_create(cont,NULL);
lv_obj_set_size(btn2,50,50);
lv_obj_set_pos(btn2,50,50);
lv_obj_t *roller = lv_roller_create(cont,NULL);//鍒涘缓婊氳疆鎺т欢
lv_roller_set_options(roller,"1n2n3n4n5n6",LV_ROLLER_MODE_INIFINITE);
lv_obj_set_pos(roller,100,100);
lv_obj_set_event_cb(roller, roller_callback);
lv_group_add_obj(group ,btn1);
lv_group_add_obj(group ,btn2);
lv_group_add_obj(group ,roller);
lv_group_set_editing(group,false);
}
看下输出情况
点击next 三个控件在循环聚焦(选中),当选中roller时候 点击enter按键,
click = 6
click = 6
click = 6
click = 6
click = 6
click = 6
click = 6
click = 6
click = 6
串口输出,从UI看roller 的滚子在循环变换
当选中btn1 时,点击 home 或 enter 按键,会又如下输出
event = 12 *key=2
event = 12 *key=2
event = 12 *key=2
event = 12 *key=2
event = 12 *key=10
event = 12 *key=10
event = 12 *key=2
event = 12 *key=2
event = 12 *key=10
event = 12 *key=2
event = 12 *key=10
event = 12 *key=10
这个可以正确监听到key 的值
总结
这种方式可以监听到按键报上来的值,但是要加到group中,不用的时候 remove掉,还有一种用法,可以直接发送时间到某一个具体的控件,lv_send_event,但是只能监听某一个具体事件(按键对应这个LV_EVENT_KEY),获取不到具体的按键值,当然可以传个其他的事件或有个全局标识获取。
以上是想某个控件响应实体按键的一个测试,并且能够获取到键值,如有不正确的地方欢迎指正
最后
以上就是碧蓝日记本为你收集整理的LVGL(7.5) 控件响应实体按键lVGL 按键的移植支持应用测试总结的全部内容,希望文章能够帮你解决LVGL(7.5) 控件响应实体按键lVGL 按键的移植支持应用测试总结所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复