当前位置:网站首页>RT thread simulator lvgl control: button button event

RT thread simulator lvgl control: button button event

2022-06-13 07:06:00 Zhangshizheng

Environment building

  • RT-Thread 4.1.0
  • BSP Simulator simulator
  • LVGL software package

function

  • LVGL Draw a button , Click button , Generate button event

Method

  • Button control ,LVGL There are official routines , It can be directly used for the verification of button events
  • route :simulator\packages\LVGL-latest\examples\get_started\lv_example_get_started_1.c
  • Operation method : hold lv_example_get_started_1.c Copy to : simulator\applications\lvgl\demo\lv_btn_01.c , Then simply modify , Add to visual studio 2022 Chinese compiler , Pay attention to modifying the header file
#include <rtthread.h>
#include "lvgl.h"

static void btn_event_cb(lv_event_t *e)
{
    
    static uint32_t cnt = 0;

    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *btn = lv_event_get_target(e);
    if (code == LV_EVENT_CLICKED)
    {
    
        cnt++;
        /*Get the first child of the button which is the label and change its text*/
        lv_obj_t *label = lv_obj_get_child(btn, 0);
        lv_label_set_text_fmt(label, "button: %d", cnt);
        rt_kprintf("%s : click count = %d\r\n", __func__, cnt);
    }
}

/** * Create a button with a label and react on click event. */
void lv_btn_01_init(void)
{
    
    lv_obj_t *btn = lv_btn_create(lv_scr_act());     /*Add a button the current screen*/
    lv_obj_set_pos(btn, 10, 10);                            /*Set its position*/
    lv_obj_set_size(btn, 120, 50);                          /*Set its size*/
    lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_ALL, NULL);           /*Assign a callback to the button*/

    lv_obj_t *label = lv_label_create(btn);          /*Add a label to the button*/
    lv_label_set_text(label, "button");                     /*Set the labels text*/
    lv_obj_center(label);
}

 Insert picture description here

Compile operation

  • Run discovery , After clicking the button , The callback function of the button executes

 Insert picture description here

Summary

  • Continue to study LVGL Use of various controls for , Later, it can be used in embedded products
原网站

版权声明
本文为[Zhangshizheng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130653553858.html