Skip to content

Commit

Permalink
Work in progress sw rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
rzeldent committed Jul 14, 2024
1 parent 34de26f commit 4355015
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 49 deletions.
51 changes: 23 additions & 28 deletions src/esp32_smartdisplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define BRIGHTNESS_DARK_ZONE 250

// Functions to be defined in the tft/touch driver
extern lv_display_t *lvgl_lcd_init(uint32_t hor_res, uint32_t ver_res);
extern lv_display_t *lvgl_lcd_init();
extern lv_indev_t *lvgl_touch_init();

lv_display_t *display;
Expand Down Expand Up @@ -192,13 +192,11 @@ void smartdisplay_init()
ledcAttachPin(GPIO_BCKL, PWM_CHANNEL_BCKL);
#endif
// Setup TFT display
display = lvgl_lcd_init(DISPLAY_WIDTH, DISPLAY_HEIGHT);
// Create drawBuffer
uint32_t drawBufferSize = sizeof(lv_color_t) * LVGL_BUFFER_PIXELS;
void *drawBuffer = heap_caps_malloc(drawBufferSize, LVGL_BUFFER_MALLOC_FLAGS);
lv_display_set_buffers(display, drawBuffer, NULL, drawBufferSize, LV_DISPLAY_RENDER_MODE_PARTIAL);
display = lvgl_lcd_init();
// Register callback for hardware rotation
lv_display_add_event_cb(display, lvgl_display_resolution_changed_callback, LV_EVENT_RESOLUTION_CHANGED, NULL);
if (!display->sw_rotate)
lv_display_add_event_cb(display, lvgl_display_resolution_changed_callback, LV_EVENT_RESOLUTION_CHANGED, NULL);

// Clear screen
lv_obj_clean(lv_scr_act());
// Turn backlight on (50%)
Expand All @@ -222,27 +220,24 @@ void smartdisplay_init()
// Thus, LV_DISPLAY_ROTATION_90 means you rotate the hardware 90 degrees clockwise, and the display rotates 90 degrees counterclockwise to compensate.
void lvgl_display_resolution_changed_callback(lv_event_t *event)
{
if (!display->sw_rotate)
const esp_lcd_panel_handle_t panel_handle = display->user_data;
switch (display->rotation)
{
const esp_lcd_panel_handle_t panel_handle = display->user_data;
switch (display->rotation)
{
case LV_DISPLAY_ROTATION_0:
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, DISPLAY_SWAP_XY));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
break;
case LV_DISPLAY_ROTATION_90:
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, !DISPLAY_SWAP_XY));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, DISPLAY_MIRROR_X, !DISPLAY_MIRROR_Y));
break;
case LV_DISPLAY_ROTATION_180:
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, DISPLAY_SWAP_XY));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, !DISPLAY_MIRROR_X, !DISPLAY_MIRROR_Y));
break;
case LV_DISPLAY_ROTATION_270:
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, !DISPLAY_SWAP_XY));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, !DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
break;
}
case LV_DISPLAY_ROTATION_0:
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, DISPLAY_SWAP_XY));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
break;
case LV_DISPLAY_ROTATION_90:
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, !DISPLAY_SWAP_XY));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, DISPLAY_MIRROR_X, !DISPLAY_MIRROR_Y));
break;
case LV_DISPLAY_ROTATION_180:
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, DISPLAY_SWAP_XY));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, !DISPLAY_MIRROR_X, !DISPLAY_MIRROR_Y));
break;
case LV_DISPLAY_ROTATION_270:
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, !DISPLAY_SWAP_XY));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, !DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
break;
}
}
8 changes: 6 additions & 2 deletions src/lvgl_panel_gc9a01_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,14 @@ void gc9a01_lv_flush(lv_display_t *display, const lv_area_t *area, uint8_t *px_m
ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap(panel_handle, area->x1, area->y1, area->x2 + 1, area->y2 + 1, px_map));
};

