How to use lv_indev_drv_t with hardware buttons?

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.
  • Be sure you have checked the FAQ and read the relevant part of the documentation.
  • If applicable use the Simulator to eliminate hardware related issues.

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

Description

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

STM32L476RG

What LVGL version are you using?

v8.3

What do you want to achieve?

I want to convert it to another image when I press the button. However, read_cb does not work when the button is pressed.

What have you tried so far?

Code to reproduce

Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.

The code block(s) should be formatted like:

/*You code here*/

void button(void)
{
	static lv_indev_t *indev;
	lv_indev_drv_t indev_drv;

	//Register a button input device
	lv_indev_drv_init(&indev_drv);

	indev_drv.type = LV_INDEV_TYPE_BUTTON;
	indev_drv.read_cb = input_read;

	indev = lv_indev_drv_register(&indev_drv);

	 static const lv_point_t btn_points_1[1] = {
	        {120, 90}   //Button 0 -> x:10; y:10//
	    };
	    lv_indev_set_button_points(indev_button_1, btn_points_1);

	lv_obj_t *btn = lv_btn_create(lv_scr_act());
		lv_obj_set_size(btn, 40, 40);
		lv_obj_set_pos(btn, 0, 0);
	    lv_obj_add_event_cb(btn, btn_event_handler, LV_EVENT_ALL, NULL);
}

static int8_t button_get_pressed_id_1(void)
{
    uint8_t i;

    //Check to buttons see which is being pressed (assume there are 2 buttons)//
    for(i = 0; i < 1; i++) {
        //Return the pressed button's ID//
        if(button_is_pressed_1(i)) {
            return i;
        }
    }

    //No button pressed//
    return -1;
}

//Test if `id` button is pressed or not//
static bool button_is_pressed_1(uint8_t id)
{

    if(id == 0)
    {
      if(HAL_GPIO_ReadPin(USER_BUTTON_GPIO_Port, USER_BUTTON_Pin) == GPIO_PIN_RESET)
      {
        return true;
      }

      else if(HAL_GPIO_ReadPin(USER_BUTTON_GPIO_Port, USER_BUTTON_Pin) == GPIO_PIN_SET)
    	  return false;
    }


    return false;
}
uint8_t btn_read(void) {

  if(HAL_GPIO_ReadPin(USER_BUTTON_GPIO_Port, USER_BUTTON_Pin) == 0){
	  HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_SET);
	  return 1;
  }
  else
	  return 0;

}


static void btn_event_handler(lv_event_t * e)
{

    //Original Code
    lv_event_code_t code = lv_event_get_code(e);

    if(code == LV_EVENT_PRESSED)
    {
      //Turn the LED ON
    	HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_SET);
//      lv_led_on(led1);
      lv_label_set_text(label, "Pressed");                    //Set the labels text//
          lv_obj_center(label);
//      lv_label_set_text(labelCounter, "Something Pressed !!!"); //For  debugging
    }

    else if(code == LV_EVENT_RELEASED)
    {
      //Turn the LED OFF
    	HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_RESET);
//      lv_led_off(led1);
      lv_label_set_text(label, "Nothing Pressed ");                     //Set the labels text//
          lv_obj_center(label);
//      lv_label_set_text(labelCounter, "Nothing Pressed"); //For  debugging
    }

}

Screenshot and/or video

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