AberLED shield library
Library for the bicolor LED (and TFT screen) shield used in CS12020
TFT_ST7735.h
Go to the documentation of this file.
1 /***************************************************
2  Arduino TFT graphics library targetted at the UNO
3  and Mega boards.
4 
5  This library has been derived from the Adafruit_GFX
6  library and the associated driver library. See text
7  at the end of this file.
8 
9  This is a standalone library that contains the
10  hardware driver, the graphics funtions and the
11  proportional fonts.
12 
13  The larger fonts are Run Length Encoded to reduce
14  their FLASH footprint.
15 
16  ****************************************************/
17 
18 #define INITR_GREENTAB 0x0
19 #define INITR_REDTAB 0x1
20 #define INITR_BLACKTAB 0x2
21 #define INITR_GREENTAB2 0x3 // Use if you get random pixels on two edges of green tab display
22 #define INITB 0xB
23 
24 // Include header file that defines the fonts loaded and the pins to be used
25 #include <User_Setup.h>
26 
27 // Include header file from the FastLED library for fast pin toggling using direct port access
28 #include <TFT_FastPin.h>
29 
30 // Stop fonts being loaded multiple times
31 #ifndef _TFT_ST7735H_
32 #define _TFT_ST7735H_
33 
34 // Only load the fonts defined in User_Setup.h (to save space)
35 // Set flag so RLE rendering code is optionally compiled
36 #ifdef LOAD_GLCD
37  #include "glcdfont.c"
38 #endif
39 
40 #ifdef LOAD_FONT2
41  #include <Fonts/Font16.h>
42 #endif
43 
44 #ifdef LOAD_FONT4
45  #include <Fonts/Font32rle.h>
46  #define LOAD_RLE
47 #endif
48 
49 #ifdef LOAD_FONT6
50  #include <Fonts/Font64rle.h>
51  #ifndef LOAD_RLE
52  #define LOAD_RLE
53  #endif
54 #endif
55 
56 #ifdef LOAD_FONT7
57  #include <Fonts/Font7srle.h>
58  #ifndef LOAD_RLE
59  #define LOAD_RLE
60  #endif
61 #endif
62 
63 #ifdef LOAD_FONT8
64  #include <Fonts/Font72rle.h>
65  #ifndef LOAD_RLE
66  #define LOAD_RLE
67  #endif
68 #endif
69 
70 #include <Arduino.h>
71 #include <Print.h>
72 
73 #include <avr/pgmspace.h>
74 
75 // Swap any type
76 template <typename T> static inline void
77 tftswap(T& a, T& b) { T t = a; a = b; b = t; }
78 
79 //These define the ports and port bits used for the chip select (CS) and data/command (DC) lines
80 #define TFT_CS_L FastPin<TFT_CS>::lo()
81 #define TFT_DC_C FastPin<TFT_DC>::lo()
82 #define TFT_CS_H FastPin<TFT_CS>::hi()
83 #define TFT_DC_D FastPin<TFT_DC>::hi()
84 
85 //These enumerate the text plotting alignment (reference datum point)
86 #define TL_DATUM 0 // Top left (default)
87 #define TC_DATUM 1 // Top centre
88 #define TR_DATUM 2 // Top right
89 #define ML_DATUM 3 // Middle left
90 #define CL_DATUM 3 // Centre left, same as above
91 #define MC_DATUM 4 // Middle centre
92 #define CC_DATUM 4 // Centre centre, same as above
93 #define MR_DATUM 5 // Middle right
94 #define CR_DATUM 5 // Centre right, same as above
95 #define BL_DATUM 6 // Bottom left
96 #define BC_DATUM 7 // Bottom centre
97 #define BR_DATUM 8 // Bottom right
98 
99 
100 // Change the width and height if required (defined in portrait mode)
101 // or use the constructor to over-ride defaults
102 #define ST7735_TFTWIDTH 128
103 #define ST7735_TFTHEIGHT 160
104 
105 #define ST7735_INIT_DELAY 0x80
106 
107 // These are the ST7735 control registers
108 // some flags for initR() :(
109 
110 #define ST7735_TFTWIDTH 128
111 #define ST7735_TFTHEIGHT 160
112 
113 #define ST7735_NOP 0x00
114 #define ST7735_SWRESET 0x01
115 #define ST7735_RDDID 0x04
116 #define ST7735_RDDST 0x09
117 
118 #define ST7735_SLPIN 0x10
119 #define ST7735_SLPOUT 0x11
120 #define ST7735_PTLON 0x12
121 #define ST7735_NORON 0x13
122 
123 #define ST7735_INVOFF 0x20
124 #define ST7735_INVON 0x21
125 #define ST7735_DISPOFF 0x28
126 #define ST7735_DISPON 0x29
127 #define ST7735_CASET 0x2A
128 #define ST7735_RASET 0x2B
129 #define ST7735_RAMWR 0x2C
130 #define ST7735_RAMRD 0x2E
131 
132 #define ST7735_PTLAR 0x30
133 #define ST7735_COLMOD 0x3A
134 #define ST7735_MADCTL 0x36
135 
136 #define ST7735_FRMCTR1 0xB1
137 #define ST7735_FRMCTR2 0xB2
138 #define ST7735_FRMCTR3 0xB3
139 #define ST7735_INVCTR 0xB4
140 #define ST7735_DISSET5 0xB6
141 
142 #define ST7735_PWCTR1 0xC0
143 #define ST7735_PWCTR2 0xC1
144 #define ST7735_PWCTR3 0xC2
145 #define ST7735_PWCTR4 0xC3
146 #define ST7735_PWCTR5 0xC4
147 #define ST7735_VMCTR1 0xC5
148 
149 #define ST7735_RDID1 0xDA
150 #define ST7735_RDID2 0xDB
151 #define ST7735_RDID3 0xDC
152 #define ST7735_RDID4 0xDD
153 
154 #define ST7735_PWCTR6 0xFC
155 
156 #define ST7735_GMCTRP1 0xE0
157 #define ST7735_GMCTRN1 0xE1
158 
159 
160 #define MADCTL_MY 0x80
161 #define MADCTL_MX 0x40
162 #define MADCTL_MV 0x20
163 #define MADCTL_ML 0x10
164 #define MADCTL_RGB 0x00
165 #define MADCTL_BGR 0x08
166 #define MADCTL_MH 0x04
167 
168 // New color definitions use for all my libraries
169 #define TFT_BLACK 0x0000 /* 0, 0, 0 */
170 #define TFT_NAVY 0x000F /* 0, 0, 128 */
171 #define TFT_DARKGREEN 0x03E0 /* 0, 128, 0 */
172 #define TFT_DARKCYAN 0x03EF /* 0, 128, 128 */
173 #define TFT_MAROON 0x7800 /* 128, 0, 0 */
174 #define TFT_PURPLE 0x780F /* 128, 0, 128 */
175 #define TFT_OLIVE 0x7BE0 /* 128, 128, 0 */
176 #define TFT_LIGHTGREY 0xC618 /* 192, 192, 192 */
177 #define TFT_DARKGREY 0x7BEF /* 128, 128, 128 */
178 #define TFT_BLUE 0x001F /* 0, 0, 255 */
179 #define TFT_GREEN 0x07E0 /* 0, 255, 0 */
180 #define TFT_CYAN 0x07FF /* 0, 255, 255 */
181 #define TFT_RED 0xF800 /* 255, 0, 0 */
182 #define TFT_MAGENTA 0xF81F /* 255, 0, 255 */
183 #define TFT_YELLOW 0xFFE0 /* 255, 255, 0 */
184 #define TFT_WHITE 0xFFFF /* 255, 255, 255 */
185 #define TFT_ORANGE 0xFD20 /* 255, 165, 0 */
186 #define TFT_GREENYELLOW 0xAFE5 /* 173, 255, 47 */
187 #define TFT_PINK 0xF81F
188 
189 // Color definitions for backwards compatibility
190 #define ST7735_BLACK 0x0000 /* 0, 0, 0 */
191 #define ST7735_NAVY 0x000F /* 0, 0, 128 */
192 #define ST7735_DARKGREEN 0x03E0 /* 0, 128, 0 */
193 #define ST7735_DARKCYAN 0x03EF /* 0, 128, 128 */
194 #define ST7735_MAROON 0x7800 /* 128, 0, 0 */
195 #define ST7735_PURPLE 0x780F /* 128, 0, 128 */
196 #define ST7735_OLIVE 0x7BE0 /* 128, 128, 0 */
197 #define ST7735_LIGHTGREY 0xC618 /* 192, 192, 192 */
198 #define ST7735_DARKGREY 0x7BEF /* 128, 128, 128 */
199 #define ST7735_BLUE 0x001F /* 0, 0, 255 */
200 #define ST7735_GREEN 0x07E0 /* 0, 255, 0 */
201 #define ST7735_CYAN 0x07FF /* 0, 255, 255 */
202 #define ST7735_RED 0xF800 /* 255, 0, 0 */
203 #define ST7735_MAGENTA 0xF81F /* 255, 0, 255 */
204 #define ST7735_YELLOW 0xFFE0 /* 255, 255, 0 */
205 #define ST7735_WHITE 0xFFFF /* 255, 255, 255 */
206 #define ST7735_ORANGE 0xFD20 /* 255, 165, 0 */
207 #define ST7735_GREENYELLOW 0xAFE5 /* 173, 255, 47 */
208 #define ST7735_PINK 0xF81F
209 
210 typedef struct {
211  const unsigned char *chartbl;
212  const unsigned char *widthtbl;
213  unsigned char height;
214  } fontinfo;
215 
216 // This is a structure to conveniently hold infomation on the fonts
217 // Stores font character image address pointer, width table and height
218 
219 const PROGMEM fontinfo fontdata [] = {
220  { 0, 0, 0 },
221 
222  { 0, 0, 8 },
223 
224  #ifdef LOAD_FONT2
225  { (const unsigned char *)chrtbl_f16, widtbl_f16, chr_hgt_f16},
226  #else
227  { 0, 0, 0 },
228  #endif
229 
230  { 0, 0, 0 },
231 
232  #ifdef LOAD_FONT4
233  { (const unsigned char *)chrtbl_f32, widtbl_f32, chr_hgt_f32},
234  #else
235  { 0, 0, 0 },
236  #endif
237 
238  { 0, 0, 0 },
239 
240  #ifdef LOAD_FONT6
241  { (const unsigned char *)chrtbl_f64, widtbl_f64, chr_hgt_f64},
242  #else
243  { 0, 0, 0 },
244  #endif
245 
246  #ifdef LOAD_FONT7
247  { (const unsigned char *)chrtbl_f7s, widtbl_f7s, chr_hgt_f7s},
248  #else
249  { 0, 0, 0 },
250  #endif
251 
252  #ifdef LOAD_FONT8
253  { (const unsigned char *)chrtbl_f72, widtbl_f72, chr_hgt_f72}
254  #else
255  { 0, 0, 0 }
256  #endif
257 };
258 
259 
260 // Class functions and variables
261 class TFT_ST7735 : public Print {
262 
263  public:
264 
265  TFT_ST7735(int16_t _W = ST7735_TFTWIDTH, int16_t _H = ST7735_TFTHEIGHT);
266 
267  void init(void), begin(void), // Same - begin included for backwards compatibility
268 
269  drawPixel(uint16_t x, uint16_t y, uint16_t color),
270 
271  drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t font),
272  setAddrWindow(int16_t x0, int16_t y0, int16_t x1, int16_t y1),
273 
274  pushColor(uint16_t color),
275  pushColor(uint16_t color, uint16_t len),
276 
277  pushColors(uint16_t *data, uint8_t len),
278  pushColors(uint8_t *data, uint16_t len),
279 
280  fillScreen(uint16_t color),
281 
282  writeEnd(void),
283  backupSPCR(void),
284  restoreSPCR(void),
285 
286  drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color),
287  drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color),
288  drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color),
289 
290  drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color),
291  fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color),
292  drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color),
293  fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color),
294 
295  setRotation(uint8_t r),
296  invertDisplay(boolean i),
297 
298  drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color),
299  drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color),
300  fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color),
301  fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color),
302 
303  drawEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color),
304  fillEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color),
305 
306  drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color),
307  fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color),
308 
309  drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color),
310 
311  setCursor(int16_t x, int16_t y),
312  setCursor(int16_t x, int16_t y, uint8_t font),
313  setTextColor(uint16_t color),
314  setTextColor(uint16_t fgcolor, uint16_t bgcolor),
315  setTextSize(uint8_t size),
316  setTextFont(uint8_t font),
317  setTextWrap(boolean wrap),
318  setTextDatum(uint8_t datum),
319  setTextPadding(uint16_t x_width),
320 
321  spiwrite(uint8_t),
322  writecommand(uint8_t c),
323  writedata(uint8_t d),
324  commandList(const uint8_t *addr);
325 
326  uint8_t getRotation(void);
327 
328  uint16_t fontsLoaded(void),
329  color565(uint8_t r, uint8_t g, uint8_t b);
330 
331  int16_t drawChar(unsigned int uniCode, int x, int y, int font),
332  drawNumber(long long_num,int poX, int poY, int font),
333  drawFloat(float floatNumber,int decimal,int poX, int poY, int font),
334 
335  drawString(char *string, int poX, int poY, int font),
336  drawCentreString(char *string, int dX, int poY, int font),
337  drawRightString(char *string, int dX, int poY, int font),
338 
339  height(void),
340  width(void),
341  textWidth(char *string, int font),
342  fontHeight(int font);
343 
344  virtual size_t write(uint8_t);
345 
346  private:
347 
348  void setWindow(int16_t x0, int16_t y0, int16_t x1, int16_t y1);
349 
350  uint8_t tabcolor,
351  colstart, rowstart; // some displays need this changed
352 
353  boolean hwSPI;
354 
355  uint8_t mySPCR, savedSPCR;
356 
357  int8_t _cs, _dc, _rst, _mosi, _miso, _sclk;
358 
359 
360  protected:
361 
362  int16_t _width, _height, // Display w/h as modified by current rotation
364 
366 
367  uint8_t addr_row, addr_col;
368 
369  uint8_t textfont,
370  textsize,
371  textdatum,
372  rotation;
373 
374  boolean textwrap; // If set, 'wrap' text at right edge of display
375 
376 };
377 
378 #endif
379 
380 /***************************************************
381 
382  ORIGINAL LIBRARY HEADER
383 
384  This is our library for the Adafruit ST7735 Breakout and Shield
385  ----> http://www.adafruit.com/products/1651
386 
387  Check out the links above for our tutorials and wiring diagrams
388  These displays use SPI to communicate, 4 or 5 pins are required to
389  interface (RST is optional)
390  Adafruit invests time and resources providing this open source code,
391  please support Adafruit and open-source hardware by purchasing
392  products from Adafruit!
393 
394  Written by Limor Fried/Ladyada for Adafruit Industries.
395  MIT license, all text above must be included in any redistribution
396 
397  Updated with new functions by Bodmer 14/4/15
398  ****************************************************/
void setTextDatum(uint8_t datum)
Definition: TFT_ST7735.cpp:878
unsigned char height
Definition: TFT_ST7735.h:213
int16_t textWidth(char *string, int font)
Definition: TFT_ST7735.cpp:924
int16_t drawString(char *string, int poX, int poY, int font)
const unsigned char * chartbl
Definition: TFT_ST7735.h:211
void setTextSize(uint8_t size)
Definition: TFT_ST7735.cpp:829
void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color)
void setRotation(uint8_t r)
int16_t drawRightString(char *string, int dX, int poY, int font)
#define ST7735_TFTWIDTH
Definition: TFT_ST7735.h:110
uint16_t color565(uint8_t r, uint8_t g, uint8_t b)
int16_t cursor_y
Definition: TFT_ST7735.h:362
int16_t height(void)
Definition: TFT_ST7735.cpp:915
boolean textwrap
Definition: TFT_ST7735.h:374
void drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t font)
Definition: TFT_ST7735.cpp:970
uint8_t textsize
Definition: TFT_ST7735.h:369
void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
uint16_t textcolor
Definition: TFT_ST7735.h:365
void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color)
Definition: TFT_ST7735.cpp:516
void drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color)
Definition: TFT_ST7735.cpp:438
void fillEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color)
Definition: TFT_ST7735.cpp:604
#define ST7735_TFTHEIGHT
Definition: TFT_ST7735.h:111
void begin(void)
Definition: TFT_ST7735.cpp:199
int16_t width(void)
Definition: TFT_ST7735.cpp:906
int16_t drawCentreString(char *string, int dX, int poY, int font)
const PROGMEM fontinfo fontdata[]
Definition: TFT_ST7735.h:219
uint8_t addr_row
Definition: TFT_ST7735.h:367
void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color)
Definition: TFT_ST7735.cpp:790
int16_t _height
Definition: TFT_ST7735.h:362
void spiwrite(uint8_t)
Definition: TFT_ST7735.cpp:102
void init(void)
Definition: TFT_ST7735.cpp:210
void writedata(uint8_t d)
Definition: TFT_ST7735.cpp:128
void setCursor(int16_t x, int16_t y)
Definition: TFT_ST7735.cpp:808
int16_t padX
Definition: TFT_ST7735.h:362
void drawEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color)
Definition: TFT_ST7735.cpp:560
virtual size_t write(uint8_t)
void pushColors(uint16_t *data, uint8_t len)
const unsigned char * widthtbl
Definition: TFT_ST7735.h:212
void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color)
int16_t drawFloat(float floatNumber, int decimal, int poX, int poY, int font)
uint16_t fontsloaded
Definition: TFT_ST7735.h:365
uint8_t textfont
Definition: TFT_ST7735.h:369
void backupSPCR(void)
Definition: TFT_ST7735.cpp:148
uint8_t getRotation(void)
Definition: TFT_ST7735.cpp:896
void fillScreen(uint16_t color)
Definition: TFT_ST7735.cpp:647
void pushColor(uint16_t color)
void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color)
Definition: TFT_ST7735.cpp:527
void drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color)
Definition: TFT_ST7735.cpp:670
void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color)
Definition: TFT_ST7735.cpp:705
int16_t fontHeight(int font)
Definition: TFT_ST7735.cpp:961
int16_t cursor_x
Definition: TFT_ST7735.h:362
void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color)
Definition: TFT_ST7735.cpp:717
void setTextColor(uint16_t color)
Definition: TFT_ST7735.cpp:848
void restoreSPCR(void)
Definition: TFT_ST7735.cpp:157
void setTextPadding(uint16_t x_width)
Definition: TFT_ST7735.cpp:887
TFT_ST7735(int16_t _W=ST7735_TFTWIDTH, int16_t _H=ST7735_TFTHEIGHT)
Definition: TFT_ST7735.cpp:38
void commandList(const uint8_t *addr)
Definition: TFT_ST7735.cpp:407
void writecommand(uint8_t c)
Definition: TFT_ST7735.cpp:116
void invertDisplay(boolean i)
void writeEnd(void)
Definition: TFT_ST7735.cpp:140
uint16_t textbgcolor
Definition: TFT_ST7735.h:365
uint8_t textdatum
Definition: TFT_ST7735.h:369
void setTextFont(uint8_t font)
Definition: TFT_ST7735.cpp:839
void setTextWrap(boolean wrap)
Definition: TFT_ST7735.cpp:869
uint8_t rotation
Definition: TFT_ST7735.h:369
uint8_t addr_col
Definition: TFT_ST7735.h:367
int16_t _width
Definition: TFT_ST7735.h:362
void setAddrWindow(int16_t x0, int16_t y0, int16_t x1, int16_t y1)
void drawPixel(uint16_t x, uint16_t y, uint16_t color)
void drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color)
Definition: TFT_ST7735.cpp:476
uint16_t fontsLoaded(void)
Definition: TFT_ST7735.cpp:951
int16_t drawNumber(long long_num, int poX, int poY, int font)
void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)
void fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color)
Definition: TFT_ST7735.cpp:690
void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
Definition: TFT_ST7735.cpp:657