Skip to content

Add screen flip function for ST7789 #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions Adafruit_ST7789.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_LEFT
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;
Expand Down
14 changes: 14 additions & 0 deletions Adafruit_ST7789.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 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

/// Subclass of ST77XX type display for ST7789 TFT Driver
class Adafruit_ST7789 : public Adafruit_ST77xx {
public:
Expand All @@ -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:
Expand Down