"Manually" trigger a button press and release rendering

Hi,

Description

Maybe a newbie question.

Is there any command for “Manually” press a button ?

I would like to simulate a button press remotely, for example without using vnc, ie: send a remote command via wifi.

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

ESP32

What do you want to achieve?

“Manually” trigger a button press and release rendering.

Thank’s.

lv_btn_toggle will take care of that for you.

Or lv_btn_set_state() but you still need to call lv_event_send(btn, LV_EVENT_CLICK/PRESS/RELEASE/..., NULL)

I assume you mean lv_btn_set_state?

Yes, updated, thank you!

Hi,

I tried as follows.

In btn1 callback function “event_handler_btn1”, variable “btn1_state” are returning, when my finger toggle the button on the display screen:
// returning 3 and 0 when pressed. returning 1 and 2 when pressed again.
// pressed again returning 3 and 0. pressed again returning 1 and 2 .
// and so on.

In function “toggle_manually”, variable “btn1_state” are returning, when the function is called continuously(only a test):
// first function call returning 0 (LV_BTN_STATE_REL).
// second function call returning 2 (LV_BTN_STATE_TGL_REL).
// and so on.

why not returning the same values ​​in the two functions ?

I would like to use both functions(“event_handler_btn” and “toggle_manually”) in the application.
Is the state of the button(btn1) synchronized in both functions ?
LV_BTN_STATE_TGL_REL
LV_BTN_STATE_TGL_PR

BTN1 create:

    static lv_obj_t * btn1 = NULL;       // global variable.
    static lv_obj_t * label1 = NULL;     // global variable.
    
    btn1 = lv_btn_create(lv_scr_act(), NULL);
    lv_obj_set_event_cb(btn1, event_handler_btn1);      
    lv_obj_set_size(btn1, 300, 140);    
    lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, -160);  
    lv_btn_set_toggle(btn1, true);
    lv_btn_toggle(btn1);
       
    label1 = lv_label_create(btn1, NULL);
    lv_label_set_text(label1, "BUTTON 1 - OFF");   

    lv_obj_set_opa_scale_enable(btn1, true);
    lv_obj_set_opa_scale(btn1, 100);
 
    lv_btn_set_ink_in_time(btn1, 1000);
    lv_btn_set_ink_wait_time(btn1, 100);
    lv_btn_set_ink_out_time(btn1, 100);

BTN1 callback event_handler:

static void event_handler_btn1(lv_obj_t * button1, lv_event_t event_bt1)
{
    lv_btn_state_t btn1_state = lv_btn_get_state(button1);

    //printf("btn1_state = %d\n", btn1_state );

    // returning 3 and 0 when pressed. returning 1 and 2 when pressed again.
    // pressed again returning 3 and 0. pressed again returning 1 and 2...........................


    if ( btn1_state == LV_BTN_STATE_TGL_REL )              
    {   
        lv_label_set_text(label1, "BUTTON 1 - OFF"); 
    }

    else if ( btn1_state == LV_BTN_STATE_TGL_PR )
    {      
        lv_label_set_text(label1, "BUTTON 1 - ON");  
    }      

    // printf("event_bt1 = %d\n", event_bt1 );
}

BTN1 toggle “manually”:

static void toggle_manually( )
{
    lv_btn_toggle( btn1 );      
    
    lv_btn_state_t btn1_state = lv_btn_get_state( btn1 );    

    printf("btn1_state = %d\n", btn1_state );
    
    // returning 0 (LV_BTN_STATE_REL) and 2 (LV_BTN_STATE_TGL_REL).


    if ( btn1_state == LV_BTN_STATE_TGL_REL )    // LV_BTN_STATE_TGL_REL = 2               
    {        
        lv_label_set_text(label1, "BUTTON 1 - OFF");                
    }
    else if ( btn1_state == LV_BTN_STATE_TGL_PR )    // LV_BTN_STATE_TGL_PR = 3        
    {      
        lv_label_set_text(label1, "BUTTON 1 - ON");  
    }  
}

Thank’s.

Both TGL_PR and TGL_REL are considered ON. TGL_PR in other words ON_PRESSED.

So the conditions should be

if(state == LV_BTN_STATE_TGL_REL || state == LV_BTN_STATE_TGL_PR) {... ON ...}
if(state == LV_BTN_STATE_REL || state == LV_BTN_STATE_PR) {... OFF ...}

Sorry Kisvegabor.

I didn’t understand.

In the function below is ok for me,

static void event_handler_btn1(lv_obj_t * button1, lv_event_t event_bt1)
{
    lv_btn_state_t btn1_state = lv_btn_get_state(button1);
    
    //printf("btn1_state = %d\n", btn1_state );

    // returning 3 and 0 when pressed. returning 1 and 2 when pressed again.
    // pressed again returning 3 and 0. pressed again returning 1 and 2...........................
    
    if ( btn1_state == LV_BTN_STATE_TGL_REL )              
    {        
        lv_label_set_text(label1, "BUTTON 1 - OFF");                
    }
    else if ( btn1_state == LV_BTN_STATE_TGL_PR )        
    {      
        lv_label_set_text(label1, "BUTTON 1 - ON");  
    } 
}

But in this function are strange:

static void toggle_manually( )
{
    lv_btn_toggle( btn1 );      
    
    lv_btn_state_t btn1_state = lv_btn_get_state( btn1 );
    

    // printf("btn1_state = %d\n", btn1_state );
    
    // returning 0(LV_BTN_STATE_REL) and 2(LV_BTN_STATE_TGL_REL).


    if ( btn1_state == LV_BTN_STATE_TGL_REL )              
    {        
        lv_label_set_text(label1, "BUTTON 1 - OFF");                
    }
    else if ( btn1_state == LV_BTN_STATE_REL )   // LV_BTN_STATE_TGL_PR       
    {      
        lv_label_set_text(label1, "BUTTON 1 - ON");  
    }  

}

In this function,
“static void toggle_manually( )”

“btn1_state” only returning 0(LV_BTN_STATE_REL) and 2(LV_BTN_STATE_TGL_REL).
Two states of type “REL”, released.
Shouldn’t be a rel(release) state and pr(pressed) state ?

For me, it would be more logical if “btn1_state” returned 0(LV_BTN_STATE_REL) and 1(LV_BTN_STATE_PR).

or

if “btn1_state” returned 2(LV_BTN_STATE_TGL_REL) and 3(LV_BTN_STATE_TGL_PR).

Thank’s.

Ah, I see. It happens simply because lv_btn_toggle() toggles between “normal” and “toggled” state. This is how it works.

A typical use-case for it would be a “Start/Stop” button. Let’s say you called lv_btn_set_toggle(btn, true) to make the button “toggle” on release. So the button is in REL state (“Stop”) by default. If you press it, it goes to PR state. When you release it should go to TGL_REL state (“Start”). And you can manually switch between Start and Stop with lv_btn_toggle.

If you need more precise control on states you can use lv_btn_set_state().