/***************************************************
This is a library for the Adafruit 1.8" SPI display.
This library works with the Adafruit 1.8" TFT Breakout w/SD card
----> http://www.adafruit.com/products/358
The 1.8" TFT shield
----> https://www.adafruit.com/product/802
The 1.44" TFT breakout
----> https://www.adafruit.com/product/2088
as well as Adafruit raw 1.8" TFT display
----> http://www.adafruit.com/products/618
Check out the links above for our tutorials and wiring diagrams
These displays use SPI to communicate, 4 or 5 pins are required to
interface (RST is optional)
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
MIT license, all text above must be included in any redistribution
****************************************************/
#include "Adafruit_ST7735.h"
#include <limits.h>
#include "pins_arduino.h"
#include "wiring_private.h"
#include <SPI.h>
#if !defined(ST7735_USE_HWSPI_ONLY)
// Constructor when using software SPI. All output pins are configurable.
Adafruit_ST7735::Adafruit_ST7735(int8_t cs, int8_t rs, int8_t sid, int8_t sclk, int8_t rst)
: Adafruit_GFX(ST7735_TFTWIDTH, ST7735_TFTHEIGHT_18)
{
_cs = cs;
_rs = rs;
_sid = sid;
_sclk = sclk;
_rst = rst;
hwSPI = false;
}
#endif
// 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.)
Adafruit_ST7735::Adafruit_ST7735(int8_t cs, int8_t rs, int8_t rst)
: Adafruit_GFX(ST7735_TFTWIDTH, ST7735_TFTHEIGHT_18) {
_cs = cs;
_rs = rs;
_rst = rst;
hwSPI = true;
_sid = _sclk = 0;
}
#if defined(CORE_TEENSY) && !defined(__AVR__)
#define __AVR__
#endif
#if defined(ST7735_USE_HWSPI_ONLY)
inline void Adafruit_ST7735::spiwrite(uint8_t c) {
SPI.transfer(c);
}
#elif defined(__AVR__)
inline void Adafruit_ST7735::spiwrite(uint8_t c) {
//Serial.println(c, HEX);
if (hwSPI) {
SPDR = c;
while(!(SPSR & _BV(SPIF)));
} else {
// Fast SPI bitbang swiped from LPD8806 library
for(uint8_t bit = 0x80; bit; bit >>= 1) {
if(c & bit) *dataport |= datapinmask;
else *dataport &= ~datapinmask;
*clkport |= clkpinmask;
*clkport &= ~clkpinmask;
}
}
}
#elif defined(__SAM3X8E__)
inline void Adafruit_ST7735::spiwrite(uint8_t c) {
//Serial.println(c, HEX);
if (hwSPI) {
SPI.transfer(c);
} else {
// Fast SPI bitbang swiped from LPD8806 library
for(uint8_t bit = 0x80; bit; bit >>= 1) {
if(c & bit) dataport->PIO_SODR |= datapinmask;
else dataport->PIO_CODR |= datapinmask;
clkport->PIO_SODR |= clkpinmask;
clkport->PIO_CODR |= clkpinmask;
}
}
}
#else
#error Unsupported board.
#endif
#if defined(ST7735_USE_HWSPI_WRITE16)
inline void Adafruit_ST7735::spiwrite16(uint16_t v) {
SPI.write16(v);
}
#else
inline void Adafruit_ST7735::spiwrite16(uint16_t v) {
spiwrite((uint8_t)(v >> 8));
spiwrite((uint8_t)(v >> 0));
}
#endif
#if defined(ST7735_USE_GENERIC_IO)
inline void Adafruit_ST7735::setCS(bool level) {
digitalWrite(_cs, level);
}
inline void Adafruit_ST7735::setRS(bool level) {
digitalWrite(_rs, level);
}
#elif defined(__AVR__)
inline void Adafruit_ST7735::setCS(bool level) {
if (level) {
*csport |= cspinmask;
} else {
*csport &= ~cspinmask;
}
}
inline void Adafruit_ST7735::setRS(bool level) {
if (level) {
*rsport |= rspinmask;
} else {
*rsport &= ~rspinmask;
}
}
#elif defined(__SAM3X8E__)
inline void Adafruit_ST7735::setCS(bool level) {
if (level) {
csport->PIO_SODR |= cspinmask;
} else {
csport->PIO_CODR |= cspinmask;
}
}
inline void Adafruit_ST7735::setRS(bool level) {
if (level) {
rsport->PIO_SODR |= rspinmask;
} else {
rsport->PIO_CODR |= rspinmask;
}
}
#else
#error Unsupported board.
#endif
void Adafruit_ST7735::writecommand(uint8_t c) {
setRS(false);
setCS(false);
//Serial.print("C ");
spiwrite(c);
setCS(true);
}
void Adafruit_ST7735::writedata(uint8_t c) {
setRS(true);
setCS(false);
//Serial.print("D ");
spiwrite(c);
setCS(true);
}
void Adafruit_ST7735::writedata16(uint16_t c) {
setRS(true);
setCS(false);
//Serial.print("D ");
spiwrite16(c);
setCS(true);
}
// Rather than a bazillion writecommand() and writedata() calls, screen
// initialization commands and arguments are organized in these tables
// stored in PROGMEM. The table may look bulky, but that's mostly the
// formatting -- storage-wise this is hundreds of bytes more compact
// than the equivalent code. Companion function follows.
#define DELAY 0x80
static const uint8_t PROGMEM
Bcmd[] = { // Initialization commands for 7735B screens
18, // 18 commands in list:
ST7735_SWRESET, DELAY, // 1: Software reset, no args, w/delay
50, // 50 ms delay
ST7735_SLPOUT , DELAY, // 2: Out of sleep mode, no args, w/delay
255, // 255 = 500 ms delay
ST7735_COLMOD , 1+DELAY, // 3: Set color mode, 1 arg + delay:
0x05, // 16-bit color
10, // 10 ms delay
ST7735_FRMCTR1, 3+DELAY, // 4: Frame rate control, 3 args + delay:
0x00, // fastest refresh
0x06, // 6 lines front porch
0x03, // 3 lines back porch
10, // 10 ms delay
ST7735_MADCTL , 1 , // 5: Memory access ctrl (directions), 1 arg:
0x08, // Row addr/col addr, bottom to top refresh
ST7735_DISSET5, 2 , // 6: Display settings #5, 2 args, no delay:
0x15, // 1 clk cycle nonoverlap, 2 cycle gate
// rise, 3 cycle osc equalize
0x02, // Fix on VTL
ST7735_INVCTR , 1 , // 7: Display inversion control, 1 arg:
0x0, // Line inversion
ST7735_PWCTR1 , 2+DELAY, // 8: Power control, 2 args + delay:
0x02, // GVDD = 4.7V
0x70, // 1.0uA
10, // 10 ms delay
ST7735_PWCTR2 , 1 , // 9: Power control, 1 arg, no delay:
0x05, // VGH = 14.7V, VGL = -7.35V
ST7735_PWCTR3 , 2 , // 10: Power control, 2 args, no delay:
0x01, // Opamp current small
0x02, // Boost frequency
ST7735_VMCTR1 , 2+DELAY, // 11: Power control, 2 args + delay:
0x3C, // VCOMH = 4V
0x38, // VCOML = -1.1V
10, // 10 ms delay
ST7735_PWCTR6 , 2 , // 12: Power control, 2 args, no delay:
0x11, 0x15,
ST7735_GMCTRP1,16 , // 13: Magical unicorn dust, 16 args, no delay:
0x09, 0x16, 0x09, 0x20, // (seriously though, not sure what
0x21, 0x1B, 0x13, 0x19, // these config values represent)
0x17, 0x15, 0x1E, 0x2B,
0x04, 0x05, 0x02, 0x0E,
ST7735_GMCTRN1,16+DELAY, // 14: Sparkles and rainbows, 16 args + delay:
0x0B, 0x14, 0x08, 0x1E, // (ditto)
0x22, 0x1D, 0x18, 0x1E,
0x1B, 0x1A, 0x24, 0x2B,
0x06, 0x06, 0x02, 0x0F,
10, // 10 ms delay
ST7735_CASET , 4 , // 15: Column addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 2
0x00, 0x81, // XEND = 129
ST7735_RASET , 4 , // 16: Row addr set, 4 args, no delay:
0x00, 0x02, // XSTART = 1
0x00, 0x81, // XEND = 160
ST7735_NORON , DELAY, // 17: Normal display on, no args, w/delay
10, // 10 ms dela