From 63ca6063f16cdfb15f0eb6fe5252972d6973c083 Mon Sep 17 00:00:00 2001 From: chowette Date: Fri, 14 Aug 2020 18:10:10 +0200 Subject: [PATCH 1/4] fix orientation for ILI9341 The code was using the Read command instead of the Set command --- components/sys/drivers/ili9341.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/sys/drivers/ili9341.c b/components/sys/drivers/ili9341.c index 94e90e290..58a4ffe94 100644 --- a/components/sys/drivers/ili9341.c +++ b/components/sys/drivers/ili9341.c @@ -100,7 +100,7 @@ static const uint8_t ILI9341_init[] = { 0x28, ILI9341_VMCTR2, 1, //VCM control2 0x86, - ILI9341_RDMADCTL, 1, // Memory Access Control + ST7735_MADCTL, 1, // Memory Access Control 0x48, ILI9341_PIXFMT, 1, 0x55, @@ -347,7 +347,7 @@ void ili9341_set_orientation(uint8_t m) { break; } - gdisplay_ll_command(ILI9341_RDMADCTL); + gdisplay_ll_command(ST7735_MADCTL); gdisplay_ll_data(&madctl, 1); } From 196916a0afab407a8379f2b828ad696bf06f918b Mon Sep 17 00:00:00 2001 From: chowette Date: Fri, 28 Aug 2020 16:12:36 +0200 Subject: [PATCH 2/4] Fix touch pressure not returned So there is no way to know if there is no touch event. Also return negative z if the touch read failed somewhere --- components/sys/drivers/ili9341.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/components/sys/drivers/ili9341.c b/components/sys/drivers/ili9341.c index 58a4ffe94..7afca3fff 100644 --- a/components/sys/drivers/ili9341.c +++ b/components/sys/drivers/ili9341.c @@ -385,6 +385,8 @@ void ili9341_tp_get(int *x, int *y, int *z, uint8_t raw) { result = ili9341_tp_read(0xB0, 3); if (result > 50) { // tp pressed + *z = result; + result = ili9341_tp_read(0xD0, 10); if (result >= 0) { *x = result; @@ -394,10 +396,10 @@ void ili9341_tp_get(int *x, int *y, int *z, uint8_t raw) { } } - if (result <= 50) { + if (result < 0) { *x = 0; *y = 0; - *z = 0; + *z = result; return; } From 4bf70f194cd189be27086970904e63c0bdd50abd Mon Sep 17 00:00:00 2001 From: chowette Date: Fri, 28 Aug 2020 18:26:23 +0200 Subject: [PATCH 3/4] add ILI9341_BGR variantThis variant support panel wired as BGR.Those panels also have display mirrored compare to other ILI9341 driver --- components/gdisplay/gdisplay.c | 1 + components/gdisplay/gdisplay.h | 11 +++--- components/lua/modules/hw/gdisplay.c | 1 + components/sys/drivers/ili9341.c | 52 ++++++++++++++++++++++++++-- components/sys/drivers/ili9341.h | 1 + 5 files changed, 59 insertions(+), 7 deletions(-) diff --git a/components/gdisplay/gdisplay.c b/components/gdisplay/gdisplay.c index b761b1b15..72d523c4a 100644 --- a/components/gdisplay/gdisplay.c +++ b/components/gdisplay/gdisplay.c @@ -88,6 +88,7 @@ static const gdisplay_t displaydevs[] = { {CHIPSET_ST7735G_144, st7735_init }, {CHIPSET_ST7735_096 , st7735_init }, {CHIPSET_ILI9341, ili9341_init}, + {CHIPSET_ILI9341_BGR, ili9341_init}, {CHIPSET_SSD1306_128_32, ssd1306_init}, {CHIPSET_SSD1306_128_64, ssd1306_init}, {CHIPSET_SSD1306_96_16, ssd1306_init}, diff --git a/components/gdisplay/gdisplay.h b/components/gdisplay/gdisplay.h index bcf3aceed..1c4d03cbf 100644 --- a/components/gdisplay/gdisplay.h +++ b/components/gdisplay/gdisplay.h @@ -90,20 +90,23 @@ */ #define CHIPSET_ILI9341 6 +#define CHIPSET_ILI9341_BGR 7 + +#define CHIPSET_ILI9341_VARIANT_OFFSET CHIPSET_ILI9341 /* * PCD8544 */ -#define CHIPSET_PCD8544 7 +#define CHIPSET_PCD8544 8 /* * SSDS1306 */ -#define CHIPSET_SSD1306_128_32 8 -#define CHIPSET_SSD1306_128_64 9 -#define CHIPSET_SSD1306_96_16 10 +#define CHIPSET_SSD1306_128_32 9 +#define CHIPSET_SSD1306_128_64 10 +#define CHIPSET_SSD1306_96_16 11 #define CHIPSET_SSD1306_VARIANT_OFFSET CHIPSET_SSD1306_128_32 diff --git a/components/lua/modules/hw/gdisplay.c b/components/lua/modules/hw/gdisplay.c index e2bcce817..3a61fdb34 100644 --- a/components/lua/modules/hw/gdisplay.c +++ b/components/lua/modules/hw/gdisplay.c @@ -1460,6 +1460,7 @@ static const LUA_REG_TYPE gdisplay_map[] = { { LSTRKEY( "ST7735_096" ), LINTVAL( CHIPSET_ST7735_096 ) }, { LSTRKEY( "ILI9341" ), LINTVAL( CHIPSET_ILI9341 ) }, + { LSTRKEY( "ILI9341_BGR" ), LINTVAL( CHIPSET_ILI9341_BGR ) }, { LSTRKEY( "PCD8544" ), LINTVAL( CHIPSET_PCD8544 ) }, { LSTRKEY( "SSD1306_128_32" ), LINTVAL( CHIPSET_SSD1306_128_32 ) }, diff --git a/components/sys/drivers/ili9341.c b/components/sys/drivers/ili9341.c index 7afca3fff..4d0f3f34a 100644 --- a/components/sys/drivers/ili9341.c +++ b/components/sys/drivers/ili9341.c @@ -210,7 +210,14 @@ driver_error_t *ili9341_init(uint8_t chip, uint8_t orientation, uint8_t address) caps->on = st7735_on; caps->off = st7735_off; caps->invert = st7735_invert; - caps->orientation = ili9341_set_orientation; + switch (chip) { + case CHIPSET_ILI9341: + caps->orientation = ili9341_set_orientation; + break; + case CHIPSET_ILI9341_BGR: + caps->orientation = ili9341_bgr_set_orientation; + break; + } caps->touch_get = ili9341_tp_get; caps->touch_cal = ili9341_tp_set_cal; caps->bytes_per_pixel = 2; @@ -298,11 +305,14 @@ driver_error_t *ili9341_init(uint8_t chip, uint8_t orientation, uint8_t address) switch (chipset) { case CHIPSET_ILI9341: gdisplay_ll_command_list(ILI9341_init); + ili9341_set_orientation(orientation); + break; + case CHIPSET_ILI9341_BGR: + gdisplay_ll_command_list(ILI9341_init); + ili9341_bgr_set_orientation(orientation); break; } - ili9341_set_orientation(orientation); - // Allocate buffer if (!gdisplay_ll_allocate_buffer(ST7735_BUFFER)) { return driver_error(GDISPLAY_DRIVER, GDISPLAY_ERR_NOT_ENOUGH_MEMORY, NULL); @@ -351,6 +361,42 @@ void ili9341_set_orientation(uint8_t m) { gdisplay_ll_data(&madctl, 1); } +void ili9341_bgr_set_orientation(uint8_t m) { + gdisplay_caps_t *caps = gdisplay_ll_get_caps(); + uint8_t orientation = m & 3; // can't be higher than 3 + uint8_t madctl = ST7735_MADCTL_BGR; + + caps->ystart = 0; + caps->xstart = 0; + + + switch (orientation) { + case LANDSCAPE: + madctl |= ST7735_MADCTL_MV; + caps->width = ILI9341_HEIGHT; + caps->height = ILI9341_WIDTH; + break; + case PORTRAIT: + madctl |= ST7735_MADCTL_MY; + caps->width = ILI9341_WIDTH; + caps->height = ILI9341_HEIGHT; + break; + case LANDSCAPE_FLIP: + madctl |= ST7735_MADCTL_MX | ST7735_MADCTL_MY | ST7735_MADCTL_MV; + caps->width = ILI9341_HEIGHT; + caps->height = ILI9341_WIDTH; + break; + case PORTRAIT_FLIP: + madctl |= ST7735_MADCTL_MX; + caps->width = ILI9341_WIDTH; + caps->height = ILI9341_HEIGHT; + break; + } + + gdisplay_ll_command(ST7735_MADCTL); + gdisplay_ll_data(&madctl, 1); +} + void ili9341_tp_set_cal(int calx, int caly) { tp_calx = calx; tp_caly = caly; diff --git a/components/sys/drivers/ili9341.h b/components/sys/drivers/ili9341.h index 3d1c868e8..ffd8ad8fd 100644 --- a/components/sys/drivers/ili9341.h +++ b/components/sys/drivers/ili9341.h @@ -119,6 +119,7 @@ driver_error_t *ili9341_init(uint8_t chip, uint8_t orientation, uint8_t address); void ili9341_set_orientation(uint8_t m); +void ili9341_bgr_set_orientation(uint8_t m); void ili9341_tp_get(int *x, int *y, int *z, uint8_t raw); void ili9341_tp_set_cal(int calx, int caly); From 45a6c5ac89689c671a6699dea59dc8b64d6a0dae Mon Sep 17 00:00:00 2001 From: chowette Date: Tue, 22 Sep 2020 21:54:50 +0200 Subject: [PATCH 4/4] make touch work with ILI9341_BGR driver --- components/sys/drivers/ili9341.c | 86 +++++++++++++++++++++++++++++--- components/sys/drivers/ili9341.h | 1 + 2 files changed, 80 insertions(+), 7 deletions(-) diff --git a/components/sys/drivers/ili9341.c b/components/sys/drivers/ili9341.c index 4d0f3f34a..f0adfb3d3 100644 --- a/components/sys/drivers/ili9341.c +++ b/components/sys/drivers/ili9341.c @@ -213,12 +213,13 @@ driver_error_t *ili9341_init(uint8_t chip, uint8_t orientation, uint8_t address) switch (chip) { case CHIPSET_ILI9341: caps->orientation = ili9341_set_orientation; - break; + caps->touch_get = ili9341_tp_get; + break; case CHIPSET_ILI9341_BGR: caps->orientation = ili9341_bgr_set_orientation; - break; + caps->touch_get = ili9341_bgr_tp_get; + break; } - caps->touch_get = ili9341_tp_get; caps->touch_cal = ili9341_tp_set_cal; caps->bytes_per_pixel = 2; caps->rdepth = 5; @@ -455,9 +456,10 @@ void ili9341_tp_get(int *x, int *y, int *z, uint8_t raw) { int ybottom = tp_caly & 0x3FFF; if (((xright - xleft) != 0) && ((ybottom - ytop) != 0)) { - *x = ((*x - xleft) * 320) / (xright - xleft); - *y = ((*y - ytop) * 240) / (ybottom - ytop); + *x = ((*x - xleft) * ILI9341_WIDTH) / (xright - xleft); + *y = ((*y - ytop) * ILI9341_HEIGHT) / (ybottom - ytop); } + else { *z = 0; *x = 0; @@ -466,9 +468,9 @@ void ili9341_tp_get(int *x, int *y, int *z, uint8_t raw) { } if (*x < 0) *x = 0; - if (*x > 319) *x = 319; + if (*x >= ILI9341_WIDTH) *x = ILI9341_WIDTH-1; if (*y < 0) *y = 0; - if (*y > 239) *y = 239; + if (*y >= ILI9341_HEIGHT) *y = ILI9341_HEIGHT-1; gdisplay_caps_t *caps = gdisplay_ll_get_caps(); @@ -491,4 +493,74 @@ void ili9341_tp_get(int *x, int *y, int *z, uint8_t raw) { } } +void ili9341_bgr_tp_get(int *x, int *y, int *z, uint8_t raw) { + int result = -1; + int tmp; + + *x = 0; + *y = 0; + *z = 0; + + result = ili9341_tp_read(0xB0, 3); + if (result > 50) { + // tp pressed + *z = result; + + result = ili9341_tp_read(0xD0, 10); + if (result >= 0) { + *x = result; + + result = ili9341_tp_read(0x90, 10); + if (result >= 0) *y = result; + } + } + + if (result < 0) { + *x = 0; + *y = 0; + *z = result; + return; + } + + if (!raw) { + int xleft = (tp_calx >> 16) & 0x3FFF; + int xright = tp_calx & 0x3FFF; + int ytop = (tp_caly >> 16) & 0x3FFF; + int ybottom = tp_caly & 0x3FFF; + + if (((xright - xleft) != 0) && ((ybottom - ytop) != 0)) { + *x = ((*x - xleft) * ILI9341_WIDTH) / (xright - xleft); + *y = ((*y - ytop) * ILI9341_HEIGHT) / (ybottom - ytop); + } else { + *z = 0; + *x = 0; + *y = 0; + return; + } + + if (*x < 0) *x = 0; + if (*x >= ILI9341_WIDTH) *x = ILI9341_WIDTH-1; + if (*y < 0) *y = 0; + if (*y >= ILI9341_HEIGHT) *y = ILI9341_HEIGHT-1; + + gdisplay_caps_t *caps = gdisplay_ll_get_caps(); + + switch (caps->orient) { + case LANDSCAPE: + tmp = *x; + *x = ILI9341_HEIGHT - *y - 1; + *y = tmp; + break; + case LANDSCAPE_FLIP: + tmp = *x; + *x = *y; + *y = ILI9341_WIDTH - tmp - 1; + break; + case PORTRAIT_FLIP : + *x = ILI9341_WIDTH - *x - 1; + *y = ILI9341_HEIGHT - *y - 1; + break; + } + } +} #endif diff --git a/components/sys/drivers/ili9341.h b/components/sys/drivers/ili9341.h index ffd8ad8fd..7bc1c4a1e 100644 --- a/components/sys/drivers/ili9341.h +++ b/components/sys/drivers/ili9341.h @@ -122,6 +122,7 @@ void ili9341_set_orientation(uint8_t m); void ili9341_bgr_set_orientation(uint8_t m); void ili9341_tp_get(int *x, int *y, int *z, uint8_t raw); +void ili9341_bgr_tp_get(int *x, int *y, int *z, uint8_t raw); void ili9341_tp_set_cal(int calx, int caly); #endif