Use encoder to change lv_bar value

Description

Use encoder to change lv_bar value when bar is focused

What MCU/Processor/Board and compiler are you using?

custom STM32F407

What LVGL version are you using?

Last from git

What do you want to achieve?

Change lv_bar value using encoder knob, when rotate left decrease value, right increase it
for example: Click bar -> bar keeps focus, and change its value on encoder rotation-> click to release

What have you tried so far?

Code to reproduce

//my encoder driver code 
bool encoder_read (lv_indev_drv_t * drv, lv_indev_data_t*data) 
{

	data->enc_diff = TIM3_read();
	if (data->enc_diff > 0)
		{
			data->state = LV_INDEV_STATE_PR;
			data->key = LV_KEY_LEFT;
		//	enc_to_gui = LV_KEY_NEXT;
		}
		if (data->enc_diff < 0)
		{
			data->state = LV_INDEV_STATE_PR;
			data->key = LV_KEY_RIGHT;
		//	enc_to_gui = LV_KEY_PREV;
		}
	if(!enc_pressed()){
	
    data->state = LV_INDEV_STATE_PR;
  }
  else{
    data->state = LV_INDEV_STATE_REL;
  }
	lv_refr_now(NULL);
  return false; /*No buffering so no more data read*/
}

Screenshot and/or video

If possible, add screenshots and/or videos about the current state.

Previously I tried to add an encoder with following snippets and it worked:

void hal_init(void)
{
   lv_indev_drv_t encoder_drv;
   lv_indev_drv_init(&encoder_drv);
   encoder_drv.type = LV_INDEV_TYPE_ENCODER;
   encoder_drv.read_cd = encoder_read;
   static lv_indev_t * encoder_indev = lv_indev_drv_register(&encoder_drv);
}

//Now for the application, example
void demo_simple_button(void)
{
	lv_obj_t * btn = lv_btn_create(lv_scr_act(), NULL);
	lv_obj_align(btn, NULL, LV_ALIGN_CENTER, 0, 0);
	lv_btn_set_fit2(btn, LV_FIT_TIGHT, LV_FIT_NONE);
	lv_obj_t * label = lv_label_create(btn, NULL);
	lv_label_set_text(label, "Click Me");

	lv_group_t *g = lv_group_create(); //need to create a group to tight with the target object, can be button or slidebar etc
	lv_group_add_obj(g, btn);
	lv_indev_set_group(encoder_indev, g);
}


Why lv_refr_now(NULL) is required?

@techtoys
Thanks for reply,
but i have different problem. I want to change lv_bar value on encoder movement. I will try to explain my idea. I have bar and button i want when i click on button, i want to change bar value using encoder movement.
I cant figure out, how to do it :slight_smile:

lv_refr_now its not required, i do not see it in docs, but i use this routine to refresh screen after encoder movement. In my case lv_task_handler can be blocked in certain circumstances.

Best,

You need to select the lv_bar object to get into edit mode, then you move the encoder and it will change the value. Once you’re happy with the.value you d a long press of the encoder and it will exit the edit mode.

I think your read function is wrong, check the example of this link https://docs.lvgl.io/latest/en/html/porting/indev.html#encoder

I don’t think you should be calling lv_refr_now in your read function. I recommend ensuring that lv_task_handler doesn’t block and leaving it to LVGL to manage screen refreshing.