From b74d34407fa5f332b8447e46a971e5e05cf9bd5a Mon Sep 17 00:00:00 2001 From: Klaus Musch Date: Sat, 6 Apr 2024 12:10:32 +0200 Subject: [PATCH] feat(example): make "LVGL_Arduino.ino" easier to use (#6001) --- .../arduino/LVGL_Arduino/LVGL_Arduino.ino | 7 +++-- src/drivers/display/tft_espi/lv_tft_espi.cpp | 29 ++++++++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/examples/arduino/LVGL_Arduino/LVGL_Arduino.ino b/examples/arduino/LVGL_Arduino/LVGL_Arduino.ino index 04a79f9d1..94ec0c854 100644 --- a/examples/arduino/LVGL_Arduino/LVGL_Arduino.ino +++ b/examples/arduino/LVGL_Arduino/LVGL_Arduino.ino @@ -1,5 +1,5 @@ /*Using LVGL with Arduino requires some extra steps: - *Be sure to read the docs here: https://docs.lvgl.io/master/get-started/platforms/arduino.html */ + *Be sure to read the docs here: https://docs.lvgl.io/master/integration/framework/arduino.html */ #include @@ -15,9 +15,10 @@ //#include //#include -/*Set to your screen resolution*/ +/*Set to your screen resolution and rotation*/ #define TFT_HOR_RES 320 #define TFT_VER_RES 240 +#define TFT_ROTATION LV_DISPLAY_ROTATION_0 /*LVGL draw into this buffer, 1/10 screen size usually works well. The size is in bytes*/ #define DRAW_BUF_SIZE (TFT_HOR_RES * TFT_VER_RES / 10 * (LV_COLOR_DEPTH / 8)) @@ -95,6 +96,8 @@ void setup() #if LV_USE_TFT_ESPI /*TFT_eSPI can be enabled lv_conf.h to initialize the display in a simple way*/ disp = lv_tft_espi_create(TFT_HOR_RES, TFT_VER_RES, draw_buf, sizeof(draw_buf)); + lv_display_set_rotation(disp, TFT_ROTATION); + #else /*Else create a display yourself*/ disp = lv_display_create(TFT_HOR_RES, TFT_VER_RES); diff --git a/src/drivers/display/tft_espi/lv_tft_espi.cpp b/src/drivers/display/tft_espi/lv_tft_espi.cpp index 64e3ee6ab..0b561973c 100644 --- a/src/drivers/display/tft_espi/lv_tft_espi.cpp +++ b/src/drivers/display/tft_espi/lv_tft_espi.cpp @@ -26,6 +26,7 @@ typedef struct { * STATIC PROTOTYPES **********************/ static void flush_cb(lv_display_t * disp, const lv_area_t * area, uint8_t * px_map); +static void resolution_changed_event_cb(lv_event_t * e); /********************** * STATIC VARIABLES @@ -53,9 +54,10 @@ lv_display_t * lv_tft_espi_create(uint32_t hor_res, uint32_t ver_res, void * buf dsc->tft = new TFT_eSPI(hor_res, ver_res); dsc->tft->begin(); /* TFT init */ - dsc->tft->setRotation(3); /* Landscape orientation, flipped */ + dsc->tft->setRotation(0); lv_display_set_driver_data(disp, (void *)dsc); lv_display_set_flush_cb(disp, flush_cb); + lv_display_add_event_cb(disp, resolution_changed_event_cb, LV_EVENT_RESOLUTION_CHANGED, NULL); lv_display_set_buffers(disp, (void *)buf, NULL, buf_size_bytes, LV_DISPLAY_RENDER_MODE_PARTIAL); return disp; } @@ -80,4 +82,29 @@ static void flush_cb(lv_display_t * disp, const lv_area_t * area, uint8_t * px_m } +static void resolution_changed_event_cb(lv_event_t * e) +{ + lv_display_t * disp = (lv_display_t *)lv_event_get_target(e); + lv_tft_espi_t * dsc = (lv_tft_espi_t *)lv_display_get_driver_data(disp); + int32_t hor_res = lv_display_get_horizontal_resolution(disp); + int32_t ver_res = lv_display_get_vertical_resolution(disp); + lv_display_rotation_t rot = lv_display_get_rotation(disp); + + /* handle rotation */ + switch(rot) { + case LV_DISPLAY_ROTATION_0: + dsc->tft->setRotation(0); /* Portrait orientation */ + break; + case LV_DISPLAY_ROTATION_90: + dsc->tft->setRotation(1); /* Landscape orientation */ + break; + case LV_DISPLAY_ROTATION_180: + dsc->tft->setRotation(2); /* Portrait orientation, flipped */ + break; + case LV_DISPLAY_ROTATION_270: + dsc->tft->setRotation(3); /* Landscape orientation, flipped */ + break; + } +} + #endif /*LV_USE_TFT_ESPI*/