This tutorial demonstrates how to use the STM32F429I-DISC1 Discovery kit to test LVGL's hardware button capability. The board has two push-buttons: USER and RESET [1]. We'll use the USER push-button, which is connected to I/O PA0 of the STM32F429ZIT6 [2]. The board also has six LEDs, and we'll use LD3 (green LED), which is connected to I/O PG13 of the STM32F429ZIT6 [2].
Our goal is to toggle the LD3 LED when both the hardware button (USER push-button) and the UI button are clicked.
Hardware Button Driver Registration#
Our hardware button driver register function is:
void my_button_init(void)
{
static lv_indev_t *indev;
lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv);
indev_drv.read = my_input_read;
indev_drv.type = LV_INDEV_TYPE_BUTTON;
indev = lv_indev_drv_register(&indev_drv);
static lv_point_t points_array[] = { { 20, 20 } };
lv_indev_set_button_points(indev, points_array);
}cWhere:
type:LV_INDEV_TYPE_BUTTON- External (hardware button) which is assigned to a specific point on the screenread:my_input_read- Function that reads the hardware button status (press/release)points_array: These points will be assigned to the buttons to press a specific point on the screen
Creating the UI Button#
We create a button on the screen. The points_array's first element (20px, 20px) is selected at any point within the UI button's area (x: 0-40px, y: 0-40px).
static lv_obj_t *btn = lv_btn_create(lv_scr_act(), NULL);
lv_obj_set_size(btn, 40, 40);
lv_obj_set_pos(btn, 0, 0);
lv_btn_set_action(btn, LV_BTN_ACTION_CLICK, btn_click_action);cButton Click Action#
Our btn_click_action toggles only the LD3 LED:
static lv_res_t btn_click_action(lv_obj_t *btn) {
HAL_GPIO_TogglePin(GPIOG, GPIO_PIN_13);
}cReading Hardware Button Data#
The my_btn_read function reads the hardware button data and returns the hardware button ID:
uint8_t my_btn_read() {
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 0) {
return 1;
} else {
return 0;
}
}cInput Read Function#
The my_input_read function returns true if there is still data to be read (buffered), otherwise false:
bool my_input_read(lv_indev_data_t *data){
static int8_t last_btn = 0; /* Store the last pressed button */
int8_t btn_pr = my_btn_read(); /* Get the ID (0,1,2...) of the pressed button */
if(btn_pr > 0) { /* Is there a button press? */
last_btn = btn_pr; /* Save the ID of the pressed button */
data->state = LV_INDEV_STATE_PR; /* Set the pressed state */
} else {
data->state = LV_INDEV_STATE_REL; /* Set the released state */
}
data->btn = last_btn; /* Set the last button */
return false; /* No buffering so no more data read */
}cInitialization in Main#
In the last step, we call button_init:
main() {
// GPIO Init: LED, Button
// LittlevGL
button_init();
while(1) {
// ...
}
}cA complete project example will be shared in a future update.
Acknowledgment#
We prepared this tutorial with Orhan Gunduz.