lv_display_t *lvgl_lcd_init(uint32_t hor_res, uint32_t ver_res)
lv_display_t *lvgl_lcd_init()
{
lv_display_t *display = lv_display_create(hor_res, ver_res);
lv_display_t *display = lv_display_create(DISPLAY_WIDTH, DISPLAY_HEIGHT);
log_v("display:0x%08x", display);
// Create drawBuffer
uint32_t drawBufferSize = sizeof(lv_color_t) * LVGL_BUFFER_PIXELS;
void *drawBuffer = heap_caps_malloc(drawBufferSize, LVGL_BUFFER_MALLOC_FLAGS);
lv_display_set_buffers(display, drawBuffer, NULL, drawBufferSize, LV_DISPLAY_RENDER_MODE_PARTIAL);

// Hardware rotation is supported
display->sw_rotate = 0;
Expand Down
8 changes: 6 additions & 2 deletions src/lvgl_panel_ili9341_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ void ili9341_lv_flush(lv_display_t *display, const lv_area_t *area, uint8_t *px_
ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap(panel_handle, area->x1, area->y1, area->x2 + 1, area->y2 + 1, px_map));
};

lv_display_t *lvgl_lcd_init(uint32_t hor_res, uint32_t ver_res)
lv_display_t *lvgl_lcd_init()
{
lv_display_t *display = lv_display_create(hor_res, ver_res);
lv_display_t *display = lv_display_create(DISPLAY_WIDTH, DISPLAY_HEIGHT);
log_v("display:0x%08x", display);
// Create drawBuffer
uint32_t drawBufferSize = sizeof(lv_color_t) * LVGL_BUFFER_PIXELS;
void *drawBuffer = heap_caps_malloc(drawBufferSize, LVGL_BUFFER_MALLOC_FLAGS);
lv_display_set_buffers(display, drawBuffer, NULL, drawBufferSize, LV_DISPLAY_RENDER_MODE_PARTIAL);

// Hardware rotation is supported
display->sw_rotate = 0;
Expand Down
58 changes: 50 additions & 8 deletions src/lvgl_panel_st7262_par.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,59 @@ bool direct_io_frame_trans_done(esp_lcd_panel_handle_t panel, esp_lcd_rgb_panel_
void direct_io_lv_flush(lv_display_t *display, const lv_area_t *area, uint8_t *px_map)
{
const esp_lcd_panel_handle_t panel_handle = display->user_data;
// LV_COLOR_16_SWAP is handled by mapping of the data
ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap(panel_handle, area->x1, area->y1, area->x2 + 1, area->y2 + 1, px_map));

lv_display_rotation_t rotation = lv_display_get_rotation(display);
if (rotation == LV_DISPLAY_ROTATION_0)
{
ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap(panel_handle, area->x1, area->y1, area->x2 + 1, area->y2 + 1, px_map));
return;
}

// Rotated
int32_t w = lv_area_get_width(area);
int32_t h = lv_area_get_height(area);
lv_color_format_t cf = lv_display_get_color_format(display);
uint32_t px_size = lv_color_format_get_size(cf);
size_t buf_size = w * h * px_size;
log_v("alloc rotation buffer to: %u bytes", buf_size);
void *rotation_buffer = heap_caps_malloc(buf_size, LVGL_BUFFER_MALLOC_FLAGS);
assert(rotation_buffer != NULL);

uint32_t w_stride = lv_draw_buf_width_to_stride(w, cf);
uint32_t h_stride = lv_draw_buf_width_to_stride(h, cf);

switch (rotation)
{
case LV_DISPLAY_ROTATION_90:
lv_draw_sw_rotate(px_map, rotation_buffer, w, h, w_stride, h_stride, rotation, cf);
ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap(panel_handle, area->y1, display->ver_res - area->x1 - w, area->y1 + h, display->ver_res - area->x1, rotation_buffer));
break;
case LV_DISPLAY_ROTATION_180:
lv_draw_sw_rotate(px_map, rotation_buffer, w, h, w_stride, w_stride, rotation, cf);
ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap(panel_handle, display->hor_res - area->x1 - w, display->ver_res - area->y1 - h, display->hor_res - area->x1, display->ver_res - area->y1, rotation_buffer));
break;
case LV_DISPLAY_ROTATION_270:
lv_draw_sw_rotate(px_map, rotation_buffer, w, h, w_stride, h_stride, rotation, cf);
ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap(panel_handle, display->hor_res - area->y2 - 1, area->x2 - w + 1, display->hor_res - area->y2 - 1 + h, area->x2 + 1, rotation_buffer));
break;
default:
assert(false);
break;
}

