LVGL button 类型输入坐标

一、添加输入设备

  1. lv_port_indev.c中间,增加button类型的代码
    注意点:

    • 必须加入到group中,否则不会有event传递
    • 按键返回值,对应btn_points的下标
    //    /*Register a button input device*/
        lv_indev_drv_init(&indev_drv);
        indev_drv.type = LV_INDEV_TYPE_BUTTON;
       indev_drv.read_cb = button_read;
        indev_button = lv_indev_drv_register(&indev_drv);
        lv_indev_set_group(indev_button, group);
    
    //    /*Assign buttons to points on the screen*/
        static const lv_point_t btn_points[2] = {   //对应按键 0 和 1
                {160/2, 80/2},   /*Button 0 -> x:10; y:10*/ //点屏幕中间
               {40, 100},  /*Button 1 -> x:40; y:100*/
        };
        lv_indev_set_button_points(indev_button, btn_points);//将按键和point 对应起来
    
  2. 按键处理
    注意点:

    • 返回 - 1 ,代表REL,也就是 RELEASE 松手,所以要自己是适配一下这个返回值
    /*Will be called by the library to read the button*/
    static void button_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
    {
    
        static uint8_t last_btn = 0;
    
        /*Get the pressed button's ID*/
        int8_t btn_act = button_get_pressed_id();
        if(btn_act >= 0) {
            data->state = LV_INDEV_STATE_PR;
            last_btn = btn_act;
        } else {
            data->state = LV_INDEV_STATE_REL;
        }
        /*Save the last pressed button's ID*/
        data->btn_id = last_btn;
    }
    
    /*Get ID  (0, 1, 2 ..) of the pressed button*/
    static int8_t button_get_pressed_id(void)
    {
        uint8_t i;
    
        /*Check to buttons see which is being pressed (assume there are 2 buttons)*/
        #if 0
        for(i = 0; i < 2; i++) {
            /*Return the pressed button's ID*/
            if(button_is_pressed(i)) {
                return i;
            }
        }
        #else 
    
    	uint32_t btn = my_read_key();
    	//注意按键是从 0 开始,对应 btn_points 如果没按键就返回-1
    	switch(btn)
    	{
    		case 5: //我的boot按键
    		return 0;//当成0返回
    		break;
    		default:
    		break;
    	}
    
    	
        #endif
        /*No button pressed*/
        return -1;//没按键直接返回 -1
    }
    

二、创建obj

  1. 创建obj和设置回调,监听所有EVENT

    static void my_lvgl_test(void)
    {
    	lv_obj_t * btn = lv_btn_create( lv_scr_act());
    	LV_LOG_USER("lv_scr_act: %x \r\n",lv_scr_act());
    	LV_LOG_USER("btn addr: %x \r\n",btn);
    	lv_obj_set_size( btn, 40, 20 );
    	lv_obj_add_event_cb( btn, event_handler, LV_EVENT_ALL, NULL );	// 设置事件句柄
    	lv_group_add_obj( group, btn );	// 添加进group 如果用触摸屏就不用
    	lv_obj_align( btn, LV_ALIGN_CENTER, 0, 0);
    	lv_obj_add_flag(btn, LV_OBJ_FLAG_CHECKABLE);
    	
        lv_obj_t * label1 = lv_label_create(btn);
        lv_label_set_recolor(label1, true);
        lv_label_set_text(label1, "#ffffff release#");
        //lv_obj_align(label1, btn, 0, 0);
    	//lv_label_set_text(label1, "#ff0000 red#,#00ff00 green#,#0000ff blue#");//使用了 3 次重绘色
    	lv_obj_center(label1);	
    //    lv_obj_t * label;
    //
    //    lv_obj_t * btn1 = lv_btn_create(lv_scr_act());
    //    lv_obj_add_event_cb(btn1, event_handler, LV_EVENT_ALL, NULL);
    //	lv_group_add_obj( group, btn1 );	// 添加进group 如果用触摸屏就不用
    //    lv_obj_align(btn1, LV_ALIGN_CENTER, 0, -10);
    //
    //    label = lv_label_create(btn1);
    //    lv_label_set_text(label, "Button");
    //    lv_obj_center(label);	
    }
    
  2. 回调

    /* 引入之前创建的group */
    extern lv_group_t * group;	// 如果用触摸屏就不用
    
    static void event_handler(lv_obj_t * obj, lv_event_t event)
    {
    	
        switch (event.code) {
        case LV_EVENT_CLICKED:
           
            //lv_obj_del( obj ); // 点击就消失
    		LV_LOG_USER( "btn Clicked\n" );
            break;
        default:
            break;
        }
    
    	
    	lv_event_code_t code = lv_event_get_code(obj);
        lv_obj_t * btn = lv_event_get_target(obj);
        lv_obj_t * label = lv_obj_get_child(btn, 0);
    	
    	
        LV_LOG_USER("code: %d\n",event.code);	
    	
    	
        if(code == LV_EVENT_CLICKED) {
            LV_LOG_USER("Clicked");
        }
        else if(code == LV_EVENT_VALUE_CHANGED) {
    //		lv_label_set_text(obj, "press");
            //lv_label_set_text_fmt(label, "%s", "press");
            //LV_LOG_USER("Toggled");
        }
    	
    }
    

三、调用的大致流程

  1. 注册了button类型的输入后(注意要添加到group内)lv_indev_drv_register,button的回调就会周期性调用判断是否有按键输入
  2. 所有输入设备的轮训,在lv_indev.c
  3. 当判断到按键PRESS,就会发送EVENT过去,这样应用层就能拿到了
  4. indev_proc_press中,通过button 返回的坐标,查找对应现在的obj,这样就会发送对应的EVENT了
    LVGL button 类型输入坐标_第1张图片
    LVGL button 类型输入坐标_第2张图片

你可能感兴趣的:(LVGL,LVGL)