forked from engineertype/Controleo3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controleo3LCD.cpp
419 lines (343 loc) · 10.7 KB
/
Controleo3LCD.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
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
// Written by Peter Easton
// Released under CC BY-NC-SA 3.0 license
// Build a reflow oven: http://whizoo.com
//
// LCD controller for ILI9488 controller
// For general software development, LCD_DEBUG should be enabled so that
// range checking happens on all parameters ensuring that all drawing takes
// place on the screen. For release, LCD_DEBUG can be disabled to improve
// drawing speed.
//#define LCD_DEBUG
#include "Controleo3LCD.h"
// Constructor for the TFT display
Controleo3LCD::Controleo3LCD(void)
{
// Get the addresses of Port B
portBOut = portOutputRegister(digitalPinToPort(A2));
portBMode = portModeRegister(digitalPinToPort(A2));
portBIn = portInputRegister(digitalPinToPort(A2));
flood8Reg = ((uint8_t *) portBOut) + 1;
bitmapReg = ((uint16_t *) portBOut);
}
void Controleo3LCD::begin()
{
// Set RD, WR, CD, CS and data pins to be outputs
*portBMode |= (SETBIT12 + SETBIT13 + SETBIT14 + SETBIT15 + SETBIT16 + 0xFF);
// Pull RD high so writes work
LCD_RD_ACTIVE;
// Do a hard reset
reset(HARD_RESET);
LCD_CS_ACTIVE;
writeRegister8(ILI9488_PIXELFORMAT, 0x55);
writeRegister8(ILI9488_MADCTL, ILI9488_MADCTL_MV | ILI9488_MADCTL_BGR);
write8Command(ILI9488_SLEEPOUT);
LCD_CS_IDLE;
// Wait for things to settle before returning
delay(15);
}
// Set the I/O mode on the read pin
void Controleo3LCD::readMode(boolean enable)
{
if (enable) {
// Set data pins to input mode
for (int i=0; i<8; i++) {
PORT->Group[PORTB].PINCFG[i].reg=(uint8_t)(PORT_PINCFG_INEN);
PORT->Group[PORTB].DIRCLR.reg = (uint32_t)(1<<i);
}
}
else {
// Set pin to output mode
for (int i=0; i<8; i++) {
PORT->Group[PORTB].PINCFG[i].reg&=~(uint8_t)(PORT_PINCFG_INEN);
PORT->Group[PORTB].DIRSET.reg = (uint32_t)(1<<i);
}
}
}
// Reset the TFT Display
// type = HARD_RESET or SOFT_RESET
void Controleo3LCD::reset(uint8_t type)
{
if (type == HARD_RESET) {
LCD_RESET_LOW;
delayMicroseconds(10);
LCD_RESET_HIGH;
}
else {
write8Command(ILI9488_SOFTRESET);
LCD_CS_IDLE;
}
// In both cases the display needs 5ms to recover from the reset
delay(5);
}
// Get the LCD's version
uint32_t Controleo3LCD::getLCDVersion()
{
uint32_t version = 0;
write8Command(ILI9488_READ_DISPLAY_INFO);
LCD_WR_ACTIVE;
readMode(true);
// Toggle read bit before starting to read data
LCD_RD_IDLE;
for (uint8_t i = 0; i < 3; i++) {
version = version << 8;
version += read8Data();
}
LCD_CS_IDLE;
readMode(false);
// Pull RD high so writes work
LCD_RD_ACTIVE;
LCD_WR_IDLE;
return version;
}
// Set the addressable range of the window
void Controleo3LCD::setAddrWindow(int x1, int y1, int x2, int y2)
{
#ifdef LCD_DEBUG
checkRange(x1, 0, LCD_MAX_X, "setAddrWindow:x1");
checkRange(x2, 0, LCD_MAX_X, "setAddrWindow:x2");
checkRange(y1, 0, LCD_MAX_Y, "setAddrWindow:y1");
checkRange(y2, 0, LCD_MAX_Y, "setAddrWindow:y2");
#endif
writeRegister16x2(ILI9488_COLADDRSET, x1, x2);
writeRegister16x2(ILI9488_PAGEADDRSET, y1, y2);
}
// Fills the window (set using setAddrWindow) with pixels of the specified color
// This routine is (fairly) heavily optimized for performance.
void Controleo3LCD::flood(uint16_t color, uint32_t len)
{
uint8_t high = highByte(color), low = lowByte(color);
write8Command(ILI9488_MEMORYWRITE);
// Optimize the case where high == low
if (high == low) {
// Write the first pixel to set the color
write8Data(high);
LCD_WR_IDLE;
LCD_WR_ACTIVE;
len--;
// Get the current value of the register used for the write bit
// This method is dangerous because we'll be ignoring changes in the other
// bits for the duration of these writes. However, the affected pins are
// Relays 4 to 6, Touch IRQ and some LCD pins so there is no downside.
uint8_t writeIdle = *flood8Reg & 0xDF;
uint8_t writeActive = writeIdle | 0x20;
#define WR_STROBE {*flood8Reg = writeIdle; *flood8Reg = writeActive;}
uint32_t setsOf8 = len >> 3;
len &= 0x7;
// Do 16 pixels at a time
// Time to clear screen:
// 1 at a time = 64ms
// 8 at a time = 54ms
// 16 at a time = 55ms (yes, longer)
while (setsOf8--) {
WR_STROBE;WR_STROBE;WR_STROBE;WR_STROBE;WR_STROBE;WR_STROBE;WR_STROBE;WR_STROBE;
WR_STROBE;WR_STROBE;WR_STROBE;WR_STROBE;WR_STROBE;WR_STROBE;WR_STROBE;WR_STROBE;
}
while (len--) {
WR_STROBE;
WR_STROBE;
}
return;
}
while (len--) {
write8Data(high);
write8Data(low);
}
}
// Draw a horizontal line
void Controleo3LCD::drawFastHLine(int16_t x, int16_t y, int16_t length, uint16_t color)
{
#ifdef LCD_DEBUG
checkRange(x, 0, LCD_MAX_X, "drawFastHLine:x");
checkRange(y, 0, LCD_MAX_Y, "drawFastHLine:y");
checkRange(length, 1, LCD_MAX_X - x, "drawFastHLine:length");
#endif
setAddrWindow(x, y, x + length - 1, y);
flood(color, length);
LCD_CS_IDLE;
}
// Draw a vertical line
void Controleo3LCD::drawFastVLine(int16_t x, int16_t y, int16_t length, uint16_t color)
{
#ifdef LCD_DEBUG
checkRange(x, 0, LCD_MAX_X, "drawFastVLine:x");
checkRange(y, 0, LCD_MAX_Y, "drawFastVLine:y");
checkRange(length, 1, LCD_MAX_Y - y, "drawFastVLine:length");
#endif
setAddrWindow(x, y, x, y + length - 1);
flood(color, length);
LCD_CS_IDLE;
}
// Draw a rectangle with the given color
void Controleo3LCD::drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color)
{
#ifdef LCD_DEBUG
checkRange(x, 0, LCD_MAX_X, "drawRect:x");
checkRange(y, 0, LCD_MAX_Y, "drawRect:y");
checkRange(w, 1, LCD_WIDTH - x, "drawRect:w");
checkRange(h, 1, LCD_HEIGHT - y, "drawRect:h");
#endif
drawFastHLine(x, y, w, color);
drawFastHLine(x, y+h-1, w, color);
if (h >= 3) {
drawFastVLine(x, y+1, h-2, color);
drawFastVLine(x+w-1, y+1, h-2, color);
}
}
// Fill the rectangle with the given color
void Controleo3LCD::fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t fillcolor)
{
#ifdef LCD_DEBUG
checkRange(x, 0, LCD_MAX_X, "fillRect:x");
checkRange(y, 0, LCD_MAX_Y, "fillRect:y");
checkRange(w, 1, LCD_WIDTH - x, "fillRect:w");
checkRange(h, 1, LCD_HEIGHT - y, "fillRect:h");
#endif
setAddrWindow(x, y, x + w - 1, y + h - 1);
flood(fillcolor, (uint32_t) w * (uint32_t) h);
LCD_CS_IDLE;
}
// Fill the screen with the given color
void Controleo3LCD::fillScreen(uint16_t color) {
// The screen takes rotation into account
setAddrWindow(0, 0, LCD_MAX_X, LCD_MAX_Y);
flood(color, (uint32_t) LCD_WIDTH * (uint32_t) LCD_HEIGHT);
LCD_CS_IDLE;
}
// Draw a pixel at the specified location
void Controleo3LCD::drawPixel(int16_t x, int16_t y, uint16_t color) {
#ifdef LCD_DEBUG
checkRange(x, 0, LCD_MAX_X, "drawPixel:x");
checkRange(y, 0, LCD_MAX_Y, "drawPixel:y");
#endif
setAddrWindow(x, y, x, y);
writeRegister16(ILI9488_MEMORYWRITE,color);
LCD_CS_IDLE;
}
// Start drawing a bitmap
void Controleo3LCD::startBitmap(int16_t x, int16_t y, int16_t w, int16_t h)
{
#ifdef LCD_DEBUG
checkRange(x, 0, LCD_MAX_X, "fillRect:x");
checkRange(y, 0, LCD_MAX_Y, "fillRect:y");
checkRange(w, 1, LCD_WIDTH - x, "fillRect:w");
checkRange(h, 1, LCD_HEIGHT - y, "fillRect:h");
#endif
setAddrWindow(x, y, x + w - 1, y + h - 1);
write8Command(ILI9488_MEMORYWRITE);
bitmapRegValue = *bitmapReg & 0xDF00; // Clear the write bit
}
// Draw part or all of a bitmap. The bitmaps must be 16-bit color.
// This function can be called over and over again until the whole
// bitmap has been rendered to the screen.
void Controleo3LCD::drawBitmap(uint16_t *data, uint32_t len)
{
#define write8DataBitmap(d) *bitmapReg = (bitmapRegValue + d); LCD_WR_ACTIVE;
while(len--) {
write8DataBitmap(highByte(*data));
write8DataBitmap(lowByte(*data));
data++;
}
}
// End the drawing of the bitmap
void Controleo3LCD::endBitmap()
{
LCD_CS_IDLE;
}
// Allow users to write directly to LCD registers. See ILI9488.h
void Controleo3LCD::pokeRegister(uint8_t reg)
{
write8Command(reg);
LCD_CS_IDLE;
}
// Allow users to write directly to LCD registers. See ILI9488.h
void Controleo3LCD::setRegister8(uint8_t a, uint8_t d)
{
writeRegister8(a,d);
LCD_CS_IDLE;
}
void Controleo3LCD::startReadBitmap(int16_t x, int16_t y, int16_t w, int16_t h)
{
#ifdef LCD_DEBUG
checkRange(x, 0, LCD_MAX_X, "fillRect:x");
checkRange(y, 0, LCD_MAX_Y, "fillRect:y");
checkRange(w, 1, LCD_WIDTH - x, "fillRect:w");
checkRange(h, 1, LCD_HEIGHT - y, "fillRect:h");
#endif
setAddrWindow(x, y, x + w - 1, y + h - 1);
write8Command(ILI9488_MEMORYREAD);
LCD_WR_ACTIVE;
readMode(true);
// Toggle read bit before starting to read data
LCD_RD_IDLE;
}
// Read a bitmap (used for screenshots)
void Controleo3LCD::readBitmapRGB565(uint16_t *data, uint32_t len)
{
while(len--) {
// Red
*data = (read8Data() & 0xF8) << 8;
// Green
*data += (read8Data() & 0xFC) << 3;
// Blue
*data += (read8Data() & 0xF8) >> 3;
data++;
}
}
// Draw part or all of a bitmap. The bitmaps must be 16-bit color.
// This function can be called over and over again until the whole
// bitmap has been rendered to the screen.
void Controleo3LCD::readBitmap24bit(uint8_t *data, uint32_t len)
{
while(len--) {
*(data+2) = read8Data();
*(data+1) = read8Data();
*(data+0) = read8Data();
data+= 3;
}
}
// End the drawing of the bitmap
void Controleo3LCD::endReadBitmap()
{
LCD_CS_IDLE;
readMode(false);
// Pull RD high so writes work
LCD_RD_ACTIVE;
LCD_WR_IDLE;
}
// Read 8-bits of data from the screen
uint8_t Controleo3LCD::read8Data()
{
// Toggle the read bit
LCD_RD_ACTIVE;
LCD_RD_IDLE;
return *portBIn;
}
// Convert a 24-bit RGB value to 16-bit (RGB565)
uint16_t Controleo3LCD::convertTo16Bit(uint32_t bit24)
{
uint16_t val = 0;
val = (bit24 & 0x00F80000) >> 8;
val |= (bit24 & 0x0000FC00) >> 5;
val |= (bit24 & 0x000000F8) >> 3;
return val;
}
#ifdef LCD_DEBUG
// Make sure the given value is in the range specified.
void Controleo3LCD::checkRange(int val, int low, int high, char *msg)
{
if (val < low) {
SerialUSB.print(msg);
SerialUSB.print(": value ");
SerialUSB.print(val);
SerialUSB.print(" is smaller than ");
SerialUSB.println(low);
}
if (val > high) {
SerialUSB.print(msg);
SerialUSB.print(": value ");
SerialUSB.print(val);
SerialUSB.print(" is larger than ");
SerialUSB.println(high);
}
}
#endif