free(rotation_buffer);
};

lv_display_t *lvgl_lcd_init(uint32_t hor_res, uint32_t ver_res)
lv_display_t *lvgl_lcd_init()
{
lv_display_t *display = lv_display_create(hor_res, ver_res);
lv_display_t *display = lv_display_create(DISPLAY_WIDTH, DISPLAY_HEIGHT);
log_v("display:0x%08x", display);
// Create drawBuffer
lv_color_format_t cf = lv_display_get_color_format(display);
uint32_t px_size = lv_color_format_get_size(cf);
uint32_t drawBufferSize = px_size * LVGL_BUFFER_PIXELS;
void *drawBuffer = heap_caps_malloc(drawBufferSize, LVGL_BUFFER_MALLOC_FLAGS);
lv_display_set_buffers(display, drawBuffer, NULL, drawBufferSize, LV_DISPLAY_RENDER_MODE_PARTIAL);

// Hardware rotation is not supported
display->sw_rotate = 1;
Expand Down Expand Up @@ -53,11 +98,8 @@ lv_display_t *lvgl_lcd_init(uint32_t hor_res, uint32_t ver_res)
.vsync_gpio_num = ST7262_PANEL_CONFIG_VSYNC_GPIO_NUM,
.de_gpio_num = ST7262_PANEL_CONFIG_DE_GPIO_NUM,
.pclk_gpio_num = ST7262_PANEL_CONFIG_PCLK_GPIO_NUM,
#if LV_COLOR_16_SWAP == 0
// LV_COLOR_16_SWAP is handled by mapping of the data
.data_gpio_nums = {ST7262_PANEL_CONFIG_DATA_GPIO_R0, ST7262_PANEL_CONFIG_DATA_GPIO_R1, ST7262_PANEL_CONFIG_DATA_GPIO_R2, ST7262_PANEL_CONFIG_DATA_GPIO_R3, ST7262_PANEL_CONFIG_DATA_GPIO_R4, ST7262_PANEL_CONFIG_DATA_GPIO_G0, ST7262_PANEL_CONFIG_DATA_GPIO_G1, ST7262_PANEL_CONFIG_DATA_GPIO_G2, ST7262_PANEL_CONFIG_DATA_GPIO_G3, ST7262_PANEL_CONFIG_DATA_GPIO_G4, ST7262_PANEL_CONFIG_DATA_GPIO_G5, ST7262_PANEL_CONFIG_DATA_GPIO_B0, ST7262_PANEL_CONFIG_DATA_GPIO_B1, ST7262_PANEL_CONFIG_DATA_GPIO_B2, ST7262_PANEL_CONFIG_DATA_GPIO_B3, ST7262_PANEL_CONFIG_DATA_GPIO_B4},
#else
.data_gpio_nums = {ST7262_PANEL_CONFIG_DATA_GPIO_G3, ST7262_PANEL_CONFIG_DATA_GPIO_G4, ST7262_PANEL_CONFIG_DATA_GPIO_G5, ST7262_PANEL_CONFIG_DATA_GPIO_B0, ST7262_PANEL_CONFIG_DATA_GPIO_B1, ST7262_PANEL_CONFIG_DATA_GPIO_B2, ST7262_PANEL_CONFIG_DATA_GPIO_B3, ST7262_PANEL_CONFIG_DATA_GPIO_B4, ST7262_PANEL_CONFIG_DATA_GPIO_R0, ST7262_PANEL_CONFIG_DATA_GPIO_R1, ST7262_PANEL_CONFIG_DATA_GPIO_R2, ST7262_PANEL_CONFIG_DATA_GPIO_R3, ST7262_PANEL_CONFIG_DATA_GPIO_R4, ST7262_PANEL_CONFIG_DATA_GPIO_G0, ST7262_PANEL_CONFIG_DATA_GPIO_G1, ST7262_PANEL_CONFIG_DATA_GPIO_G2},
#endif
.disp_gpio_num = ST7262_PANEL_CONFIG_DISP_GPIO_NUM,
.on_frame_trans_done = direct_io_frame_trans_done,
.user_ctx = display,
Expand Down
12 changes: 6 additions & 6 deletions src/lvgl_panel_st7701_par.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ void direct_io_lv_flush(lv_display_t *display, const lv_area_t *area, uint8_t *p
ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap(panel_handle, area->x1, area->y1, area->x2 + 1, area->y2 + 1, px_map));
};

