diff --git a/components/platform/internal/display_st7789p3.h b/components/platform/internal/display_st7789p3.h index f0c13d7..4e2dc02 100644 --- a/components/platform/internal/display_st7789p3.h +++ b/components/platform/internal/display_st7789p3.h @@ -35,6 +35,8 @@ typedef struct { size_t max_transfer_sz; uint16_t h_res; uint16_t v_res; + uint16_t x_gap; + uint16_t y_gap; uint8_t madctl_val; bool invert_color; bool swap_color_bytes; @@ -58,6 +60,8 @@ typedef struct { .max_transfer_sz = 0, \ .h_res = 320, \ .v_res = 240, \ + .x_gap = 0, \ + .y_gap = 0, \ .madctl_val = ST7789P3_MADCTL_MX | ST7789P3_MADCTL_MV, \ .invert_color = false, \ .swap_color_bytes = false, \ diff --git a/components/platform/src/display_lcd_module.c b/components/platform/src/display_lcd_module.c index c45f097..cc3cafd 100644 --- a/components/platform/src/display_lcd_module.c +++ b/components/platform/src/display_lcd_module.c @@ -19,43 +19,83 @@ static const char *TAG = "lcd_module"; #define ST7789_SPI_HOST SPI2_HOST -#define LCD_PANEL_H_RES 320 -#define LCD_PANEL_V_RES 240 #define LCD_PREVIEW_TIMEOUT_MS_DEFAULT 200 #define LCD_LOCK_TIMEOUT_MS 1000 -#define LCD_DRAW_LINES 20 +#define LCD_DEFAULT_DRAW_LINES 20 + +#ifndef CONFIG_TQ_SCREEN_PIXEL_CLOCK_HZ +#define CONFIG_TQ_SCREEN_PIXEL_CLOCK_HZ (10 * 1000 * 1000) +#endif + +#ifndef CONFIG_TQ_SCREEN_H_RES +#define CONFIG_TQ_SCREEN_H_RES 320 +#endif + +#ifndef CONFIG_TQ_SCREEN_V_RES +#define CONFIG_TQ_SCREEN_V_RES 240 +#endif #ifndef CONFIG_TQ_SCREEN_SPI_MODE #define CONFIG_TQ_SCREEN_SPI_MODE 0 #endif #ifndef CONFIG_TQ_SCREEN_DRAW_LINES -#define CONFIG_TQ_SCREEN_DRAW_LINES LCD_DRAW_LINES +#define CONFIG_TQ_SCREEN_DRAW_LINES LCD_DEFAULT_DRAW_LINES +#endif + +#ifndef CONFIG_TQ_SCREEN_COLOR_ORDER_BGR +#define CONFIG_TQ_SCREEN_COLOR_ORDER_BGR 0 #endif #ifndef CONFIG_TQ_SCREEN_BACKLIGHT_ACTIVE_HIGH #define CONFIG_TQ_SCREEN_BACKLIGHT_ACTIVE_HIGH 1 #endif +#ifndef CONFIG_TQ_SCREEN_MIRROR_X +#define CONFIG_TQ_SCREEN_MIRROR_X 1 +#endif + +#ifndef CONFIG_TQ_SCREEN_MIRROR_Y +#define CONFIG_TQ_SCREEN_MIRROR_Y 0 +#endif + +#ifndef CONFIG_TQ_SCREEN_SWAP_XY +#define CONFIG_TQ_SCREEN_SWAP_XY 1 +#endif + +#ifndef CONFIG_TQ_SCREEN_X_GAP +#define CONFIG_TQ_SCREEN_X_GAP 0 +#endif + +#ifndef CONFIG_TQ_SCREEN_Y_GAP +#define CONFIG_TQ_SCREEN_Y_GAP 0 +#endif + +#ifndef CONFIG_TQ_SCREEN_INVERT_COLOR +#define CONFIG_TQ_SCREEN_INVERT_COLOR 1 +#endif + +#ifndef CONFIG_TQ_SCREEN_SWAP_COLOR_BYTES +#define CONFIG_TQ_SCREEN_SWAP_COLOR_BYTES 1 +#endif + +#ifndef CONFIG_TQ_SCREEN_RESET_PIN +#define CONFIG_TQ_SCREEN_RESET_PIN -1 +#endif + +#ifndef CONFIG_TQ_SCREEN_BACKLIGHT_PIN +#define CONFIG_TQ_SCREEN_BACKLIGHT_PIN -1 +#endif + +#define LCD_PANEL_H_RES ((uint16_t)CONFIG_TQ_SCREEN_H_RES) +#define LCD_PANEL_V_RES ((uint16_t)CONFIG_TQ_SCREEN_V_RES) + #if CONFIG_TQ_SCREEN_BACKLIGHT_ACTIVE_HIGH #define ST7789_BACKLIGHT_ON_LEVEL 1 #else #define ST7789_BACKLIGHT_ON_LEVEL 0 #endif -typedef struct { - bool bgr; - bool swap_bytes; - bool invert; -} lcd_color_mode_t; - -// Locked profile chosen from calibration (equivalent to MODE 6). -static const lcd_color_mode_t s_default_color_mode = { - .bgr = false, - .swap_bytes = true, - .invert = true, -}; - typedef struct { st7789p3_handle_t panel; SemaphoreHandle_t lock; @@ -82,6 +122,24 @@ static int resolve_reset_pin(void) return CONFIG_TQ_SCREEN_BACKLIGHT_PIN; } +static uint8_t lcd_build_madctl(void) +{ + uint8_t madctl = 0; +#if CONFIG_TQ_SCREEN_MIRROR_X + madctl |= ST7789P3_MADCTL_MX; +#endif +#if CONFIG_TQ_SCREEN_MIRROR_Y + madctl |= ST7789P3_MADCTL_MY; +#endif +#if CONFIG_TQ_SCREEN_SWAP_XY + madctl |= ST7789P3_MADCTL_MV; +#endif +#if CONFIG_TQ_SCREEN_COLOR_ORDER_BGR + madctl |= ST7789P3_MADCTL_BGR; +#endif + return madctl; +} + static void lcd_fill_err(char *err, size_t err_len, const char *msg) { if (err != NULL && err_len > 0) { @@ -346,12 +404,11 @@ esp_err_t lcd_module_init(void) lcd_cfg.spi_mode = CONFIG_TQ_SCREEN_SPI_MODE; lcd_cfg.h_res = LCD_PANEL_H_RES; lcd_cfg.v_res = LCD_PANEL_V_RES; - lcd_cfg.madctl_val = ST7789P3_MADCTL_MX | ST7789P3_MADCTL_MV; - if (s_default_color_mode.bgr) { - lcd_cfg.madctl_val |= ST7789P3_MADCTL_BGR; - } - lcd_cfg.invert_color = s_default_color_mode.invert; - lcd_cfg.swap_color_bytes = s_default_color_mode.swap_bytes; + lcd_cfg.x_gap = (uint16_t)CONFIG_TQ_SCREEN_X_GAP; + lcd_cfg.y_gap = (uint16_t)CONFIG_TQ_SCREEN_Y_GAP; + lcd_cfg.madctl_val = lcd_build_madctl(); + lcd_cfg.invert_color = CONFIG_TQ_SCREEN_INVERT_COLOR; + lcd_cfg.swap_color_bytes = CONFIG_TQ_SCREEN_SWAP_COLOR_BYTES; lcd_cfg.max_transfer_sz = CONFIG_TQ_SCREEN_DRAW_LINES * LCD_PANEL_H_RES * sizeof(uint16_t) + 8; esp_err_t err = st7789p3_new_panel(&lcd_cfg, &s_lcd.panel); @@ -384,7 +441,7 @@ esp_err_t lcd_module_init(void) s_lcd.initialized = true; ESP_LOGI(TAG, - "st7789p3 ready: spi_host=%d mosi=%d clk=%d miso=%d dc=%d cs=%d rst=%d bk=%d pclk=%u", + "st7789p3 ready: spi_host=%d mosi=%d clk=%d miso=%d dc=%d cs=%d rst=%d bk=%d pclk=%u mode=%u res=%ux%u madctl=0x%02x gap=%u,%u invert=%d swap_bytes=%d", ST7789_SPI_HOST, CONFIG_TQ_SPI_SHARED_MOSI_PIN, CONFIG_TQ_SPI_SHARED_CLK_PIN, @@ -393,7 +450,15 @@ esp_err_t lcd_module_init(void) CONFIG_TQ_SCREEN_CS_PIN, lcd_cfg.pin_rst, CONFIG_TQ_SCREEN_BACKLIGHT_PIN, - (unsigned)lcd_cfg.pclk_hz); + (unsigned)lcd_cfg.pclk_hz, + (unsigned)lcd_cfg.spi_mode, + (unsigned)lcd_cfg.h_res, + (unsigned)lcd_cfg.v_res, + (unsigned)lcd_cfg.madctl_val, + (unsigned)lcd_cfg.x_gap, + (unsigned)lcd_cfg.y_gap, + lcd_cfg.invert_color ? 1 : 0, + lcd_cfg.swap_color_bytes ? 1 : 0); return ESP_OK; #endif } diff --git a/components/platform/src/display_st7789p3.c b/components/platform/src/display_st7789p3.c index 5d5f47e..9f8997a 100644 --- a/components/platform/src/display_st7789p3.c +++ b/components/platform/src/display_st7789p3.c @@ -419,17 +419,25 @@ esp_err_t st7789p3_set_window( return ESP_ERR_INVALID_ARG; } + uint32_t panel_x_start = (uint32_t)x_start + panel->cfg.x_gap; + uint32_t panel_y_start = (uint32_t)y_start + panel->cfg.y_gap; + uint32_t panel_x_end = (uint32_t)x_end + panel->cfg.x_gap; + uint32_t panel_y_end = (uint32_t)y_end + panel->cfg.y_gap; + if (panel_x_end == 0 || panel_y_end == 0 || panel_x_end - 1 > UINT16_MAX || panel_y_end - 1 > UINT16_MAX) { + return ESP_ERR_INVALID_ARG; + } + uint8_t x_data[4] = { - (uint8_t)(x_start >> 8), - (uint8_t)(x_start & 0xFF), - (uint8_t)((x_end - 1) >> 8), - (uint8_t)((x_end - 1) & 0xFF), + (uint8_t)(panel_x_start >> 8), + (uint8_t)(panel_x_start & 0xFF), + (uint8_t)((panel_x_end - 1) >> 8), + (uint8_t)((panel_x_end - 1) & 0xFF), }; uint8_t y_data[4] = { - (uint8_t)(y_start >> 8), - (uint8_t)(y_start & 0xFF), - (uint8_t)((y_end - 1) >> 8), - (uint8_t)((y_end - 1) & 0xFF), + (uint8_t)(panel_y_start >> 8), + (uint8_t)(panel_y_start & 0xFF), + (uint8_t)((panel_y_end - 1) >> 8), + (uint8_t)((panel_y_end - 1) & 0xFF), }; esp_err_t ret = st7789p3_send_cmd(panel, 0x2A); diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index defa11b..0434daf 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -487,7 +487,7 @@ config TQ_SCREEN_PIXEL_CLOCK_HZ config TQ_SCREEN_H_RES int "ST7789 horizontal resolution" range 64 480 - default 240 + default 320 depends on TQ_SCREEN_ENABLE config TQ_SCREEN_V_RES @@ -510,6 +510,16 @@ config TQ_SCREEN_SPI_MODE config TQ_SCREEN_COLOR_ORDER_BGR bool "ST7789 color order is BGR" + default n + depends on TQ_SCREEN_ENABLE + +config TQ_SCREEN_INVERT_COLOR + bool "ST7789 invert color" + default y + depends on TQ_SCREEN_ENABLE + +config TQ_SCREEN_SWAP_COLOR_BYTES + bool "ST7789 swap color bytes" default y depends on TQ_SCREEN_ENABLE @@ -520,7 +530,7 @@ config TQ_SCREEN_BACKLIGHT_ACTIVE_HIGH config TQ_SCREEN_MIRROR_X bool "ST7789 mirror X axis" - default n + default y depends on TQ_SCREEN_ENABLE config TQ_SCREEN_MIRROR_Y @@ -530,7 +540,7 @@ config TQ_SCREEN_MIRROR_Y config TQ_SCREEN_SWAP_XY bool "ST7789 swap X/Y axis" - default n + default y depends on TQ_SCREEN_ENABLE config TQ_SCREEN_X_GAP diff --git a/sdkconfig.defaults b/sdkconfig.defaults index 40c0650..7071698 100644 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -92,10 +92,16 @@ CONFIG_TQ_SCREEN_BACKLIGHT_PIN=48 CONFIG_TQ_SCREEN_ENABLE=y CONFIG_TQ_SCREEN_TEST_PATTERN_ON_BOOT=y CONFIG_TQ_SCREEN_PIXEL_CLOCK_HZ=10000000 -CONFIG_TQ_SCREEN_H_RES=240 +CONFIG_TQ_SCREEN_H_RES=320 CONFIG_TQ_SCREEN_V_RES=240 CONFIG_TQ_SCREEN_DRAW_LINES=20 CONFIG_TQ_SCREEN_SPI_MODE=0 -CONFIG_TQ_SCREEN_COLOR_ORDER_BGR=y +# CONFIG_TQ_SCREEN_COLOR_ORDER_BGR is not set +CONFIG_TQ_SCREEN_INVERT_COLOR=y +CONFIG_TQ_SCREEN_SWAP_COLOR_BYTES=y CONFIG_TQ_SCREEN_BACKLIGHT_ACTIVE_HIGH=y +CONFIG_TQ_SCREEN_MIRROR_X=y +CONFIG_TQ_SCREEN_SWAP_XY=y +CONFIG_TQ_SCREEN_X_GAP=0 +CONFIG_TQ_SCREEN_Y_GAP=0 CONFIG_TQ_SCREEN_RESET_PIN=-1