-
Notifications
You must be signed in to change notification settings - Fork 0
/
MD_Max72xx_Test
640 lines (553 loc) · 14.3 KB
/
MD_Max72xx_Test
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// Program to exercise the MD_MAX72XX library
//
// Uses most of the functions in the library
#include <MD_MAX72xx.h>
// Turn on debug statements to the serial output
#define DEBUG 1
#if DEBUG
#define PRINT(s, x) { Serial.print(F(s)); Serial.print(x); }
#define PRINTS(x) Serial.print(F(x))
#define PRINTD(x) Serial.println(x, DEC)
#else
#define PRINT(s, x)
#define PRINTS(x)
#define PRINTD(x)
#endif
// Define the number of devices we have in the chain and the hardware interface
// NOTE: These pin numbers will probably not work with your hardware and may
// need to be adapted
#define MAX_DEVICES 2
#define CLK_PIN 11 // or SCK
#define DATA_PIN 12 // or MOSI
#define CS_PIN 10 // or SS
// SPI hardware interface
//MD_MAX72XX mx = MD_MAX72XX(CS_PIN, MAX_DEVICES);
// Arbitrary pins
MD_MAX72XX mx = MD_MAX72XX(DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
// We always wait a bit between updates of the display
#define DELAYTIME 100 // in milliseconds
void scrollText(char *p)
{
uint8_t charWidth;
uint8_t cBuf[8]; // this should be ok for all built-in fonts
PRINTS("\nScrolling text");
mx.clear();
while (*p != '\0')
{
charWidth = mx.getChar(*p++, sizeof(cBuf)/sizeof(cBuf[0]), cBuf);
for (uint8_t i=0; i<charWidth + 1; i++) // allow space between characters
{
mx.transform(MD_MAX72XX::TSL);
if (i < charWidth)
mx.setColumn(0, cBuf[i]);
delay(DELAYTIME);
}
}
}
void zeroPointSet()
// Demonstrates the use of setPoint and
// show where the zero point is in the display
{
PRINTS("\nZero point highlight");
mx.clear();
if (MAX_DEVICES > 1)
mx.setChar((2*COL_SIZE)-1, '0');
for (uint8_t i=0; i<ROW_SIZE; i++)
{
mx.setPoint(i, i, true);
mx.setPoint(0, i, true);
mx.setPoint(i, 0, true);
delay(DELAYTIME);
}
delay(DELAYTIME*3);
}
void lines()
// Demonstrate the use of drawLine().
// fan out lines from each corner for up to 4 device blocks
{
PRINTS("\nLines");
const uint8_t stepSize = 3;
const uint8_t maxDev = (MAX_DEVICES > 4 ? 4 : MAX_DEVICES);
mx.clear();
for (uint16_t c=0; c<(maxDev*COL_SIZE)-1; c+=stepSize)
{
mx.drawLine(0, 0, ROW_SIZE-1, c, true);
delay(DELAYTIME);
}
mx.clear();
for (uint16_t c=0; c<(maxDev*COL_SIZE)-1; c+=stepSize)
{
mx.drawLine(ROW_SIZE-1, 0, 0, c, true);
delay(DELAYTIME);
}
mx.clear();
for (uint16_t c=0; c<(maxDev*COL_SIZE)-1; c+=stepSize)
{
mx.drawLine(ROW_SIZE-1, (MAX_DEVICES*COL_SIZE)-1, 0, (MAX_DEVICES*COL_SIZE)-1-c, true);
delay(DELAYTIME);
}
mx.clear();
for (uint16_t c=0; c<(maxDev*COL_SIZE)-1; c+=stepSize)
{
mx.drawLine(0, (MAX_DEVICES*COL_SIZE)-1, ROW_SIZE-1, (MAX_DEVICES*COL_SIZE)-1-c, true);
delay(DELAYTIME);
}
}
void rows()
// Demonstrates the use of setRow()
{
PRINTS("\nRows 0->7");
mx.clear();
for (uint8_t row=0; row<ROW_SIZE; row++)
{
mx.setRow(row, 0xff);
delay(DELAYTIME);
mx.setRow(row, 0x00);
}
}
void columns()
// Demonstrates the use of setColumn()
{
PRINTS("\nCols 0->max");
mx.clear();
for (uint8_t col=0; col<mx.getColumnCount(); col++)
{
mx.setColumn(col, 0xff);
delay(DELAYTIME/MAX_DEVICES);
mx.setColumn(col, 0x00);
}
}
void cross()
// Combination of setRow() and setColumn() with user controlled
// display updates to ensure concurrent changes.
{
PRINTS("\nMoving cross");
mx.clear();
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::OFF);
// diagonally down the display R to L
for (uint8_t i=0; i<ROW_SIZE; i++)
{
for (uint8_t j=0; j<MAX_DEVICES; j++)
{
mx.setColumn(j, i, 0xff);
mx.setRow(j, i, 0xff);
}
mx.update();
delay(3*DELAYTIME);
for (uint8_t j=0; j<MAX_DEVICES; j++)
{
mx.setColumn(j, i, 0x00);
mx.setRow(j, i, 0x00);
}
}
// moving up the display on the R
for (int8_t i=ROW_SIZE-1; i>=0; i--)
{
for (uint8_t j=0; j<MAX_DEVICES; j++)
{
mx.setColumn(j, i, 0xff);
mx.setRow(j, ROW_SIZE-1, 0xff);
}
mx.update();
delay(3*DELAYTIME);
for (uint8_t j=0; j<MAX_DEVICES; j++)
{
mx.setColumn(j, i, 0x00);
mx.setRow(j, ROW_SIZE-1, 0x00);
}
}
// diagonally up the display L to R
for (uint8_t i=0; i<ROW_SIZE; i++)
{
for (uint8_t j=0; j<MAX_DEVICES; j++)
{
mx.setColumn(j, i, 0xff);
mx.setRow(j, ROW_SIZE-1-i, 0xff);
}
mx.update();
delay(3*DELAYTIME);
for (uint8_t j=0; j<MAX_DEVICES; j++)
{
mx.setColumn(j, i, 0x00);
mx.setRow(j, ROW_SIZE-1-i, 0x00);
}
}
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
}
void bullseye()
// Demonstrate the use of buffer based repeated patterns
// across all devices.
{
PRINTS("\nBullseye");
mx.clear();
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::OFF);
for (uint8_t n=0; n<3; n++)
{
byte b = 0xff;
int i = 0;
while (b != 0x00)
{
for (uint8_t j=0; j<MAX_DEVICES+1; j++)
{
mx.setRow(j, i, b);
mx.setColumn(j, i, b);
mx.setRow(j, ROW_SIZE-1-i, b);
mx.setColumn(j, COL_SIZE-1-i, b);
}
mx.update();
delay(3*DELAYTIME);
for (uint8_t j=0; j<MAX_DEVICES+1; j++)
{
mx.setRow(j, i, 0);
mx.setColumn(j, i, 0);
mx.setRow(j, ROW_SIZE-1-i, 0);
mx.setColumn(j, COL_SIZE-1-i, 0);
}
bitClear(b, i);
bitClear(b, 7-i);
i++;
}
while (b != 0xff)
{
for (uint8_t j=0; j<MAX_DEVICES+1; j++)
{
mx.setRow(j, i, b);
mx.setColumn(j, i, b);
mx.setRow(j, ROW_SIZE-1-i, b);
mx.setColumn(j, COL_SIZE-1-i, b);
}
mx.update();
delay(3*DELAYTIME);
for (uint8_t j=0; j<MAX_DEVICES+1; j++)
{
mx.setRow(j, i, 0);
mx.setColumn(j, i, 0);
mx.setRow(j, ROW_SIZE-1-i, 0);
mx.setColumn(j, COL_SIZE-1-i, 0);
}
i--;
bitSet(b, i);
bitSet(b, 7-i);
}
}
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
}
void stripe()
// Demonstrates animation of a diagonal stripe moving across the display
// with points plotted outside the display region ignored.
{
const uint16_t maxCol = MAX_DEVICES*ROW_SIZE;
const uint8_t stripeWidth = 10;
PRINTS("\nEach individually by row then col");
mx.clear();
for (uint16_t col=0; col<maxCol + ROW_SIZE + stripeWidth; col++)
{
for (uint8_t row=0; row < ROW_SIZE; row++)
{
mx.setPoint(row, col-row, true);
mx.setPoint(row, col-row - stripeWidth, false);
}
delay(DELAYTIME);
}
}
void spiral()
// setPoint() used to draw a spiral across the whole display
{
PRINTS("\nSpiral in");
int rmin = 0, rmax = ROW_SIZE-1;
int cmin = 0, cmax = (COL_SIZE*MAX_DEVICES)-1;
mx.clear();
while ((rmax > rmin) && (cmax > cmin))
{
// do row
for (int i=cmin; i<=cmax; i++)
{
mx.setPoint(rmin, i, true);
delay(DELAYTIME/MAX_DEVICES);
}
rmin++;
// do column
for (uint8_t i=rmin; i<=rmax; i++)
{
mx.setPoint(i, cmax, true);
delay(DELAYTIME/MAX_DEVICES);
}
cmax--;
// do row
for (int i=cmax; i>=cmin; i--)
{
mx.setPoint(rmax, i, true);
delay(DELAYTIME/MAX_DEVICES);
}
rmax--;
// do column
for (uint8_t i=rmax; i>=rmin; i--)
{
mx.setPoint(i, cmin, true);
delay(DELAYTIME/MAX_DEVICES);
}
cmin++;
}
}
void bounce()
// Animation of a bouncing ball
{
const int minC = 0;
const int maxC = mx.getColumnCount()-1;
const int minR = 0;
const int maxR = ROW_SIZE-1;
int nCounter = 0;
int r = 0, c = 2;
int8_t dR = 1, dC = 1; // delta row and column
PRINTS("\nBouncing ball");
mx.clear();
while (nCounter++ < 200)
{
mx.setPoint(r, c, false);
r += dR;
c += dC;
mx.setPoint(r, c, true);
delay(DELAYTIME/2);
if ((r == minR) || (r == maxR))
dR = -dR;
if ((c == minC) || (c == maxC))
dC = -dC;
}
}
void intensity()
// Demonstrates the control of display intensity (brightness) across
// the full range.
{
uint8_t row;
PRINTS("\nVary intensity ");
mx.clear();
// Grow and get brighter
row = 0;
for (int8_t i=0; i<=MAX_INTENSITY; i++)
{
mx.control(MD_MAX72XX::INTENSITY, i);
if (i%2 == 0)
mx.setRow(row++, 0xff);
delay(DELAYTIME*3);
}
mx.control(MD_MAX72XX::INTENSITY, 8);
}
void blinking()
// Uses the test function of the MAX72xx to blink the display on and off.
{
int nDelay = 1000;
PRINTS("\nBlinking");
mx.clear();
while (nDelay > 0)
{
mx.control(MD_MAX72XX::TEST, MD_MAX72XX::ON);
delay(nDelay);
mx.control(MD_MAX72XX::TEST, MD_MAX72XX::OFF);
delay(nDelay);
nDelay -= DELAYTIME;
}
}
void scanLimit(void)
// Uses scan limit function to restrict the number of rows displayed.
{
PRINTS("\nScan Limit");
mx.clear();
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::OFF);
for (uint8_t row=0; row<ROW_SIZE; row++)
mx.setRow(row, 0xff);
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
for (int8_t s=MAX_SCANLIMIT; s>=0; s--)
{
mx.control(MD_MAX72XX::SCANLIMIT, s);
delay(DELAYTIME*5);
}
mx.control(MD_MAX72XX::SCANLIMIT, MAX_SCANLIMIT);
}
void transformation1()
// Demonstrates the use of transform() to move bitmaps on the display
// In this case a user defined bitmap is created and animated.
{
uint8_t arrow[COL_SIZE] =
{
0b00001000,
0b00011100,
0b00111110,
0b01111111,
0b00011100,
0b00011100,
0b00111110,
0b00000000
};
MD_MAX72XX::transformType_t t[] =
{
MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL,
MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL,
MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL,
MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL,
MD_MAX72XX::TFLR,
MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR,
MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR,
MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR,
MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR,
MD_MAX72XX::TRC,
MD_MAX72XX::TSD, MD_MAX72XX::TSD, MD_MAX72XX::TSD, MD_MAX72XX::TSD,
MD_MAX72XX::TSD, MD_MAX72XX::TSD, MD_MAX72XX::TSD, MD_MAX72XX::TSD,
MD_MAX72XX::TFUD,
MD_MAX72XX::TSU, MD_MAX72XX::TSU, MD_MAX72XX::TSU, MD_MAX72XX::TSU,
MD_MAX72XX::TSU, MD_MAX72XX::TSU, MD_MAX72XX::TSU, MD_MAX72XX::TSU,
MD_MAX72XX::TINV,
MD_MAX72XX::TRC, MD_MAX72XX::TRC, MD_MAX72XX::TRC, MD_MAX72XX::TRC,
MD_MAX72XX::TINV
};
PRINTS("\nTransformation1");
mx.clear();
// use the arrow bitmap
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::OFF);
for (uint8_t j=0; j<mx.getDeviceCount(); j++)
mx.setBuffer(((j+1)*COL_SIZE)-1, COL_SIZE, arrow);
mx.control(MD_MAX72XX::UPDATE, MD_MAX72XX::ON);
delay(DELAYTIME);
// run through the transformations
mx.control(MD_MAX72XX::WRAPAROUND, MD_MAX72XX::ON);
for (uint8_t i=0; i<(sizeof(t)/sizeof(t[0])); i++)
{
mx.transform(t[i]);
delay(DELAYTIME*4);
}
mx.control(MD_MAX72XX::WRAPAROUND, MD_MAX72XX::OFF);
}
void transformation2()
// Demonstrates the use of transform() to move bitmaps on the display
// In this case font characters are loaded into the display for animation.
{
MD_MAX72XX::transformType_t t[] =
{
MD_MAX72XX::TINV,
MD_MAX72XX::TRC, MD_MAX72XX::TRC, MD_MAX72XX::TRC, MD_MAX72XX::TRC,
MD_MAX72XX::TINV,
MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL,
MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR,
MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR,
MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL, MD_MAX72XX::TSL,
MD_MAX72XX::TSR, MD_MAX72XX::TSR, MD_MAX72XX::TSR,
MD_MAX72XX::TSD, MD_MAX72XX::TSU, MD_MAX72XX::TSD, MD_MAX72XX::TSU,
MD_MAX72XX::TFLR, MD_MAX72XX::TFLR, MD_MAX72XX::TFUD, MD_MAX72XX::TFUD
};
PRINTS("\nTransformation2");
mx.clear();
mx.control(MD_MAX72XX::WRAPAROUND, MD_MAX72XX::OFF);
// draw something that will show changes
for (uint8_t j=0; j<mx.getDeviceCount(); j++)
{
mx.setChar(((j+1)*COL_SIZE)-1, '0'+j);
}
delay(DELAYTIME*5);
// run thru transformations
for (uint8_t i=0; i<(sizeof(t)/sizeof(t[0])); i++)
{
mx.transform(t[i]);
delay(DELAYTIME*3);
}
}
void wrapText()
// Display text and animate scrolling using auto wraparound of the buffer
{
PRINTS("\nwrapText");
mx.clear();
mx.wraparound(MD_MAX72XX::ON);
// draw something that will show changes
for (uint16_t j=0; j<mx.getDeviceCount(); j++)
{
mx.setChar(((j+1)*COL_SIZE)-1, (j&1 ? 'M' : 'W'));
}
delay(DELAYTIME*5);
// run thru transformations
for (uint16_t i=0; i<3*COL_SIZE*MAX_DEVICES; i++)
{
mx.transform(MD_MAX72XX::TSL);
delay(DELAYTIME/2);
}
for (uint16_t i=0; i<3*COL_SIZE*MAX_DEVICES; i++)
{
mx.transform(MD_MAX72XX::TSR);
delay(DELAYTIME/2);
}
for (uint8_t i=0; i<ROW_SIZE; i++)
{
mx.transform(MD_MAX72XX::TSU);
delay(DELAYTIME*2);
}
for (uint8_t i=0; i<ROW_SIZE; i++)
{
mx.transform(MD_MAX72XX::TSD);
delay(DELAYTIME*2);
}
mx.wraparound(MD_MAX72XX::OFF);
}
void showCharset(void)
// Run through display of the the entire font characters set
{
mx.clear();
mx.update(MD_MAX72XX::OFF);
for (uint16_t i=0; i<256; i++)
{
mx.clear(0);
mx.setChar(COL_SIZE-1, i);
if (MAX_DEVICES >= 3)
{
char hex[3];
sprintf(hex, "%02X", i);
mx.clear(1);
mx.setChar((2*COL_SIZE)-1,hex[1]);
mx.clear(2);
mx.setChar((3*COL_SIZE)-1,hex[0]);
}
mx.update();
delay(DELAYTIME*2);
}
mx.update(MD_MAX72XX::ON);
}
void setup()
{
mx.begin();
#if DEBUG || ENABLE_FONT_ADJUST
Serial.begin(57600);
delay(1000);
#if ENABLE_FONT_ADJUST
// mx.adjustFont();
#endif // ENABLE_FONT_ADJUST
#endif
PRINTS("\n[MD_MAX72XX Test & Demo]");
// scrollText("MD_MAX72xx Test ");
}
void loop()
{
#if 1
scrollText("Graphics ");
zeroPointSet();
lines();
rows();
columns();
cross();
stripe();
bullseye();
bounce();
spiral();
#endif
#if 1
scrollText("Control ");
intensity();
scanLimit();
blinking();
#endif
#if 1
scrollText("Transform ");
transformation1();
transformation2();
#endif
#if 1
scrollText("Charset ");
wrapText();
showCharset();
#endif
}