Skip to content
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

Support for Landscape native ILI9341 modules, and RGB (vs BGR) modules. #67

Open
wants to merge 4 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
50 changes: 33 additions & 17 deletions ILI9341_t3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@
// Teensy 3.1 can only generate 30 MHz SPI when running at 120 MHz (overclock)
// At all other speeds, SPI.beginTransaction() will use the fastest available clock

#define WIDTH ILI9341_TFTWIDTH
#define HEIGHT ILI9341_TFTHEIGHT

// Constructor when using hardware SPI. Faster, but must use SPI pins
// specific to each board type (e.g. 11,13 for Uno, 51,52 for Mega, etc.)
ILI9341_t3::ILI9341_t3(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_t sclk, uint8_t miso)
Expand All @@ -71,8 +68,10 @@ ILI9341_t3::ILI9341_t3(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_
_mosi = mosi;
_sclk = sclk;
_miso = miso;
_width = WIDTH;
_height = HEIGHT;
native_width = ILI9341_TFTWIDTH;
native_height = ILI9341_TFTHEIGHT;
_width = native_width;
_height = native_height;
rotation = 0;
cursor_y = cursor_x = 0;
textsize = 1;
Expand All @@ -82,6 +81,7 @@ ILI9341_t3::ILI9341_t3(uint8_t cs, uint8_t dc, uint8_t rst, uint8_t mosi, uint8_
// Added to see how much impact actually using non hardware CS pin might be
_cspinmask = 0;
_csport = NULL;
setBGR();
}

void ILI9341_t3::setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
Expand Down Expand Up @@ -278,31 +278,47 @@ void ILI9341_t3::setRotation(uint8_t m)
writecommand_cont(ILI9341_MADCTL);
switch (rotation) {
case 0:
writedata8_last(MADCTL_MX | MADCTL_BGR);
_width = ILI9341_TFTWIDTH;
_height = ILI9341_TFTHEIGHT;
writedata8_last(MADCTL_MX | madctl_bgr);
_width = native_width;
_height = native_height;
break;
case 1:
writedata8_last(MADCTL_MV | MADCTL_BGR);
_width = ILI9341_TFTHEIGHT;
_height = ILI9341_TFTWIDTH;
writedata8_last(MADCTL_MV | madctl_bgr);
_width = native_height;
_height = native_width;
break;
case 2:
writedata8_last(MADCTL_MY | MADCTL_BGR);
_width = ILI9341_TFTWIDTH;
_height = ILI9341_TFTHEIGHT;
writedata8_last(MADCTL_MY | madctl_bgr);
_width = native_width;
_height = native_height;
break;
case 3:
writedata8_last(MADCTL_MX | MADCTL_MY | MADCTL_MV | MADCTL_BGR);
_width = ILI9341_TFTHEIGHT;
_height = ILI9341_TFTWIDTH;
writedata8_last(MADCTL_MX | MADCTL_MY | MADCTL_MV | madctl_bgr);
_width = native_height;
_height = native_width;
break;
}
endSPITransaction();
cursor_x = 0;
cursor_y = 0;
}

void ILI9341_t3::setBGR(bool b) {
madctl_bgr = b ? MADCTL_BGR : MADCTL_RGB;
}

bool ILI9341_t3::getBGR() {
return madctl_bgr == MADCTL_BGR;
}

void ILI9341_t3::setLandscape(bool l) {
if (l != getLandscape()) {
int16_t tmp = native_width;
native_width = native_height;
native_height = tmp;
}
}

void ILI9341_t3::setScroll(uint16_t offset)
{
beginSPITransaction(_clock);
Expand Down
16 changes: 16 additions & 0 deletions ILI9341_t3.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ typedef struct {
#define ILI9341_SPICLOCK 30000000
#define ILI9341_SPICLOCK_READ 6500000

//#ifndef swap
//#define swap(a, b) { typeof(a) t = a; a = b; b = t; }
//#endif

class ILI9341_t3 : public Print
{
public:
Expand All @@ -195,6 +199,15 @@ class ILI9341_t3 : public Print
void fillScreenVGradient(uint16_t color1, uint16_t color2);
void fillScreenHGradient(uint16_t color1, uint16_t color2);

void setLandscape(bool l=true);
void setPortrait(bool p=true) { setLandscape(!p); }
bool getLandscape() { return native_width > native_height; }
bool getPortrait() { return !getLandscape(); }
void setBGR(bool b=true);
void setRGB(bool r=true) { setBGR(!r); }
bool getBGR();
bool getRGB() { return !madctl_bgr; }

void setRotation(uint8_t r);
void setScroll(uint16_t offset);
void invertDisplay(boolean i);
Expand Down Expand Up @@ -299,6 +312,7 @@ class ILI9341_t3 : public Print
protected:
unsigned long _clock = ILI9341_SPICLOCK;
int16_t _width, _height; // Display w/h as modified by current rotation
int16_t native_width, native_height;
int16_t cursor_x, cursor_y;
uint16_t textcolor, textbgcolor;
uint8_t textsize, rotation;
Expand All @@ -309,6 +323,8 @@ class ILI9341_t3 : public Print
uint8_t _cs, _dc;
uint8_t pcs_data, pcs_command;
uint8_t _miso, _mosi, _sclk;
uint8_t madctl_bgr;

// add support to allow only one hardware CS (used for dc)
#if defined(__IMXRT1052__) || defined(__IMXRT1062__) // Teensy 4.x
uint32_t _cspinmask;
Expand Down