-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibtft.c
More file actions
385 lines (331 loc) · 8.87 KB
/
libtft.c
File metadata and controls
385 lines (331 loc) · 8.87 KB
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
/*
* File: libtft.c
* Author: torsten.roemer@luniks.net
*
* Created on 6. November 2023, 18:45
*/
#include "libtft.h"
static width_t _displayWidth;
static height_t _displayHeight;
static bool _hflip;
static bool _vflip;
static bool _bgr;
/**
* Sends the given command to the display.
*
* @param cmd
*/
static void displayCmd(uint8_t cmd) {
_tftSetCmd();
_tftTx(cmd);
}
/**
* Sends the given data to the display.
*
* @param data
*/
static void displayData(uint8_t data) {
_tftSetData();
_tftTx(data);
}
/**
* Sets horizontal and/or vertical flip.
*
* @param hflip
* @param vflip
*/
static void madctl(bool hflip, bool vflip) {
// Memory data access control
uint8_t madctl = 0b00110110;
if (_hflip) madctl |= (1 << 7);
if (_vflip) madctl |= (1 << 6);
if (_bgr) madctl |= (1 << 3);
if (hflip) {
// Row Address Order (MY)
madctl ^= (1 << 7);
}
if (vflip) {
// Column Address Order (MX)
madctl ^= (1 << 6);
}
_tftSel();
displayCmd(TFT_MADCTL);
displayData(madctl);
_tftDes();
}
/**
* Sets the given column start and end address.
*
* @param xs start address
* @param xe end address
*/
static void caset(x_t xs, x_t xe) {
_tftSel();
displayCmd(TFT_CASET);
displayData(xs >> 8);
displayData(xs);
displayData(xe >> 8);
displayData(xe);
_tftDes();
}
/**
* Sets the given row start and end address.
*
* @param ys start address
* @param ye end address
*/
static void raset(y_t ys, y_t ye) {
_tftSel();
displayCmd(TFT_RASET);
displayData(ys >> 8);
displayData(ys);
displayData(ye >> 8);
displayData(ye);
_tftDes();
}
/**
* Converts the given 8 pixel in 1-Bit monochrome to 16-Bit RGB (5/6/5) color
* stored in the given array of 16 bytes, with the given background and
* foreground color.
*
* @param mono 8 pixel in 1-Bit monochrome
* @param rgb 8 pixel in 16-Bit RGB (5/6/5) color
* @param bg background color
* @param fg foreground color
*/
static void mono1ToRGB16(uint8_t mono, uint8_t *rgb,
uint16_t bg, uint16_t fg) {
for (uint8_t i = 0; i < 16; i++) {
uint8_t _bg = i % 2 == 0 ? bg >> 8 : bg;
uint8_t _fg = i % 2 == 0 ? fg >> 8 : fg;
rgb[i] = (mono & (1 << ((15 - i) >> 1))) ? _fg : _bg;
}
}
/*
* Converts the given two pixel in 4-Bit greyscale to 16-Bit RGB (5/6/5) color
* stored in the given array of four bytes.
*
* @param grey two pixel in 4-Bit greyscale
* @param rgb two pixel in 16-Bit RGB (5/6/5) color
*/
static void grey4ToRGB16(uint8_t grey, uint8_t *rgb) {
uint8_t grey4 = ((grey >> 4) & 1);
uint8_t grey0 = ((grey >> 0) & 1);
rgb[0] = (grey & 0xf0);
rgb[0] |= (grey4 << 3);
rgb[0] |= (grey >> 5);
rgb[1] = ((grey & 0xf0) << 3);
rgb[1] |= ((grey & 0xf0) >> 3);
rgb[1] |= (grey4 << 6) | (grey4 << 5) | (grey4 << 0);
rgb[2] = (grey << 4);
rgb[2] |= (grey0 << 3);
rgb[2] |= ((grey & 0x0f) >> 1);
rgb[3] = (grey << 7);
rgb[3] |= ((grey & 0x0f) << 1);
rgb[3] |= (grey0 << 6) | (grey0 << 5) | (grey0 << 0);
}
/*
* Converts the given pixel in 8-Bit RGB (3/3/2) to 16-Bit RGB (5/6/5) color
* stored in the given array of two bytes.
*
* @param rgb8 one pixel in 8-Bit RGB (3/3/2) color
* @param rgb one pixel in 16-Bit RGB (5/6/5) color
*/
static void rgb8ToRGB16(uint8_t rgb8, uint8_t *rgb) {
rgb[0] = (rgb8 & 0xe0);
rgb[0] |= (rgb8 & 0x20) >> 1;
rgb[0] |= (rgb8 & 0x3c) >> 2;
rgb[1] = (rgb8 & 0x04) << 5;
rgb[1] |= (rgb8 & 0x04) << 4;
rgb[1] |= (rgb8 & 0x07) << 3;
rgb[1] |= (rgb8 & 0x01) << 2;
rgb[1] |= (rgb8 & 0x01) << 1;
rgb[1] |= (rgb8 & 0x01);
}
void tftInit(width_t width, height_t height,
bool hflip, bool vflip,
bool bgr, bool invert) {
_displayWidth = width;
_displayHeight = height;
_hflip = hflip;
_vflip = vflip;
_bgr = bgr;
_tftDelay60();
// Hardware reset
_tftReset();
_tftDelay60();
_tftDelay60();
// Software reset
_tftSel();
displayCmd(TFT_SWRESET);
_tftDes();
_tftDelay60();
// Sleep out & booster on
_tftSel();
displayCmd(TFT_SLPOUT);
_tftDes();
_tftDelay60();
// Partial off (Normal)
_tftSel();
displayCmd(TFT_NORON);
_tftDes();
// Display Inversion on/off
_tftSel();
displayCmd(invert ? TFT_INVON : TFT_INVOFF);
_tftDes();
// Interface pixel format
_tftSel();
displayCmd(TFT_COLMOD);
displayData(0b00111101);
_tftDes();
// Display on
_tftSel();
displayCmd(TFT_DISPON);
_tftDes();
// Sleep in & booster off
// _tftSel();
// displayCmd(SLPIN);
// _tftDes();
// printString("Display init done\r\n");
}
void tftWriteStart(void) {
// Memory write
_tftSel();
displayCmd(TFT_RAMWR);
_tftSetData();
}
void tftWriteRestart(void) {
_tftSel();
}
void tftWriteByte(uint8_t byte) {
// Memory write
_tftTx(byte);
}
void tftWriteEnd(void) {
// Memory write
_tftDes();
}
void tftFillArea(x_t x, x_t y,
width_t width, height_t height,
uint16_t color) {
madctl(false, false);
// X address start/end
uint16_t xs = x;
uint16_t xe = x + width - 1;
caset(xs, xe);
// Y address start/end
uint16_t ys = y;
uint16_t ye = y + height - 1;
raset(ys, ye);
tftWriteStart();
bytes_t pixels = (bytes_t)width * (bytes_t)height;
for (bytes_t i = 0; i < pixels; i++) {
_tftTx(color >> 8);
_tftTx(color);
}
tftWriteEnd();
}
void tftSetArea(x_t x, y_t y,
width_t width, height_t height,
bool hflip, bool vflip) {
madctl(hflip, vflip);
// X address start/end
uint16_t xs = x;
uint16_t xe = x + width - 1;
if (hflip) {
xs = _displayWidth - x - width;
xe = _displayWidth - x - 1;
}
caset(xs, xe);
// Y address start/end
uint16_t ys = y;
uint16_t ye = y + height - 1;
if (vflip) {
ys = _displayHeight - y - height;
ye = _displayHeight - y - 1;
}
raset(ys, ye);
}
void tftSetFrame(uint16_t color) {
tftFillArea(0, 0, _displayWidth, _displayHeight, color);
}
void tftWriteData(const __flash uint8_t *bitmap,
width_t width, height_t height,
space_t space, uint16_t bg, uint16_t fg) {
tftWriteStart();
switch (space) {
case SPACE_MONO1: {
bytes_t bytes = width * height / 8;
for (uint16_t i = 0; i < bytes; i++) {
uint8_t rgb[16];
mono1ToRGB16(bitmap[i], rgb, bg, fg);
for (uint8_t j = 0; j < 16; j++) {
_tftTx(rgb[j]);
}
}
}; break;
case SPACE_GREY4: {
bytes_t bytes = width * height / 2;
for (uint16_t i = 0; i < bytes; i++) {
uint8_t rgb[4];
grey4ToRGB16(bitmap[i], rgb);
for (uint8_t j = 0; j < 4; j++) {
_tftTx(rgb[j]);
}
}
}; break;
case SPACE_RGB8: {
bytes_t bytes = width * height;
for (uint16_t i = 0; i < bytes; i++) {
uint8_t rgb[2];
rgb8ToRGB16(bitmap[i], rgb);
for (uint8_t j = 0; j < 2; j++) {
_tftTx(rgb[j]);
}
}
}; break;
default: {
// SPACE_RGB16
bytes_t bytes = width * height * 2;
for (uint16_t i = 0; i < bytes; i++) {
_tftTx(bitmap[i]);
}
}
}
tftWriteEnd();
}
width_t tftWriteBitmap(x_t x, y_t y, uint16_t index,
uint16_t bg, uint16_t fg) {
const __flash Bitmap *bitmap = &bitmaps[index];
tftSetArea(x, y, bitmap->width, bitmap->height, false, false);
tftWriteData(bitmap->bitmap, bitmap->width, bitmap->height,
bitmap->space, bg, fg);
return bitmap->width;
}
width_t tftWriteGlyph(x_t x, y_t y, const __flash Font *font, code_t code,
uint16_t bg, uint16_t fg) {
const __flash Glyph *glyph = getGlyphAddress(font, code);
tftSetArea(x, y, glyph->width, font->height, false, false);
tftWriteData(glyph->bitmap, glyph->width, font->height,
font->space, bg, fg);
return glyph->width;
}
width_t tftWriteString(x_t x, y_t y, const __flash Font *font, const char *string,
uint16_t bg, uint16_t fg) {
width_t xorig = x;
uint8_t offset = 0;
for (; *string != '\0'; string++) {
uint8_t c = (uint8_t) *string;
if (c == 194) {
// multibyte
} else if (c == 195) {
// multibyte, add 64 to get code point
offset = 64;
} else {
code_t code = c + offset;
x += tftWriteGlyph(x, y, font, code, bg, fg);
offset = 0;
}
}
return x - xorig;
}