-
Notifications
You must be signed in to change notification settings - Fork 32
/
TFT_ST7735.h
398 lines (303 loc) · 12.1 KB
/
TFT_ST7735.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/***************************************************
Arduino TFT graphics library targetted at the UNO
and Mega boards.
This library has been derived from the Adafruit_GFX
library and the associated driver library. See text
at the end of this file.
This is a standalone library that contains the
hardware driver, the graphics funtions and the
proportional fonts.
The larger fonts are Run Length Encoded to reduce
their FLASH footprint.
****************************************************/
#define INITR_GREENTAB 0x0
#define INITR_REDTAB 0x1
#define INITR_BLACKTAB 0x2
#define INITR_GREENTAB2 0x3 // Use if you get random pixels on two edges of green tab display
#define INITB 0xB
// Include header file that defines the fonts loaded and the pins to be used
#include <User_Setup.h>
// Include header file from the FastLED library for fast pin toggling using direct port access
#include <TFT_FastPin.h>
// Stop fonts being loaded multiple times
#ifndef _TFT_ST7735H_
#define _TFT_ST7735H_
// Only load the fonts defined in User_Setup.h (to save space)
// Set flag so RLE rendering code is optionally compiled
#ifdef LOAD_GLCD
#include <Fonts/glcdfont.c>
#endif
#ifdef LOAD_FONT2
#include <Fonts/Font16.h>
#endif
#ifdef LOAD_FONT4
#include <Fonts/Font32rle.h>
#define LOAD_RLE
#endif
#ifdef LOAD_FONT6
#include <Fonts/Font64rle.h>
#ifndef LOAD_RLE
#define LOAD_RLE
#endif
#endif
#ifdef LOAD_FONT7
#include <Fonts/Font7srle.h>
#ifndef LOAD_RLE
#define LOAD_RLE
#endif
#endif
#ifdef LOAD_FONT8
#include <Fonts/Font72rle.h>
#ifndef LOAD_RLE
#define LOAD_RLE
#endif
#endif
#include <Arduino.h>
#include <Print.h>
#include <avr/pgmspace.h>
// Swap any type
template <typename T> static inline void
tftswap(T& a, T& b) { T t = a; a = b; b = t; }
//These define the ports and port bits used for the chip select (CS) and data/command (DC) lines
#define TFT_CS_L FastPin<TFT_CS>::lo()
#define TFT_DC_C FastPin<TFT_DC>::lo()
#define TFT_CS_H FastPin<TFT_CS>::hi()
#define TFT_DC_D FastPin<TFT_DC>::hi()
//These enumerate the text plotting alignment (reference datum point)
#define TL_DATUM 0 // Top left (default)
#define TC_DATUM 1 // Top centre
#define TR_DATUM 2 // Top right
#define ML_DATUM 3 // Middle left
#define CL_DATUM 3 // Centre left, same as above
#define MC_DATUM 4 // Middle centre
#define CC_DATUM 4 // Centre centre, same as above
#define MR_DATUM 5 // Middle right
#define CR_DATUM 5 // Centre right, same as above
#define BL_DATUM 6 // Bottom left
#define BC_DATUM 7 // Bottom centre
#define BR_DATUM 8 // Bottom right
// Change the width and height if required (defined in portrait mode)
// or use the constructor to over-ride defaults
#define ST7735_TFTWIDTH 128
#define ST7735_TFTHEIGHT 160
#define ST7735_INIT_DELAY 0x80
// These are the ST7735 control registers
// some flags for initR() :(
#define ST7735_TFTWIDTH 128
#define ST7735_TFTHEIGHT 160
#define ST7735_NOP 0x00
#define ST7735_SWRESET 0x01
#define ST7735_RDDID 0x04
#define ST7735_RDDST 0x09
#define ST7735_SLPIN 0x10
#define ST7735_SLPOUT 0x11
#define ST7735_PTLON 0x12
#define ST7735_NORON 0x13
#define ST7735_INVOFF 0x20
#define ST7735_INVON 0x21
#define ST7735_DISPOFF 0x28
#define ST7735_DISPON 0x29
#define ST7735_CASET 0x2A
#define ST7735_RASET 0x2B
#define ST7735_RAMWR 0x2C
#define ST7735_RAMRD 0x2E
#define ST7735_PTLAR 0x30
#define ST7735_COLMOD 0x3A
#define ST7735_MADCTL 0x36
#define ST7735_FRMCTR1 0xB1
#define ST7735_FRMCTR2 0xB2
#define ST7735_FRMCTR3 0xB3
#define ST7735_INVCTR 0xB4
#define ST7735_DISSET5 0xB6
#define ST7735_PWCTR1 0xC0
#define ST7735_PWCTR2 0xC1
#define ST7735_PWCTR3 0xC2
#define ST7735_PWCTR4 0xC3
#define ST7735_PWCTR5 0xC4
#define ST7735_VMCTR1 0xC5
#define ST7735_RDID1 0xDA
#define ST7735_RDID2 0xDB
#define ST7735_RDID3 0xDC
#define ST7735_RDID4 0xDD
#define ST7735_PWCTR6 0xFC
#define ST7735_GMCTRP1 0xE0
#define ST7735_GMCTRN1 0xE1
#define MADCTL_MY 0x80
#define MADCTL_MX 0x40
#define MADCTL_MV 0x20
#define MADCTL_ML 0x10
#define MADCTL_RGB 0x00
#define MADCTL_BGR 0x08
#define MADCTL_MH 0x04
// New color definitions use for all my libraries
#define TFT_BLACK 0x0000 /* 0, 0, 0 */
#define TFT_NAVY 0x000F /* 0, 0, 128 */
#define TFT_DARKGREEN 0x03E0 /* 0, 128, 0 */
#define TFT_DARKCYAN 0x03EF /* 0, 128, 128 */
#define TFT_MAROON 0x7800 /* 128, 0, 0 */
#define TFT_PURPLE 0x780F /* 128, 0, 128 */
#define TFT_OLIVE 0x7BE0 /* 128, 128, 0 */
#define TFT_LIGHTGREY 0xC618 /* 192, 192, 192 */
#define TFT_DARKGREY 0x7BEF /* 128, 128, 128 */
#define TFT_BLUE 0x001F /* 0, 0, 255 */
#define TFT_GREEN 0x07E0 /* 0, 255, 0 */
#define TFT_CYAN 0x07FF /* 0, 255, 255 */
#define TFT_RED 0xF800 /* 255, 0, 0 */
#define TFT_MAGENTA 0xF81F /* 255, 0, 255 */
#define TFT_YELLOW 0xFFE0 /* 255, 255, 0 */
#define TFT_WHITE 0xFFFF /* 255, 255, 255 */
#define TFT_ORANGE 0xFD20 /* 255, 165, 0 */
#define TFT_GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define TFT_PINK 0xF81F
// Color definitions for backwards compatibility
#define ST7735_BLACK 0x0000 /* 0, 0, 0 */
#define ST7735_NAVY 0x000F /* 0, 0, 128 */
#define ST7735_DARKGREEN 0x03E0 /* 0, 128, 0 */
#define ST7735_DARKCYAN 0x03EF /* 0, 128, 128 */
#define ST7735_MAROON 0x7800 /* 128, 0, 0 */
#define ST7735_PURPLE 0x780F /* 128, 0, 128 */
#define ST7735_OLIVE 0x7BE0 /* 128, 128, 0 */
#define ST7735_LIGHTGREY 0xC618 /* 192, 192, 192 */
#define ST7735_DARKGREY 0x7BEF /* 128, 128, 128 */
#define ST7735_BLUE 0x001F /* 0, 0, 255 */
#define ST7735_GREEN 0x07E0 /* 0, 255, 0 */
#define ST7735_CYAN 0x07FF /* 0, 255, 255 */
#define ST7735_RED 0xF800 /* 255, 0, 0 */
#define ST7735_MAGENTA 0xF81F /* 255, 0, 255 */
#define ST7735_YELLOW 0xFFE0 /* 255, 255, 0 */
#define ST7735_WHITE 0xFFFF /* 255, 255, 255 */
#define ST7735_ORANGE 0xFD20 /* 255, 165, 0 */
#define ST7735_GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define ST7735_PINK 0xF81F
typedef struct {
const unsigned char *chartbl;
const unsigned char *widthtbl;
unsigned char height;
} fontinfo;
// This is a structure to conveniently hold infomation on the fonts
// Stores font character image address pointer, width table and height
const PROGMEM fontinfo fontdata [] = {
{ 0, 0, 0 },
{ 0, 0, 8 },
#ifdef LOAD_FONT2
{ (const unsigned char *)chrtbl_f16, widtbl_f16, chr_hgt_f16},
#else
{ 0, 0, 0 },
#endif
{ 0, 0, 0 },
#ifdef LOAD_FONT4
{ (const unsigned char *)chrtbl_f32, widtbl_f32, chr_hgt_f32},
#else
{ 0, 0, 0 },
#endif
{ 0, 0, 0 },
#ifdef LOAD_FONT6
{ (const unsigned char *)chrtbl_f64, widtbl_f64, chr_hgt_f64},
#else
{ 0, 0, 0 },
#endif
#ifdef LOAD_FONT7
{ (const unsigned char *)chrtbl_f7s, widtbl_f7s, chr_hgt_f7s},
#else
{ 0, 0, 0 },
#endif
#ifdef LOAD_FONT8
{ (const unsigned char *)chrtbl_f72, widtbl_f72, chr_hgt_f72}
#else
{ 0, 0, 0 }
#endif
};
// Class functions and variables
class TFT_ST7735 : public Print {
public:
TFT_ST7735(int16_t _W = ST7735_TFTWIDTH, int16_t _H = ST7735_TFTHEIGHT);
void init(void), begin(void), // Same - begin included for backwards compatibility
drawPixel(uint16_t x, uint16_t y, uint16_t color),
drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t font),
setAddrWindow(int16_t x0, int16_t y0, int16_t x1, int16_t y1),
pushColor(uint16_t color),
pushColor(uint16_t color, uint16_t len),
pushColors(uint16_t *data, uint8_t len),
pushColors(uint8_t *data, uint16_t len),
fillScreen(uint16_t color),
writeEnd(void),
backupSPCR(void),
restoreSPCR(void),
drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color),
drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color),
drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color),
drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color),
fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color),
drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color),
fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color),
setRotation(uint8_t r),
invertDisplay(boolean i),
drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color),
drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color),
fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color),
fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color),
drawEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color),
fillEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color),
drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color),
fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color),
drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color),
setCursor(int16_t x, int16_t y),
setCursor(int16_t x, int16_t y, uint8_t font),
setTextColor(uint16_t color),
setTextColor(uint16_t fgcolor, uint16_t bgcolor),
setTextSize(uint8_t size),
setTextFont(uint8_t font),
setTextWrap(boolean wrap),
setTextDatum(uint8_t datum),
setTextPadding(uint16_t x_width),
spiwrite(uint8_t),
writecommand(uint8_t c),
writedata(uint8_t d),
commandList(const uint8_t *addr);
uint8_t getRotation(void);
uint16_t fontsLoaded(void),
color565(uint8_t r, uint8_t g, uint8_t b);
int16_t drawChar(unsigned int uniCode, int x, int y, int font),
drawNumber(long long_num,int poX, int poY, int font),
drawFloat(float floatNumber,int decimal,int poX, int poY, int font),
drawString(char *string, int poX, int poY, int font),
drawCentreString(char *string, int dX, int poY, int font),
drawRightString(char *string, int dX, int poY, int font),
height(void),
width(void),
textWidth(char *string, int font),
fontHeight(int font);
virtual size_t write(uint8_t);
private:
void setWindow(int16_t x0, int16_t y0, int16_t x1, int16_t y1);
uint8_t tabcolor,
colstart, rowstart; // some displays need this changed
boolean hwSPI;
uint8_t mySPCR, savedSPCR;
int8_t _cs, _dc, _rst, _mosi, _miso, _sclk;
protected:
int16_t _width, _height, // Display w/h as modified by current rotation
cursor_x, cursor_y, padX;
uint16_t textcolor, textbgcolor, fontsloaded;
uint8_t addr_row, addr_col;
uint8_t textfont,
textsize,
textdatum,
rotation;
boolean textwrap; // If set, 'wrap' text at right edge of display
};
#endif
/***************************************************
ORIGINAL LIBRARY HEADER
This is our library for the Adafruit ST7735 Breakout and Shield
----> http://www.adafruit.com/products/1651
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
Updated with new functions by Bodmer 14/4/15
****************************************************/