forked from freetronics/DMD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DMD.cpp
548 lines (489 loc) · 19.2 KB
/
DMD.cpp
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
/*--------------------------------------------------------------------------------------
DMD.cpp - Function and support library for the Freetronics DMD, a 512 LED matrix display
panel arranged in a 32 x 16 layout.
Copyright (C) 2011 Marc Alexander (info <at> freetronics <dot> com)
Note that the DMD library uses the SPI port for the fastest, low overhead writing to the
display. Keep an eye on conflicts if there are any other devices running from the same
SPI port, and that the chip select on those devices is correctly set to be inactive
when the DMD is being written to.
---
This program is free software: you can redistribute it and/or modify it under the terms
of the version 3 GNU General Public License as published by the Free Software Foundation.
This program 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/>.
--------------------------------------------------------------------------------------*/
#include "DMD.h"
/*--------------------------------------------------------------------------------------
Setup and instantiation of DMD library
Note this currently uses the SPI port for the fastest performance to the DMD, be
careful of possible conflicts with other SPI port devices
--------------------------------------------------------------------------------------*/
DMD::DMD(byte panelsWide, byte panelsHigh)
{
uint16_t ui;
DisplaysWide=panelsWide;
DisplaysHigh=panelsHigh;
DisplaysTotal=DisplaysWide*DisplaysHigh;
row1 = DisplaysTotal<<4;
row2 = DisplaysTotal<<5;
row3 = ((DisplaysTotal<<2)*3)<<2;
bDMDScreenRAM = (byte *) malloc(DisplaysTotal*DMD_RAM_SIZE_BYTES);
// initialize the SPI port
SPI.begin(); // probably don't need this since it inits the port pins only, which we do just below with the appropriate DMD interface setup
SPI.setBitOrder(MSBFIRST); //
SPI.setDataMode(SPI_MODE0); // CPOL=0, CPHA=0
SPI.setClockDivider(SPI_CLOCK_DIV4); // system clock / 4 = 4MHz SPI CLK to shift registers. If using a short cable, can put SPI_CLOCK_DIV2 here for 2x faster updates
digitalWrite(PIN_DMD_A, LOW); //
digitalWrite(PIN_DMD_B, LOW); //
digitalWrite(PIN_DMD_CLK, LOW); //
digitalWrite(PIN_DMD_SCLK, LOW); //
digitalWrite(PIN_DMD_R_DATA, HIGH); //
digitalWrite(PIN_DMD_nOE, LOW); //
pinMode(PIN_DMD_A, OUTPUT); //
pinMode(PIN_DMD_B, OUTPUT); //
pinMode(PIN_DMD_CLK, OUTPUT); //
pinMode(PIN_DMD_SCLK, OUTPUT); //
pinMode(PIN_DMD_R_DATA, OUTPUT); //
pinMode(PIN_DMD_nOE, OUTPUT); //
clearScreen(true);
// init the scan line/ram pointer to the required start point
bDMDByte = 0;
}
//DMD::~DMD()
//{
// // nothing needed here
//}
/*--------------------------------------------------------------------------------------
Set or clear a pixel at the x and y location (0,0 is the top left corner)
--------------------------------------------------------------------------------------*/
void
DMD::writePixel(unsigned int bX, unsigned int bY, byte bGraphicsMode, byte bPixel)
{
unsigned int uiDMDRAMPointer;
if (bX >= (DMD_PIXELS_ACROSS*DisplaysWide) || bY >= (DMD_PIXELS_DOWN * DisplaysHigh)) {
return;
}
byte panel=(bX/DMD_PIXELS_ACROSS) + (DisplaysWide*(bY/DMD_PIXELS_DOWN));
bX=(bX % DMD_PIXELS_ACROSS) + (panel<<5);
bY=bY % DMD_PIXELS_DOWN;
//set pointer to DMD RAM byte to be modified
uiDMDRAMPointer = bX/8 + bY*(DisplaysTotal<<2);
byte lookup = bPixelLookupTable[bX & 0x07];
switch (bGraphicsMode) {
case GRAPHICS_NORMAL:
if (bPixel == true)
bDMDScreenRAM[uiDMDRAMPointer] &= ~lookup; // zero bit is pixel on
else
bDMDScreenRAM[uiDMDRAMPointer] |= lookup; // one bit is pixel off
break;
case GRAPHICS_INVERSE:
if (bPixel == false)
bDMDScreenRAM[uiDMDRAMPointer] &= ~lookup; // zero bit is pixel on
else
bDMDScreenRAM[uiDMDRAMPointer] |= lookup; // one bit is pixel off
break;
case GRAPHICS_TOGGLE:
if (bPixel == true) {
if ((bDMDScreenRAM[uiDMDRAMPointer] & lookup) == 0)
bDMDScreenRAM[uiDMDRAMPointer] |= lookup; // one bit is pixel off
else
bDMDScreenRAM[uiDMDRAMPointer] &= ~lookup; // one bit is pixel off
}
break;
case GRAPHICS_OR:
//only set pixels on
if (bPixel == true)
bDMDScreenRAM[uiDMDRAMPointer] &= ~lookup; // zero bit is pixel on
break;
case GRAPHICS_NOR:
//only clear on pixels
if ((bPixel == true) &&
((bDMDScreenRAM[uiDMDRAMPointer] & lookup) == 0))
bDMDScreenRAM[uiDMDRAMPointer] |= lookup; // one bit is pixel off
break;
}
}
void DMD::drawString(int bX, int bY, const char *bChars, byte length,
byte bGraphicsMode)
{
if (bX >= (DMD_PIXELS_ACROSS*DisplaysWide) || bY >= DMD_PIXELS_DOWN * DisplaysHigh)
return;
uint8_t height = pgm_read_byte(this->Font + FONT_HEIGHT);
if (bY+height<0) return;
int strWidth = 0;
this->drawLine(bX -1 , bY, bX -1 , bY + height, GRAPHICS_INVERSE);
for (int i = 0; i < length; i++) {
int charWide = this->drawChar(bX+strWidth, bY, bChars[i], bGraphicsMode);
if (charWide > 0) {
strWidth += charWide ;
this->drawLine(bX + strWidth , bY, bX + strWidth , bY + height, GRAPHICS_INVERSE);
strWidth++;
} else if (charWide < 0) {
return;
}
if ((bX + strWidth) >= DMD_PIXELS_ACROSS * DisplaysWide || bY >= DMD_PIXELS_DOWN * DisplaysHigh) return;
}
}
void DMD::drawMarquee(const char *bChars, byte length, int left, int top)
{
marqueeWidth = 0;
for (int i = 0; i < length; i++) {
marqueeText[i] = bChars[i];
marqueeWidth += charWidth(bChars[i]) + 1;
}
marqueeHeight=pgm_read_byte(this->Font + FONT_HEIGHT);
marqueeText[length] = '\0';
marqueeOffsetY = top;
marqueeOffsetX = left;
marqueeLength = length;
drawString(marqueeOffsetX, marqueeOffsetY, marqueeText, marqueeLength,
GRAPHICS_NORMAL);
}
boolean DMD::stepMarquee(int amountX, int amountY)
{
boolean ret=false;
marqueeOffsetX += amountX;
marqueeOffsetY += amountY;
if (marqueeOffsetX < -marqueeWidth) {
marqueeOffsetX = DMD_PIXELS_ACROSS * DisplaysWide;
clearScreen(true);
ret=true;
} else if (marqueeOffsetX > DMD_PIXELS_ACROSS * DisplaysWide) {
marqueeOffsetX = -marqueeWidth;
clearScreen(true);
ret=true;
}
if (marqueeOffsetY < -marqueeHeight) {
marqueeOffsetY = DMD_PIXELS_DOWN * DisplaysHigh;
clearScreen(true);
ret=true;
} else if (marqueeOffsetY > DMD_PIXELS_DOWN * DisplaysHigh) {
marqueeOffsetY = -marqueeHeight;
clearScreen(true);
ret=true;
}
// Special case horizontal scrolling to improve speed
if (amountY==0 && amountX==-1) {
// Shift entire screen one bit
for (int i=0; i<DMD_RAM_SIZE_BYTES*DisplaysTotal;i++) {
if ((i%(DisplaysWide*4)) == (DisplaysWide*4) -1) {
bDMDScreenRAM[i]=(bDMDScreenRAM[i]<<1)+1;
} else {
bDMDScreenRAM[i]=(bDMDScreenRAM[i]<<1) + ((bDMDScreenRAM[i+1] & 0x80) >>7);
}
}
// Redraw last char on screen
int strWidth=marqueeOffsetX;
for (byte i=0; i < marqueeLength; i++) {
int wide = charWidth(marqueeText[i]);
if (strWidth+wide >= DisplaysWide*DMD_PIXELS_ACROSS) {
drawChar(strWidth, marqueeOffsetY,marqueeText[i],GRAPHICS_NORMAL);
return ret;
}
strWidth += wide+1;
}
} else if (amountY==0 && amountX==1) {
// Shift entire screen one bit
for (int i=(DMD_RAM_SIZE_BYTES*DisplaysTotal)-1; i>=0;i--) {
if ((i%(DisplaysWide*4)) == 0) {
bDMDScreenRAM[i]=(bDMDScreenRAM[i]>>1)+128;
} else {
bDMDScreenRAM[i]=(bDMDScreenRAM[i]>>1) + ((bDMDScreenRAM[i-1] & 1) <<7);
}
}
// Redraw last char on screen
int strWidth=marqueeOffsetX;
for (byte i=0; i < marqueeLength; i++) {
int wide = charWidth(marqueeText[i]);
if (strWidth+wide >= 0) {
drawChar(strWidth, marqueeOffsetY,marqueeText[i],GRAPHICS_NORMAL);
return ret;
}
strWidth += wide+1;
}
} else {
drawString(marqueeOffsetX, marqueeOffsetY, marqueeText, marqueeLength,
GRAPHICS_NORMAL);
}
return ret;
}
/*--------------------------------------------------------------------------------------
Clear the screen in DMD RAM
--------------------------------------------------------------------------------------*/
void DMD::clearScreen(byte bNormal)
{
if (bNormal) // clear all pixels
memset(bDMDScreenRAM,0xFF,DMD_RAM_SIZE_BYTES*DisplaysTotal);
else // set all pixels
memset(bDMDScreenRAM,0x00,DMD_RAM_SIZE_BYTES*DisplaysTotal);
}
/*--------------------------------------------------------------------------------------
Draw or clear a line from x1,y1 to x2,y2
--------------------------------------------------------------------------------------*/
void DMD::drawLine(int x1, int y1, int x2, int y2, byte bGraphicsMode)
{
int dy = y2 - y1;
int dx = x2 - x1;
int stepx, stepy;
if (dy < 0) {
dy = -dy;
stepy = -1;
} else {
stepy = 1;
}
if (dx < 0) {
dx = -dx;
stepx = -1;
} else {
stepx = 1;
}
dy <<= 1; // dy is now 2*dy
dx <<= 1; // dx is now 2*dx
writePixel(x1, y1, bGraphicsMode, true);
if (dx > dy) {
int fraction = dy - (dx >> 1); // same as 2*dy - dx
while (x1 != x2) {
if (fraction >= 0) {
y1 += stepy;
fraction -= dx; // same as fraction -= 2*dx
}
x1 += stepx;
fraction += dy; // same as fraction -= 2*dy
writePixel(x1, y1, bGraphicsMode, true);
}
} else {
int fraction = dx - (dy >> 1);
while (y1 != y2) {
if (fraction >= 0) {
x1 += stepx;
fraction -= dy;
}
y1 += stepy;
fraction += dx;
writePixel(x1, y1, bGraphicsMode, true);
}
}
}
/*--------------------------------------------------------------------------------------
Draw or clear a circle of radius r at x,y centre
--------------------------------------------------------------------------------------*/
void DMD::drawCircle(int xCenter, int yCenter, int radius,
byte bGraphicsMode)
{
int x = 0;
int y = radius;
int p = (5 - radius * 4) / 4;
drawCircleSub(xCenter, yCenter, x, y, bGraphicsMode);
while (x < y) {
x++;
if (p < 0) {
p += 2 * x + 1;
} else {
y--;
p += 2 * (x - y) + 1;
}
drawCircleSub(xCenter, yCenter, x, y, bGraphicsMode);
}
}
void DMD::drawCircleSub(int cx, int cy, int x, int y, byte bGraphicsMode)
{
if (x == 0) {
writePixel(cx, cy + y, bGraphicsMode, true);
writePixel(cx, cy - y, bGraphicsMode, true);
writePixel(cx + y, cy, bGraphicsMode, true);
writePixel(cx - y, cy, bGraphicsMode, true);
} else if (x == y) {
writePixel(cx + x, cy + y, bGraphicsMode, true);
writePixel(cx - x, cy + y, bGraphicsMode, true);
writePixel(cx + x, cy - y, bGraphicsMode, true);
writePixel(cx - x, cy - y, bGraphicsMode, true);
} else if (x < y) {
writePixel(cx + x, cy + y, bGraphicsMode, true);
writePixel(cx - x, cy + y, bGraphicsMode, true);
writePixel(cx + x, cy - y, bGraphicsMode, true);
writePixel(cx - x, cy - y, bGraphicsMode, true);
writePixel(cx + y, cy + x, bGraphicsMode, true);
writePixel(cx - y, cy + x, bGraphicsMode, true);
writePixel(cx + y, cy - x, bGraphicsMode, true);
writePixel(cx - y, cy - x, bGraphicsMode, true);
}
}
/*--------------------------------------------------------------------------------------
Draw or clear a box(rectangle) with a single pixel border
--------------------------------------------------------------------------------------*/
void DMD::drawBox(int x1, int y1, int x2, int y2, byte bGraphicsMode)
{
drawLine(x1, y1, x2, y1, bGraphicsMode);
drawLine(x2, y1, x2, y2, bGraphicsMode);
drawLine(x2, y2, x1, y2, bGraphicsMode);
drawLine(x1, y2, x1, y1, bGraphicsMode);
}
/*--------------------------------------------------------------------------------------
Draw or clear a filled box(rectangle) with a single pixel border
--------------------------------------------------------------------------------------*/
void DMD::drawFilledBox(int x1, int y1, int x2, int y2,
byte bGraphicsMode)
{
for (int b = x1; b <= x2; b++) {
drawLine(b, y1, b, y2, bGraphicsMode);
}
}
/*--------------------------------------------------------------------------------------
Draw the selected test pattern
--------------------------------------------------------------------------------------*/
void DMD::drawTestPattern(byte bPattern)
{
unsigned int ui;
int numPixels=DisplaysTotal * DMD_PIXELS_ACROSS * DMD_PIXELS_DOWN;
int pixelsWide=DMD_PIXELS_ACROSS*DisplaysWide;
for (ui = 0; ui < numPixels; ui++) {
switch (bPattern) {
case PATTERN_ALT_0: // every alternate pixel, first pixel on
if ((ui & pixelsWide) == 0)
//even row
writePixel((ui & (pixelsWide-1)), ((ui & ~(pixelsWide-1)) / pixelsWide), GRAPHICS_NORMAL, ui & 1);
else
//odd row
writePixel((ui & (pixelsWide-1)), ((ui & ~(pixelsWide-1)) / pixelsWide), GRAPHICS_NORMAL, !(ui & 1));
break;
case PATTERN_ALT_1: // every alternate pixel, first pixel off
if ((ui & pixelsWide) == 0)
//even row
writePixel((ui & (pixelsWide-1)), ((ui & ~(pixelsWide-1)) / pixelsWide), GRAPHICS_NORMAL, !(ui & 1));
else
//odd row
writePixel((ui & (pixelsWide-1)), ((ui & ~(pixelsWide-1)) / pixelsWide), GRAPHICS_NORMAL, ui & 1);
break;
case PATTERN_STRIPE_0: // vertical stripes, first stripe on
writePixel((ui & (pixelsWide-1)), ((ui & ~(pixelsWide-1)) / pixelsWide), GRAPHICS_NORMAL, ui & 1);
break;
case PATTERN_STRIPE_1: // vertical stripes, first stripe off
writePixel((ui & (pixelsWide-1)), ((ui & ~(pixelsWide-1)) / pixelsWide), GRAPHICS_NORMAL, !(ui & 1));
break;
}
}
}
/*--------------------------------------------------------------------------------------
Scan the dot matrix LED panel display, from the RAM mirror out to the display hardware.
Call 4 times to scan the whole display which is made up of 4 interleaved rows within the 16 total rows.
Insert the calls to this function into the main loop for the highest call rate, or from a timer interrupt
--------------------------------------------------------------------------------------*/
void DMD::scanDisplayBySPI()
{
//if PIN_OTHER_SPI_nCS is in use during a DMD scan request then scanDisplayBySPI() will exit without conflict! (and skip that scan)
if( digitalRead( PIN_OTHER_SPI_nCS ) == HIGH )
{
//SPI transfer pixels to the display hardware shift registers
int rowsize=DisplaysTotal<<2;
int offset=rowsize * bDMDByte;
for (int i=0;i<rowsize;i++) {
SPI.transfer(bDMDScreenRAM[offset+i+row3]);
SPI.transfer(bDMDScreenRAM[offset+i+row2]);
SPI.transfer(bDMDScreenRAM[offset+i+row1]);
SPI.transfer(bDMDScreenRAM[offset+i]);
}
OE_DMD_ROWS_OFF();
LATCH_DMD_SHIFT_REG_TO_OUTPUT();
switch (bDMDByte) {
case 0: // row 1, 5, 9, 13 were clocked out
LIGHT_DMD_ROW_01_05_09_13();
bDMDByte=1;
break;
case 1: // row 2, 6, 10, 14 were clocked out
LIGHT_DMD_ROW_02_06_10_14();
bDMDByte=2;
break;
case 2: // row 3, 7, 11, 15 were clocked out
LIGHT_DMD_ROW_03_07_11_15();
bDMDByte=3;
break;
case 3: // row 4, 8, 12, 16 were clocked out
LIGHT_DMD_ROW_04_08_12_16();
bDMDByte=0;
break;
}
OE_DMD_ROWS_ON();
}
}
void DMD::selectFont(const uint8_t * font)
{
this->Font = font;
}
int DMD::drawChar(const int bX, const int bY, const unsigned char letter, byte bGraphicsMode)
{
if (bX > (DMD_PIXELS_ACROSS*DisplaysWide) || bY > (DMD_PIXELS_DOWN*DisplaysHigh) ) return -1;
unsigned char c = letter;
uint8_t height = pgm_read_byte(this->Font + FONT_HEIGHT);
if (c == ' ') {
int charWide = charWidth(' ');
this->drawFilledBox(bX, bY, bX + charWide, bY + height, GRAPHICS_INVERSE);
return charWide;
}
uint8_t width = 0;
uint8_t bytes = (height + 7) / 8;
uint8_t firstChar = pgm_read_byte(this->Font + FONT_FIRST_CHAR);
uint8_t charCount = pgm_read_byte(this->Font + FONT_CHAR_COUNT);
uint16_t index = 0;
if (c < firstChar || c >= (firstChar + charCount)) return 0;
c -= firstChar;
if (pgm_read_byte(this->Font + FONT_LENGTH) == 0
&& pgm_read_byte(this->Font + FONT_LENGTH + 1) == 0) {
// zero length is flag indicating fixed width font (array does not contain width data entries)
width = pgm_read_byte(this->Font + FONT_FIXED_WIDTH);
index = c * bytes * width + FONT_WIDTH_TABLE;
} else {
// variable width font, read width data, to get the index
for (uint8_t i = 0; i < c; i++) {
index += pgm_read_byte(this->Font + FONT_WIDTH_TABLE + i);
}
index = index * bytes + charCount + FONT_WIDTH_TABLE;
width = pgm_read_byte(this->Font + FONT_WIDTH_TABLE + c);
}
if (bX < -width || bY < -height) return width;
// last but not least, draw the character
for (uint8_t j = 0; j < width; j++) { // Width
for (uint8_t i = bytes - 1; i < 254; i--) { // Vertical Bytes
uint8_t data = pgm_read_byte(this->Font + index + j + (i * width));
int offset = (i * 8);
if ((i == bytes - 1) && bytes > 1) {
offset = height - 8;
}
for (uint8_t k = 0; k < 8; k++) { // Vertical bits
if ((offset+k >= i*8) && (offset+k <= height)) {
if (data & (1 << k)) {
writePixel(bX + j, bY + offset + k, bGraphicsMode, true);
} else {
writePixel(bX + j, bY + offset + k, bGraphicsMode, false);
}
}
}
}
}
return width;
}
int DMD::charWidth(const unsigned char letter)
{
unsigned char c = letter;
// Space is often not included in font so use width of 'n'
if (c == ' ') c = 'n';
uint8_t width = 0;
uint8_t firstChar = pgm_read_byte(this->Font + FONT_FIRST_CHAR);
uint8_t charCount = pgm_read_byte(this->Font + FONT_CHAR_COUNT);
uint16_t index = 0;
if (c < firstChar || c >= (firstChar + charCount)) {
return 0;
}
c -= firstChar;
if (pgm_read_byte(this->Font + FONT_LENGTH) == 0
&& pgm_read_byte(this->Font + FONT_LENGTH + 1) == 0) {
// zero length is flag indicating fixed width font (array does not contain width data entries)
width = pgm_read_byte(this->Font + FONT_FIXED_WIDTH);
} else {
// variable width font, read width data
width = pgm_read_byte(this->Font + FONT_WIDTH_TABLE + c);
}
return width;
}