forked from PaulStoffregen/ILI9341_t3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HX8357_t3.h
445 lines (392 loc) · 16 KB
/
HX8357_t3.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// https://github.com/PaulStoffregen/HX8357_t3
// http://forum.pjrc.com/threads/26305-Highly-optimized-HX8357-(320x240-TFT-color-display)-library
/***************************************************
This is our library for the Adafruit HX8357 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
****************************************************/
// <SoftEgg>
//Additional graphics routines by Tim Trzepacz, SoftEgg LLC added December 2015
//(And then accidentally deleted and rewritten March 2016. Oops!)
//Gradient support
//----------------
// fillRectVGradient - fills area with vertical gradient
// fillRectHGradient - fills area with horizontal gradient
// fillScreenVGradient - fills screen with vertical gradient
// fillScreenHGradient - fills screen with horizontal gradient
//Additional Color Support
//------------------------
// color565toRGB - converts 565 format 16 bit color to RGB
// color565toRGB14 - converts 16 bit 565 format color to 14 bit RGB (2 bits clear for math and sign)
// RGB14tocolor565 - converts 14 bit RGB back to 16 bit 565 format color
//Low Memory Bitmap Support
//-------------------------
// writeRect8BPP - write 8 bit per pixel paletted bitmap
// writeRect4BPP - write 4 bit per pixel paletted bitmap
// writeRect2BPP - write 2 bit per pixel paletted bitmap
// writeRect1BPP - write 1 bit per pixel paletted bitmap
//String Pixel Length support
//---------------------------
// strPixelLen - gets pixel length of given ASCII string
// <\SoftEgg>
#ifndef _HX8357_t3H_
#define _HX8357_t3H_
#ifdef __cplusplus
#include "Arduino.h"
#endif
#define HX8357D 0xD
#define HX8357B 0xB
// Previously set to 240x320 (size of ILI9341 screen). Changed to make the library work for the HX8357 controller
#define HX8357_TFTWIDTH 320
#define HX8357_TFTHEIGHT 480
#define HX8357_NOP 0x00
#define HX8357_SWRESET 0x01
#define HX8357_RDDID 0x04
#define HX8357_RDDST 0x09
#define HX8357_SLPIN 0x10
#define HX8357_SLPOUT 0x11
#define HX8357B_PTLON 0x12
#define HX8357B_NORON 0x13
#define HX8357_RDMODE 0x0A
#define HX8357_RDMADCTL 0x0B
#define HX8357_RDPIXFMT 0x0C
#define HX8357_RDIMGFMT 0x0D
#define HX8357_RDSELFDIAG 0x0F
#define HX8357_INVOFF 0x20
#define HX8357_INVON 0x21
#define HX8357_GAMMASET 0x26
#define HX8357_DISPOFF 0x28
#define HX8357_DISPON 0x29
#define HX8357_CASET 0x2A
#define HX8357_PASET 0x2B
#define HX8357_RAMWR 0x2C
#define HX8357_RAMRD 0x2E
#define HX8357B_PTLAR 0x30
#define HX8357_TEON 0x35
#define HX8357_TEARLINE 0x44
#define HX8357_MADCTL 0x36
#define HX8357_VSCRSADD 0x37
#define HX8357_COLMOD 0x3A
#define HX8357_SETOSC 0xB0
#define HX8357_SETPWR1 0xB1
#define HX8357B_SETDISPLAY 0xB2
#define HX8357_SETRGB 0xB3
#define HX8357D_SETCOM 0xB6
#define HX8357B_SETDISPMODE 0xB4
#define HX8357D_SETCYC 0xB4
#define HX8357B_SETOTP 0xB7
#define HX8357D_SETC 0xB9
#define HX8357B_SET_PANEL_DRIVING 0xC0
#define HX8357D_SETSTBA 0xC0
#define HX8357B_SETDGC 0xC1
#define HX8357B_SETID 0xC3
#define HX8357B_SETDDB 0xC4
#define HX8357B_SETDISPLAYFRAME 0xC5
#define HX8357B_GAMMASET 0xC8
#define HX8357B_SETCABC 0xC9
#define HX8357_SETPANEL 0xCC
#define HX8357B_SETPOWER 0xD0
#define HX8357B_SETVCOM 0xD1
#define HX8357B_SETPWRNORMAL 0xD2
#define HX8357B_RDID1 0xDA
#define HX8357B_RDID2 0xDB
#define HX8357B_RDID3 0xDC
#define HX8357B_RDID4 0xDD
#define HX8357D_SETGAMMA 0xE0
#define HX8357B_SETGAMMA 0xC8
#define HX8357B_SETPANELRELATED 0xE9
/*
#define HX8357_PWCTR6 0xFC
*/
// Color definitions
#define HX8357_BLACK 0x0000 /* 0, 0, 0 */
#define HX8357_NAVY 0x000F /* 0, 0, 128 */
#define HX8357_DARKGREEN 0x03E0 /* 0, 128, 0 */
#define HX8357_DARKCYAN 0x03EF /* 0, 128, 128 */
#define HX8357_MAROON 0x7800 /* 128, 0, 0 */
#define HX8357_PURPLE 0x780F /* 128, 0, 128 */
#define HX8357_OLIVE 0x7BE0 /* 128, 128, 0 */
#define HX8357_LIGHTGREY 0xC618 /* 192, 192, 192 */
#define HX8357_DARKGREY 0x7BEF /* 128, 128, 128 */
#define HX8357_BLUE 0x001F /* 0, 0, 255 */
#define HX8357_GREEN 0x07E0 /* 0, 255, 0 */
#define HX8357_CYAN 0x07FF /* 0, 255, 255 */
#define HX8357_RED 0xF800 /* 255, 0, 0 */
#define HX8357_MAGENTA 0xF81F /* 255, 0, 255 */
#define HX8357_YELLOW 0xFFE0 /* 255, 255, 0 */
#define HX8357_WHITE 0xFFFF /* 255, 255, 255 */
#define HX8357_ORANGE 0xFD20 /* 255, 165, 0 */
#define HX8357_GREENYELLOW 0xAFE5 /* 173, 255, 47 */
#define HX8357_PINK 0xF81F
#define CL(_r,_g,_b) ((((_r)&0xF8)<<8)|(((_g)&0xFC)<<3)|((_b)>>3))
#define sint16_t int16_t
typedef struct {
const unsigned char *index;
const unsigned char *unicode;
const unsigned char *data;
unsigned char version;
unsigned char reserved;
unsigned char index1_first;
unsigned char index1_last;
unsigned char index2_first;
unsigned char index2_last;
unsigned char bits_index;
unsigned char bits_width;
unsigned char bits_height;
unsigned char bits_xoffset;
unsigned char bits_yoffset;
unsigned char bits_delta;
unsigned char line_space;
unsigned char cap_height;
} HX8357_t3_font_t;
#ifdef __cplusplus
class HX8357_t3 : public Print
{
public:
HX8357_t3(uint8_t _CS, uint8_t _DC, uint8_t _RST = 255, uint8_t _MOSI=11, uint8_t _SCLK=13, uint8_t _MISO=12);
void begin(uint8_t type);
void sleep(bool enable);
void pushColor(uint16_t color);
void fillScreen(uint16_t color);
void drawPixel(int16_t x, int16_t y, uint16_t color);
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);
void fillRectHGradient(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color1, uint16_t color2);
void fillRectVGradient(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color1, uint16_t color2);
void fillScreenVGradient(uint16_t color1, uint16_t color2);
void fillScreenHGradient(uint16_t color1, uint16_t color2);
void setRotation(uint8_t r);
void setScroll(uint16_t offset);
void invertDisplay(boolean i);
void setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1);
// Pass 8-bit (each) R,G,B, get back 16-bit packed color
static uint16_t color565(uint8_t r, uint8_t g, uint8_t b) {
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}
//color565toRGB - converts 565 format 16 bit color to RGB
static void color565toRGB(uint16_t color, uint8_t &r, uint8_t &g, uint8_t &b) {
r = (color>>8)&0x00F8;
g = (color>>3)&0x00FC;
b = (color<<3)&0x00F8;
}
//color565toRGB14 - converts 16 bit 565 format color to 14 bit RGB (2 bits clear for math and sign)
//returns 00rrrrr000000000,00gggggg00000000,00bbbbb000000000
//thus not overloading sign, and allowing up to double for additions for fixed point delta
static void color565toRGB14(uint16_t color, int16_t &r, int16_t &g, int16_t &b) {
r = (color>>2)&0x3E00;
g = (color<<3)&0x3F00;
b = (color<<9)&0x3E00;
}
//RGB14tocolor565 - converts 14 bit RGB back to 16 bit 565 format color
static uint16_t RGB14tocolor565(int16_t r, int16_t g, int16_t b)
{
return (((r & 0x3E00) << 2) | ((g & 0x3F00) >>3) | ((b & 0x3E00) >> 9));
}
//uint8_t readdata(void);
uint8_t readcommand8(uint8_t reg, uint8_t index = 0);
// Added functions to read pixel data...
uint16_t readPixel(int16_t x, int16_t y);
void readRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t *pcolors);
void writeRect(int16_t x, int16_t y, int16_t w, int16_t h, const uint16_t *pcolors);
// writeRect8BPP - write 8 bit per pixel paletted bitmap
// bitmap data in array at pixels, one byte per pixel
// color palette data in array at palette
void writeRect8BPP(int16_t x, int16_t y, int16_t w, int16_t h, const uint8_t *pixels, const uint16_t * palette );
// writeRect4BPP - write 4 bit per pixel paletted bitmap
// bitmap data in array at pixels, 4 bits per pixel
// color palette data in array at palette
// width must be at least 2 pixels
void writeRect4BPP(int16_t x, int16_t y, int16_t w, int16_t h, const uint8_t *pixels, const uint16_t * palette );
// writeRect2BPP - write 2 bit per pixel paletted bitmap
// bitmap data in array at pixels, 4 bits per pixel
// color palette data in array at palette
// width must be at least 4 pixels
void writeRect2BPP(int16_t x, int16_t y, int16_t w, int16_t h, const uint8_t *pixels, const uint16_t * palette );
// writeRect1BPP - write 1 bit per pixel paletted bitmap
// bitmap data in array at pixels, 4 bits per pixel
// color palette data in array at palette
// width must be at least 8 pixels
void writeRect1BPP(int16_t x, int16_t y, int16_t w, int16_t h, const uint8_t *pixels, const uint16_t * palette );
// from Adafruit_GFX.h
void drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
void drawCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, uint16_t color);
void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername, int16_t delta, uint16_t color);
void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);
void drawRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color);
void fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h, int16_t radius, uint16_t color);
void drawBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color);
void drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size);
void setCursor(int16_t x, int16_t y);
void getCursor(int16_t *x, int16_t *y);
void setTextColor(uint16_t c);
void setTextColor(uint16_t c, uint16_t bg);
void setTextSize(uint8_t s);
uint8_t getTextSize();
void setTextWrap(boolean w);
boolean getTextWrap();
virtual size_t write(uint8_t);
int16_t width(void) { return _width; }
int16_t height(void) { return _height; }
uint8_t getRotation(void);
void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color);
void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);
int16_t getCursorX(void) const { return cursor_x; }
int16_t getCursorY(void) const { return cursor_y; }
void setFont(const HX8357_t3_font_t &f) { font = &f; }
void setFontAdafruit(void) { font = NULL; }
void drawFontChar(unsigned int c);
int16_t strPixelLen(char * str);
protected:
int16_t _width, _height; // Display w/h as modified by current rotation
int16_t cursor_x, cursor_y;
uint16_t textcolor, textbgcolor;
uint8_t textsize, rotation;
boolean wrap; // If set, 'wrap' text at right edge of display
const HX8357_t3_font_t *font;
uint8_t _rst;
uint8_t _cs, _dc;
uint8_t pcs_data, pcs_command;
uint8_t _miso, _mosi, _sclk;
void setAddr(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
__attribute__((always_inline)) {
writecommand_cont(HX8357_CASET); // Column addr set
writedata16_cont(x0); // XSTART
writedata16_cont(x1); // XEND
writecommand_cont(HX8357_PASET); // Row addr set
writedata16_cont(y0); // YSTART
writedata16_cont(y1); // YEND
}
//void waitFifoNotFull(void) __attribute__((always_inline)) {
void waitFifoNotFull(void) {
uint32_t sr;
uint32_t tmp __attribute__((unused));
do {
sr = KINETISK_SPI0.SR;
if (sr & 0xF0) tmp = KINETISK_SPI0.POPR; // drain RX FIFO
} while ((sr & (15 << 12)) > (3 << 12));
}
void waitFifoEmpty(void) {
uint32_t sr;
uint32_t tmp __attribute__((unused));
do {
sr = KINETISK_SPI0.SR;
if (sr & 0xF0) tmp = KINETISK_SPI0.POPR; // drain RX FIFO
} while ((sr & 0xF0F0) > 0); // wait both RX & TX empty
}
void waitTransmitComplete(void) __attribute__((always_inline)) {
uint32_t tmp __attribute__((unused));
while (!(KINETISK_SPI0.SR & SPI_SR_TCF)) ; // wait until final output done
tmp = KINETISK_SPI0.POPR; // drain the final RX FIFO word
}
void waitTransmitComplete(uint32_t mcr) __attribute__((always_inline)) {
uint32_t tmp __attribute__((unused));
while (1) {
uint32_t sr = KINETISK_SPI0.SR;
if (sr & SPI_SR_EOQF) break; // wait for last transmit
if (sr & 0xF0) tmp = KINETISK_SPI0.POPR;
}
KINETISK_SPI0.SR = SPI_SR_EOQF;
SPI0_MCR = mcr;
while (KINETISK_SPI0.SR & 0xF0) {
tmp = KINETISK_SPI0.POPR;
}
}
void writecommand_cont(uint8_t c) __attribute__((always_inline)) {
KINETISK_SPI0.PUSHR = c | (pcs_command << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_CONT;
waitFifoNotFull();
}
void writedata8_cont(uint8_t c) __attribute__((always_inline)) {
KINETISK_SPI0.PUSHR = c | (pcs_data << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_CONT;
waitFifoNotFull();
}
void writedata16_cont(uint16_t d) __attribute__((always_inline)) {
KINETISK_SPI0.PUSHR = d | (pcs_data << 16) | SPI_PUSHR_CTAS(1) | SPI_PUSHR_CONT;
waitFifoNotFull();
}
void writecommand_last(uint8_t c) __attribute__((always_inline)) {
uint32_t mcr = SPI0_MCR;
KINETISK_SPI0.PUSHR = c | (pcs_command << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_EOQ;
waitTransmitComplete(mcr);
}
void writedata8_last(uint8_t c) __attribute__((always_inline)) {
uint32_t mcr = SPI0_MCR;
KINETISK_SPI0.PUSHR = c | (pcs_data << 16) | SPI_PUSHR_CTAS(0) | SPI_PUSHR_EOQ;
waitTransmitComplete(mcr);
}
void writedata16_last(uint16_t d) __attribute__((always_inline)) {
uint32_t mcr = SPI0_MCR;
KINETISK_SPI0.PUSHR = d | (pcs_data << 16) | SPI_PUSHR_CTAS(1) | SPI_PUSHR_EOQ;
waitTransmitComplete(mcr);
}
void HLine(int16_t x, int16_t y, int16_t w, uint16_t color)
__attribute__((always_inline)) {
if((x >= _width) || (y >= _height) || (y < 0)) return;
if(x < 0) { w += x; x = 0; }
if((x+w-1) >= _width) w = _width-x;
setAddr(x, y, x+w-1, y);
writecommand_cont(HX8357_RAMWR);
do { writedata16_cont(color); } while (--w > 0);
}
void VLine(int16_t x, int16_t y, int16_t h, uint16_t color)
__attribute__((always_inline)) {
if((x >= _width) || (x < 0) || (y >= _height)) return;
if(y < 0) { h += y; y = 0; }
if((y+h-1) >= _height) h = _height-y;
setAddr(x, y, x, y+h-1);
writecommand_cont(HX8357_RAMWR);
do { writedata16_cont(color); } while (--h > 0);
}
void Pixel(int16_t x, int16_t y, uint16_t color)
__attribute__((always_inline)) {
if((x >= _width) || (x < 0) || (y >= _height) || (y < 0)) return;
setAddr(x, y, x, y);
writecommand_cont(HX8357_RAMWR);
writedata16_cont(color);
}
void drawFontBits(uint32_t bits, uint32_t numbits, uint32_t x, uint32_t y, uint32_t repeat);
};
#ifndef swap
#define swap(a, b) { typeof(a) t = a; a = b; b = t; }
#endif
// To avoid conflict when also using Adafruit_GFX or any Adafruit library
// which depends on Adafruit_GFX, #include the Adafruit library *BEFORE*
// you #include HX8357_t3.h.
#ifndef _ADAFRUIT_GFX_H
class Adafruit_GFX_Button {
public:
Adafruit_GFX_Button(void) { _gfx = NULL; }
void initButton(HX8357_t3 *gfx, int16_t x, int16_t y,
uint8_t w, uint8_t h,
uint16_t outline, uint16_t fill, uint16_t textcolor,
const char *label, uint8_t textsize);
void drawButton(bool inverted = false);
bool contains(int16_t x, int16_t y);
void press(boolean p) {
laststate = currstate;
currstate = p;
}
bool isPressed() { return currstate; }
bool justPressed() { return (currstate && !laststate); }
bool justReleased() { return (!currstate && laststate); }
private:
HX8357_t3 *_gfx;
int16_t _x, _y;
uint16_t _w, _h;
uint8_t _textsize;
uint16_t _outlinecolor, _fillcolor, _textcolor;
char _label[10];
boolean currstate, laststate;
};
#endif
#endif // __cplusplus
#endif