Adapted for SPITFT in latest Adafruit_GFX library

This commit is contained in:
Phillip Burgess committed 2018-08-10 23:25:27 -07:00
1 parent 188587d0c2
commit 0330bdfd8f
3 files changed
+143 -862

No files matched your search

+61 -700
View File
@@ -6,7 +6,7 @@
* @section intro_sec Introduction
*
* This is the documentation for Adafruit's ILI9341 driver for the
* Arduino platform.
* Arduino platform.
*
* This library works with the Adafruit 2.8" Touch Shield V2 (SPI)
* http://www.adafruit.com/products/1651
@@ -20,7 +20,7 @@
* 2.2" 18-bit color TFT LCD display with microSD card breakout - ILI9340
* https://www.adafruit.com/product/1770
*
* TFT FeatherWing - 2.4" 320x240 Touchscreen For All Feathers
* TFT FeatherWing - 2.4" 320x240 Touchscreen For All Feathers
* https://www.adafruit.com/product/3315
*
* These displays use SPI to communicate, 4 or 5 pins are required
@@ -49,169 +49,33 @@
#include "Adafruit_ILI9341.h"
#ifndef ARDUINO_STM32_FEATHER
#include "pins_arduino.h"
#ifndef RASPI
#ifndef RASPI
#include "wiring_private.h"
#endif
#endif
#endif
#include <limits.h>
#define MADCTL_MY 0x80 ///< Bottom to top
#define MADCTL_MX 0x40 ///< Right to left
#define MADCTL_MV 0x20 ///< Reverse Mode
#define MADCTL_ML 0x10 ///< LCD refresh Bottom to top
#define MADCTL_RGB 0x00 ///< Red-Green-Blue pixel order
#define MADCTL_BGR 0x08 ///< Blue-Green-Red pixel order
#define MADCTL_MH 0x04 ///< LCD refresh right to left
/*
* Control Pins
* */
#ifdef USE_FAST_PINIO
#define SPI_DC_HIGH() *dcport |= dcpinmask ///< set DataCommand pin high
#define SPI_DC_LOW() *dcport &= ~dcpinmask ///< set DataCommand pin low
#define SPI_CS_HIGH() *csport |= cspinmask ///< set ChipSelect pin high
#define SPI_CS_LOW() *csport &= ~cspinmask ///< set ChipSelect pin high
#else
#define SPI_DC_HIGH() digitalWrite(_dc, HIGH) ///< set DataCommand pin high
#define SPI_DC_LOW() digitalWrite(_dc, LOW) ///< set DataCommand pin low
#define SPI_CS_HIGH() digitalWrite(_cs, HIGH) ///< set ChipSelect pin high
#define SPI_CS_LOW() digitalWrite(_cs, LOW) ///< set ChipSelect pin high
#endif
/*
* Software SPI Macros
* */
#ifdef USE_FAST_PINIO
#define SSPI_MOSI_HIGH() *mosiport |= mosipinmask ///< Bitbang MOSI high
#define SSPI_MOSI_LOW() *mosiport &= ~mosipinmask ///< Bitbang MOSI low
#define SSPI_SCK_HIGH() *clkport |= clkpinmask ///< Bitbang SCLK high
#define SSPI_SCK_LOW() *clkport &= ~clkpinmask ///< Bitbang SCLK low
#define SSPI_MISO_READ() ((*misoport & misopinmask) != 0) ///< Bitbang read MISO
#else
#define SSPI_MOSI_HIGH() digitalWrite(_mosi, HIGH) ///< Bitbang MOSI high
#define SSPI_MOSI_LOW() digitalWrite(_mosi, LOW) ///< Bitbang MOSI low
#define SSPI_SCK_HIGH() digitalWrite(_sclk, HIGH) ///< Bitbang SCLK high
#define SSPI_SCK_LOW() digitalWrite(_sclk, LOW) ///< Bitbang SCLK low
#define SSPI_MISO_READ() digitalRead(_miso) ///< Bitbang read MISO
#endif
#define SSPI_BEGIN_TRANSACTION() ///< Software SPI begin
#define SSPI_END_TRANSACTION() ///< Software SPI end
#define SSPI_WRITE(v) spiWrite(v) ///< Software SPI write 8 bits
#define SSPI_WRITE16(s) SSPI_WRITE((s) >> 8); SSPI_WRITE(s) ///< Software SPI write 16 bits
#define SSPI_WRITE32(l) SSPI_WRITE((l) >> 24); SSPI_WRITE((l) >> 16); SSPI_WRITE((l) >> 8); SSPI_WRITE(l) ///< Software SPI write 32 bits
#define SSPI_WRITE_PIXELS(c,l) for(uint32_t i=0; i<(l); i+=2){ SSPI_WRITE(((uint8_t*)(c))[i+1]); SSPI_WRITE(((uint8_t*)(c))[i]); } ///< Software SPI write 'l' pixels (16 bits each)
/*
* Hardware SPI Macros
* */
#ifndef ESP32
#define SPI_OBJECT SPI ///< Default SPI hardware peripheral
#else
#define SPI_OBJECT _spi ///< Default SPI hardware peripheral
#endif
#if defined (__AVR__) || defined(TEENSYDUINO) || defined(ARDUINO_ARCH_STM32F1)
#define HSPI_SET_CLOCK() SPI_OBJECT.setClockDivider(SPI_CLOCK_DIV2);
#elif defined (__arm__)
#define HSPI_SET_CLOCK() SPI_OBJECT.setClockDivider(11);
#elif defined(ESP8266) || defined(ESP32)
#define HSPI_SET_CLOCK() SPI_OBJECT.setFrequency(_freq);
#elif defined(RASPI)
#define HSPI_SET_CLOCK() SPI_OBJECT.setClock(_freq);
#elif defined(ARDUINO_ARCH_STM32F1)
#define HSPI_SET_CLOCK() SPI_OBJECT.setClock(_freq);
#else
#define HSPI_SET_CLOCK() ///< Hardware SPI set clock frequency
#endif
#ifdef SPI_HAS_TRANSACTION
#define HSPI_BEGIN_TRANSACTION() SPI_OBJECT.beginTransaction(SPISettings(_freq, MSBFIRST, SPI_MODE0))
#define HSPI_END_TRANSACTION() SPI_OBJECT.endTransaction()
#else
#define HSPI_BEGIN_TRANSACTION() HSPI_SET_CLOCK(); SPI_OBJECT.setBitOrder(MSBFIRST); SPI_OBJECT.setDataMode(SPI_MODE0) ///< Hardware SPI begin transaction
#define HSPI_END_TRANSACTION() ///< Hardware SPI end transaction
#endif
#ifdef ESP32
#define SPI_HAS_WRITE_PIXELS
#endif
#if defined(ESP8266) || defined(ESP32)
// Optimized SPI (ESP8266 and ESP32)
#define HSPI_READ() SPI_OBJECT.transfer(0) ///< Hardware SPI read 8 bits
#define HSPI_WRITE(b) SPI_OBJECT.write(b) ///< Hardware SPI write 8 bits
#define HSPI_WRITE16(s) SPI_OBJECT.write16(s) ///< Hardware SPI write 16 bits
#define HSPI_WRITE32(l) SPI_OBJECT.write32(l) ///< Hardware SPI write 32 bits
#ifdef SPI_HAS_WRITE_PIXELS
#define SPI_MAX_PIXELS_AT_ONCE 32
#define HSPI_WRITE_PIXELS(c,l) SPI_OBJECT.writePixels(c,l)
#else
#define HSPI_WRITE_PIXELS(c,l) for(uint32_t i=0; i<((l)/2); i++){ SPI_WRITE16(((uint16_t*)(c))[i]); }
#endif
#else
// Standard Byte-by-Byte SPI
#if defined (__AVR__) || defined(TEENSYDUINO)
static inline uint8_t _avr_spi_read(void) __attribute__((always_inline));
static inline uint8_t _avr_spi_read(void) {
uint8_t r = 0;
SPDR = r;
while(!(SPSR & _BV(SPIF)));
r = SPDR;
return r;
}
#define HSPI_WRITE(b) {SPDR = (b); while(!(SPSR & _BV(SPIF)));}
#define HSPI_READ() _avr_spi_read()
#else
#define HSPI_WRITE(b) SPI_OBJECT.transfer((uint8_t)(b)) ///< Hardware SPI write 8 bits
#define HSPI_READ() HSPI_WRITE(0) ///< Hardware SPI read 8 bits
#endif
#define HSPI_WRITE16(s) HSPI_WRITE((s) >> 8); HSPI_WRITE(s) ///< Hardware SPI write 16 bits
#define HSPI_WRITE32(l) HSPI_WRITE((l) >> 24); HSPI_WRITE((l) >> 16); HSPI_WRITE((l) >> 8); HSPI_WRITE(l) ///< Hardware SPI write 32 bits
#define HSPI_WRITE_PIXELS(c,l) for(uint32_t i=0; i<(l); i+=2){ HSPI_WRITE(((uint8_t*)(c))[i+1]); HSPI_WRITE(((uint8_t*)(c))[i]); } ///< Hardware SPI write 'l' pixels 16-bits each
#endif
/*
* Final SPI Macros
* */
#if defined (ARDUINO_ARCH_ARC32) || defined (ARDUINO_MAXIM)
#define SPI_DEFAULT_FREQ 16000000
#define SPI_DEFAULT_FREQ 16000000
#elif defined (__AVR__) || defined(TEENSYDUINO)
#define SPI_DEFAULT_FREQ 8000000
#define SPI_DEFAULT_FREQ 8000000
#elif defined(ESP8266) || defined(ESP32)
#define SPI_DEFAULT_FREQ 40000000
#define SPI_DEFAULT_FREQ 40000000
#elif defined(RASPI)
#define SPI_DEFAULT_FREQ 80000000
#define SPI_DEFAULT_FREQ 80000000
#elif defined(ARDUINO_ARCH_STM32F1)
#define SPI_DEFAULT_FREQ 36000000
#define SPI_DEFAULT_FREQ 36000000
#else
#define SPI_DEFAULT_FREQ 24000000 ///< Default SPI data clock frequency
#define SPI_DEFAULT_FREQ 24000000 ///< Default SPI data clock frequency
#endif
#define SPI_BEGIN() if(_sclk < 0){SPI_OBJECT.begin();} ///< SPI initialize
#define SPI_BEGIN_TRANSACTION() if(_sclk < 0){HSPI_BEGIN_TRANSACTION();} ///< SPI begin transaction
#define SPI_END_TRANSACTION() if(_sclk < 0){HSPI_END_TRANSACTION();} ///< SPI end transaction
#define SPI_WRITE16(s) if(_sclk < 0){HSPI_WRITE16(s);}else{SSPI_WRITE16(s);} ///< SPI write 16 bits
#define SPI_WRITE32(l) if(_sclk < 0){HSPI_WRITE32(l);}else{SSPI_WRITE32(l);} ///< SPI write 32 bits
#define SPI_WRITE_PIXELS(c,l) if(_sclk < 0){HSPI_WRITE_PIXELS(c,l);}else{SSPI_WRITE_PIXELS(c,l);} ///< SPI write 'l' pixels of 16-bits each
/**************************************************************************/
/*!
@brief Pass 8-bit (each) R,G,B, get back 16-bit packed color
This function converts 8-8-8 RGB data to 16-bit 5-6-5
@param red Red 8 bit color
@param green Green 8 bit color
@param blue Blue 8 bit color
@return Unsigned 16-bit down-sampled color in 5-6-5 format
*/
/**************************************************************************/
uint16_t Adafruit_ILI9341::color565(uint8_t red, uint8_t green, uint8_t blue) {
return ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
}
#define MADCTL_MY 0x80 ///< Bottom to top
#define MADCTL_MX 0x40 ///< Right to left
#define MADCTL_MV 0x20 ///< Reverse Mode
#define MADCTL_ML 0x10 ///< LCD refresh Bottom to top
#define MADCTL_RGB 0x00 ///< Red-Green-Blue pixel order
#define MADCTL_BGR 0x08 ///< Blue-Green-Red pixel order
#define MADCTL_MH 0x04 ///< LCD refresh right to left
/**************************************************************************/
/*!
@@ -225,63 +89,48 @@ uint16_t Adafruit_ILI9341::color565(uint8_t red, uint8_t green, uint8_t blue) {
*/
/**************************************************************************/
Adafruit_ILI9341::Adafruit_ILI9341(int8_t cs, int8_t dc, int8_t mosi,
int8_t sclk, int8_t rst, int8_t miso) : Adafruit_GFX(ILI9341_TFTWIDTH, ILI9341_TFTHEIGHT) {
_cs = cs;
_dc = dc;
_rst = rst;
_sclk = sclk;
_mosi = mosi;
_miso = miso;
_freq = 0;
#ifdef USE_FAST_PINIO
csport = portOutputRegister(digitalPinToPort(_cs));
cspinmask = digitalPinToBitMask(_cs);
dcport = portOutputRegister(digitalPinToPort(_dc));
dcpinmask = digitalPinToBitMask(_dc);
clkport = portOutputRegister(digitalPinToPort(_sclk));
clkpinmask = digitalPinToBitMask(_sclk);
mosiport = portOutputRegister(digitalPinToPort(_mosi));
mosipinmask = digitalPinToBitMask(_mosi);
if(miso >= 0){
misoport = portInputRegister(digitalPinToPort(_miso));
misopinmask = digitalPinToBitMask(_miso);
} else {
misoport = 0;
misopinmask = 0;
}
#endif
int8_t sclk, int8_t rst, int8_t miso) : Adafruit_SPITFT(ILI9341_TFTWIDTH, ILI9341_TFTHEIGHT, cs, dc, mosi, sclk, rst, miso) {
}
/**************************************************************************/
/*!
@brief Instantiate Adafruit ILI9341 driver with software SPI
@brief Instantiate Adafruit ILI9341 driver with hardware SPI
@param cs Chip select pin #
@param dc Data/Command pin #
@param rst Reset pin # (optional, pass -1 if unused)
*/
/**************************************************************************/
Adafruit_ILI9341::Adafruit_ILI9341(int8_t cs, int8_t dc, int8_t rst) : Adafruit_GFX(ILI9341_TFTWIDTH, ILI9341_TFTHEIGHT) {
_cs = cs;
_dc = dc;
_rst = rst;
_sclk = -1;
_mosi = -1;
_miso = -1;
_freq = 0;
#ifdef USE_FAST_PINIO
csport = portOutputRegister(digitalPinToPort(_cs));
cspinmask = digitalPinToBitMask(_cs);
dcport = portOutputRegister(digitalPinToPort(_dc));
dcpinmask = digitalPinToBitMask(_dc);
clkport = 0;
clkpinmask = 0;
mosiport = 0;
mosipinmask = 0;
misoport = 0;
misopinmask = 0;
#endif
Adafruit_ILI9341::Adafruit_ILI9341(int8_t cs, int8_t dc, int8_t rst) : Adafruit_SPITFT(ILI9341_TFTWIDTH, ILI9341_TFTHEIGHT, cs, dc, rst) {
}
static const uint8_t PROGMEM initcmd[] = {
0xEF, 3, 0x03, 0x80, 0x02,
0xCF, 3, 0x00, 0xC1, 0x30,
0xED, 4, 0x64, 0x03, 0x12, 0x81,
0xE8, 3, 0x85, 0x00, 0x78,
0xCB, 5, 0x39, 0x2C, 0x00, 0x34, 0x02,
0xF7, 1, 0x20,
0xEA, 2, 0x00, 0x00,
ILI9341_PWCTR1 , 1, 0x23, // Power control VRH[5:0]
ILI9341_PWCTR2 , 1, 0x10, // Power control SAP[2:0];BT[3:0]
ILI9341_VMCTR1 , 2, 0x3e, 0x28, // VCM control
ILI9341_VMCTR2 , 1, 0x86, // VCM control2
ILI9341_MADCTL , 1, 0x48, // Memory Access Control
ILI9341_VSCRSADD, 1, 0x00, // Vertical scroll zero
ILI9341_PIXFMT , 1, 0x55,
ILI9341_FRMCTR1 , 2, 0x00, 0x18,
ILI9341_DFUNCTR , 3, 0x08, 0x82, 0x27, // Display Function Control
0xF2, 1, 0x00, // 3Gamma Function Disable
ILI9341_GAMMASET , 1, 0x01, // Gamma curve selected
ILI9341_GMCTRP1 , 15, 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, // Set Gamma
0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00,
ILI9341_GMCTRN1 , 15, 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, // Set Gamma
0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F,
ILI9341_SLPOUT , 0x80, // Exit Sleep
ILI9341_DISPON , 0x80, // Display on
0x00 // End of list
};
/**************************************************************************/
/*!
@brief Initialize ILI9341 chip
@@ -289,163 +138,24 @@ Adafruit_ILI9341::Adafruit_ILI9341(int8_t cs, int8_t dc, int8_t rst) : Adafruit_
@param freq Desired SPI clock frequency
*/
/**************************************************************************/
#ifdef ESP32
void Adafruit_ILI9341::begin(uint32_t freq, SPIClass &spi)
#else
void Adafruit_ILI9341::begin(uint32_t freq)
#endif
{
#ifdef ESP32
_spi = spi;
#endif
if(!freq){
freq = SPI_DEFAULT_FREQ;
}
void Adafruit_ILI9341::begin(uint32_t freq) {
if(!freq) freq = SPI_DEFAULT_FREQ;
_freq = freq;
// Control Pins
pinMode(_dc, OUTPUT);
digitalWrite(_dc, LOW);
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
// Software SPI
if(_sclk >= 0){
pinMode(_mosi, OUTPUT);
digitalWrite(_mosi, LOW);
pinMode(_sclk, OUTPUT);
digitalWrite(_sclk, HIGH);
if(_miso >= 0){
pinMode(_miso, INPUT);
}
}
// Hardware SPI
SPI_BEGIN();
// toggle RST low to reset
if (_rst >= 0) {
pinMode(_rst, OUTPUT);
digitalWrite(_rst, HIGH);
delay(100);
digitalWrite(_rst, LOW);
delay(100);
digitalWrite(_rst, HIGH);
delay(200);
}
initSPI(freq);
startWrite();
writeCommand(0xEF);
spiWrite(0x03);
spiWrite(0x80);
spiWrite(0x02);
uint8_t cmd, x, numArgs;
const uint8_t *addr = initcmd;
while((cmd = pgm_read_byte(addr++)) > 0) {
writeCommand(cmd);
x = pgm_read_byte(addr++);
numArgs = x & 0x7F;
while(numArgs--) spiWrite(pgm_read_byte(addr++));
if(x & 0x80) delay(120);
}
writeCommand(0xCF);
spiWrite(0x00);
spiWrite(0XC1);
spiWrite(0X30);
writeCommand(0xED);
spiWrite(0x64);
spiWrite(0x03);
spiWrite(0X12);
spiWrite(0X81);
writeCommand(0xE8);
spiWrite(0x85);
spiWrite(0x00);
spiWrite(0x78);
writeCommand(0xCB);
spiWrite(0x39);
spiWrite(0x2C);
spiWrite(0x00);
spiWrite(0x34);
spiWrite(0x02);
writeCommand(0xF7);
spiWrite(0x20);
writeCommand(0xEA);
spiWrite(0x00);
spiWrite(0x00);
writeCommand(ILI9341_PWCTR1); //Power control
spiWrite(0x23); //VRH[5:0]
writeCommand(ILI9341_PWCTR2); //Power control
spiWrite(0x10); //SAP[2:0];BT[3:0]
writeCommand(ILI9341_VMCTR1); //VCM control
spiWrite(0x3e);
spiWrite(0x28);
writeCommand(ILI9341_VMCTR2); //VCM control2
spiWrite(0x86); //--
writeCommand(ILI9341_MADCTL); // Memory Access Control
spiWrite(0x48);
writeCommand(ILI9341_VSCRSADD); // Vertical scroll
SPI_WRITE16(0); // Zero
writeCommand(ILI9341_PIXFMT);
spiWrite(0x55);
writeCommand(ILI9341_FRMCTR1);
spiWrite(0x00);
spiWrite(0x18);
writeCommand(ILI9341_DFUNCTR); // Display Function Control
spiWrite(0x08);
spiWrite(0x82);
spiWrite(0x27);
writeCommand(0xF2); // 3Gamma Function Disable
spiWrite(0x00);
writeCommand(ILI9341_GAMMASET); //Gamma curve selected
spiWrite(0x01);
writeCommand(ILI9341_GMCTRP1); //Set Gamma
spiWrite(0x0F);
spiWrite(0x31);
spiWrite(0x2B);
spiWrite(0x0C);
spiWrite(0x0E);
spiWrite(0x08);
spiWrite(0x4E);
spiWrite(0xF1);
spiWrite(0x37);
spiWrite(0x07);
spiWrite(0x10);
spiWrite(0x03);
spiWrite(0x0E);
spiWrite(0x09);
spiWrite(0x00);
writeCommand(ILI9341_GMCTRN1); //Set Gamma
spiWrite(0x00);
spiWrite(0x0E);
spiWrite(0x14);
spiWrite(0x03);
spiWrite(0x11);
spiWrite(0x07);
spiWrite(0x31);
spiWrite(0xC1);
spiWrite(0x48);
spiWrite(0x08);
spiWrite(0x0F);
spiWrite(0x0C);
spiWrite(0x31);
spiWrite(0x36);
spiWrite(0x0F);
writeCommand(ILI9341_SLPOUT); //Exit Sleep
delay(120);
writeCommand(ILI9341_DISPON); //Display on
delay(120);
endWrite();
_width = ILI9341_TFTWIDTH;
@@ -534,271 +244,6 @@ void Adafruit_ILI9341::setAddrWindow(uint16_t x, uint16_t y, uint16_t w, uint16_
writeCommand(ILI9341_RAMWR); // write to RAM
}
/**************************************************************************/
/*!
@brief Blit 1 pixel of color without setting up SPI transaction
@param color 16-bits of 5-6-5 color data
*/
/**************************************************************************/
void Adafruit_ILI9341::pushColor(uint16_t color) {
SPI_WRITE16(color);
}
/**************************************************************************/
/*!
@brief Blit 1 pixel of color without setting up SPI transaction
@param color 16-bits of 5-6-5 color data
*/
/**************************************************************************/
void Adafruit_ILI9341::writePixel(uint16_t color){
SPI_WRITE16(color);
}
/**************************************************************************/
/*!
@brief Blit 'len' pixels of color without setting up SPI transaction
@param colors Array of 16-bit 5-6-5 color data
@param len Number of 16-bit pixels in colors array
*/
/**************************************************************************/
void Adafruit_ILI9341::writePixels(uint16_t * colors, uint32_t len){
SPI_WRITE_PIXELS((uint8_t*)colors , len * 2);
}
/**************************************************************************/
/*!
@brief Blit 'len' pixels of a single color without setting up SPI transaction
@param color 16-bits of 5-6-5 color data
@param len Number of 16-bit pixels you want to write out with same color
*/
/**************************************************************************/
void Adafruit_ILI9341::writeColor(uint16_t color, uint32_t len){
#ifdef SPI_HAS_WRITE_PIXELS
if(_sclk >= 0){
for (uint32_t t=0; t<len; t++){
writePixel(color);
}
return;
}
static uint16_t temp[SPI_MAX_PIXELS_AT_ONCE];
size_t blen = (len > SPI_MAX_PIXELS_AT_ONCE)?SPI_MAX_PIXELS_AT_ONCE:len;
uint16_t tlen = 0;
for (uint32_t t=0; t<blen; t++){
temp[t] = color;
}
while(len){
tlen = (len>blen)?blen:len;
writePixels(temp, tlen);
len -= tlen;
}
#else
uint8_t hi = color >> 8, lo = color;
if(_sclk < 0){ //AVR Optimization
for (uint32_t t=len; t; t--){
HSPI_WRITE(hi);
HSPI_WRITE(lo);
}
return;
}
for (uint32_t t=len; t; t--){
spiWrite(hi);
spiWrite(lo);
}
#endif
}
/**************************************************************************/
/*!
@brief Draw a single pixel, DOES NOT set up SPI transaction
@param x TFT X location
@param y TFT Y location
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_ILI9341::writePixel(int16_t x, int16_t y, uint16_t color) {
if((x < 0) ||(x >= _width) || (y < 0) || (y >= _height)) return;
setAddrWindow(x,y,1,1);
writePixel(color);
}
/**************************************************************************/
/*!
@brief Fill a rectangle, DOES NOT set up SPI transaction
@param x TFT X location begin
@param y TFT Y location begin
@param w Width of rectangle
@param h Height of rectangle
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_ILI9341::writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color){
if((x >= _width) || (y >= _height)) return;
int16_t x2 = x + w - 1, y2 = y + h - 1;
if((x2 < 0) || (y2 < 0)) return;
// Clip left/top
if(x < 0) {
x = 0;
w = x2 + 1;
}
if(y < 0) {
y = 0;
h = y2 + 1;
}
// Clip right/bottom
if(x2 >= _width) w = _width - x;
if(y2 >= _height) h = _height - y;
int32_t len = (int32_t)w * h;
setAddrWindow(x, y, w, h);
writeColor(color, len);
}
/**************************************************************************/
/*!
@brief Draw a vertical line, DOES NOT set up SPI transaction
@param x TFT X location begin
@param y TFT Y location begin
@param l Length of line in pixels
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_ILI9341::writeFastVLine(int16_t x, int16_t y, int16_t l, uint16_t color){
writeFillRect(x, y, 1, l, color);
}
/**************************************************************************/
/*!
@brief Draw a horizontal line, DOES NOT set up SPI transaction
@param x TFT X location begin
@param y TFT Y location begin
@param l Length of line in pixels
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_ILI9341::writeFastHLine(int16_t x, int16_t y, int16_t l, uint16_t color){
writeFillRect(x, y, l, 1, color);
}
/**************************************************************************/
/*!
@brief Draw a single pixel, includes code for SPI transaction
@param x TFT X location
@param y TFT Y location
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_ILI9341::drawPixel(int16_t x, int16_t y, uint16_t color){
startWrite();
writePixel(x, y, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a vertical line, includes code for SPI transaction
@param x TFT X location begin
@param y TFT Y location begin
@param l Length of line in pixels
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_ILI9341::drawFastVLine(int16_t x, int16_t y,
int16_t l, uint16_t color) {
startWrite();
writeFastVLine(x, y, l, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a horizontal line, includes code for SPI transaction
@param x TFT X location begin
@param y TFT Y location begin
@param l Length of line in pixels
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_ILI9341::drawFastHLine(int16_t x, int16_t y,
int16_t l, uint16_t color) {
startWrite();
writeFastHLine(x, y, l, color);
endWrite();
}
/**************************************************************************/
/*!
@brief Fill a rectangle, includes code for SPI transaction
@param x TFT X location begin
@param y TFT Y location begin
@param w Width of rectangle
@param h Height of rectangle
@param color 16-bit 5-6-5 Color to fill with
*/
/**************************************************************************/
void Adafruit_ILI9341::fillRect(int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t color) {
startWrite();
writeFillRect(x,y,w,h,color);
endWrite();
}
/**************************************************************************/
/*!
@brief Draw RGB rectangle of data from RAM to a location on screen
Adapted from https://github.com/PaulStoffregen/ILI9341_t3
by Marc MERLIN. See examples/pictureEmbed to use this.
5/6/2017: function name and arguments have changed for compatibility
with current GFX library and to avoid naming problems in prior
implementation. Formerly drawBitmap() with arguments in different order.
@param x TFT X location begin
@param y TFT Y location begin
@param pcolors Pointer to 16-bit color data
@param w Width of pcolors rectangle
@param h Height of pcolors rectangle
*/
/**************************************************************************/
void Adafruit_ILI9341::drawRGBBitmap(int16_t x, int16_t y,
uint16_t *pcolors, int16_t w, int16_t h) {
int16_t x2, y2; // Lower-right coord
if(( x >= _width ) || // Off-edge right
( y >= _height) || // " top
((x2 = (x+w-1)) < 0 ) || // " left
((y2 = (y+h-1)) < 0) ) return; // " bottom
int16_t bx1=0, by1=0, // Clipped top-left within bitmap
saveW=w; // Save original bitmap width value
if(x < 0) { // Clip left
w += x;
bx1 = -x;
x = 0;
}
if(y < 0) { // Clip top
h += y;
by1 = -y;
y = 0;
}
if(x2 >= _width ) w = _width - x; // Clip right
if(y2 >= _height) h = _height - y; // Clip bottom
pcolors += by1 * saveW + bx1; // Offset bitmap ptr to clipped top-left
startWrite();
setAddrWindow(x, y, w, h); // Clipped area
while(h--) { // For each (clipped) scanline...
writePixels(pcolors, w); // Push one (clipped) row
pcolors += saveW; // Advance pointer by one full (unclipped) line
}
endWrite();
}
/**************************************************************************/
/*!
@brief Read 8 bits of data from ILI9341 configuration memory. NOT from RAM!
@@ -810,9 +255,7 @@ void Adafruit_ILI9341::drawRGBBitmap(int16_t x, int16_t y,
/**************************************************************************/
uint8_t Adafruit_ILI9341::readcommand8(uint8_t command, uint8_t index) {
uint32_t freq = _freq;
if(_freq > 24000000){
_freq = 24000000;
}
if(_freq > 24000000) _freq = 24000000;
startWrite();
writeCommand(0xD9); // woo sekret command?
spiWrite(0x10 + index);
@@ -823,85 +266,3 @@ uint8_t Adafruit_ILI9341::readcommand8(uint8_t command, uint8_t index) {
return r;
}
/**************************************************************************/
/*!
@brief Begin SPI transaction, for software or hardware SPI
*/
/**************************************************************************/
void Adafruit_ILI9341::startWrite(void){
SPI_BEGIN_TRANSACTION();
SPI_CS_LOW();
}
/**************************************************************************/
/*!
@brief End SPI transaction, for software or hardware SPI
*/
/**************************************************************************/
void Adafruit_ILI9341::endWrite(void){
SPI_CS_HIGH();
SPI_END_TRANSACTION();
}
/**************************************************************************/
/*!
@brief Write 8-bit data to command/register (DataCommand line low).
Does not set up SPI transaction.
@param cmd The command/register to transmit
*/
/**************************************************************************/
void Adafruit_ILI9341::writeCommand(uint8_t cmd){
SPI_DC_LOW();
spiWrite(cmd);
SPI_DC_HIGH();
}
/**************************************************************************/
/*!
@brief Read 8-bit data via hardware or software SPI. Does not set up SPI transaction.
@returns One byte of data from SPI
*/
/**************************************************************************/
uint8_t Adafruit_ILI9341::spiRead() {
if(_sclk < 0){
return HSPI_READ();
}
if(_miso < 0){
return 0;
}
uint8_t r = 0;
for (uint8_t i=0; i<8; i++) {
SSPI_SCK_LOW();
SSPI_SCK_HIGH();
r <<= 1;
if (SSPI_MISO_READ()){
r |= 0x1;
}
}
return r;
}
/**************************************************************************/
/*!
@brief Write 8-bit data via hardware or software SPI. Does not set up SPI transaction.
@param b Byte of data to write over SPI
*/
/**************************************************************************/
void Adafruit_ILI9341::spiWrite(uint8_t b) {
if(_sclk < 0){
HSPI_WRITE(b);
return;
}
for(uint8_t bit = 0x80; bit; bit >>= 1){
if((b) & bit){
SSPI_MOSI_HIGH();
} else {
SSPI_MOSI_LOW();
}
SSPI_SCK_LOW();
SSPI_SCK_HIGH();
}
}