forked from theZiz/ev3c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ev3c_lcd.c
521 lines (492 loc) · 11.2 KB
/
ev3c_lcd.c
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/* This file is part of ev3c.
* Ev3c 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 2 of the License, or
* (at your option) any later version.
*
* Ev3c 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 ev3c. If not, see <http://www.gnu.org/licenses/>
*
* For feedback and questions about my Files and Projects please mail me,
* Alexander Matthes (Ziz) , ziz_at_mailbox.org, http://github.com/theZiz */
#include "ev3c_lcd.h"
#include "linux/kd.h"
#include "large_font.xbm"
#define LARGE_FONT_PIXEL(x,y) ((large_font_bits[((x) >> 3) + (y) * 32 ] & (1 << ((x) & 7))) >> ((x) & 7))
#define LARGE_LETTER_WIDTH 16
#define LARGE_LETTER_HEIGHT 16
#include "normal_font.xbm"
#define NORMAL_FONT_PIXEL(x,y) ((normal_font_bits[((x) >> 3) + (y) * 16 ] & (1 << ((x) & 7))) >> ((x) & 7))
#define NORMAL_LETTER_WIDTH 8
#define NORMAL_LETTER_HEIGHT 9
#include "small_font.xbm"
#define SMALL_FONT_PIXEL(x,y) ((small_font_bits[((x) >> 3) + (y) * 16 ] & (1 << ((x) & 7))) >> ((x) & 7))
#define SMALL_LETTER_WIDTH 8
#define SMALL_LETTER_HEIGHT 8
#include "tiny_font.xbm"
#define TINY_FONT_PIXEL(x,y) ((tiny_font_bits[((x) >> 3) + (y) * 16 ] & (1 << ((x) & 7))) >> ((x) & 7))
#define TINY_LETTER_WIDTH 5
#define TINY_FONT_WIDTH 8
#define TINY_LETTER_HEIGHT 7
int32_t __fbfd, __vtfd;
unsigned char* __fbp = NULL;
void ev3_init_lcd()
{
__fbfd = open("/dev/fb0", O_RDWR);
__fbp = (char*)mmap(0, EV3_SY_LCD, PROT_READ | PROT_WRITE, MAP_SHARED, __fbfd, 0);
__vtfd = open("/dev/tty", O_RDONLY);
ioctl(__vtfd, KDSETMODE, KD_GRAPHICS);
}
void ev3_clear_lcd()
{
memset(__fbp,0,EV3_SY_LCD);
}
#define EV3_PIXEL_SET(x,y) __fbp[((x) >> 3) + (y) * EV3_S_LCD] |= 1 << ((x) & 7)
#define EV3_PIXEL_UNSET(x,y) __fbp[((x) >> 3) + (y) * EV3_S_LCD] &= 0xFF ^ (1 << ((x) & 7))
#define EV3_PIXEL(x,y,bit) {if (bit) EV3_PIXEL_SET(x,y); else EV3_PIXEL_UNSET(x,y);}
void ev3_pixel_lcd(int32_t x,int32_t y,int32_t bit)
{
EV3_PIXEL(x,y,bit);
}
void ev3_text_lcd_large(int32_t x,int32_t y,const char* text)
{
int32_t i;
for (i = 0; text[i]; i++)
{
if (text[i] < ' ' || text[i] > 127)
continue;
int32_t c_x = ((text[i] - ' ') % 16) * LARGE_LETTER_WIDTH;
int32_t c_y = ((text[i] - ' ') / 16) * LARGE_LETTER_HEIGHT;
int32_t a;
for (a = 0; a < LARGE_LETTER_WIDTH; a++)
{
int32_t X = x + i * LARGE_LETTER_WIDTH + a;
if (X < 0)
continue;
if (X >= (int32_t)EV3_X_LCD)
return;
int32_t b;
for (b = 0; b < LARGE_LETTER_HEIGHT; b++)
{
int32_t Y = y + b;
if (Y < 0)
continue;
if (Y >= (int32_t)EV3_Y_LCD)
break;
if (LARGE_FONT_PIXEL(c_x + a,c_y + b))
EV3_PIXEL_SET(X,Y);
}
}
}
}
void ev3_text_lcd_normal(int32_t x,int32_t y,const char* text)
{
int32_t i;
for (i = 0; text[i]; i++)
{
if (text[i] < ' ' || text[i] > 127)
continue;
int32_t c_x = ((text[i] - ' ') % 16) * NORMAL_LETTER_WIDTH;
int32_t c_y = ((text[i] - ' ') / 16) * NORMAL_LETTER_HEIGHT;
int32_t a;
for (a = 0; a < NORMAL_LETTER_WIDTH; a++)
{
int32_t X = x + i * NORMAL_LETTER_WIDTH + a;
if (X < 0)
continue;
if (X >= (int32_t)EV3_X_LCD)
return;
int32_t b;
for (b = 0; b < NORMAL_LETTER_HEIGHT; b++)
{
int32_t Y = y + b;
if (Y < 0)
continue;
if (Y >= (int32_t)EV3_Y_LCD)
break;
if (NORMAL_FONT_PIXEL(c_x + a,c_y + b))
EV3_PIXEL_SET(X,Y);
}
}
}
}
void ev3_text_lcd_small(int32_t x,int32_t y,const char* text)
{
int32_t i;
for (i = 0; text[i]; i++)
{
if (text[i] < ' ' || text[i] > 127)
continue;
int32_t c_x = ((text[i] - ' ') % 16) * SMALL_LETTER_WIDTH;
int32_t c_y = ((text[i] - ' ') / 16) * SMALL_LETTER_HEIGHT;
int32_t a;
for (a = 0; a < SMALL_LETTER_WIDTH; a++)
{
int32_t X = x + i * SMALL_LETTER_WIDTH + a;
if (X < 0)
continue;
if (X >= (int32_t)EV3_X_LCD)
return;
int32_t b;
for (b = 0; b < SMALL_LETTER_HEIGHT; b++)
{
int32_t Y = y + b;
if (Y < 0)
continue;
if (Y >= (int32_t)EV3_Y_LCD)
break;
if (SMALL_FONT_PIXEL(c_x + a,c_y + b))
EV3_PIXEL_SET(X,Y);
}
}
}
}
void ev3_text_lcd_tiny(int32_t x,int32_t y,const char* text)
{
int32_t i;
for (i = 0; text[i]; i++)
{
if (text[i] < ' ' || text[i] > 127)
continue;
int32_t c_x = ((text[i] - ' ') % 16) * TINY_FONT_WIDTH;
int32_t c_y = ((text[i] - ' ') / 16) * TINY_LETTER_HEIGHT;
int32_t a;
for (a = 0; a < TINY_LETTER_WIDTH; a++)
{
int32_t X = x + i * TINY_LETTER_WIDTH + a;
if (X < 0)
continue;
if (X >= (int32_t)EV3_X_LCD)
return;
int32_t b;
for (b = 0; b < TINY_LETTER_HEIGHT; b++)
{
int32_t Y = y + b;
if (Y < 0)
continue;
if (Y >= (int32_t)EV3_Y_LCD)
break;
if (TINY_FONT_PIXEL(c_x + a,c_y + b))
EV3_PIXEL_SET(X,Y);
}
}
}
}
void ev3_rectangle_lcd(int32_t x,int32_t y,int32_t w,int32_t h,int32_t bit)
{
int32_t a,b;
int32_t minx = x;
int32_t miny = y;
int32_t maxx = x+w;
int32_t maxy = y+h;
if (minx >= (int32_t)EV3_X_LCD)
return;
if (miny >= (int32_t)EV3_Y_LCD)
return;
if (maxx < 0)
return;
if (maxy < 0)
return;
if (minx < 0)
minx = 0;
if (miny < 0)
miny = 0;
if (maxx >= EV3_X_LCD)
maxx = EV3_X_LCD-1;
if (maxy >= EV3_Y_LCD)
maxy = EV3_Y_LCD-1;
if (bit)
for (a = minx; a <= maxx; a++)
for (b = miny; b <= maxy; b++)
EV3_PIXEL_SET(a,b);
else
for (a = minx; a <= maxx; a++)
for (b = miny; b <= maxy; b++)
EV3_PIXEL_UNSET(a,b);
}
void ev3_rectangle_lcd_out(int32_t x,int32_t y,int32_t w,int32_t h,int32_t bit)
{
int32_t a;
int32_t minx = x;
int32_t miny = y;
int32_t maxx = x+w;
int32_t maxy = y+h;
if (minx >= (int32_t)EV3_X_LCD)
return;
if (miny >= (int32_t)EV3_Y_LCD)
return;
if (maxx < 0)
return;
if (maxy < 0)
return;
if (minx < 0)
minx = 0;
if (miny < 0)
miny = 0;
if (maxx >= EV3_X_LCD)
maxx = EV3_X_LCD-1;
if (maxy >= EV3_Y_LCD)
maxy = EV3_Y_LCD-1;
if (bit)
{
if (y == miny)
for (a = minx; a <= maxx; a++)
EV3_PIXEL_SET(a,miny);
if (y+w == maxy)
for (a = minx; a <= maxx; a++)
EV3_PIXEL_SET(a,maxy);
if (x == minx)
for (a = miny+1; a <= maxy-1; a++)
EV3_PIXEL_SET(minx,a);
if (x+h == maxx)
for (a = miny+1; a <= maxy-1; a++)
EV3_PIXEL_SET(maxx,a);
}
else
{
if (y == miny)
for (a = minx; a <= maxx; a++)
EV3_PIXEL_UNSET(a,miny);
if (y+w == maxy)
for (a = minx; a <= maxx; a++)
EV3_PIXEL_UNSET(a,maxy);
if (x == minx)
for (a = miny+1; a <= maxy-1; a++)
EV3_PIXEL_UNSET(minx,a);
if (x+h == maxx)
for (a = miny+1; a <= maxy-1; a++)
EV3_PIXEL_UNSET(maxx,a);
}
}
void ev3_circle_lcd(int32_t x,int32_t y,int32_t r,int32_t bit)
{
int32_t a,b;
int32_t minx = x-r;
int32_t miny = y-r;
int32_t maxx = x+r;
int32_t maxy = y+r;
if (minx >= (int32_t)EV3_X_LCD)
return;
if (miny >= (int32_t)EV3_Y_LCD)
return;
if (maxx < 0)
return;
if (maxy < 0)
return;
if (minx < 0)
minx = 0;
if (miny < 0)
miny = 0;
if (maxx >= EV3_X_LCD)
maxx = EV3_X_LCD-1;
if (maxy >= EV3_Y_LCD)
maxy = EV3_Y_LCD-1;
if (bit)
for (a = minx; a <= maxx; a++)
for (b = miny; b <= maxy; b++)
{
if ((a-x)*(a-x) + (b-y)*(b-y) >= r*r)
continue;
EV3_PIXEL_SET(a,b);
}
else
for (a = minx; a <= maxx; a++)
for (b = miny; b <= maxy; b++)
{
if ((a-x)*(a-x) + (b-y)*(b-y) >= r*r)
continue;
EV3_PIXEL_UNSET(a,b);
}
}
#define MIRROR_PIXEL_SET(a,b) \
{ \
if (y+b >= 0 && y+b < EV3_Y_LCD) \
{ \
if (x+a >= 0 && x+a < EV3_X_LCD) \
EV3_PIXEL_SET(x+a,y+b); \
if (x-a >= 0 && x-a < EV3_X_LCD) \
EV3_PIXEL_SET(x-a,y+b); \
} \
if (y-b >= 0 && y-b < EV3_Y_LCD) \
{ \
if (x+a >= 0 && x+a < EV3_X_LCD) \
EV3_PIXEL_SET(x+a,y-b); \
if (x-a >= 0 && x-a < EV3_X_LCD) \
EV3_PIXEL_SET(x-a,y-b); \
} \
}
#define MIRROR_PIXEL_UNSET(a,b) \
{ \
if (y+b >= 0 && y+b < EV3_Y_LCD) \
{ \
if (x+a >= 0 && x+a < EV3_X_LCD) \
EV3_PIXEL_UNSET(x+a,y+b); \
if (x-a >= 0 && x-a < EV3_X_LCD) \
EV3_PIXEL_UNSET(x-a,y+b); \
} \
if (y-b >= 0 && y-b < EV3_Y_LCD) \
{ \
if (x+a >= 0 && x+a < EV3_X_LCD) \
EV3_PIXEL_UNSET(x+a,y-b); \
if (x-a >= 0 && x-a < EV3_X_LCD) \
EV3_PIXEL_UNSET(x-a,y-b); \
} \
}
void ev3_circle_lcd_out(int32_t x,int32_t y,int32_t r,int32_t bit)
{
int32_t a,b;
if (bit)
{
a = r;
for (b = 0; b <= r; b++)
{
int once = 0;
while (a*a + b*b >= r*r)
{
once = 1;
a--;
MIRROR_PIXEL_SET(a,b)
if (a == 0)
break;
}
if (!once)
MIRROR_PIXEL_SET(a,b)
}
}
else
{
a = r;
for (b = 0; b <= r; b++)
{
int once = 0;
while (a*a + b*b >= r*r)
{
once = 1;
a--;
MIRROR_PIXEL_UNSET(a,b)
if (a == 0)
break;
}
if (!once)
MIRROR_PIXEL_UNSET(a,b)
}
}
}
void ev3_ellipse_lcd(int32_t x,int32_t y,int32_t rx,int32_t ry,int32_t bit)
{
int32_t a,b;
int32_t minx = x-rx;
int32_t miny = y-ry;
int32_t maxx = x+rx;
int32_t maxy = y+ry;
if (minx >= (int32_t)EV3_X_LCD)
return;
if (miny >= (int32_t)EV3_Y_LCD)
return;
if (maxx < 0)
return;
if (maxy < 0)
return;
if (minx < 0)
minx = 0;
if (miny < 0)
miny = 0;
if (maxx >= EV3_X_LCD)
maxx = EV3_X_LCD-1;
if (maxy >= EV3_Y_LCD)
maxy = EV3_Y_LCD-1;
if (bit)
for (a = minx; a <= maxx; a++)
for (b = miny; b <= maxy; b++)
{
if ((a-x)*(a-x)*ry*ry + (b-y)*(b-y)*rx*rx >= rx*rx*ry*ry)
continue;
EV3_PIXEL_SET(a,b);
}
else
for (a = minx; a <= maxx; a++)
for (b = miny; b <= maxy; b++)
{
if ((a-x)*(a-x)*ry*ry + (b-y)*(b-y)*rx*rx >= rx*rx*ry*ry)
continue;
EV3_PIXEL_UNSET(a,b);
}
}
void ev3_ellipse_lcd_out(int32_t x,int32_t y,int32_t rx,int32_t ry,int32_t bit)
{
int32_t a,b;
if (bit)
{
a = rx;
for (b = 0; b <= ry; b++)
{
int once = 0;
while (a*a*ry*ry + b*b*rx*rx >= rx*rx*ry*ry)
{
once = 1;
a--;
MIRROR_PIXEL_SET(a,b)
if (a == 0)
break;
}
if (!once)
MIRROR_PIXEL_SET(a,b)
}
}
else
{
a = rx;
for (b = 0; b <= ry; b++)
{
int once = 0;
while (a*a*ry*ry + b*b*rx*rx >= rx*rx*ry*ry)
{
once = 1;
a--;
MIRROR_PIXEL_UNSET(a,b)
if (a == 0)
break;
}
if (!once)
MIRROR_PIXEL_UNSET(a,b)
}
}
}
void ev3_line_lcd(int32_t x0, int32_t y0, int32_t x1, int32_t y1,int32_t bit)
{
int32_t dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
int32_t dy = -abs(y1-y0), sy = y0<y1 ? 1 : -1;
int32_t err = dx+dy, e2;
if (bit)
for(;;)
{
if (x0 >= 0 && x0 < (int32_t)EV3_X_LCD && y0 >= 0 && y0 < (int32_t)EV3_Y_LCD)
EV3_PIXEL(x0,y0,1);
if (x0==x1 && y0==y1) break;
e2 = 2*err;
if (e2 > dy) { err += dy; x0 += sx; } /* e_xy+e_x > 0 */
if (e2 < dx) { err += dx; y0 += sy; } /* e_xy+e_y < 0 */
}
else
for(;;)
{
if (x0 >= 0 && x0 < (int32_t)EV3_X_LCD && y0 >= 0 && y0 < (int32_t)EV3_Y_LCD)
EV3_PIXEL(x0,y0,0);
if (x0==x1 && y0==y1) break;
e2 = 2*err;
if (e2 > dy) { err += dy; x0 += sx; } /* e_xy+e_x > 0 */
if (e2 < dx) { err += dx; y0 += sy; } /* e_xy+e_y < 0 */
}
}
void ev3_quit_lcd()
{
munmap(__fbp, EV3_SY_LCD);
close(__fbfd);
ioctl(__vtfd, KDSETMODE, KD_TEXT);
close(__vtfd);
}