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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
/*
ChibiOS/RT - Copyright (C) 2012
Joel Bodenmann aka Tectu <joel@unormal.org>
This file is part of ChibiOS-LCD-Driver.
ChibiOS-LCD-Driver is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
ChibiOS-LCD-Driver is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Emulation routines included into gdisp_lld.c
*/
/*
Even though this is a software emulation of a low level driver
most validation doesn't need to happen here as eventually
we call a real low level driver routine and if validation is
required - it will do it.
*/
#include "ch.h"
#include "hal.h"
#include "gdisp.h"
#if HAL_USE_GDISP || defined(__DOXYGEN__)
#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#else
# define UNUSED(x) x
#endif
#if !GDISP_HARDWARE_POWERCONTROL
void gdisp_lld_setpowermode(gdisp_powermode_t UNUSED(powerMode)) {
}
#endif
#if !GDISP_HARDWARE_ORIENTATION
void gdisp_lld_setorientation(gdisp_orientation_t UNUSED(newOrientation)) {
}
#endif
#if !GDISP_HARDWARE_CLEARS
void gdisp_lld_clear(color_t color) {
gdisp_lld_fillarea(0, 0, GDISP.Width, GDISP.Height, color);
}
#endif
#if !GDISP_HARDWARE_LINES
void gdisp_lld_drawline(coord_t x0, coord_t y0, coord_t x1, coord_t y1, color_t color) {
int16_t dy, dx;
int16_t addx, addy;
int16_t P, diff, i;
#if GDISP_HARDWARE_FILLS || GDISP_HARDWARE_SCROLL
// speed improvement if vertical or horizontal
if (x0 == x1) {
if (y1 > y0)
gdisp_lld_fillarea(x0, y0, 1, y1-y0+1, color);
else
gdisp_lld_fillarea(x0, y1, 1, y0-y1+1, color);
return;
}
if (y0 == y1) {
if (x1 > x0)
gdisp_lld_fillarea(x0, y0, x1-x0+1, 1, color);
else
gdisp_lld_fillarea(x0, y1, x0-x1+1, 1, color);
return;
}
#endif
if (x1 >= x0) {
dx = x1 - x0;
addx = 1;
} else {
dx = x0 - x1;
addx = -1;
}
if (y1 >= y0) {
dy = y1 - y0;
addy = 1;
} else {
dy = y0 - y1;
addy = -1;
}
if (dx >= dy) {
dy *= 2;
P = dy - dx;
diff = P - dx;
for(i=0; i<=dx; ++i) {
gdisp_lld_drawpixel(x0, y0, color);
if (P < 0) {
P += dy;
x0 += addx;
} else {
P += diff;
x0 += addx;
y0 += addy;
}
}
} else {
dx *= 2;
P = dx - dy;
diff = P - dy;
for(i=0; i<=dy; ++i) {
gdisp_lld_drawpixel(x0, y0, color);
if (P < 0) {
P += dx;
y0 += addy;
} else {
P += diff;
x0 += addx;
y0 += addy;
}
}
}
}
#endif
#if !GDISP_HARDWARE_FILLS
void gdisp_lld_fillarea(coord_t x, coord_t y, coord_t cx, coord_t cy, color_t color) {
#if GDISP_HARDWARE_SCROLL
gdisp_lld_verticalscroll(x, y, cx, cy, cy, color);
#elif GDISP_HARDWARE_LINES
coord_t x1, y1;
x1 = x + cx - 1;
y1 = y + cy;
for(; y < y1; y++)
gdisp_lld_drawline(x, y, x1, y, color);
#else
coord_t x0, x1, y1;
x0 = x;
x1 = x + cx;
y1 = y + cy;
for(; y < y1; y++)
for(x = x0; x < x1; x++)
gdisp_lld_drawpixel(x, y, color);
#endif
}
#endif
#if !GDISP_HARDWARE_BITFILLS
void gdisp_lld_blitarea(coord_t x, coord_t y, coord_t cx, coord_t cy, pixel_t *buffer) {
coord_t x0, x1, y1;
x0 = x;
x1 = x + cx;
y1 = y + cy;
for(; y < y1; y++)
for(x = x0; x < x1; x++)
gdisp_lld_drawpixel(x, y, *buffer++);
}
#endif
#if GDISP_NEED_CIRCLE && !GDISP_HARDWARE_CIRCLES
void gdisp_lld_drawcircle(coord_t x, coord_t y, coord_t radius, color_t color) {
int16_t a, b, P;
a = 0;
b = radius;
P = 1 - radius;
do {
gdisp_lld_drawpixel(a+x, b+y, color);
gdisp_lld_drawpixel(b+x, a+y, color);
gdisp_lld_drawpixel(x-a, b+y, color);
gdisp_lld_drawpixel(x-b, a+y, color);
gdisp_lld_drawpixel(b+x, y-a, color);
gdisp_lld_drawpixel(a+x, y-b, color);
gdisp_lld_drawpixel(x-a, y-b, color);
gdisp_lld_drawpixel(x-b, y-a, color);
if (P < 0)
P += 3 + 2*a++;
else
P += 5 + 2*(a++ - b--);
} while(a <= b);
}
#endif
#if GDISP_NEED_CIRCLE && !GDISP_HARDWARE_CIRCLEFILLS
void gdisp_lld_fillcircle(coord_t x, coord_t y, coord_t radius, color_t color) {
int16_t a, b, P;
a = 0;
b = radius;
P = 1 - radius;
do {
gdisp_lld_drawline(x-a, y+b, x+a, y+b, color);
gdisp_lld_drawline(x-a, y-b, x+a, y-b, color);
gdisp_lld_drawline(x-b, y+a, x+b, y+a, color);
gdisp_lld_drawline(x-b, y-a, x+b, y-a, color);
if (P < 0)
P += 3 + 2*a++;
else
P += 5 + 2*(a++ - b--);
} while(a <= b);
}
#endif
#if GDISP_NEED_ELLIPSE && !GDISP_HARDWARE_ELLIPSES
void gdisp_lld_drawellipse(coord_t x, coord_t y, coord_t a, coord_t b, color_t color) {
int dx = 0, dy = b; /* im I. Quadranten von links oben nach rechts unten */
long a2 = a*a, b2 = b*b;
long err = b2-(2*b-1)*a2, e2; /* Fehler im 1. Schritt */
do {
gdisp_lld_drawpixel(x+dx, y+dy, color); /* I. Quadrant */
gdisp_lld_drawpixel(x-dx, y+dy, color); /* II. Quadrant */
gdisp_lld_drawpixel(x-dx, y-dy, color); /* III. Quadrant */
gdisp_lld_drawpixel(x+dx, y-dy, color); /* IV. Quadrant */
e2 = 2*err;
if(e2 < (2*dx+1)*b2) {
dx++;
err += (2*dx+1)*b2;
}
if(e2 > -(2*dy-1)*a2) {
dy--;
err -= (2*dy-1)*a2;
}
} while(dy >= 0);
while(dx++ < a) { /* fehlerhafter Abbruch bei flachen Ellipsen (b=1) */
gdisp_lld_drawpixel(x+dx, y, color); /* -> Spitze der Ellipse vollenden */
gdisp_lld_drawpixel(x-dx, y, color);
}
}
#endif
#if GDISP_NEED_ELLIPSE && !GDISP_HARDWARE_ELLIPSEFILLS
void gdisp_lld_fillellipse(coord_t x, coord_t y, coord_t a, coord_t b, color_t color) {
int dx = 0, dy = b; /* im I. Quadranten von links oben nach rechts unten */
long a2 = a*a, b2 = b*b;
long err = b2-(2*b-1)*a2, e2; /* Fehler im 1. Schritt */
do {
gdisp_lld_drawline(x-dx,y+dy,x+dx,y+dy, color);
gdisp_lld_drawline(x-dx,y-dy,x+dx,y-dy, color);
e2 = 2*err;
if(e2 < (2*dx+1)*b2) {
dx++;
err += (2*dx+1)*b2;
}
if(e2 > -(2*dy-1)*a2) {
dy--;
err -= (2*dy-1)*a2;
}
} while(dy >= 0);
while(dx++ < a) { /* fehlerhafter Abbruch bei flachen Ellipsen (b=1) */
gdisp_lld_drawpixel(x+dx, y, color); /* -> Spitze der Ellipse vollenden */
gdisp_lld_drawpixel(x-dx, y, color);
}
}
#endif
#if GDISP_NEED_TEXT && !GDISP_HARDWARE_TEXT
#include "gdisp_fonts.h"
#endif
#if GDISP_NEED_TEXT && !GDISP_HARDWARE_TEXT
void gdisp_lld_drawchar(coord_t x, coord_t y, char c, font_t font, color_t color) {
const fontcolumn_t *ptr;
fontcolumn_t column;
coord_t width, height, xscale, yscale;
coord_t i, j, xs, ys;
/* Check we actually have something to print */
width = _getCharWidth(font, c);
if (!width) return;
xscale = font->xscale;
yscale = font->yscale;
height = font->height * yscale;
width *= xscale;
ptr = _getCharData(font, c);
/* Loop through the data and display. The font data is LSBit first, down the column */
for(i=0; i < width; i+=xscale) {
/* Get the font bitmap data for the column */
column = *ptr++;
/* Draw each pixel */
for(j=0; j < height; j+=yscale, column >>= 1) {
if (column & 0x01) {
for(xs=0; xs < xscale; xs++)
for(ys=0; ys < yscale; ys++)
gdisp_lld_drawpixel(x+i+xs, y+j+ys, color);
}
}
}
}
#endif
#if GDISP_NEED_TEXT && !GDISP_HARDWARE_TEXTFILLS
void gdisp_lld_fillchar(coord_t x, coord_t y, char c, font_t font, color_t color, color_t bgcolor) {
coord_t width, height;
coord_t xscale, yscale;
/* Check we actually have something to print */
width = _getCharWidth(font, c);
if (!width) return;
xscale = font->xscale;
yscale = font->yscale;
height = font->height * yscale;
width *= xscale;
/* Method 1: Use background fill and then draw the text */
#if GDISP_HARDWARE_TEXT || GDISP_SOFTWARE_TEXTFILLDRAW
/* Fill the area */
gdisp_lld_fillarea(x, y, width, height, bgcolor);
/* Draw the text */
gdisp_lld_drawchar(x, y, c, font, color);
/* Method 2: Create a single column bitmap and then blit it */
#elif GDISP_HARDWARE_BITFILLS && GDISP_SOFTWARE_TEXTBLITCOLUMN
{
const fontcolumn_t *ptr;
fontcolumn_t column;
coord_t i, j, xs, ys;
/* Working buffer for fast non-transparent text rendering [patch by Badger]
This needs to be larger than the largest character we can print.
Assume the max is double sized by one column.
*/
static pixel_t buf[sizeof(fontcolumn_t)*8*2];
#if GDISP_NEED_VALIDATION
/* Check our buffer is big enough */
if (height > sizeof(buf)/sizeof(buf[0])) return;
#endif
ptr = _getCharData(font, c);
/* Loop through the data and display. The font data is LSBit first, down the column */
for(i = 0; i < width; i+=xscale) {
/* Get the font bitmap data for the column */
column = *ptr++;
/* Draw each pixel */
for(j = 0; j < height; j+=yscale, column >>= 1) {
if (column & 0x01) {
for(ys=0; ys < yscale; ys++)
gdispPackPixels(buf, 1, j+ys, 0, color);
} else {
for(ys=0; ys < yscale; ys++)
gdispPackPixels(buf, 1, j+ys, 0, bgcolor);
}
}
for(xs=0; xs < xscale; xs++)
gdisp_lld_blitarea(x+i+xs, y, 1, height, buf);
}
}
/* Method 3: Create a character bitmap and then blit it */
#elif GDISP_HARDWARE_BITFILLS
{
const fontcolumn_t *ptr;
fontcolumn_t column;
coord_t i, j, xs, ys;
/* Working buffer for fast non-transparent text rendering [patch by Badger]
This needs to be larger than the largest character we can print.
Assume the max is double sized.
*/
static pixel_t buf[20*(sizeof(fontcolumn_t)*8)*2];
#if GDISP_NEED_VALIDATION
/* Check our buffer is big enough */
if (width * height > sizeof(buf)/sizeof(buf[0])) return;
#endif
ptr = _getCharData(font, c);
/* Loop through the data and display. The font data is LSBit first, down the column */
for(i = 0; i < width; i+=xscale) {
/* Get the font bitmap data for the column */
column = *ptr++;
/* Draw each pixel */
for(j = 0; j < height; j+=yscale, column >>= 1) {
if (column & 0x01) {
for(xs=0; xs < xscale; xs++)
for(ys=0; ys < yscale; ys++)
gdispPackPixels(buf, width, i+xs, j+ys, color);
} else {
for(xs=0; xs < xscale; xs++)
for(ys=0; ys < yscale; ys++)
gdispPackPixels(buf, width, i+xs, j+ys, bgcolor);
}
}
}
/* [Patch by Badger] Write all in one stroke */
gdisp_lld_blitarea(x, y, width, height, buf);
}
/* Method 4: Draw pixel by pixel */
#else
{
const fontcolumn_t *ptr;
fontcolumn_t column;
coord_t i, j, xs, ys;
ptr = _getCharData(font, c);
/* Loop through the data and display. The font data is LSBit first, down the column */
for(i = 0; i < width; i+=xscale) {
/* Get the font bitmap data for the column */
column = *ptr++;
/* Draw each pixel */
for(j = 0; j < height; j+=yscale, column >>= 1) {
if (column & 0x01) {
for(xs=0; xs < xscale; xs++)
for(ys=0; ys < yscale; ys++)
gdisp_lld_drawpixel(x+i, y+j, color);
} else {
for(xs=0; xs < xscale; xs++)
for(ys=0; ys < yscale; ys++)
gdisp_lld_drawpixel(x+i, y+j, bgcolor);
}
}
}
}
#endif
}
#endif
#if GDISP_NEED_CONTROL && !GDISP_HARDWARE_CONTROL
void gdisp_lld_control(int UNUSED(what), void *UNUSED(value)) {
/* Ignore everything */
}
#endif
#endif /* HAL_USE_GDISP */
|