-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlib_max7219.c
522 lines (457 loc) · 10.6 KB
/
lib_max7219.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
/*
* File name: lib_max7219.c
* Date first: 02/27/2018
* Date last: 12/13/2022
*
* Description: STM8 Library for MAX7219 LED array.
*
* Author: Richard Hodges
*
* Copyright (C) 2018, 2020 Richard Hodges. All rights reserved.
* Permission is hereby granted for any use.
*
*
******************************************************************************
*
*/
#include "stm8s_header.h"
#include "lib_max7219.h"
#include "lib_max7219.font"
#define PORT_ODR PD_ODR
#define PORT_DDR PD_DDR
#define PORT_CR1 PD_CR1
#define _PORT_ODR _PD_ODR
#define PIN_CLK 1 /* D1 is module CLK */
#define PIN_LOAD 2 /* D2 is module CS/LOAD */
#define PIN_DATA 3 /* D3 is module DIN */
#define SEG_INVALID 0x49 /* "invalid", three horizontal bars */
static char led_type;
static char led_count;
static char led_row; /* line or unit# */
static char led_col; /* digit (7-seg) or dot column, always 0-7 */
static char opt_wrap; /* wrap around to first row? */
static char opt_marquee; /* begin marquee */
static char opt_prerender; /* Render into buffer instead of display */
static void emit_all(int); /* issue command to all units */
static void emit_word(int); /* issue command word and load */
static void emit_part(int); /* not last word */
static void emit_load(void); /* issue load command */
static void send_digit(int); /* send digit/line to display */
static char code_7seg(char); /* get 7-segment code for ASCII character */
static char *code_dots(char); /* get bitmap for ASCII character */
const char segs_digits[];
const char segs_alpha[];
#if (MAX7219_DOT) || (MAX7219_GRAPH)
static void send_grcol(char); /* send graphic column */
static void send_graph(void); /* send 8x8 graphics to unit */
static void zero_cache(void); /* clear the graphics cache */
static char graph_cache[8];
static char graph_pixel;
static char *renderbuf; /* Pre-render buffer */
#endif
/******************************************************************************
*
* Initialize array
* in: type, device count
*/
void m7219_init(char type, char count)
{
PORT_ODR &= 0xf1; /* start pins low */
PORT_DDR |= 0x0e; /* D1, D2, D3 outputs */
PORT_CR1 |= 0x0e; /* enable source output */
led_type = type;
led_count = count;
led_row = 0;
led_col = 0;
opt_wrap = 1;
opt_marquee = 0;
emit_all(0x0900); /* no BCD decode for any digits */
emit_all(0x0b07); /* scan all digits */
m7219_bright(6); /* brightness */
emit_all(0x0c01); /* normal operation */
m7219_clear();
#if (MAX7219_DOT) || (MAX7219_GRAPH)
graph_pixel = 1;
zero_cache();
#endif
}
/******************************************************************************
*
* Set cursor position
* in: line or unit# (0-max), column
*/
void m7219_curs(char line, char col)
{
led_row = line;
led_col = col;
#if (MAX7219_DOT) || (MAX7219_GRAPH)
graph_pixel = 1 << col;
zero_cache();
#endif
}
/******************************************************************************
*
* Write number/string to unit
* in: number/string
*/
void m7219_puts(const char *str)
{
char c;
while (c = *str++) {
if (*str == '.') {
c |= 0x80;
str++;
}
m7219_putc(c);
}
}
/******************************************************************************
*
* Get 7-segment code for ASCII character
*/
static char code_7seg(char c)
{
if (c == ' ')
return 0;
if (c == '-')
return 1;
if (c == '_')
return 8;
if ((c & 0xf0) == 0x30)
return segs_digits[c & 0x0f];
if (c < 'A' ||
c > 'U')
return SEG_INVALID;
return segs_alpha[c - 'A'];
}
/******************************************************************************
*
* Write digit/char to unit
* in: digit or char (bit-7 for decimal point)
*/
void m7219_putc(char c)
{
char pos, segs;
int offset;
char *bm, bw; /* char bitmap and width */
if (led_type == MAX7219_7SEG) {
segs = code_7seg(c & 0x7f);
segs |= (c & 0x80);
pos = 8 - led_col;
send_digit((pos << 8) | segs);
return;
}
#ifdef MAX7219_DOT
if (led_type == MAX7219_DOT) {
c -= 32;
if (c > 95)
c = 95;
offset = c * 5;
bm = font_map + offset;
bw = 5;
while (bw--)
send_grcol(*bm++);
send_grcol(0); /* space between chars */
if (led_col)
send_graph();
return;
}
#endif
#ifdef MAX7219_GRAPH
if (led_type == MAX7219_GRAPH) {
send_grcol(c);
if (led_col)
send_graph();
return;
}
#endif
}
/* 7-Segment unit positions: 8 7 6 5 4 3 2 1
*
* Dot matrix positions for horizontal (row/digit# and bit):
*
* 8: 0 1 2 3 4 5 6 7
* 7: 0 1 2 3 4 5 6 7
* 6: 0 1 2 3 4 5 6 7
* 5: 0 1 2 3 4 5 6 7
* 4: 0 1 2 3 4 5 6 7
* 3: 0 1 2 3 4 5 6 7
* 2: 0 1 2 3 4 5 6 7
* 1: 0 1 2 3 4 5 6 7 <= graph_cache[0]
*
******************************************************************************
*
* Send digit or dot column to display
* in: word with digit# and segs/dots
*/
static void send_digit(int digit)
{
int i;
for (i = led_count; i > 0; i--)
if (i == led_row + 1)
emit_part(digit);
else
emit_part(0);
emit_load();
led_col++;
if (led_col & 8) {
led_col = 0;
led_row++;
if (led_row == led_count)
if (opt_wrap)
led_row = 0;
}
}
#if (MAX7219_DOT) || (MAX7219_GRAPH)
/******************************************************************************
*
* Send graphics column to 8x8 graphics cache
* in 8-bit column
*/
static void send_grcol(char dots)
{
char i, mask;
char *cache;
cache = graph_cache;
mask = ~graph_pixel;
if (!opt_marquee)
for (i = 0; i < 8; i++) {
*cache &= mask;
if (dots & 0x80)
*cache |= graph_pixel;
cache++;
dots <<= 1;
}
graph_pixel <<= 1;
led_col++;
if (led_col & 8) {
if (!opt_marquee)
send_graph();
graph_pixel = 1;
led_col = 0;
led_row++;
zero_cache();
if (led_row == led_count) {
if (opt_wrap | opt_marquee)
led_row = 0;
opt_marquee = 0;
}
}
}
/******************************************************************************
*
* Send 8x8 graphics cache to display
*/
static void send_graph(void)
{
char row, i;
char *cache, *pre;
int digit;
cache = graph_cache;
if (opt_prerender) {
pre = renderbuf + 8 * led_row;
for (row = 1; row <= 8; row++)
*pre++ = *cache++;
return;
}
for (row = 1; row <= 8; row++) {
digit = *cache++;
digit |= row << 8;
for (i = led_count; i > 0; i--)
if (i == led_row + 1)
emit_part(digit);
else
emit_part(0);
emit_load();
}
}
/******************************************************************************
*
* Clear the graphics cache
*/
static void zero_cache(void)
{
__asm
ldw x, #_graph_cache
clr (x)
clr (1, x)
clr (2, x)
clr (3, x)
clr (4, x)
clr (5, x)
clr (6, x)
clr (7, x)
__endasm;
}
#ifdef MAX7219_PRERENDER
/******************************************************************************
*
* Pre-render text into raw pixel buffer.
*
* in: pixel buffer, pixels to skip
*/
void m7219_prerender(char *pixbuf, char skip)
{
renderbuf = pixbuf;
if (skip) /* Skip pixels, then wrap around. */
m7219_curs(led_count - 1, 8 - skip);
else
m7219_curs(0, 0);
opt_prerender = 1;
}
/******************************************************************************
*
* Send pre-rendered pixel buffer to displays.
*
* in: pixel buffer
*/
void m7219_sendbuf(char *pixbuf)
{
unsigned char i, *ptr;
int row, command;
int bufsize;
bufsize = 8 * led_count;
ptr = pixbuf + bufsize;
ptr--; /* Start with last LED module. */
for (row = 0x800; row >= 0x100; row -= 0x100) {
for (i = 0; i < led_count; i++) {
command = row;
command |= *ptr; /* Command is row# and data */
emit_part(command);
ptr -= 8; /* Previous LED module */
}
emit_load();
ptr += bufsize - 1; /* Go to last LED and next row */
}
}
#endif /* MAX7219_PRERENDER */
#endif /* (MAX7219_DOT) || (MAX7219_GRAPH) */
/******************************************************************************
*
* Clear all displays
*/
void m7219_clear(void)
{
m7219_curs(0, 0);
do
m7219_putc(' ');
while (led_col | led_row); /* fixme: when opt_wrap is off */
}
/******************************************************************************
*
* Set LED intensity (and clear test mode)
* in: 0 (minimum) to 15 (maximum)
*/
void m7219_bright(char intensity)
{
emit_all(0x0a00 | intensity);
}
/******************************************************************************
*
* Send command to all controllers
*/
static void emit_all(int w)
{
int i;
for (i = 0; i < led_count; i++)
emit_part(w);
emit_load();
}
/******************************************************************************
*
* Send word to controller(s)
*/
static void emit_word(int w)
{
emit_part(w);
emit_load();
}
/******************************************************************************
*
* Send one word as part of a chain
*/
static void emit_part(int w)
{
w;
__asm
#if __SDCCCALL == 0
ldw x, (3, sp)
#endif
push #16
00001$:
rlcw x
bccm _PORT_ODR, #PIN_DATA
dec (1, sp)
bset _PORT_ODR, #PIN_CLK
bres _PORT_ODR, #PIN_CLK
jrne 00001$
pop a
__endasm;
}
/******************************************************************************
*
* Send load signal
*/
static void emit_load(void)
{
__asm
bset _PORT_ODR, #PIN_LOAD
bres _PORT_ODR, #PIN_LOAD
__endasm;
}
/******************************************************************************
*
* Set LED options
*/
void m7219_option(char opt)
{
switch (opt) {
case MAX7219_WRAP :
opt_wrap = 1;
break;
case MAX7219_NOWRAP :
opt_wrap = 0;
break;
case MAX7219_MARQUEE :
opt_marquee = 1;
break;
}
}
/******************************************************************************
*
* aaa 666
* f b 1 5
* f b 1 5
* ggg 000
* e c 2 4
* e c 2 4
* ddd h 333 7
*/
const char segs_digits[16] = {
0x7e, 0x30, 0x6d, 0x79, 0x33, /* 0 - 4 */
0x5b, 0x5f, 0x70, 0x7f, 0x7b, /* 5 - 9 */
SEG_INVALID, SEG_INVALID, SEG_INVALID, SEG_INVALID, SEG_INVALID, SEG_INVALID
};
const char segs_alpha[21] = {
0x77, /* A */
0x1f, /* B */
0x4e, /* C */
0x3d, /* D */
0x4f, /* E */
0x47, /* F */
SEG_INVALID, /* G */
0x37, /* H */
0x30, /* I shared with 1 */
0x38, /* J */
SEG_INVALID, /* K */
0x0e, /* L */
SEG_INVALID, /* M */
0x15, /* N (small n) */
0x1d, /* O (small o) */
0x67, /* P */
SEG_INVALID, /* Q */
0x05, /* R (small r) */
0x5b, /* S shared with 5 */
0x0f, /* T (small t) */
0x3e /* U */
};