Event_handler_cb

Hi,

Description

I noticed that event_handler_cb is called multiple times even if the event does not change.
I think that call back call is asynchronous and are called by the core lvgl at a pre-set time.

For example in a button press function callback, if i increment a variable, the variable increment multiple times with only one rapid press.

I need to filter the event inside the calback ???
Like:

    static event_t event_type = event_press;

    if ( event_type != event_new ) 
    {
         event_type = even_new;       

         ligh_a_led();       
    }

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

ESP32.

What do you want to achieve?

Only one call per event type.

What have you tried so far?

Code to reproduce

static int16_t bt1Time = 1000;

static void event_handler_btn3(lv_obj_t * button3, lv_event_t event_bt3)
{
    lv_btn_state_t btn3_state = lv_btn_get_state(button3);


    if ( btn3_state == LV_BTN_STATE_TGL_REL )              
    {        
         lv_label_set_text(label3, "OFF");    // change button label.                
    }
    else if ( btn3_state == LV_BTN_STATE_TGL_PR )        
    {      
         lv_label_set_text(label3, "ON");    // change button label.
    }
 


   bt1Time = bt1Time - 100;
    if ( bt1Time < 0 )
        bt1Time = 0;

    lv_btn_set_ink_in_time(btn1, (uint16_t) bt1Time);    

    my_label_print( );    // print bt1Time value.
 }

Hi,

I debugged “lv_event_t” through the serial port.

Now it’s working.
Below is an example for those who are killing themselves like me :).

static int16_t bt1Time = 1000;

static void event_handler_btn3(lv_obj_t * button3, lv_event_t event_bt3)
{
    lv_btn_state_t btn3_state = lv_btn_get_state(button3);


    if ( btn3_state == LV_BTN_STATE_TGL_REL )              
    {        
         lv_label_set_text(label3, "OFF");                
    }
    else if ( btn3_state == LV_BTN_STATE_TGL_PR )        
    {      
         lv_label_set_text(label3, "ON");  
    }
    
    
    char buff[32];
    snprintf(buff, 32, "%u", event_bt3);
    printf("event_bt3 = %s\n", buff );


    if ( ( event_bt3 == LV_EVENT_PRESSED ) || ( event_bt3 == LV_EVENT_LONG_PRESSED_REPEAT ) )    
    {
        bt1Time = bt1Time - 100;
        if ( bt1Time < 0 )
            bt1Time = 0;

        lv_btn_set_ink_in_time(btn1, (uint16_t) bt1Time);    

        my_label_print( );    
    }
}


void my_label_print( )
{
    static int16_t prev_value = 0;

    if(prev_value != bt1Time) 
    {
        if( lv_obj_get_screen(my_label) == lv_scr_act() )
        {
            char buf[32];
            snprintf(buf, 32, "%d", bt1Time);
            lv_label_set_text(my_label, buf);
        }
        prev_value = bt1Time;
    }

}

The event for btn’s obj, it is better by checking only LV_EVENT_CLICKED.

If you want to create toggle button with changing label “ON” or “OFF” on it.
This simple code is example

  lv_obj_t *toggle_btn = lv_btn_create(lv_scr_act(), NULL);
    lv_obj_t *label    = lv_label_create(toggle_btn, NULL);
      lv_label_set_text(label, "OFF");  

    lv_obj_set_user_data  (toggle_btn, label);    
    lv_btn_set_ink_in_time(toggle_btn, 1000);
    lv_btn_set_toggle     (toggle_btn, true);

    lv_obj_set_event_cb   (toggle_btn, [](lv_obj_t* btn, lv_event_t event){
      if(event != LV_EVENT_CLICKED) return;

      lv_obj_t* label = (lv_obj_t*) lv_obj_get_user_data(btn);

      if( lv_btn_get_state(btn) == LV_BTN_STATE_TGL_REL)
        lv_label_set_text(label, "ON");

      else if( lv_btn_get_state(btn) == LV_BTN_STATE_REL)
        lv_label_set_text(label, "OFF");
    });

The “LV_EVENT_CLICKED” is actuated only on release. I want on pressed state not in release state.

And with “LV_EVENT_LONG_PRESSED_REPEAT” the variable increment or decrement repeatedly after “LV_INDEV_LONG_PRESS_TIME”. Repetition time after long press time are equal “LV_INDEV_LONG_PRESS_REP_TIME”.

So when pressing the button, the variable increments “1” or “100” in my case and keeping the button pressed for time “LV_INDEV_LONG_PRESS_TIME”, the button will increment every “LV_INDEV_LONG_PRESS_REP_TIME”. Values ​​in milliseconds.

I never used the “lv_obj_set_user_data” and “lv_obj_set_event_cb”. I’ll take a look.

Thank’s.

Then you want LV_EVENT_PRESSED. There is lots of information about events here.

Yes.
I’m already using LV_EVENT_PRESSED in my code above.

Thank’s,