lvgl8.4
代码如下
#include <lvgl.h> #include <Arduino_GFX_Library.h> #include "bsp_cst816.h" #include "ui/ui.h" #define EXAMPLE_PIN_NUM_LCD_SCLK 39 #define EXAMPLE_PIN_NUM_LCD_MOSI 38 #define EXAMPLE_PIN_NUM_LCD_MISO 40 #define EXAMPLE_PIN_NUM_LCD_DC 42 #define EXAMPLE_PIN_NUM_LCD_RST -1 #define EXAMPLE_PIN_NUM_LCD_CS 45 #define EXAMPLE_PIN_NUM_LCD_BL 1 #define EXAMPLE_PIN_NUM_TP_SDA 48 #define EXAMPLE_PIN_NUM_TP_SCL 47 #define LEDC_FREQ 5000 #define LEDC_TIMER_10_BIT 10 #define EXAMPLE_LCD_ROTATION 0 #define EXAMPLE_LCD_H_RES 240 #define EXAMPLE_LCD_V_RES 320 Arduino_DataBus *bus = new Arduino_ESP32SPI( EXAMPLE_PIN_NUM_LCD_DC /* DC */, EXAMPLE_PIN_NUM_LCD_CS /* CS */, EXAMPLE_PIN_NUM_LCD_SCLK /* SCK */, EXAMPLE_PIN_NUM_LCD_MOSI /* MOSI */, EXAMPLE_PIN_NUM_LCD_MISO /* MISO */); Arduino_GFX *gfx = new Arduino_ST7789( bus, EXAMPLE_PIN_NUM_LCD_RST /* RST */, EXAMPLE_LCD_ROTATION /* rotation */, true /* IPS */, EXAMPLE_LCD_H_RES /* width */, EXAMPLE_LCD_V_RES /* height */); uint32_t screenWidth; uint32_t screenHeight; uint32_t bufSize; lv_disp_draw_buf_t draw_buf; lv_color_t *disp_draw_buf; lv_disp_drv_t disp_drv; void my_disp_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p) { lv_disp_flush_ready(disp_drv); } void my_touchpad_read(lv_indev_drv_t *indev_drv, lv_indev_data_t *data) { uint16_t touchpad_x; uint16_t touchpad_y; bsp_touch_read(); if (bsp_touch_get_coordinates(&touchpad_x, &touchpad_y)) { data->point.x = touchpad_x; data->point.y = touchpad_y; data->state = LV_INDEV_STATE_PRESSED; } else { data->state = LV_INDEV_STATE_RELEASED; } } void setup() { Serial.begin(115200); Serial.println("Arduino_GFX LVGL_Arduino_v8 example "); if (!gfx->begin()) { Serial.println("gfx->begin() failed!"); } gfx->fillScreen(BLACK); #ifdef EXAMPLE_PIN_NUM_LCD_BL ledcAttach(EXAMPLE_PIN_NUM_LCD_BL, LEDC_FREQ, LEDC_TIMER_10_BIT); ledcWrite(EXAMPLE_PIN_NUM_LCD_BL, (1 << LEDC_TIMER_10_BIT) / 100 * 80); #endif Wire.begin(EXAMPLE_PIN_NUM_TP_SDA, EXAMPLE_PIN_NUM_TP_SCL); bsp_touch_init(&Wire, gfx->getRotation(), gfx->width(), gfx->height()); lv_init(); screenWidth = gfx->width(); screenHeight = gfx->height(); bufSize = screenWidth * screenHeight; disp_draw_buf = (lv_color_t *)heap_caps_malloc(bufSize * 2, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); if (!disp_draw_buf) { disp_draw_buf = (lv_color_t *)heap_caps_malloc(bufSize * 2, MALLOC_CAP_8BIT); } if (!disp_draw_buf) { Serial.println("LVGL disp_draw_buf allocate failed!"); } else { lv_disp_draw_buf_init(&draw_buf, disp_draw_buf, NULL, bufSize); lv_disp_drv_init(&disp_drv); disp_drv.hor_res = screenWidth; disp_drv.ver_res = screenHeight; disp_drv.flush_cb = my_disp_flush; disp_drv.draw_buf = &draw_buf; disp_drv.direct_mode = true; lv_disp_drv_register(&disp_drv); static lv_indev_drv_t indev_drv; lv_indev_drv_init(&indev_drv); indev_drv.type = LV_INDEV_TYPE_POINTER; indev_drv.read_cb = my_touchpad_read; lv_indev_drv_register(&indev_drv); } Serial.println("Setup done"); ui_init(); } void loop() { lv_timer_handler(); ui_tick(); #if (LV_COLOR_16_SWAP != 0) gfx->draw16bitBeRGBBitmap(0, 0, (uint16_t *)disp_draw_buf, screenWidth, screenHeight); #else gfx->draw16bitRGBBitmap(0, 0, (uint16_t *)disp_draw_buf, screenWidth, screenHeight); #endif delay(5); }