LVGL7.5版本 触摸与键盘输入(API)对接(一)

LVGL7.5版本 触摸与键盘输入(API)对接(一)

前言:

  • 前提条件:
  • 已完成移植lvgl
  • 本篇文章将重点讲解Keypad键盘按键

一、indev输入设备的种类介绍

  1. Touchpad (触摸板,例如电容屏、电阻屏等)
  2. Mouse (鼠标)
  3. Keypad (键盘)
  4. Encoder (编码器)
  5. Button (外部按键)

二、移植Touchpad与Keypad

void lv_port_indev_init(void)
{
    lv_indev_drv_t indev_drv;

    /*------------------
     * Touchpad
     * -----------------*/
	//触摸屏
    /*Initialize your touchpad if you have*/
    touchpad_init();

    /*Register a touchpad input device*/
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = touchpad_read;
    indev_touchpad = lv_indev_drv_register(&indev_drv);

	//按键
	    /*------------------
     * Keypad
     * -----------------*/

    /*Initialize your keypad or keyboard if you have*/
    keypad_init();

    /*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);
		
    /* Later you should create group(s) with `lv_group_t * group = lv_group_create()`,
     * add objects to the group with `lv_group_add_obj(group, obj)`
     * and assign this input device to group to navigate in it:
     * `lv_indev_set_group(indev_keypad, group);` */
}

Touchpad

触摸屏移植只需要结合自己的TP驱动完成这两个函数

/*Return true is the touchpad is pressed*/
static bool touchpad_is_pressed(void)
{	
	  if (TouchItOccurred == SET)
    {
      TouchItOccurred = RESET;
			/*Your code comes here*/
			//判断是否有被按下
			BSP_TS_GetState(&TS_State);
			if(TS_State.touchDetected)
			{
				if(TS_State.touchEventId[0]==0x03)
				{
					return true;
				}
			}
	  }
    return false;
}

/*Get the x and y coordinates if the touchpad is pressed*/
static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y)
{
		BSP_TS_GetState(&TS_State);
		if(TS_State.touchDetected)
		{
    	/*Your code comes here*/
    	//按下所获得的坐标
		(*x) = TS_State.touchX[0];
    	(*y) = TS_State.touchY[0];
		}			
		//log("x %d,y %d",TS_State.touchX[0],TS_State.touchY[0]);

}

Keypad

目前只做了一个按键返回值做为测试

/* 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;
        }

        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)
{
		uint8_t key_val=0;
    /*Your code comes here*/
    	//读取按键是否被按下
		key_val=KEY_Scan(0);
		if(key_val>0)
		return key_val;
    return 0;
}

Keypad测试示例

此测试利用tabview创建tab1与tab2,在两个tab分别使用不同的图片,按下按键由tab1->tab2,再次按下又由tab2->tab1

lv_obj_t *nihao;
lv_obj_t *tab1;
lv_obj_t *tab2;
lv_group_t *test_group;		//定义一个新的对象组
LV_IMG_DECLARE(huoxingtest); //此处为测试定义的图片
LV_IMG_DECLARE(muxingtest);//此处为测试定义的图片
extern lv_indev_t * indev_keypad;

static void event_callback(lv_obj_t * obj, lv_event_t event)
{
	if(event == LV_EVENT_KEY){
		const uint32_t *key = lv_event_get_data();	
		if(*key == LV_KEY_RIGHT){
			if(obj==tab1)
			{
				lv_group_focus_next(test_group);//切换焦点obj
				lv_tabview_set_tab_act(nihao,1,LV_ANIM_OFF);
			}
			else if(obj==tab2)
			{
				lv_group_focus_prev(test_group);
				lv_tabview_set_tab_act(nihao,0,LV_ANIM_OFF);
			}
		}
	}
}

void app_test(void)
{
	test_group=lv_group_create();	//创建对象组
	lv_indev_set_group(indev_keypad,test_group);	//关联输入设备与对象组
	
	
	nihao=lv_tabview_create(lv_scr_act(),NULL);
	lv_obj_set_pos(nihao,0,0);
	lv_obj_set_size(nihao,390,390);
	lv_tabview_set_btns_pos(nihao,LV_TABVIEW_TAB_POS_NONE);
	lv_obj_set_style_local_border_width(nihao,LV_PAGE_PART_BG,LV_STATE_DEFAULT,0);
	lv_obj_set_style_local_bg_color(nihao,LV_PAGE_PART_BG,LV_STATE_DEFAULT,LV_COLOR_BLACK);
	
	tab1=lv_tabview_add_tab(nihao,"tab1");
	lv_page_set_scrlbar_mode(tab1,LV_SCRLBAR_MODE_OFF);
	lv_obj_t *img1=lv_img_create(tab1,NULL);
	lv_obj_set_pos(img1,0,0);
	lv_img_set_src(img1,&huoxingtest);
	lv_obj_set_event_cb(tab1,event_callback);
	
	tab2=lv_tabview_add_tab(nihao,"tab2");
	lv_page_set_scrlbar_mode(tab2,LV_SCRLBAR_MODE_OFF);
	lv_obj_t *img2=lv_img_create(tab2,NULL);
	lv_obj_set_pos(img2,0,0);
	lv_img_set_src(img2,&muxingtest);
	lv_obj_set_event_cb(tab2,event_callback);

	lv_group_add_obj(test_group,tab1);
	lv_group_add_obj(test_group,tab2);
	
	lv_group_set_editing(test_group,false);

}

本篇完

总结:按键触发响应,需要将对象加入group中,通过焦点的切换完成对event事件具体的响应,KEY值做出对应的策略,目前只做了一个按键对应的KEY的测试,后续将测试不同键值对应的操作,如果文中有不正确的地方,欢迎赐教!!!


版权声明:本文为mingzongliang原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。