forked from moononournation/Arduino_GFX
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArduino_TFT_18bit.cpp
337 lines (319 loc) · 10.8 KB
/
Arduino_TFT_18bit.cpp
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
/*
* start rewrite from:
* https://github.com/adafruit/Adafruit-GFX-Library.git
*/
#include "Arduino_DataBus.h"
#include "Arduino_GFX.h"
#include "Arduino_TFT_18bit.h"
Arduino_TFT_18bit::Arduino_TFT_18bit(
Arduino_DataBus *bus, int8_t rst, uint8_t r,
bool ips, int16_t w, int16_t h,
uint8_t col_offset1, uint8_t row_offset1, uint8_t col_offset2, uint8_t row_offset2)
: Arduino_TFT(bus, rst, r, ips, w, h, col_offset1, row_offset1, col_offset2, row_offset2)
{
}
void Arduino_TFT_18bit::writeColor(uint16_t color)
{
_bus->write((color & 0xF800) >> 8);
_bus->write((color & 0x07E0) >> 3);
_bus->write((color & 0x001F) << 3);
}
void Arduino_TFT_18bit::writeRepeat(uint16_t color, uint32_t len)
{
#if defined(ESP8266) || defined(ESP32)
uint8_t c[3] = {(uint8_t)((color & 0xF800) >> 8), (uint8_t)((color & 0x07E0) >> 3), (uint8_t)((color & 0x001F) << 3)};
_bus->writePattern(c, 3, len);
#else
uint8_t c1 = (uint8_t)((color & 0xF800) >> 8);
uint8_t c2 = (uint8_t)((color & 0x07E0) >> 3);
uint8_t c3 = (uint8_t)((color & 0x001F) << 3);
while (len--)
{
_bus->write(c1);
_bus->write(c2);
_bus->write(c3);
}
#endif
}
void Arduino_TFT_18bit::writePixels(uint16_t *data, uint32_t len)
{
uint16_t d;
while (len--)
{
d = *data++;
_bus->write((d & 0xF800) >> 8);
_bus->write((d & 0x07E0) >> 3);
_bus->write((d & 0x001F) << 3);
}
}
// TFT optimization code, too big for ATMEL family
#if defined(ARDUINO_ARCH_SAMD) || defined(ESP8266) || defined(ESP32)
// TFT tuned BITMAP / XBITMAP / GRAYSCALE / RGB BITMAP FUNCTIONS ---------------------
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 1-bit image at the specified (x,y) position, using the specified foreground (for set bits) and background (unset bits) colors.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw pixels with
@param bg 16-bit 5-6-5 Color to draw background with
*/
/**************************************************************************/
void Arduino_TFT_18bit::drawBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h,
uint16_t color, uint16_t bg)
{
uint16_t d;
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
writeAddrWindow(x, y, w, h);
for (int16_t j = 0; j < h; j++, y++)
{
for (int16_t i = 0; i < w; i++)
{
if (i & 7)
{
byte <<= 1;
}
else
{
byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
}
d = (byte & 0x80) ? color : bg;
_bus->write((d & 0xF800) >> 8);
_bus->write((d & 0x07E0) >> 3);
_bus->write((d & 0x001F) << 3);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 1-bit image at the specified (x,y) position, using the specified foreground (for set bits) and background (unset bits) colors.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with monochrome bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
@param color 16-bit 5-6-5 Color to draw pixels with
@param bg 16-bit 5-6-5 Color to draw background with
*/
/**************************************************************************/
void Arduino_TFT_18bit::drawBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg)
{
uint16_t d;
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
startWrite();
writeAddrWindow(x, y, w, h);
for (int16_t j = 0; j < h; j++, y++)
{
for (int16_t i = 0; i < w; i++)
{
if (i & 7)
{
byte <<= 1;
}
else
{
byte = bitmap[j * byteWidth + i / 8];
}
d = (byte & 0x80) ? color : bg;
_bus->write((d & 0xF800) >> 8);
_bus->write((d & 0x07E0) >> 3);
_bus->write((d & 0x001F) << 3);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 8-bit image (grayscale) at the specified (x,y) pos.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Arduino_TFT_18bit::drawGrayscaleBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h)
{
uint8_t v;
startWrite();
writeAddrWindow(x, y, w, h);
for (int16_t j = 0; j < h; j++, y++)
{
for (int16_t i = 0; i < w; i++)
{
v = (uint8_t)pgm_read_byte(&bitmap[j * w + i]);
_bus->write(v);
_bus->write(v);
_bus->write(v);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 8-bit image (grayscale) at the specified (x,y) pos.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with grayscale bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Arduino_TFT_18bit::drawGrayscaleBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h)
{
uint8_t v;
startWrite();
writeAddrWindow(x, y, w, h);
for (int16_t j = 0; j < h; j++, y++)
{
for (int16_t i = 0; i < w; i++)
{
v = bitmap[j * w + i];
_bus->write(v);
_bus->write(v);
_bus->write(v);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a Indexed 16-bit image (RGB 5/6/5) at the specified (x,y) position.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Arduino_TFT_18bit::drawIndexedBitmap(int16_t x, int16_t y,
uint8_t *bitmap, uint16_t *color_index, int16_t w, int16_t h)
{
uint16_t d;
uint32_t len = w * h;
startWrite();
writeAddrWindow(x, y, w, h);
while (len--)
{
d = color_index[*(bitmap++)];
_bus->write((d & 0xF800) >> 8);
_bus->write((d & 0x07E0) >> 3);
_bus->write((d & 0x001F) << 3);
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position.
For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Arduino_TFT_18bit::draw16bitRGBBitmap(int16_t x, int16_t y,
const uint16_t bitmap[], int16_t w, int16_t h)
{
uint16_t d;
startWrite();
writeAddrWindow(x, y, w, h);
for (int16_t j = 0; j < h; j++, y++)
{
for (int16_t i = 0; i < w; i++)
{
d = pgm_read_word(&bitmap[j * w + i]);
_bus->write((d & 0xF800) >> 8);
_bus->write((d & 0x07E0) >> 3);
_bus->write((d & 0x001F) << 3);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position.
For 16-bit display devices; no color reduction performed.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Arduino_TFT_18bit::draw16bitRGBBitmap(int16_t x, int16_t y,
uint16_t *bitmap, int16_t w, int16_t h)
{
uint16_t d;
startWrite();
writeAddrWindow(x, y, w, h);
for (int16_t j = 0; j < h; j++, y++)
{
for (int16_t i = 0; i < w; i++)
{
d = bitmap[j * w + i];
_bus->write((d & 0xF800) >> 8);
_bus->write((d & 0x07E0) >> 3);
_bus->write((d & 0x001F) << 3);
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a PROGMEM-resident 24-bit image (RGB 5/6/5) at the specified (x,y) position.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Arduino_TFT_18bit::draw24bitRGBBitmap(int16_t x, int16_t y,
const uint8_t bitmap[], int16_t w, int16_t h)
{
int16_t offset = 0;
startWrite();
writeAddrWindow(x, y, w, h);
for (int16_t j = 0; j < h; j++, y++)
{
for (int16_t i = 0; i < w; i++)
{
_bus->write(pgm_read_byte(&bitmap[offset++]));
_bus->write(pgm_read_byte(&bitmap[offset++]));
_bus->write(pgm_read_byte(&bitmap[offset++]));
}
}
endWrite();
}
/**************************************************************************/
/*!
@brief Draw a RAM-resident 24-bit image (RGB 5/6/5) at the specified (x,y) position.
@param x Top left corner x coordinate
@param y Top left corner y coordinate
@param bitmap byte array with 16-bit color bitmap
@param w Width of bitmap in pixels
@param h Height of bitmap in pixels
*/
/**************************************************************************/
void Arduino_TFT_18bit::draw24bitRGBBitmap(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h)
{
int16_t offset = 0;
startWrite();
writeAddrWindow(x, y, w, h);
_bus->writeBytes(bitmap, w * h * 3);
endWrite();
}
#endif // defined(ARDUINO_ARCH_SAMD) || defined(ESP8266) || defined(ESP32)