/
lcd.c
executable file
·576 lines (515 loc) · 16 KB
/
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// Filename: lcd.c
// Description: Hardware abstraction layer for Raspi-LCD
//
// Open Source Licensing GPL 3
//
// Author: Martin Steppuhn, www.emsystech.de
// Author: Dr. Valentin Illich, www.valentins-qtsolutions.de
//--------------------------------------------------------------------------------------------------
//=== Includes =====================================================================================
#include "std_c.h"
#include "lcd.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "raspilcd.h"
//=== Preprocessing directives (#define) ===========================================================
//=== Type definitions (typedef) ===================================================================
//=== Global constants =============================================================================
//=== Global variables =============================================================================
//=== Local constants =============================================================================
#include "font_terminal_6x8.inc"
#include "font_terminal_12x16.inc"
#include "font_fixedsys_8x15.inc"
#include "font_lucida_10x16.inc"
#ifdef QT_EMULATION
#include "QtEmulation.h"
#endif
//=== Local variables ==============================================================================
uint8 framebuffer[LCD_WIDTH][LCD_HEIGHT/8];
uint8 PenColor;
uint8 FillColor;
uint8 FontNumber;
void myprintf( const char *format,...)
{
static char buffer[4096];
va_list list;
va_start(list,format);
vsprintf(buffer,format,list);
#ifdef QT_EMULATION
Qt_printf(buffer);
#else
printf("%s",buffer);
#endif
va_end(list);
}
//=== Local function prototypes ====================================================================
void lcd_write_cmd(uint8 d);
void lcd_write_data(uint8 d);
void lcd_set_xy(uint8 x,uint8 ypage);
//--------------------------------------------------------------------------------------------------
// Name: LCD_SSetPenColor
// Function: Set pen color
//
// Parameter: 0=White 1=Black
// Return: -
//--------------------------------------------------------------------------------------------------
void LCD_SetPenColor(uint8 c)
{
#ifdef QT_EMULATION
Qt_SetPenColor(c);
#else
PenColor = c;
#endif
}
//--------------------------------------------------------------------------------------------------
// Name: LCD_SSetFillColor
// Function: Set fill color
//
// Parameter: -1=transparent, 0=white, 1=black
// Return:
//--------------------------------------------------------------------------------------------------
void LCD_SetFillColor(int8 c)
{
#ifdef QT_EMULATION
Qt_SetFillColor(c);
#else
FillColor = c;
#endif
}
//--------------------------------------------------------------------------------------------------
// Name: LCD_SSetFont
// Function: Set font
//
// Parameter: 0..3 for Fonts
// Return: -
//--------------------------------------------------------------------------------------------------
void LCD_SetFont(uint8 f)
{
FontNumber = f;
}
//--------------------------------------------------------------------------------------------------
// Name: LCD_ClearScreen
// Function: Clear Screen
//
// Parameter: -
// Return: -
//--------------------------------------------------------------------------------------------------
void LCD_ClearScreen(void)
{
#ifdef QT_EMULATION
Qt_ClearScreen();
#else
uint16 x,y;
for(y=0;y<(LCD_HEIGHT/8);y++)
{
for(x=0;x<LCD_WIDTH;x++) framebuffer[x][y] = 0;
}
#endif
}
//--------------------------------------------------------------------------------------------------
// Name: LCD_PutPixel
// Function: Set single pixel at x,x
//
// Parameter: x,y position, color
// Return: -
//--------------------------------------------------------------------------------------------------
void LCD_PutPixel(uint8 x,uint8 y,uint8 color)
{
#ifdef QT_EMULATION
Qt_PutPixel(x,y,color);
#else
if((x < LCD_WIDTH) && (y < LCD_HEIGHT))
{
if(color) framebuffer[x][y>>3] |= (1<<(y & 7));
else framebuffer[x][y>>3] &= ~(1<<(y & 7));
}
#endif
}
//--------------------------------------------------------------------------------------------------
// Name: LCD_DrawLine
// Function: Draw line from xy to xy
//
// Parameter: Start and endpoint
// Return: -
//--------------------------------------------------------------------------------------------------
void LCD_DrawLine(uint8 x0,uint8 y0,uint8 x1,uint8 y1)
{
#ifdef QT_EMULATION
Qt_DrawLine(x0,y0,x1,y1);
#else
int16 dx,sx,dy,sy,err,e2; // for Bresenham
int8 i;
if(y0 == y1) // horizontale Linie
{
if(x0 > x1) { i=x0; x0=x1; x1=i; } // swap direction
while (x0 <= x1) LCD_PutPixel(x0++,y0,PenColor);
}
else if(x0 == x1) // vertikale Linie
{
if(y0 > y1) { i=y0; y0=x1; y1=i; } // swap direction
while (y0 <= y1) LCD_PutPixel(x0,y0++,PenColor);
}
else // Bresenham Algorithmus
{
dx = abs(x1-x0);
sx = x0<x1 ? 1 : -1;
dy = -abs(y1-y0);
sy = y0<y1 ? 1 : -1;
err = dx+dy;
for(;;)
{
LCD_PutPixel(x0,y0,PenColor);
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 */
}
}
#endif
}
//--------------------------------------------------------------------------------------------------
// Name: LCD_DrawCircle
// Function: Draw circle with centerpont an radius
// linewidth=1 pixel, no fill
//
// Parameter: xy centerpoint and radius
// Return: -
//--------------------------------------------------------------------------------------------------
void LCD_DrawCircle(uint8 x0,uint8 y0,uint8 radius)
{
#ifdef QT_EMULATION
Qt_DrawCircle(x0,y0,radius);
#else
int f = 1 - radius;
int ddF_x = 0;
int ddF_y = -2 * radius;
int x = 0;
int y = radius;
LCD_PutPixel(x0, y0 + radius,PenColor);
LCD_PutPixel(x0, y0 - radius,PenColor);
LCD_PutPixel(x0 + radius, y0,PenColor);
LCD_PutPixel(x0 - radius, y0,PenColor);
while(x < y)
{
if(f >= 0)
{
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x + 1;
LCD_PutPixel(x0 + x, y0 + y,PenColor);
LCD_PutPixel(x0 - x, y0 + y,PenColor);
LCD_PutPixel(x0 + x, y0 - y,PenColor);
LCD_PutPixel(x0 - x, y0 - y,PenColor);
LCD_PutPixel(x0 + y, y0 + x,PenColor);
LCD_PutPixel(x0 - y, y0 + x,PenColor);
LCD_PutPixel(x0 + y, y0 - x,PenColor);
LCD_PutPixel(x0 - y, y0 - x,PenColor);
}
#endif
}
//--------------------------------------------------------------------------------------------------
// Name: LCD_DrawEllipse
// Function: Draw ellipse with centerpont an "radius"
// linewidth=1 pixel, no fill
//
// Parameter: xy centerpoint and "x,y radius"
// Return: -
//--------------------------------------------------------------------------------------------------
void LCD_DrawEllipse(int xm, int ym, int a, int b)
{
#ifdef QT_EMULATION
Qt_DrawEllipse(xm,ym,a,b);
#else
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
{
LCD_PutPixel(xm+dx, ym+dy,PenColor); /* I. Quadrant */
LCD_PutPixel(xm-dx, ym+dy,PenColor); /* II. Quadrant */
LCD_PutPixel(xm-dx, ym-dy,PenColor); /* III. Quadrant */
LCD_PutPixel(xm+dx, ym-dy,PenColor); /* 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) */
LCD_PutPixel(xm+dx, ym,PenColor); /* -> Spitze der Ellipse vollenden */
LCD_PutPixel(xm-dx, ym,PenColor);
}
#endif
}
//--------------------------------------------------------------------------------------------------
// Name: LCD_DrawRect
// Function: Draw rectangle, with pencolor and fillcolor
//
// Parameter: two diagonal points and linewidth
// Return: -
//--------------------------------------------------------------------------------------------------
void LCD_DrawRect(uint8 x0,uint8 y0,uint8 x1,uint8 y1,uint8 line)
{
#ifdef QT_EMULATION
Qt_DrawRect(x0,y0,x1,y1,line);
#else
uint8 x,y;
y = y0;
while(y<=y1) // zeilenweise durcharbeiten
{
x = x0;
while(x<=x1)
{
if((y < y0 + line) || (y > y1 - line) || (x < x0 + line) || (x > x1 - line))
{
LCD_PutPixel(x,y,PenColor);
}
else
{
if( FillColor == 0) LCD_PutPixel(x,y,0);
else if(FillColor == 1) LCD_PutPixel(x,y,1);
}
x++;
}
y++;
}
#endif
}
//--------------------------------------------------------------------------------------------------
// Name: LCD_PrintXY
// Function: Print String
//
// Parameter: x,y startpoint, string
// Return: -
//--------------------------------------------------------------------------------------------------
void LCD_PrintXY(uint8 x0,uint8 y0,const char *s)
{
uint8 ix,iy,y;
const uint8 *font;
const uint8 *pt;
uint8 d;
char c;
uint8 char_width,char_height,char_size;
uint8 i_char_height;
if( FontNumber == 1) font = font_fixedsys_8x15;
else if(FontNumber == 2) font= font_lucida_10x16;
else if(FontNumber == 3) font = font_terminal_12x16;
else font = font_terminal_6x8;
char_size = *font++;
char_width = *font++;
char_height = *font++;
while(*s)
{
c = 0;
if(*s > 31) c = (*s) - 32;
pt = &font[(uint16)c * char_size];
i_char_height = char_height;
y = y0;
while(i_char_height)
{
for(ix=0;ix<char_width;ix++)
{
d = *pt++;
for(iy=0;iy<8;iy++) // je ein Byte vertikal ausgeben
{
if(d & (1<<iy)) LCD_PutPixel(x0+ix,y+iy,1);
else LCD_PutPixel(x0+ix,y+iy,0);
}
}
i_char_height = (i_char_height >= 8) ? i_char_height - 8 : 0;
y+=8; // naechste "Page"
}
x0+=char_width;
s++; // naechstes Zeichen
}
}
//--------------------------------------------------------------------------------------------------
// Name: LCD_DrawBitmap
// Function: Draw bitmap at startingpoint
//
// Parameter: Startpoint xy, pointer to bitmaparray
// Return: -
//--------------------------------------------------------------------------------------------------
void LCD_DrawBitmap(uint8 x0,uint8 y0,const uint8 *bmp)
{
uint8 width,height;
uint16 ix,iy;
width = *bmp++;
height = *bmp++;
for(iy=0;iy<height;iy++)
{
for(ix=0;ix<width;ix++)
{
if(bmp[ix+((iy/8)*width)] & (1<<(iy & 7))) LCD_PutPixel(x0+ix,y0+iy,1);
else LCD_PutPixel(x0+ix,y0+iy,0);
}
}
}
/*
//--------------------------------------------------------------------------------------------------
// Name: LCD_DrawBitmapFromFile
// Function: Draw bitmap from file UNDER CONSTRUCTION !!!
//
// Parameter:
// Return:
//--------------------------------------------------------------------------------------------------
void LCD_DrawBitmapFromFile(uint8 x0,uint8 y0,const char *filename)
{
uint8 width,height;
uint16 ix,iy;
FILE *fp;
uint8 buf[10000];
uint8 i;
fp = fopen(filename, "rb"); // open as file
if(fp != NULL)
{
fread(buf, 1, 58,fp);
fread(buf,1,1024,fp);
width = 128;
height = 64;
i = 0;
for(iy=0;iy<height;iy++)
{
for(ix=0;ix<width;ix++)
{
if(buf[(ix/8)+(iy*(width/8))] & (0x80>>(ix & 7))) LCD_PutPixel(x0+ix,y0+(height-iy),0);
else LCD_PutPixel(x0+ix,y0+(height-iy),1);
}
}
}
}
*/
//--------------------------------------------------------------------------------------------------
// Name: LCD_WriteFramebuffer
// Function: Transfer Framebuffer (RAM) to display via SPI
//
// Parameter: -
// Return: -
//--------------------------------------------------------------------------------------------------
void LCD_WriteFramebuffer(void)
{
#ifdef QT_EMULATION
Qt_WriteFramebuffer();
#else
uint8 x;
lcd_set_xy(0,0); for(x=0;x<LCD_WIDTH;x++) lcd_write_data(framebuffer[x][0]);
lcd_set_xy(0,1); for(x=0;x<LCD_WIDTH;x++) lcd_write_data(framebuffer[x][1]);
lcd_set_xy(0,2); for(x=0;x<LCD_WIDTH;x++) lcd_write_data(framebuffer[x][2]);
lcd_set_xy(0,3); for(x=0;x<LCD_WIDTH;x++) lcd_write_data(framebuffer[x][3]);
lcd_set_xy(0,4); for(x=0;x<LCD_WIDTH;x++) lcd_write_data(framebuffer[x][4]);
lcd_set_xy(0,5); for(x=0;x<LCD_WIDTH;x++) lcd_write_data(framebuffer[x][5]);
lcd_set_xy(0,6); for(x=0;x<LCD_WIDTH;x++) lcd_write_data(framebuffer[x][6]);
lcd_set_xy(0,7); for(x=0;x<LCD_WIDTH;x++) lcd_write_data(framebuffer[x][7]);
#endif
}
//--------------------------------------------------------------------------------------------------
// Name: LCD_SetContrast
// Function: Kontrasteinstellung
//
// Parameter: Kontrast (0..63)
// Return: -
//--------------------------------------------------------------------------------------------------
void LCD_SetContrast(uint8 contrast)
{
#ifdef QT_EMULATION
Qt_SetContrast(contrast);
#else
lcd_write_cmd(0x81);
lcd_write_cmd(contrast);
#endif
}
//--------------------------------------------------------------------------------------------------
// Name: LCD_Init
// Function: Init displaycontroller
//
// Parameter: -
// Return: -
//--------------------------------------------------------------------------------------------------
void LCD_Init(void)
{
#ifdef QT_EMULATION
return; // QT-Simul!
#endif
LCD_RST_CLR; // Reset LCD-Display
SleepMs(50); // Wait ~ 200ms
LCD_RST_SET;
SleepMs(200); // Wait ~ 200ms
lcd_write_cmd(0xE2); // reset
lcd_write_cmd(0x40); // operating mode
lcd_write_cmd(0xA1); // RAM addr SEG reverse (gespiegelt)
lcd_write_cmd(0xC0); // scan direction
lcd_write_cmd(0xA4); // normal display
lcd_write_cmd(0xA6); // LDC normal
lcd_write_cmd(0xA2); // LCD voltage 1/9 bias
lcd_write_cmd(0x2F); // power control
lcd_write_cmd(0x27); // resistor ratio Rb/Ra
lcd_write_cmd(0x81); // contrast (?) elektrisch
lcd_write_cmd(6); // contrast wert
lcd_write_cmd(0xFA);
lcd_write_cmd(0x90);
lcd_write_cmd(0xAF); // display on
LCD_ClearScreen();
LCD_WriteFramebuffer();
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
// Name: LcdWriteCmd
// Function: Kommandobyte an Display senden
//
// Parameter: Byte
// Return: -
//--------------------------------------------------------------------------------------------------
void lcd_write_cmd(uint8 d)
{
#ifdef QT_EMULATION
return; // QT-Simul!
#endif
LCD_CS_CLR;
LCD_RS_CLR; // Command Mode
LCD_SPI_PUTC(d);
LCD_SPI_WAIT_BUSY;
LCD_CS_SET;
}
//--------------------------------------------------------------------------------------------------
// Name: lcd_write_data
// Function: Datenbyte an Display senden
//
// Parameter: Byte
// Return: -
//--------------------------------------------------------------------------------------------------
void lcd_write_data(uint8 d)
{
#ifdef QT_EMULATION
return; // QT-Simul!
#endif
LCD_CS_CLR;
LCD_RS_SET; // Data Mode
LCD_SPI_PUTC(d);
LCD_SPI_WAIT_BUSY;
LCD_CS_SET;
}
//--------------------------------------------------------------------------------------------------
// Name: LcdSetXY
// Function: Position für Ausgabe festlegen (Koordinaten)
//
// Parameter: X Position (Pixel), Y Position (Page / 8 Pixel Blocks)
// Return: -
//--------------------------------------------------------------------------------------------------
void lcd_set_xy(uint8 x,uint8 ypage)
{
#ifdef QT_EMULATION
return; // QT-Simul!
#endif
x += LCD_X_OFFSET;
lcd_write_cmd(0x00 + (x & 0x0F));
lcd_write_cmd(0x10 + ((x>>4) & 0x0F));
lcd_write_cmd(0xB0 + (ypage & 0x07));
}