forked from adafruit/Adafruit-GFX-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_SPITFT.h
154 lines (129 loc) · 6.75 KB
/
Adafruit_SPITFT.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifndef _ADAFRUIT_SPITFT_
#define _ADAFRUIT_SPITFT_
#if !defined(__AVR_ATtiny85__) // NOT A CHANCE of this stuff working on ATtiny
#if ARDUINO >= 100
#include "Arduino.h"
#include "Print.h"
#else
#include "WProgram.h"
#endif
#include <SPI.h>
#include "Adafruit_GFX.h"
#define USE_FAST_PINIO ///< If set, use PORT access instead of digitalWrite()
//#define USE_SPI_DMA ///< If set, use SPI DMA if available
// If DMA is enabled, Arduino sketch MUST #include <Adafruit_ZeroDMA.h>
// Estimated RAM usage:
// 4 bytes/pixel on display major axis + 8 bytes/pixel on minor axis,
// e.g. 320x240 pixels = 320 * 4 + 240 * 8 = 3,200 bytes.
#if !defined(ARDUINO_ARCH_SAMD)
#undef USE_SPI_DMA ///< Only for SAMD chips
#endif
#ifdef USE_SPI_DMA
#pragma message ("SPI DMA IS ENABLED. HIGHLY EXPERIMENTAL.")
#include <Adafruit_ZeroDMA.h>
#endif
#if defined(__AVR__)
typedef volatile uint8_t RwReg;
#elif defined(ARDUINO_STM32_FEATHER)
typedef volatile uint32 RwReg;
#undef USE_FAST_PINIO
typedef class HardwareSPI SPIClass;
#elif defined(__OPENCR__) || defined (__OPENCM904__)
#undef USE_FAST_PINIO
#elif defined(ARDUINO_FEATHER52) || defined(__arm__)
typedef volatile uint32_t RwReg;
#elif defined(ESP32) || defined(ESP8266)
typedef volatile uint32_t RwReg;
#undef USE_FAST_PINIO
#else
#undef USE_FAST_PINIO
#endif
#include "Adafruit_SPITFT_Macros.h"
/// A heavily optimized SPI display subclass of GFX. Manages SPI bitbanging, transactions, DMA, etc! Despite being called SPITFT, the classic SPI data/command interface is also used by OLEDs.
class Adafruit_SPITFT : public Adafruit_GFX {
public:
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);
Adafruit_SPITFT(uint16_t w, uint16_t h, int8_t _CS, int8_t _DC, int8_t _RST = -1);
Adafruit_SPITFT(uint16_t w, uint16_t h, SPIClass *spiClass, int8_t _CS, int8_t _DC, int8_t _RST = -1);
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
void initSPI(uint32_t freq);
// Required Non-Transaction
void drawPixel(int16_t x, int16_t y, uint16_t color);
// Transaction API
void startWrite(void);
void endWrite(void);
void writePixel(int16_t x, int16_t y, uint16_t color);
void writeFillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
void writeFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
void writeFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
// Transaction API not used by GFX
/*!
@brief SPI displays set an address window rectangle for blitting pixels
@param x Top left corner x coordinate
@param y Top left corner x coordinate
@param w Width of window
@param h Height of window
*/
virtual void setAddrWindow(uint16_t x, uint16_t y, uint16_t w, uint16_t h) = 0;
/*!
@brief Write a 2-byte color (must have a transaction in progress)
@param color 16-bit 5-6-5 Color to draw
*/
void inline writePixel(uint16_t color) { SPI_WRITE16(color); }
void writePixels(uint16_t * colors, uint32_t len);
void writeColor(uint16_t color, uint32_t len);
void pushColor(uint16_t color);
// Recommended Non-Transaction
void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);
void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
using Adafruit_GFX::drawRGBBitmap; // Check base class first
void drawRGBBitmap(int16_t x, int16_t y,
uint16_t *pcolors, int16_t w, int16_t h);
void invertDisplay(boolean i);
uint16_t color565(uint8_t r, uint8_t g, uint8_t b);
void writeCommand(uint8_t cmd);
void spiWrite(uint8_t v);
uint8_t spiRead(void);
protected:
SPIClass *_spi; ///< The SPI device we want to use (set in constructor)
uint32_t _freq; ///< SPI clock frequency (for hardware SPI)
#if defined (__AVR__) || defined(TEENSYDUINO) || defined (ESP8266) || defined (ESP32)
int8_t _cs, _dc, _rst, _sclk, _mosi, _miso;
#else
int32_t _cs, ///< Arduino pin # for chip-select pin
_dc, ///< Arduino pin # for data-command pin
_rst, ///< Arduino pin # for reset pin
_sclk, ///< Arduino pin # for SPI clock pin
_mosi, ///< Arduino pin # for SPI MOSI pin
_miso; ///< Arduino pin # for SPI MISO pin
#endif
#ifdef USE_FAST_PINIO
volatile RwReg *mosiport, ///< Direct chip register for toggling MOSI with fast bitbang IO
*misoport, ///< Direct chip register for toggling MISO with fast bitbang IO
*clkport, ///< Direct chip register for toggling CLK with fast bitbang IO
*dcport, ///< Direct chip register for toggling DC with fast bitbang IO
*csport; ///< Direct chip register for toggling CS with fast bitbang IO
RwReg mosipinmask, ///< bitmask for turning on/off MOSI with fast register bitbang IO
misopinmask, ///< bitmask for turning on/off MISO with fast register bitbang IO
clkpinmask, ///< bitmask for turning on/off CLK with fast register bitbang IO
cspinmask, ///< bitmask for turning on/off CS with fast register bitbang IO
dcpinmask; ///< bitmask for turning on/off DC with fast register bitbang IO
#endif
uint8_t invertOnCommand = 0, ///< SPI command byte to turn on invert
invertOffCommand = 0; ///< SPI command byte to turn off invert
int16_t _xstart = 0; ///< Many displays don't have pixels starting at (0,0) of the internal framebuffer, this is the x offset from 0 to align
int16_t _ystart = 0; ///< Many displays don't have pixels starting at (0,0) of the internal framebuffer, this is the y offset from 0 to align
#ifdef USE_SPI_DMA
Adafruit_ZeroDMA dma; ///< DMA instance
DmacDescriptor *dptr = NULL; ///< 1st descriptor
DmacDescriptor *descriptor = NULL; ///< Allocated descriptor list
uint16_t *pixelBuf[2]; ///< Working buffers
uint16_t maxFillLen; ///< Max pixels per DMA xfer
uint16_t lastFillColor = 0; ///< Last color used w/fill
uint32_t lastFillLen = 0; ///< # of pixels w/last fill
uint8_t onePixelBuf; ///< For hi==lo fill
#endif
};
#endif // !__AVR_ATtiny85__
#endif // !_ADAFRUIT_SPITFT_