From 8e86ca133bbc49e63870f046f95d856fdc1a16aa Mon Sep 17 00:00:00 2001 From: Gozi Date: Wed, 4 Aug 2021 22:46:55 +0800 Subject: [PATCH 1/2] Add screen flip function for ST7789 Describe screen rotation in a human-readable way --- Adafruit_ST7789.cpp | 55 +++++++++++++++++++++++++++++++++++---------- Adafruit_ST7789.h | 14 ++++++++++++ 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/Adafruit_ST7789.cpp b/Adafruit_ST7789.cpp index 9fd2f00..91748b1 100644 --- a/Adafruit_ST7789.cpp +++ b/Adafruit_ST7789.cpp @@ -134,36 +134,67 @@ void Adafruit_ST7789::init(uint16_t width, uint16_t height, uint8_t mode) { */ /**************************************************************************/ void Adafruit_ST7789::setRotation(uint8_t m) { + setRotation(m, false); +} + +/**************************************************************************/ +/*! + @brief Set origin of (0,0) and orientation of TFT display + @param m The index for rotation, from 0-3 inclusive + @param flip Flip the screen horizontally +*/ +/**************************************************************************/ +void Adafruit_ST7789::setRotation(uint8_t m, bool flip) { uint8_t madctl = 0; rotation = m & 3; // can't be higher than 3 switch (rotation) { - case 0: - madctl = ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB; + case 0: // 180deg ST77XX_ROTATE_BOTTOM + if (flip) { + madctl = ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB; + } else { + madctl = ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_RGB; + } _xstart = _colstart; _ystart = _rowstart; _width = windowWidth; _height = windowHeight; break; - case 1: - madctl = ST77XX_MADCTL_MY | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB; - _xstart = _rowstart; - _ystart = _colstart; + case 1: // 270deg ST77XX_ROTATE_RIGHT + if (flip) { + madctl = ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB; + _xstart = _rowstart2; + _ystart = _colstart2; + } else { + madctl = ST77XX_MADCTL_MY | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB; + _xstart = _rowstart; + _ystart = _colstart; + } _height = windowWidth; _width = windowHeight; break; - case 2: - madctl = ST77XX_MADCTL_RGB; + case 2: // 0deg ST77XX_ROTATE_NONE + if (flip) { + madctl = ST77XX_MADCTL_MX | ST77XX_MADCTL_RGB; + } else { + madctl = ST77XX_MADCTL_RGB; + } _xstart = _colstart2; _ystart = _rowstart2; _width = windowWidth; _height = windowHeight; break; - case 3: - madctl = ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB; - _xstart = _rowstart2; - _ystart = _colstart2; + case 3: // 90deg ST77XX_ROTATE_RIGHT + if (flip) { + madctl = ST77XX_MADCTL_MX | ST77XX_MADCTL_MY | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB; + _xstart = _rowstart; + _ystart = _colstart; + } else { + madctl = ST77XX_MADCTL_MX | ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB; + _xstart = _rowstart2; + _ystart = _colstart2; + } _height = windowWidth; _width = windowHeight; break; diff --git a/Adafruit_ST7789.h b/Adafruit_ST7789.h index 7fe09d6..0d6e673 100644 --- a/Adafruit_ST7789.h +++ b/Adafruit_ST7789.h @@ -3,6 +3,19 @@ #include "Adafruit_ST77xx.h" +// Define the screen rotation position in a clockwise direction +// +// NONE +// LEFT + RIGHT +// BOTTOM +// +#define ST77XX_ROTATE_NONE 2 +#define ST77XX_ROTATE_RIGHT 1 +#define ST77XX_ROTATE_LEFT 3 +#define ST77XX_ROTATE_BOTTOM 0 +// Flip the screen horizontally, useful in some applications that use mirrors. +#define ST77XX_FLIP_HORIZONTAL true + /// Subclass of ST77XX type display for ST7789 TFT Driver class Adafruit_ST7789 : public Adafruit_ST77xx { public: @@ -14,6 +27,7 @@ class Adafruit_ST7789 : public Adafruit_ST77xx { #endif // end !ESP8266 void setRotation(uint8_t m); + void setRotation(uint8_t r, bool flip); void init(uint16_t width, uint16_t height, uint8_t spiMode = SPI_MODE0); protected: From 774ad81f47690bb273c124da7ef997ef7c8e3a47 Mon Sep 17 00:00:00 2001 From: Gozi Date: Fri, 6 Aug 2021 03:50:24 +0800 Subject: [PATCH 2/2] fix direction map error --- Adafruit_ST7789.cpp | 2 +- Adafruit_ST7789.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Adafruit_ST7789.cpp b/Adafruit_ST7789.cpp index 91748b1..210395a 100644 --- a/Adafruit_ST7789.cpp +++ b/Adafruit_ST7789.cpp @@ -161,7 +161,7 @@ void Adafruit_ST7789::setRotation(uint8_t m, bool flip) { _width = windowWidth; _height = windowHeight; break; - case 1: // 270deg ST77XX_ROTATE_RIGHT + case 1: // 270deg ST77XX_ROTATE_LEFT if (flip) { madctl = ST77XX_MADCTL_MV | ST77XX_MADCTL_RGB; _xstart = _rowstart2; diff --git a/Adafruit_ST7789.h b/Adafruit_ST7789.h index 0d6e673..7ae8542 100644 --- a/Adafruit_ST7789.h +++ b/Adafruit_ST7789.h @@ -10,8 +10,8 @@ // BOTTOM // #define ST77XX_ROTATE_NONE 2 -#define ST77XX_ROTATE_RIGHT 1 -#define ST77XX_ROTATE_LEFT 3 +#define ST77XX_ROTATE_RIGHT 3 +#define ST77XX_ROTATE_LEFT 1 #define ST77XX_ROTATE_BOTTOM 0 // Flip the screen horizontally, useful in some applications that use mirrors. #define ST77XX_FLIP_HORIZONTAL true