#include "public.h" #include "esp_lcd_st7735.h" #include "driver/gpio.h" #include "driver/spi_master.h" #include "esp_timer.h" #include "esp_lcd_panel_io.h" #include "esp_lcd_panel_vendor.h" #include "esp_lcd_panel_ops.h" #include "esp_lvgl_port.h" #include "gui_guider.h" #include "custom.h" #include "button_gpio.h" #include "iot_button.h" #include "esp_lvgl_port_knob.h" static const char *TAG = "lcd"; lv_ui guider_ui; /* LCD IO and panel */ esp_lcd_panel_io_handle_t io_handle = NULL; esp_lcd_panel_handle_t panel_handle = NULL; /* LCD size */ #define EXAMPLE_LCD_H_RES (128) #define EXAMPLE_LCD_V_RES (160) /* LCD settings */ #define EXAMPLE_LCD_SPI_NUM (SPI2_HOST) #define EXAMPLE_LCD_PIXEL_CLK_HZ (40 * 1000 * 1000) #define EXAMPLE_LCD_CMD_BITS (8) #define EXAMPLE_LCD_PARAM_BITS (8) #define EXAMPLE_LCD_COLOR_SPACE (ESP_LCD_COLOR_SPACE_BGR) #define EXAMPLE_LCD_BITS_PER_PIXEL (16) #define EXAMPLE_LCD_DRAW_BUFF_DOUBLE (1) #define EXAMPLE_LCD_DRAW_BUFF_HEIGHT (50) #define EXAMPLE_LCD_BL_ON_LEVEL (1) /* LVGL display and touch */ static lv_display_t *lvgl_disp = NULL; lv_indev_t *lvgl_button_indev = NULL; #define BUTTON_COUNT 3 // 假设有 3 个按钮 #define BUTTON_ACTIVE_LEVEL 0 static button_handle_t buttons[BUTTON_COUNT]; // 存储按钮句柄 static const uint32_t button_pins[BUTTON_COUNT] = {GPIO_NUM_0, GPIO_NUM_40, GPIO_NUM_41}; // 按钮 GPIO lv_group_t *Key_Matrix_Group; void button_event_cb(void *arg, void *usr_data) { iot_button_print_event((button_handle_t)arg); uint32_t btn_idx = (uint32_t)usr_data; // 获取按钮索引 printf("Button %ld\n", btn_idx); } void button_init_all() { for (int i = 0; i < BUTTON_COUNT; i++) { button_config_t btn_cfg; memset(&btn_cfg, 0, sizeof(button_config_t)); button_gpio_config_t gpio_cfg = { .gpio_num = button_pins[i], .active_level = BUTTON_ACTIVE_LEVEL, .enable_power_save = true, .disable_pull = false, }; esp_err_t ret = iot_button_new_gpio_device(&btn_cfg, &gpio_cfg, &buttons[i]); assert(ret == ESP_OK); // 绑定事件回调,并传递按钮索引 // ret = iot_button_register_cb(buttons[i], BUTTON_PRESS_DOWN, NULL, button_event_cb, (void *)i); // ret |= iot_button_register_cb(buttons[i], BUTTON_PRESS_UP, NULL, button_event_cb, (void *)i); // ret |= iot_button_register_cb(buttons[i], BUTTON_PRESS_REPEAT, NULL, button_event_cb, (void *)i); // ret |= iot_button_register_cb(buttons[i], BUTTON_SINGLE_CLICK, NULL, button_event_cb, (void *)i); // ret |= iot_button_register_cb(buttons[i], BUTTON_DOUBLE_CLICK, NULL, button_event_cb, (void *)i); // ret |= iot_button_register_cb(buttons[i], BUTTON_LONG_PRESS_START, NULL, button_event_cb, (void *)i); // ret |= iot_button_register_cb(buttons[i], BUTTON_LONG_PRESS_HOLD, NULL, button_event_cb, (void *)i); // ret |= iot_button_register_cb(buttons[i], BUTTON_LONG_PRESS_UP, NULL, button_event_cb, (void *)i); // ret |= iot_button_register_cb(buttons[i], BUTTON_PRESS_END, NULL, button_event_cb, (void *)i); // ESP_ERROR_CHECK(ret); } } static esp_err_t app_lvgl_init(void) { /* Initialize LVGL */ const lvgl_port_cfg_t lvgl_cfg = { .task_priority = 4, /* LVGL task priority */ .task_stack = 4096, /* LVGL task stack size */ .task_affinity = -1, /* LVGL task pinned to core (-1 is no affinity) */ .task_max_sleep_ms = 500, /* Maximum sleep in LVGL task */ .timer_period_ms = 5 /* LVGL timer tick period in ms */ }; ESP_RETURN_ON_ERROR(lvgl_port_init(&lvgl_cfg), TAG, "LVGL port initialization failed"); /* Add LCD screen */ ESP_LOGD(TAG, "Add LCD screen"); const lvgl_port_display_cfg_t disp_cfg = { .io_handle = io_handle, .panel_handle = panel_handle, .buffer_size = EXAMPLE_LCD_H_RES * EXAMPLE_LCD_DRAW_BUFF_HEIGHT, .double_buffer = EXAMPLE_LCD_DRAW_BUFF_DOUBLE, .hres = EXAMPLE_LCD_H_RES, .vres = EXAMPLE_LCD_V_RES, .monochrome = false, #if LVGL_VERSION_MAJOR >= 9 .color_format = LV_COLOR_FORMAT_RGB565, #endif .rotation = { .swap_xy = false, .mirror_x = false, .mirror_y = false, }, .flags = { .buff_dma = true, #if LVGL_VERSION_MAJOR >= 9 .swap_bytes = true, #endif } }; lvgl_disp = lvgl_port_add_disp(&disp_cfg); /* Add KEY input (for selected screen) */ const lvgl_port_nav_btns_cfg_t button_cfg = { .disp = lvgl_disp, .button_prev = buttons[2], .button_enter = buttons[1], .button_next = buttons[0], }; lvgl_button_indev = lvgl_port_add_navigation_buttons(&button_cfg); #if USE_KEYBOARD keyboard_init(); lv_indev_drv_t indev_drv_keyboard; lv_indev_drv_init(&indev_drv_keyboard); /*Basic initialization*/ indev_drv_keyboard.type = LV_INDEV_TYPE_KEYPAD; indev_drv_keyboard.read_cb = keyboard_read; //lv_gesture_dir_t lv_indev_get_gesture_dir(const lv_indev_t * indev) indev_drv_keyboard.disp = disp_300x240; lv_indev_t* indev_keyboard = lv_indev_drv_register(&indev_drv_keyboard); lv_indev_drv_update(indev_keyboard,&indev_drv_keyboard); Key_Matrix_Group = lv_group_create(); //创建组 lv_indev_set_group(indev_keyboard, Key_Matrix_Group); //将组绑定到输入设备 #endif //ui->screen_btn_1 return ESP_OK; } static void app_main_display(void) { custom_init(&guider_ui); setup_ui(&guider_ui); // events_init(&guider_ui); lv_indev_set_group(lvgl_button_indev, guider_ui.user_obj.home_btn_group); //将组绑定到输入设备 lv_group_add_obj(guider_ui.user_obj.home_btn_group,guider_ui.screen_btn_1); lv_group_add_obj(guider_ui.user_obj.home_btn_group,guider_ui.screen_btn_2); lv_group_add_obj(guider_ui.user_obj.home_btn_group,guider_ui.screen_btn_3); /* Task unlock */ lvgl_port_unlock(); } void Gui_Task(void *pvParameter) { //esp_lcd_panel_handle_t panel_handle = NULL; (void)pvParameter; ESP_LOGI(TAG, "Initialize SPI bus"); spi_bus_config_t buscfg = st7735_PANEL_BUS_SPI_CONFIG(GPIO_NUM_36,GPIO_NUM_35,128*160*2); ESP_ERROR_CHECK(spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO)); ESP_LOGI(TAG, "Install panel IO"); esp_lcd_panel_io_spi_config_t io_config = st7735_PANEL_IO_SPI_CONFIG(GPIO_NUM_34,GPIO_NUM_38,NULL,NULL); ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)SPI2_HOST, &io_config, &io_handle)); ESP_LOGI(TAG, "Install st7735 panel driver"); esp_lcd_panel_dev_config_t panel_config = { .reset_gpio_num = GPIO_NUM_37, .rgb_endian = LCD_RGB_ENDIAN_RGB, .bits_per_pixel = 16, .color_space = ESP_LCD_COLOR_SPACE_RGB, }; ESP_ERROR_CHECK(esp_lcd_new_panel_st7735(io_handle, &panel_config, &panel_handle)); ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle)); ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle)); ESP_LOGI(TAG, "Turn on LCD backlight"); esp_lcd_panel_swap_xy(panel_handle, false); esp_lcd_panel_mirror(panel_handle, false, false);//设置旋转 esp_lcd_panel_invert_color(panel_handle,true);//设置反色 // uint8_t* lcd_data; // lcd_data = malloc(128*160*2); // memset(&lcd_data[0],0x00,128*160*2); // esp_lcd_panel_draw_bitmap(panel_handle, 0, 0, 64, 80, &lcd_data[0]); // memset(&lcd_data[0],0xff,128*160*2); // esp_lcd_panel_draw_bitmap(panel_handle, 30, 80, 128, 160, &lcd_data[0]); // vTaskDelay(1000 / portTICK_PERIOD_MS); button_init_all(); app_lvgl_init(); app_main_display(); vTaskDelete(NULL); }