|
|

楼主 |
发表于 2025-5-29 12:00:28
|
显示全部楼层
换个姿式继续点灯, LVGL9 触摸 开关 控制LED.
- #include <unistd.h>
- #include <pthread.h>
- #include <time.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "lvgl/lvgl.h"
- #include "lvgl/src/core/lv_global.h"
-
- #define GPIO_PIN "199" // 假设控制的 GPIO 引脚编号为 PG7
- #define GPIO_PATH "/sys/class/gpio/gpio" GPIO_PIN
- lv_obj_t * switch1;
- //---------- 初始化 GPIO 引脚
- void init_gpio(void) {
- // 导出 GPIO 引脚
- FILE *fp = fopen("/sys/class/gpio/export", "w");
- if (fp) {
- fprintf(fp, "%s", GPIO_PIN);
- fclose(fp);
- }
-
- // 设置 GPIO 引脚为输出模式
- fp = fopen(GPIO_PATH "/direction", "w");
- if (fp) {
- fprintf(fp, "out");
- fclose(fp);
- }
- }
- //----------- 设置 GPIO 引脚的电平
- void set_gpio_level(bool level) {
- FILE *fp = fopen(GPIO_PATH "/value", "w");
- if (fp) {
- fprintf(fp, "%d", level ? 1 : 0);
- fclose(fp);
- }
- }
- //-------- 定义事件回调函数
- void switch_event_cb(lv_event_t * e) {
- bool is_on = lv_obj_has_state(switch1, LV_STATE_CHECKED);
- printf("LED %s \n",is_on?"ON":"OFF");
- set_gpio_level(!is_on); // GPIO 低电平为 ON
- }
- //-----------------------------------------
- static void lv_linux_disp_init(void)
- {
- const char *device = "/dev/fb0";// LCD
- lv_display_t * disp = lv_linux_fbdev_create();
- //lv_linux_init_input_pointer(disp);
- lv_indev_t *touch = lv_evdev_create(LV_INDEV_TYPE_POINTER, "/dev/input/event1");
- lv_indev_set_display(touch, disp);
- lv_linux_fbdev_set_file(disp, device);
- }
- //------------------------------------------------------
- void lv_linux_run_loop(void)
- {
- uint32_t idle_time;
- /*Handle LVGL tasks*/
- while(1) {
- idle_time = lv_timer_handler(); /*Returns the time to the next timer execution*/
- usleep(idle_time * 1000);
- }
- }
-
- //--------------------------------------------------------
- int main(int argc, char **argv)
- {
-
- /* Initialize LVGL. */
- lv_init();
- /* Initialize the configured backend SDL2, FBDEV, libDRM or wayland */
- lv_linux_disp_init();
-
- // 创建父容器
- lv_obj_t * parent = lv_obj_create(lv_scr_act());
- lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN); // 设置为垂直布局
- lv_obj_set_flex_align(parent, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
- lv_obj_set_size(parent, 1024, 400);
- // 创建一个新的样式
- static lv_style_t style_border;
- lv_style_init(&style_border);
- // 设置边框宽度
- lv_style_set_border_width(&style_border, 1); // 设置边框宽度为1像素
- // 设置边框颜色
- lv_style_set_border_color(&style_border, lv_color_hex(0xff0000));//红色
- lv_obj_add_style(parent, &style_border, LV_PART_MAIN);
- // 创建
- switch1 = lv_switch_create(parent);
- lv_obj_set_size(switch1, 200, 100);
- lv_obj_add_event_cb(switch1, switch_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
- lv_obj_add_style(switch1, &style_border, LV_PART_MAIN);
-
- init_gpio();
- lv_linux_run_loop();
- return 0;
- }
复制代码 |
|