Help in getting a button click to increment a roller

Important: unclear posts may not receive useful answers.

Before posting

  • Get familiar with Markdown to format and structure your post
  • Be sure to update lvgl from the latest version from the master branch.
  • Read the

Delete this section if you read and applied the mentioned points.

Description

Need help with Button click to increment Roller

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

Win32 Simulator

What do you want to achieve?

I want to be able to click on a button which then increments a roller widget.

What have you tried so far?

Code to reproduce

Add the relevant code snippets here.

The code block(s) should be between ```c and ``` tags:

static void event_handlerHour(lv_event_t* e2)
{
    lv_event_code_t code = lv_event_get_code(e2);
    lv_obj_t* obj2 = (lv_obj_t*)lv_event_get_target(e2);
    if (code == LV_EVENT_VALUE_CHANGED) {
        char buf[32];
        lv_roller_get_selected_str(obj2, buf, sizeof(buf));
        LV_LOG_USER("Selected hour: %s\n", buf);
    }
}

/**
 * An infinite roller with the name of the months
 */
void lv_hour_roller(void)
{
    lv_obj_t* roller1 = lv_roller_create(lv_scr_act());
    lv_roller_set_options(roller1,
        "01\n""02\n""03\n""04\n""05\n""06\n""07\n""08\n""09\n""10\n""11\n""12",
        LV_ROLLER_MODE_INFINITE);

    lv_roller_set_visible_row_count(roller1, 5);
    lv_obj_align(roller1, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_event(roller1, event_handlerHour, LV_EVENT_KEY, NULL);
}

static void event_handler(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if (code == LV_EVENT_CLICKED) {
        LV_LOG_USER("Clicked");
        lv_obj_send_event(obj2, LV_EVENT_CLICKED, NULL); // ERROR HERE, OBJ2 not defined
    }
    else if (code == LV_EVENT_VALUE_CHANGED) {
        LV_LOG_USER("Toggled");
    }
}

void lv_button(void)
{
    lv_obj_t* label;
   
    lv_obj_t* btn1 = lv_btn_create(lv_scr_act());
    lv_obj_align(btn1, LV_ALIGN_BOTTOM_MID, 0, -25);
    lv_obj_add_event(btn1, event_handler, LV_EVENT_CLICKED, NULL);    
    label = lv_label_create(btn1);
    lv_label_set_text(label, "Up");
    lv_obj_center(label);
}

There error is in the event_handler(). OBJ2 is not defined. What is supposed to go here?
I guess I don’t understand how the “lv_obj_send_event()” function is supposed to work. Is there another way to send a button click to the roller? If there is, can some kind soul point me to the part in the docs that explains it? Or maybe an example?

Thanks,
Richard.

You can do this in the button’s click event:

lv_roller_set_selected(roller, lv_roller_get_selected(roller) + 1, LV_ANIM_ON);

Thank you so much for pointing me in the proper direction.

I do have one issue. I am getting an error when I use your suggested line of code " ```
lv_roller_set_selected(roller, lv_roller_get_selected(roller) + 1, LV_ANIM_ON);

The error message says:
lv_roller_get_selected: Asserted at expression: obj != NULL (The object is NULL)

When I try "uint16_t current_hour = lv_roller_get_selected(roller1);" I get the same error.

I don't know if this makes a difference or not but I'm using the Win32 simulator.
Any ideas what is wrong?  Thanks again,

Richard

This works for me in lvgl v9

lv_obj_t* roller;

static void event_handlerHour(lv_event_t* e2)
{
    lv_event_code_t code = lv_event_get_code(e2);
    lv_obj_t* obj2 = (lv_obj_t*)lv_event_get_target(e2);
    if (code == LV_EVENT_VALUE_CHANGED) {
        char buf[32];
        lv_roller_get_selected_str(obj2, buf, sizeof(buf));
        LV_LOG_USER("Selected hour: %s\n", buf);
    }
}

static void button_event_handler(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if (code == LV_EVENT_CLICKED) {
        LV_LOG_USER("Clicked");
        lv_roller_set_selected(roller, lv_roller_get_selected(roller) + 1, LV_ANIM_ON);
    }
    else if (code == LV_EVENT_VALUE_CHANGED) {
        LV_LOG_USER("Toggled");
    }
}

int main () {

   /* Setup */

  roller = lv_roller_create(lv_scr_act());
  lv_roller_set_options(roller,
      "01\n""02\n""03\n""04\n""05\n""06\n""07\n""08\n""09\n""10\n""11\n""12",
      LV_ROLLER_MODE_INFINITE);

  lv_roller_set_visible_row_count(roller, 5);
  lv_obj_align(roller, LV_ALIGN_CENTER, 0, 0);
  lv_obj_add_event(roller, event_handlerHour, LV_EVENT_KEY, NULL);

  lv_obj_t* label;

  lv_obj_t* btn1 = lv_btn_create(lv_scr_act());
  lv_obj_align(btn1, LV_ALIGN_BOTTOM_MID, 0, -25);
  lv_obj_add_event(btn1, button_event_handler, LV_EVENT_CLICKED, NULL);
  label = lv_label_create(btn1);
  lv_label_set_text(label, "Up");
  lv_obj_center(label);

  /* Infinite loop */



}
1 Like

Thanks Carlos. You saved me a ton frustration.