Skip to content

Commit f5e1649

Browse files
committed
add support for selective SPI device!
1 parent 3a14516 commit f5e1649

File tree

4 files changed

+37
-18
lines changed

4 files changed

+37
-18
lines changed

Adafruit_SPITFT.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,29 @@ Adafruit_SPITFT::Adafruit_SPITFT(uint16_t w, uint16_t h,
126126
/**************************************************************************/
127127
Adafruit_SPITFT::Adafruit_SPITFT(uint16_t w, uint16_t h,
128128
int8_t cs, int8_t dc, int8_t rst)
129+
: Adafruit_SPITFT(w, h, &SPI, cs, dc, rst)
130+
{
131+
// We just call the hardware SPI instantiator with the default SPI device (&SPI)
132+
}
133+
134+
/**************************************************************************/
135+
/*!
136+
@brief Instantiate Adafruit SPI display driver with hardware SPI
137+
@param w Display width in pixels
138+
@param h Display height in pixels
139+
@param spiClass A pointer to an SPI hardware interface, e.g. &SPI1
140+
@param cs Chip select pin #
141+
@param dc Data/Command pin #
142+
@param rst Reset pin # (optional, pass -1 if unused)
143+
*/
144+
/**************************************************************************/
145+
Adafruit_SPITFT::Adafruit_SPITFT(uint16_t w, uint16_t h, SPIClass *spiClass,
146+
int8_t cs, int8_t dc, int8_t rst)
129147
: Adafruit_GFX(w, h) {
130148
_cs = cs;
131149
_dc = dc;
132150
_rst = rst;
151+
_spi = spiClass;
133152
_sclk = -1;
134153
_mosi = -1;
135154
_miso = -1;

Adafruit_SPITFT.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Adafruit_SPITFT : public Adafruit_GFX {
3737
public:
3838
Adafruit_SPITFT(uint16_t w, uint16_t h, int8_t _CS, int8_t _DC, int8_t _MOSI, int8_t _SCLK, int8_t _RST = -1, int8_t _MISO = -1);
3939
Adafruit_SPITFT(uint16_t w, uint16_t h, int8_t _CS, int8_t _DC, int8_t _RST = -1);
40+
Adafruit_SPITFT(uint16_t w, uint16_t h, SPIClass *spiClass, int8_t _CS, int8_t _DC, int8_t _RST = -1);
4041

4142
virtual void begin(uint32_t freq) = 0; ///< Virtual begin() function to set SPI frequency, must be overridden in subclass. @param freq Maximum SPI hardware clock speed
4243

@@ -87,6 +88,7 @@ class Adafruit_SPITFT : public Adafruit_GFX {
8788
uint16_t color565(uint8_t r, uint8_t g, uint8_t b);
8889

8990
protected:
91+
SPIClass *_spi; ///< The SPI device we want to use (set in constructor)
9092
uint32_t _freq; ///< SPI clock frequency (for hardware SPI)
9193
#if defined (__AVR__) || defined(TEENSYDUINO) || defined (ESP8266) || defined (ESP32)
9294
int8_t _cs, _dc, _rst, _sclk, _mosi, _miso;

Adafruit_SPITFT_Macros.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,25 @@
4646
* Hardware SPI Macros
4747
* */
4848

49-
#define SPI_OBJECT SPI
50-
5149
#if defined (__AVR__) || defined(TEENSYDUINO) || defined(ARDUINO_ARCH_STM32F1)
52-
#define HSPI_SET_CLOCK() SPI_OBJECT.setClockDivider(SPI_CLOCK_DIV2);
50+
#define HSPI_SET_CLOCK() _spi->setClockDivider(SPI_CLOCK_DIV2);
5351
#elif defined (__arm__)
54-
#define HSPI_SET_CLOCK() SPI_OBJECT.setClockDivider(11);
52+
#define HSPI_SET_CLOCK() _spi->setClockDivider(11);
5553
#elif defined(ESP8266) || defined(ESP32)
56-
#define HSPI_SET_CLOCK() SPI_OBJECT.setFrequency(_freq);
54+
#define HSPI_SET_CLOCK() _spi->setFrequency(_freq);
5755
#elif defined(RASPI)
58-
#define HSPI_SET_CLOCK() SPI_OBJECT.setClock(_freq);
56+
#define HSPI_SET_CLOCK() _spi->setClock(_freq);
5957
#elif defined(ARDUINO_ARCH_STM32F1)
60-
#define HSPI_SET_CLOCK() SPI_OBJECT.setClock(_freq);
58+
#define HSPI_SET_CLOCK() _spi->setClock(_freq);
6159
#else
6260
#define HSPI_SET_CLOCK()
6361
#endif
6462

6563
#ifdef SPI_HAS_TRANSACTION
66-
#define HSPI_BEGIN_TRANSACTION() SPI_OBJECT.beginTransaction(SPISettings(_freq, MSBFIRST, SPI_MODE0))
67-
#define HSPI_END_TRANSACTION() SPI_OBJECT.endTransaction()
64+
#define HSPI_BEGIN_TRANSACTION() _spi->beginTransaction(SPISettings(_freq, MSBFIRST, SPI_MODE0))
65+
#define HSPI_END_TRANSACTION() _spi->endTransaction()
6866
#else
69-
#define HSPI_BEGIN_TRANSACTION() HSPI_SET_CLOCK(); SPI_OBJECT.setBitOrder(MSBFIRST); SPI_OBJECT.setDataMode(SPI_MODE0)
67+
#define HSPI_BEGIN_TRANSACTION() HSPI_SET_CLOCK(); _spi->setBitOrder(MSBFIRST); _spi->setDataMode(SPI_MODE0)
7068
#define HSPI_END_TRANSACTION()
7169
#endif
7270

@@ -75,13 +73,13 @@
7573
#endif
7674
#if defined(ESP8266) || defined(ESP32)
7775
// Optimized SPI (ESP8266 and ESP32)
78-
#define HSPI_READ() SPI_OBJECT.transfer(0)
79-
#define HSPI_WRITE(b) SPI_OBJECT.write(b)
80-
#define HSPI_WRITE16(s) SPI_OBJECT.write16(s)
81-
#define HSPI_WRITE32(l) SPI_OBJECT.write32(l)
76+
#define HSPI_READ() _spi->transfer(0)
77+
#define HSPI_WRITE(b) _spi->write(b)
78+
#define HSPI_WRITE16(s) _spi->write16(s)
79+
#define HSPI_WRITE32(l) _spi->write32(l)
8280
#ifdef SPI_HAS_WRITE_PIXELS
8381
#define SPI_MAX_PIXELS_AT_ONCE 32
84-
#define HSPI_WRITE_PIXELS(c,l) SPI_OBJECT.writePixels(c,l)
82+
#define HSPI_WRITE_PIXELS(c,l) _spi->writePixels(c,l)
8583
#else
8684
#define HSPI_WRITE_PIXELS(c,l) for(uint32_t i=0; i<((l)/2); i++){ SPI_WRITE16(((uint16_t*)(c))[i]); }
8785
#endif
@@ -100,15 +98,15 @@ static inline uint8_t _avr_spi_read(void) {
10098
#define HSPI_WRITE(b) {SPDR = (b); while(!(SPSR & _BV(SPIF)));}
10199
#define HSPI_READ() _avr_spi_read()
102100
#else
103-
#define HSPI_WRITE(b) SPI_OBJECT.transfer((uint8_t)(b))
101+
#define HSPI_WRITE(b) _spi->transfer((uint8_t)(b))
104102
#define HSPI_READ() HSPI_WRITE(0)
105103
#endif
106104
#define HSPI_WRITE16(s) HSPI_WRITE((s) >> 8); HSPI_WRITE(s)
107105
#define HSPI_WRITE32(l) HSPI_WRITE((l) >> 24); HSPI_WRITE((l) >> 16); HSPI_WRITE((l) >> 8); HSPI_WRITE(l)
108106
#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]); }
109107
#endif
110108

111-
#define SPI_BEGIN() if(_sclk < 0){SPI_OBJECT.begin();}
109+
#define SPI_BEGIN() if(_sclk < 0){_spi->begin();}
112110
#define SPI_BEGIN_TRANSACTION() if(_sclk < 0){HSPI_BEGIN_TRANSACTION();}
113111
#define SPI_END_TRANSACTION() if(_sclk < 0){HSPI_END_TRANSACTION();}
114112
#define SPI_WRITE16(s) if(_sclk < 0){HSPI_WRITE16(s);}else{SSPI_WRITE16(s);}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Adafruit GFX Library
2-
version=1.2.9
2+
version=1.3.0
33
author=Adafruit
44
maintainer=Adafruit <[email protected]>
55
sentence=Adafruit GFX graphics core library, this is the 'core' class that all our other graphics libraries derive from.

0 commit comments

Comments
 (0)