Lv_slider value

Hi Team,

Description

i am using lv_slider in my project. when i changed the value by using knob the value is changing.
but when i printing the slider value it printing all values in my console

What do you want to achieve?

i want to print the final value i.e., if i stop the knob at 60 i want to print 60 in my console. don’t want to print all the values from 0 to 60. Please look into the below code. if buf value is >0 then i want to set the text ON or if buf value is ==0 then i want to set the text to OFF. for this i want the final value to do this.

What have you tried so far?

lv_obj_t * slider = lv_slider_create(lv_scr_act(), NULL);
              lv_obj_set_width(slider, 70);
              lv_obj_set_height(slider, 600);
              lv_obj_set_event_cb(slider, slider_event_cb);
              lv_slider_set_range(slider, 0, 100);
              lv_slider_set_style(slider, LV_SLIDER_STYLE_BG, &style_bg);
              lv_slider_set_style(slider, LV_SLIDER_STYLE_INDIC,&style_indic);
              lv_slider_set_style(slider, LV_SLIDER_STYLE_KNOB, &style_knob);
              lv_obj_align(slider,msg_text,LV_ALIGN_OUT_BOTTOM_MID,-100,30);
              /* Create a label below the slider */
              slider_label = lv_label_create(lv_scr_act(), NULL);
              lv_label_set_style(slider_label, LV_LABEL_STYLE_MAIN, &style_title_opa);
              lv_label_set_text(slider_label, "0");
              lv_obj_set_auto_realign(slider_label, true);
              lv_obj_align(slider_label,slider,LV_ALIGN_OUT_BOTTOM_MID,0,20);

static void slider_event_cb(lv_obj_t * slider, lv_event_t event)
{
    if(event == LV_EVENT_VALUE_CHANGED) {
        static char buf[4]; /* max 3 bytes for number plus 1 null terminating byte */
        snprintf(buf, 4, "%u", lv_slider_get_value(slider));
        lv_label_set_text(slider_label, buf);

        if(buf==0){
        	lv_label_set_text(on_off,"OFF");
        }else if(buf>0){
        	lv_label_set_text(on_off,"ON");
        }
    }
}

Thanks.

You should check the value on LV_EVENT_RELEASED. LV_EVENT_VALUE_CHANGED fires every time the value changes.

1 Like