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

RT thread simulator lvgl control: switch switch button control

2022-06-13 06:59:00 Zhangshizheng

Preface

  • switch button : Switch button control , It also uses a lot , Here, I am familiar with the relevant operations

  • switch There are two states : On and off

Environment building

  • RT-Thread 4.1.0 Or the latest version

  • BSP Simulator simulator

  • LVGL software package

function

  • LVGL Draw a picture Switch Switch button , Draw a picture The label shows the status of the switch button

Operation method

  • Here it is LVGL In the official routine , eureka switch Operation of space , A little familiar with Related large API, To write switch Button test code

  • Storage location :simulator\applications\lvgl\demo\lv_switch_01.c, The code is as follows

#include <rtthread.h>
#include <lvgl.h>

static lv_obj_t *lbl_status = NULL;

static void sw_event_cb(lv_event_t *e)
{
    
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *sw = lv_event_get_target(e);

    if (lv_obj_has_state(sw, LV_STATE_CHECKED))
    {
    
        rt_kprintf("%s : power on \r\n", __func__);
        lv_label_set_text(lbl_status, "ON");
        lv_obj_align_to(lbl_status, sw, LV_ALIGN_OUT_RIGHT_MID, 20, 0);    /*Align top of the slider*/
    }
    else
    {
    
        lv_label_set_text(lbl_status, "OFF");
        lv_obj_align_to(lbl_status, sw, LV_ALIGN_OUT_RIGHT_MID, 20, 0);    /*Align top of the slider*/
        rt_kprintf("%s : power off \r\n", __func__);
    }
}

/** * Show an example to switch button */
void lv_switch_01_init(void)
{
    
    static rt_bool_t power_flag = RT_FALSE;

    lv_obj_t *sw = lv_switch_create(lv_scr_act());
    lv_obj_align(sw, LV_ALIGN_TOP_LEFT, 50, 20);
    lv_obj_add_event_cb(sw, sw_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
    lv_obj_clear_state(sw, LV_STATE_CHECKED);

    /* Create a label */
    lbl_status = lv_label_create(lv_scr_act());
    lv_label_set_text(lbl_status, "OFF");
    lv_obj_align_to(lbl_status, sw, LV_ALIGN_OUT_RIGHT_MID, 20, 0);
}
  • Call location :simulator\packages\LVGL-latest\env_support\rt-thread\lv_rt_thread_port.c, It can also be placed in other places , Add the code file to visual studio 2022 In our project

 Insert picture description here

Compile and run  Insert picture description here

  • Add to RT-Thread Simulator Visual Studio 2022 In our project , Compile and run , The effect is as follows :

switch Switch button API

  • Set up switch Default on state :lv_obj_add_state(sw, LV_STATE_CHECKED);

  • Set up switch Off by default ( gray ) state :lv_obj_clear_state(sw, LV_STATE_CHECKED);

  • switch Whether it is on :lv_obj_has_state(sw, LV_STATE_CHECKED)

  • Set up switch State change callback function :lv_obj_add_event_cb(sw, sw_event_cb, LV_EVENT_VALUE_CHANGED, NULL);

Summary

  • Switch button Switch Widely used , Such as some power control, etc

  • Use RT-Thread Simulator debugging LVGL, More efficient and convenient , And related code , It is easy to transplant to embedded hardware circuit board

原网站

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