Skip to content

Commit dd2f5f0

Browse files
committed
Workaround for 240x240 screen rotation issue
- The st7789 controller is for 320x240 screen, but the screen is 240x240, therefore when screen is rotated, an offset of 80 must be added depending on the orientation - Also added some comments
1 parent f5896a3 commit dd2f5f0

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

esp32/examples/ttgo_demo/components/lcd/include/iot_lcd.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class CEspLcd: public Adafruit_GFX
234234
void invertDisplay(bool i);
235235

236236
/*Not useful for user, sets the Region of Interest window*/
237-
inline void setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);
237+
void setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);
238238

239239
/**
240240
* @brief Scroll on Y-axis

esp32/examples/ttgo_demo/components/lcd/iot_lcd.cpp

+24-8
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ POSSIBILITY OF SUCH DAMAGE.
5151
#include "sdkconfig.h"
5252

5353
/*Rotation Defines*/
54-
#define MADCTL_MY 0x80
55-
#define MADCTL_MX 0x40
56-
#define MADCTL_MV 0x20
54+
#define MADCTL_MY 0x80 // Y-mirror
55+
#define MADCTL_MX 0x40 // X-mirror
56+
#define MADCTL_MV 0x20 // X-Y exchange
5757
#define MADCTL_ML 0x10
5858
#define MADCTL_RGB 0x00
5959
#define MADCTL_BGR 0x08
60-
#define MADCTL_MH 0x04
60+
#define MADCTL_MH 0x04 // Horizontal lcd refresh direction
6161

6262

6363
#define SWAPBYTES(i) ((i>>8) | (i<<8))
@@ -103,6 +103,22 @@ void CEspLcd::setSpiBus(lcd_conf_t *lcd_conf)
103103

104104
void CEspLcd::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
105105
{
106+
/*
107+
* If screen is 240x240, an offset must be added when Y is mirrored because
108+
* st7789 controller is for 320x240 and therefore the starting address is 80
109+
* pixels below the screen.
110+
* TODO: adding a start_x and start_y would be cleaner and safer
111+
*/
112+
if(m_width == 240 && m_height == 240) {
113+
if (rotation == 2) { // Y is mirrored
114+
y0 += 80;
115+
y1 += 80;
116+
} else if (rotation == 3) { // X-Y are exchanged
117+
x0 += 80;
118+
x1 += 80;
119+
}
120+
}
121+
106122
xSemaphoreTakeRecursive(spi_mux, portMAX_DELAY);
107123
transmitCmdData(LCD_CASET, MAKEWORD(x0 >> 8, x0 & 0xFF, x1 >> 8, x1 & 0xFF));
108124
transmitCmdData(LCD_PASET, MAKEWORD(y0 >> 8, y0 & 0xFF, y1 >> 8, y1 & 0xFF));
@@ -409,22 +425,22 @@ void CEspLcd::setRotation(uint8_t m)
409425
uint8_t data = 0;
410426
rotation = m % 4; //Can't be more than 3
411427
switch (rotation) {
412-
case 0:
428+
case 0: //
413429
data = MADCTL_MX | MADCTL_BGR;
414430
_width = m_width;
415431
_height = m_height;
416432
break;
417-
case 1:
433+
case 1: // 90° (counterclockwise)
418434
data = MADCTL_MV | MADCTL_BGR;
419435
_width = m_height;
420436
_height = m_width;
421437
break;
422-
case 2:
438+
case 2: // 180° (counterclockwise)
423439
data = MADCTL_MY | MADCTL_BGR;
424440
_width = m_width;
425441
_height = m_height;
426442
break;
427-
case 3:
443+
case 3: // 270° (counterclockwise)
428444
data = MADCTL_MX | MADCTL_MY | MADCTL_MV | MADCTL_BGR;
429445
_width = m_height;
430446
_height = m_width;

0 commit comments

Comments
 (0)