lv_display_t *lvgl_lcd_init(uint32_t hor_res, uint32_t ver_res)
lv_display_t *lvgl_lcd_init()
{
lv_display_t *display = lv_display_create(hor_res, ver_res);
lv_display_t *display = lv_display_create(DISPLAY_WIDTH, DISPLAY_HEIGHT);
log_v("display:0x%08x", display);
// Create drawBuffer
uint32_t drawBufferSize = sizeof(lv_color_t) * LVGL_BUFFER_PIXELS;
void *drawBuffer = heap_caps_malloc(drawBufferSize, LVGL_BUFFER_MALLOC_FLAGS);
lv_display_set_buffers(display, drawBuffer, NULL, drawBufferSize, LV_DISPLAY_RENDER_MODE_PARTIAL);

// Hardware rotation is not supported
display->sw_rotate = 1;
Expand Down Expand Up @@ -73,11 +77,7 @@ lv_display_t *lvgl_lcd_init(uint32_t hor_res, uint32_t ver_res)
.vsync_gpio_num = ST7701_PANEL_CONFIG_VSYNC_GPIO_NUM,
.de_gpio_num = ST7701_PANEL_CONFIG_DE_GPIO_NUM,
.pclk_gpio_num = ST7701_PANEL_CONFIG_PCLK_GPIO_NUM,
#if LV_COLOR_16_SWAP == 0
.data_gpio_nums = {ST7701_PANEL_CONFIG_DATA_GPIO_R0, ST7701_PANEL_CONFIG_DATA_GPIO_R1, ST7701_PANEL_CONFIG_DATA_GPIO_R2, ST7701_PANEL_CONFIG_DATA_GPIO_R3, ST7701_PANEL_CONFIG_DATA_GPIO_R4, ST7701_PANEL_CONFIG_DATA_GPIO_G0, ST7701_PANEL_CONFIG_DATA_GPIO_G1, ST7701_PANEL_CONFIG_DATA_GPIO_G2, ST7701_PANEL_CONFIG_DATA_GPIO_G3, ST7701_PANEL_CONFIG_DATA_GPIO_G4, ST7701_PANEL_CONFIG_DATA_GPIO_G5, ST7701_PANEL_CONFIG_DATA_GPIO_B0, ST7701_PANEL_CONFIG_DATA_GPIO_B1, ST7701_PANEL_CONFIG_DATA_GPIO_B2, ST7701_PANEL_CONFIG_DATA_GPIO_B3, ST7701_PANEL_CONFIG_DATA_GPIO_B4},
#else
.data_gpio_nums = {ST7701_PANEL_CONFIG_DATA_GPIO_G3, ST7701_PANEL_CONFIG_DATA_GPIO_G4, ST7701_PANEL_CONFIG_DATA_GPIO_G5, ST7701_PANEL_CONFIG_DATA_GPIO_B0, ST7701_PANEL_CONFIG_DATA_GPIO_B1, ST7701_PANEL_CONFIG_DATA_GPIO_B2, ST7701_PANEL_CONFIG_DATA_GPIO_B3, ST7701_PANEL_CONFIG_DATA_GPIO_B4, ST7701_PANEL_CONFIG_DATA_GPIO_R0, ST7701_PANEL_CONFIG_DATA_GPIO_R1, ST7701_PANEL_CONFIG_DATA_GPIO_R2, ST7701_PANEL_CONFIG_DATA_GPIO_R3, ST7701_PANEL_CONFIG_DATA_GPIO_R4, ST7701_PANEL_CONFIG_DATA_GPIO_G0, ST7701_PANEL_CONFIG_DATA_GPIO_G1, ST7701_PANEL_CONFIG_DATA_GPIO_G2},
#endif
.disp_gpio_num = ST7701_PANEL_CONFIG_DISP_GPIO_NUM,
.on_frame_trans_done = direct_io_frame_trans_done,
.user_ctx = display,
Expand Down
6 changes: 5 additions & 1 deletion src/lvgl_panel_st7789_i80.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ void st7789_lv_flush(lv_display_t *drv, const lv_area_t *area, uint8_t *px_map)

lv_display_t *lvgl_lcd_init(uint32_t hor_res, uint32_t ver_res)
{
lv_display_t *display = lv_display_create(hor_res, ver_res);
lv_display_t *display = lv_display_create(DISPLAY_WIDTH, DISPLAY_HEIGHT);
log_v("display:0x%08x", display);
// Create drawBuffer
uint32_t drawBufferSize = sizeof(lv_color_t) * LVGL_BUFFER_PIXELS;
void *drawBuffer = heap_caps_malloc(drawBufferSize, LVGL_BUFFER_MALLOC_FLAGS);
lv_display_set_buffers(display, drawBuffer, NULL, drawBufferSize, LV_DISPLAY_RENDER_MODE_PARTIAL);

// Hardware rotation is supported
display->sw_rotate = 0;
Expand Down
6 changes: 5 additions & 1 deletion src/lvgl_panel_st7789_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ void st7789_lv_flush(lv_display_t *display, const lv_area_t *area, uint8_t *px_m

lv_display_t *lvgl_lcd_init(uint32_t hor_res, uint32_t ver_res)
{
lv_display_t *display = lv_display_create(hor_res, ver_res);
lv_display_t *display = lv_display_create(DISPLAY_WIDTH, DISPLAY_HEIGHT);
log_v("display:0x%08x", display);
// Create drawBuffer
uint32_t drawBufferSize = sizeof(lv_color_t) * LVGL_BUFFER_PIXELS;
void *drawBuffer = heap_caps_malloc(drawBufferSize, LVGL_BUFFER_MALLOC_FLAGS);
lv_display_set_buffers(display, drawBuffer, NULL, drawBufferSize, LV_DISPLAY_RENDER_MODE_PARTIAL);

// Hardware rotation is supported
display->sw_rotate = 0;
Expand Down
6 changes: 5 additions & 1 deletion src/lvgl_panel_st7796_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ void st7796_lv_flush(lv_display_t *display, const lv_area_t *area, uint8_t *px_m

lv_display_t *lvgl_lcd_init(uint32_t hor_res, uint32_t ver_res)
{
lv_display_t *display = lv_display_create(hor_res, ver_res);
lv_display_t *display = lv_display_create(DISPLAY_WIDTH, DISPLAY_HEIGHT);
log_v("display:0x%08x", display);
// Create drawBuffer
uint32_t drawBufferSize = sizeof(lv_color_t) * LVGL_BUFFER_PIXELS;
void *drawBuffer = heap_caps_malloc(drawBufferSize, LVGL_BUFFER_MALLOC_FLAGS);
lv_display_set_buffers(display, drawBuffer, NULL, drawBufferSize, LV_DISPLAY_RENDER_MODE_PARTIAL);

// Hardware rotation is supported
display->sw_rotate = 0;
Expand Down

0 comments on commit 4355015

Please sign in to comment.