-
Notifications
You must be signed in to change notification settings - Fork 3
/
lcd.c
551 lines (448 loc) · 10.5 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
/*
* Copyright 2010 Erik Gilling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdint.h>
#include <avr/io.h>
#include "lcd.h"
#include "pins.h"
#undef EPSON
#define PHILLIPS
/* Epson commands */
#define DISON 0xAF
#define DISOFF 0xAE
#define DISNOR 0xA6
#define DISINV 0xA7
#define SLPIN 0x95
#define SLPOUT 0x94
#define COMSCN 0xBB
#define DISCTL 0xCA
#define PASET 0x75
#define CASET 0x15
#define DATCTL 0xBC
#define RGBSET8 0xCE
#define RAMWR 0x5C
#define RAMRD 0x5D
#define PTLIN 0xA8
#define PTLOUT 0xA9
#define RMWIN 0xE0
#define RMWOUT 0xEE
#define ASCSET 0xAA
#define SCSTART 0xAB
#define OSCON 0xD1
#define OSCOFF 0xD2
#define PWRCTR 0x20
#define VOLCTR 0x81
#define VOLUP 0xD6
#define VOLDOWN 0xD7
#define TMPGRD 0x82
#define EPCTIN 0xCD
#define EPCOUT 0xCC
#define EPMWR 0xFC
#define EPMRD 0xFD
#define EPSRRD1 0x7C
#define EPSRRD2 0x7D
#define NOP 0x25
/* Phillips commands */
#define NOPP 0x00
#define BSTRON 0x03
#define SLEEPIN 0x10
#define SLEEPOUT 0x11
#define NORON 0x13
#define INVOFF 0x20
#define INVON 0x21
#define SETCON 0x25
#define DISPOFF 0x28
#define DISPON 0x29
#define CASETP 0x2A
#define PASETP 0x2B
#define RAMWRP 0x2C
#define RGBSET 0x2D
#define MADCTL 0x36
#define COLMOD 0x3A
#define DISCTR 0xB9
#define EC 0xC0
static inline void bit(uint8_t d, uint8_t data, uint8_t mask)
{
if (data & mask) {
PORTD = d | _BV(D_LCD_MOSI);
__asm__ __volatile__("nop");
PORTD = d | _BV(D_LCD_SCK) | _BV(D_LCD_MOSI);
} else {
PORTD = d;
__asm__ __volatile__("nop");
PORTD = d | _BV(D_LCD_SCK);
}
}
void lcd_data(uint8_t data)
{
uint8_t d = PORTD;
d &= ~_BV(D_LCD_SEL) & ~_BV(D_LCD_SCK) & ~_BV(D_LCD_MOSI);
/* D/C_N = 1 (data) */
PORTD = d | _BV(D_LCD_MOSI);
PORTD = d | _BV(D_LCD_MOSI) | _BV(D_LCD_SCK);
bit(d, data, 0x80);
bit(d, data, 0x40);
bit(d, data, 0x20);
bit(d, data, 0x10);
bit(d, data, 0x08);
bit(d, data, 0x04);
bit(d, data, 0x02);
bit(d, data, 0x01);
PORTD = d;
PORTD = d | _BV(D_LCD_SEL);
}
void lcd_cmd(uint8_t cmd)
{
uint8_t d = PORTD;
d &= ~_BV(D_LCD_SEL) & ~_BV(D_LCD_SCK) & ~_BV(D_LCD_MOSI);
/* D/C_N = 0 (data) */
PORTD = d;
PORTD = d | _BV(D_LCD_SCK);
bit(d, cmd, 0x80);
bit(d, cmd, 0x40);
bit(d, cmd, 0x20);
bit(d, cmd, 0x10);
bit(d, cmd, 0x08);
bit(d, cmd, 0x04);
bit(d, cmd, 0x02);
bit(d, cmd, 0x01);
PORTD = d;
PORTD = d | _BV(D_LCD_SEL);
}
#ifdef EPSON
static inline void epson_cmd(uint8_t cmd)
{
lcd_cmd(cmd);
}
static inline void epson_data(uint8_t data)
{
lcd_data(data);
}
#else
static inline void epson_cmd(uint8_t cmd)
{
}
static inline void epson_data(uint8_t data)
{
}
#endif
#ifdef PHILLIPS
static inline void phillips_cmd(uint8_t cmd)
{
lcd_cmd(cmd);
}
static inline void phillips_data(uint8_t data)
{
lcd_data(data);
}
#else
static inline void phillips_cmd(uint8_t cmd)
{
}
static inline void phillips_data(uint8_t data)
{
}
#endif
void lcd_set_aperature(uint8_t x, uint8_t y, uint8_t w, uint8_t h)
{
epson_cmd(CASET);
phillips_cmd(CASETP);
lcd_data(x + 1);
lcd_data(x + w);
epson_cmd(PASET);
phillips_cmd(PASETP);
lcd_data(y + 1);
lcd_data(y + h);
}
void lcd_fill(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t color)
{
int i;
color = ~color;
lcd_set_aperature(x, y, w, h);
epson_cmd(RAMWR);
phillips_cmd(RAMWRP);
for (i = 0; i < (w * h + 1) / 2; i++) {
lcd_data((color >> 4) & 0xFF);
lcd_data(((color & 0xF) << 4) | ((color >> 8) & 0xf));
lcd_data(color & 0xFF);
}
}
void lcd_draw_rect(uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint16_t color)
{
lcd_fill(x, y, w, 1, color);
lcd_fill(x, y, 1, h, color);
lcd_fill(x, y + h - 1, w, 1, color);
lcd_fill(x + w - 1, y, 1, h, color);
}
void lcd_draw_bitmap4(uint8_t x, uint8_t y, struct bitmap4 *bm)
{
uint16_t colors[4];
int i;
uint8_t data;
colors[0] = ~bm->colors[0];
colors[1] = ~bm->colors[1];
colors[2] = ~bm->colors[2];
colors[3] = ~bm->colors[3];
lcd_set_aperature(x, y, bm->w, bm->h);
epson_cmd(RAMWR);
phillips_cmd(RAMWRP);
for (i = 0; i < (bm->w * bm->h) / 4; i++) {
uint16_t c1, c2;
data = pgm_read_byte(bm->data + i);
c1 = colors[data & 0x3];
data >>= 2;
c2 = colors[data & 0x3];
data >>= 2;
lcd_data((c1 >> 4) & 0xFF);
lcd_data(((c1 & 0xF) << 4) | ((c2 >> 8) & 0xF));
lcd_data(c2 & 0xFF);
c1 = colors[data & 0x3];
data >>= 2;
c2 = colors[data & 0x3];
data >>= 2;
lcd_data((c1 >> 4) & 0xFF);
lcd_data(((c1 & 0xF) << 4) | ((c2 >> 8) & 0xF));
lcd_data(c2 & 0xFF);
}
}
void lcd_draw_graph(uint8_t x, uint8_t y, uint8_t h,
int32_t *data, uint8_t len, uint8_t idx,
int32_t scale_low, int32_t scale_high,
int32_t set_point,
uint16_t fg_color, uint16_t bg_color,
uint16_t sp_fg_color, uint16_t sp_bg_color)
{
int32_t inc = (scale_high - scale_low) / (h - 1);
int32_t val = scale_high;
int r, c;
uint8_t d;
bg_color = ~bg_color;
fg_color = ~fg_color;
sp_fg_color = ~sp_fg_color;
sp_bg_color = ~sp_bg_color;
lcd_set_aperature(x, y, len, h);
epson_cmd(RAMWR);
phillips_cmd(RAMWRP);
for (r = 0; r < h; r++) {
d = idx;
for (c = 0; c < len; c += 2) {
uint16_t c1, c2;
c1 = data[idx] > val ? fg_color : bg_color;
idx++;
if (idx == len)
idx = 0;
c2 = data[idx] > val ? fg_color : bg_color;
idx++;
if (idx == len)
idx = 0;
if (val > set_point && (val - inc) < set_point ) {
c1 = c1 == fg_color ? sp_fg_color : sp_bg_color;
c2 = c2 == fg_color ? sp_fg_color : sp_bg_color;
}
lcd_data((c1 >> 4) & 0xFF);
lcd_data(((c1 & 0xF) << 4) | ((c2 >> 8) & 0xF));
lcd_data(c2 & 0xFF);
}
val -= inc;
}
}
uint8_t lcd_print_char(uint8_t x, uint8_t y, char c,
struct font *font, uint16_t fg_color,
uint16_t bg_color)
{
int i, j;
struct font_glyph glyph;
uint8_t data;
bg_color = ~bg_color;
fg_color = ~fg_color;
if (c < font->min_char || c > font->max_char)
return 0;
memcpy_P(&glyph, font->info + c - font->min_char, sizeof(glyph));
lcd_set_aperature(x, y, glyph.w, font->h);
if (glyph.w == 0)
return 0;
epson_cmd(RAMWR);
phillips_cmd(RAMWRP);
for (i = 0; i < (glyph.w * font->h) / 8; i++) {
data = pgm_read_byte(font->data + glyph.idx + i);
for (j = 0; j < 4; j++) {
uint16_t c1 = data & 0x1 ? fg_color : bg_color;
uint16_t c2 = data & 0x2 ? fg_color : bg_color;
lcd_data((c1 >> 4) & 0xFF);
lcd_data(((c1 & 0xF) << 4) | ((c2 >> 8) & 0xF));
lcd_data(c2 & 0xFF);
data >>= 2;
}
}
lcd_fill(x + glyph.w, y, 1, font->h, ~bg_color);
return glyph.w + 1;
}
uint8_t lcd_print_string_P(uint8_t x, uint8_t y, PGM_P str,
struct font *font, uint16_t fg_color,
uint16_t bg_color)
{
char c;
uint8_t w = 0;
while ((c = pgm_read_byte(str++))) {
w += lcd_print_char(x + w, y, c, font, fg_color, bg_color);
}
return w;
}
uint8_t lcd_string_width_P(PGM_P str, struct font *font)
{
struct font_glyph glyph;
char c;
uint8_t w = 0;
while ((c = pgm_read_byte(str++))) {
if (c < font->min_char || c > font->max_char)
continue;
memcpy_P(&glyph, font->info + c - font->min_char, sizeof(glyph));
w += glyph.w + 1;
}
return w;
}
static prog_uint8_t digits[] = {'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'A', 'B',
'C', 'D', 'E', 'F'};
uint8_t lcd_print_hex(uint8_t x, uint8_t y, uint8_t data,
struct font *font, uint16_t fg_color,
uint16_t bg_color)
{
char c;
uint8_t w = 0;
c = pgm_read_byte(digits + (data >> 4));
w += lcd_print_char(x + w, y, c, font, fg_color, bg_color);
c = pgm_read_byte(digits + (data & 0xf));
w += lcd_print_char(x + w, y, c, font, fg_color, bg_color);
return w;
}
uint8_t lcd_print_hex16(uint8_t x, uint8_t y, uint16_t data,
struct font *font, uint16_t fg_color,
uint16_t bg_color)
{
uint8_t w = 0;
w += lcd_print_hex(x + w, y, data >> 8, font, fg_color, bg_color);
w += lcd_print_hex(x + w, y, data & 0xff, font, fg_color, bg_color);
return w;
}
uint8_t lcd_print_dec(uint8_t x, uint8_t y, uint8_t data,
struct font *font, uint16_t fg_color,
uint16_t bg_color)
{
uint8_t w = 0;
char buf[3];
int8_t i = 2;
if (data == 0)
return lcd_print_char(x + w, y, '0',
font, fg_color, bg_color);
while (data) {
buf[i] = pgm_read_byte(digits + (data % 10));
i--;
data /= 10;
}
for (i++; i < 3; i++) {
w += lcd_print_char(x + w, y, buf[i], font, fg_color, bg_color);
}
return w;
}
uint8_t lcd_print_fixed(uint8_t x, uint8_t y, int32_t val, uint8_t digits,
struct font *font, uint16_t fg_color,
uint16_t bg_color)
{
int8_t n;
uint8_t w = 0;
int mult;
n = val >> 8;
if (n < 0) {
w += lcd_print_char(x + w, y, '-', font, fg_color, bg_color);
n = -n;
}
val &= 0xff;
switch(digits) {
case 1:
mult = 10;
break;
case 2:
mult = 100;
break;
default:
mult = 1000;
digits = 3;
break;
}
val = (val * mult + 128) / 256;;
if (val == mult)
n++;
w += lcd_print_dec(x, y, n, font, fg_color, bg_color);
w += lcd_print_char(x + w, y, '.', font, fg_color, bg_color);
while (digits--) {
mult /= 10;
w += lcd_print_char(x + w, y, '0' + (val / mult) % 10,
font, fg_color, bg_color);
}
return w;
}
void lcd_init(void)
{
uint8_t d = PORTD;
d &= ~_BV(D_LCD_SCK) & ~_BV(D_LCD_MOSI);
d |= _BV(D_LCD_RESET) | _BV(D_LCD_SEL) | _BV(D_LCD_BL);
PORTD = d;
/* display control */
epson_cmd(DISCTL);
epson_data(0x0c);
epson_data(0x20);
epson_data(0x00);
epson_data(0x01);
/* common scanning direction */
epson_cmd(COMSCN);
epson_data(0x01);
/* internal oscillator on */
epson_cmd(OSCON);
/* wake up */
epson_cmd(SLPOUT);
phillips_cmd(SLEEPOUT);
/* turn everything on */
epson_cmd(PWRCTR);
epson_data(0x0f);
/* booster on */
phillips_cmd(BSTRON);
/* invert display mode */
epson_cmd(DISINV);
phillips_cmd(INVON);
/* data control */
epson_cmd(DATCTL);
epson_data(0x03);
epson_data(0x00);
epson_data(0x02);
/* memory access control */
phillips_cmd(MADCTL);
phillips_data(0xc8);
/* color mode */
phillips_cmd(COLMOD);
phillips_data(0x03);
/* contrast */
epson_cmd(VOLCTR);
epson_data(0x24);
epson_data(0x03);
phillips_cmd(SETCON);
phillips_data(0x3c);
epson_cmd(NOP);
epson_cmd(NOPP);
/* display on */
epson_cmd(DISON);
phillips_cmd(DISPON);
}