-
Notifications
You must be signed in to change notification settings - Fork 19
/
neomatrix_config.h
840 lines (730 loc) · 31.4 KB
/
neomatrix_config.h
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
#ifndef neomatrix_config_h
#define neomatrix_config_h
/* There are 2 major backends
1) SmartMatrix (via SmartMatrix::GFX)
2) Not SmartMatrix (via FastLED::NeoMatrix or FastLED_SPITFT::GFX)
All backends end up using the same Framebuffer::GFX as the base class
but SmartMatrix is sufficiently different to need its own exceptions and handling
(for one, with SmartMatrix, the buffer is actually handled by SmartMatrix whereas
the other libraries define their own FastLED CRGB buffer (RGB888) ).
Backends you should choose from (define 1):
- SMARTMATRIX
- SSD1331 (96x64 TFT)
- ST7735_128b128
- ST7735_128b160
- ILI9341
- Everything below is NeoMatrix in different patterns:
M32B8X3 M16BY16T4 M64BY64 are 3 examples of NEOMATRIX defines
(3 tiled 32x8, 4 tiled 16x16, and a single zigzag 64x64 array)
- FASTLED_SDL is auto defined if you use https://github.com/MarcFork/FastLEDonPc
- ARDUINOONPC is auto defined by https://github.com/marcmerlin/ArduinoOnPc-FastLED-GFX-LEDMatrix
LEDMATRIX is a separate define you'd set before including this file and
adds the LEDMatrix API if you need it.
The TL;DR is you shouldn't bother with it if you already have the GFX
and FastLED APIs, unless you can use fancy wavy scrolling colored fonts or the
sprite support in LEDMatrix.
You should need to modify this file at all unless you are adding new matrix
definitions and/or changing pin mappings for TFT screens. To choose which backend
to use, set the define before you include the file.
*/
#if defined(ARDUINOONPC)
#if defined(__ARMEL__)
#pragma message "Detected ARDUINOONPC on ARM (guessing rPi), will use FastLED_RPIRGBPanel_GFX"
#define RPIRGBPANEL
#else
#pragma message "Detected ARDUINOONPC, will use FastLED_TFTWrapper_GFX"
#define TFTWRAPPER
#endif
#endif
#if !defined(SMARTMATRIX) && !defined(SSD1331) && !defined(ST7735_128b128) && !defined(ST7735_128b160) && !defined(ILI9341) && !defined(M32B8X3) && !defined(M16BY16T4) && !defined(M64BY64)
/*
For my own benefit, I use some CPU architectures to default to some backends
in the defines below, but you should define your own before including this
file and the code below won't run
*/
#ifdef ESP8266
#define SSD1331
#define SSD1331_ROTATE 1
// ESP8266 shirt with neopixel strips
//#define M32B8X3
//#define M16BY16T4
#endif
#ifdef ESP32
//#define ST7735_128b128
//#define ST7735_128b160
#define SMARTMATRIX
//#define M64BY64
#endif
// Teensy 3.6
#ifdef __MK66FX1M0__
#define ILI9341
#define ILI_ROTATE 1
#endif
#endif
#include <Adafruit_GFX.h>
bool init_done = 0;
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
//============================================================================
// Matrix defines (SMARTMATRIX vs NEOMATRIX and size)
//============================================================================
// The ESP32 FastLED defines below must be defined before FastLED.h is loaded
#ifndef SMARTMATRIX
#ifdef ESP32
// Allow infrared for old FastLED versions
#define FASTLED_ALLOW_INTERRUPTS 1
// Newer Samguyver ESP32 FastLED has a new I2S implementation that can be
// better (or worse) than then default RMT which only supports 8 channels.
#define FASTLED_ESP32_I2S
#pragma message "Please use https://github.com/samguyer/FastLED.git if stock FastLED is unstable with ESP32"
#endif
#else
// CHANGEME, see MatrixHardware_ESP32_V0.h in SmartMatrix/src
#define GPIOPINOUT 4
#pragma message "Compiling for SMARTMATRIX with NEOMATRIX API"
#include <SmartLEDShieldV4.h> // if you're using SmartLED Shield V4 hardware
#include <SmartMatrix3.h>
#include <SmartMatrix_GFX.h>
#endif // SMARTMATRIX
#include <FastLED.h>
#ifdef LEDMATRIX
// Please use https://github.com/marcmerlin/LEDMatrix/ at lesat as recent as
// https://github.com/marcmerlin/LEDMatrix/commit/597ce703e924d45b2e676d6558c4c74a8ebc6991
// or https://github.com/Jorgen-VikingGod/LEDMatrix/commit/a11e74c8cd5b933021b6e15eb067280a52691449
// zero copy/no malloc code to work.
#include <LEDMatrix.h>
#endif
#if defined(SMARTMATRIX)
uint8_t matrix_brightness = 255;
#ifdef ESP32
#pragma message "Compiling for ESP32 with 64x32 16 scan panel and 64x96 resolution"
const uint8_t kPanelType = SMARTMATRIX_HUB75_32ROW_MOD16SCAN; // use SMARTMATRIX_HUB75_16ROW_MOD8SCAN for common 16x32 panels
const uint16_t MATRIX_TILE_WIDTH = 64; // width of EACH NEOPIXEL MATRIX (not total display)
const uint16_t MATRIX_TILE_HEIGHT= 96; // height of each matrix
#elif defined(__MK66FX1M0__) // my teensy 3.6 is connected to a 64x64 panel
#pragma message "Compiling for Teensy with 64x64 32 scan panel"
//const uint8_t kPanelType = SMARTMATRIX_HUB75_32ROW_MOD16SCAN; // use SMARTMATRIX_HUB75_16ROW_MOD8SCAN for common 16x32 panels
const uint8_t kPanelType = SMARTMATRIX_HUB75_64ROW_MOD32SCAN;
const uint16_t MATRIX_TILE_WIDTH = 64; // width of EACH NEOPIXEL MATRIX (not total display)
const uint16_t MATRIX_TILE_HEIGHT= 64; // height of each matrix
#else
#error Unknown architecture (not ESP32 or teensy 3.5/6)
#endif
// Used by LEDMatrix
const uint8_t MATRIX_TILE_H = 1; // number of matrices arranged horizontally
const uint8_t MATRIX_TILE_V = 1; // number of matrices arranged vertically
// Used by NeoMatrix
const uint16_t mw = MATRIX_TILE_WIDTH * MATRIX_TILE_H;
const uint16_t mh = MATRIX_TILE_HEIGHT * MATRIX_TILE_V;
const uint32_t NUMMATRIX = mw*mh;
/// SmartMatrix Defines
#define COLOR_DEPTH 24 // known working: 24, 48 - If the sketch uses type `rgb24` directly, COLOR_DEPTH must be 24
const uint8_t kMatrixWidth = mw;
const uint8_t kMatrixHeight = mh;
const uint8_t kRefreshDepth = 24; // known working: 24, 36, 48
const uint8_t kDmaBufferRows = 2; // known working: 2-4, use 2 to save memory, more to keep from dropping frames and automatically lowering refresh rate
const uint8_t kMatrixOptions = (SMARTMATRIX_OPTIONS_NONE); // see http://docs.pixelmatix.com/SmartMatrix for options
const uint8_t kBackgroundLayerOptions = (SM_BACKGROUND_OPTIONS_NONE);
SMARTMATRIX_ALLOCATE_BUFFERS(matrixLayer, kMatrixWidth, kMatrixHeight, kRefreshDepth, kDmaBufferRows, kPanelType, kMatrixOptions);
SMARTMATRIX_ALLOCATE_BACKGROUND_LAYER(backgroundLayer, kMatrixWidth, kMatrixHeight, COLOR_DEPTH, kBackgroundLayerOptions);
#ifdef LEDMATRIX
// cLEDMatrix defines
cLEDMatrix<MATRIX_TILE_WIDTH, -MATRIX_TILE_HEIGHT, HORIZONTAL_MATRIX,
MATRIX_TILE_H, MATRIX_TILE_V, HORIZONTAL_BLOCKS> ledmatrix(false);
#endif
CRGB *matrixleds;
void show_callback();
SmartMatrix_GFX *matrix = new SmartMatrix_GFX(matrixleds, mw, mh, show_callback);
// Sadly this callback function must be copied around with this init code
void show_callback() {
// memcpy(backgroundLayer.backBuffer(), matrixleds, kMatrixHeight*kMatrixWidth*3);
// backgroundLayer.swapBuffers(false);
backgroundLayer.swapBuffers(true);
//matrixleds = (CRGB *)backgroundLayer.getRealBackBuffer());
matrixleds = (CRGB *)backgroundLayer.backBuffer();
matrix->newLedsPtr(matrixleds);
#ifdef LEDMATRIX
ledmatrix.SetLEDArray(matrixleds);
#endif
}
//----------------------------------------------------------------------------
#elif defined(ILI9341)
#include "Adafruit_ILI9341.h"
#include <FastLED_SPITFT_GFX.h>
uint8_t matrix_brightness = 255;
#if ILI_ROTATE == 0
const uint16_t MATRIX_TILE_WIDTH = 320;
const uint16_t MATRIX_TILE_HEIGHT= 240;
#else
const uint16_t MATRIX_TILE_WIDTH = 240;
const uint16_t MATRIX_TILE_HEIGHT= 320;
#endif
//
// Used by LEDMatrix
const uint8_t MATRIX_TILE_H = 1; // number of matrices arranged horizontally
const uint8_t MATRIX_TILE_V = 1; // number of matrices arranged vertically
// Used by NeoMatrix
const uint16_t mw = MATRIX_TILE_WIDTH * MATRIX_TILE_H;
const uint16_t mh = MATRIX_TILE_HEIGHT * MATRIX_TILE_V;
const uint32_t NUMMATRIX = mw*mh;
#ifdef LEDMATRIX
// cLEDMatrix defines
cLEDMatrix<MATRIX_TILE_WIDTH, -MATRIX_TILE_HEIGHT, HORIZONTAL_MATRIX,
MATRIX_TILE_H, MATRIX_TILE_V, HORIZONTAL_BLOCKS> ledmatrix(false);
#endif
CRGB *matrixleds;
#if defined(__MK66FX1M0__)
#define TFT_MISO 12
#define TFT_CLK 13
#define TFT_MOSI 11
#define TFT_DC 10
#define TFT_RST 23
#define TFT_CS 22
//Adafruit_ILI9341 *tft = new Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);
Adafruit_ILI9341 *tft = new Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
#else
// HWSPI default
#define TFT_MISO 19
#define TFT_CLK 18
#define TFT_MOSI 23
#define TFT_DC 27
// this is the TFT reset pin. It seems required on my board
#define TFT_RST 26
#define TFT_CS 25
Adafruit_ILI9341 *tft = new Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
#endif
FastLED_SPITFT_GFX *matrix = new FastLED_SPITFT_GFX(matrixleds, mw, mh, mw, mh, tft, 0);
//----------------------------------------------------------------------------
#elif defined(TFTWRAPPER)
#include "TFT_LinuxWrapper.h"
#include <FastLED_TFTWrapper_GFX.h>
uint8_t matrix_brightness = 255;
const uint16_t MATRIX_TILE_WIDTH = 160;
const uint16_t MATRIX_TILE_HEIGHT= 128;
//
// Used by LEDMatrix
const uint8_t MATRIX_TILE_H = 1; // number of matrices arranged horizontally
const uint8_t MATRIX_TILE_V = 1; // number of matrices arranged vertically
// Used by NeoMatrix
const uint16_t mw = MATRIX_TILE_WIDTH * MATRIX_TILE_H;
const uint16_t mh = MATRIX_TILE_HEIGHT * MATRIX_TILE_V;
const uint32_t NUMMATRIX = mw*mh;
#ifdef LEDMATRIX
// cLEDMatrix defines
cLEDMatrix<MATRIX_TILE_WIDTH, -MATRIX_TILE_HEIGHT, HORIZONTAL_MATRIX,
MATRIX_TILE_H, MATRIX_TILE_V, HORIZONTAL_BLOCKS> ledmatrix(false);
#endif
CRGB *matrixleds;
TFT_LinuxWrapper *tft = new TFT_LinuxWrapper(160, 128);
FastLED_TFTWrapper_GFX *matrix = new FastLED_TFTWrapper_GFX(matrixleds, mw, mh, tft);
//----------------------------------------------------------------------------
#elif defined(RPIRGBPANEL)
#include <FastLED_RPIRGBPanel_GFX.h>
// https://github.com/hzeller/rpi-rgb-led-matrix
// Arduino min/max conflict with g++ math min/max
#undef min
#undef max
#include <led-matrix.h>
uint8_t matrix_brightness = 255;
const uint16_t MATRIX_TILE_WIDTH = 128;
const uint16_t MATRIX_TILE_HEIGHT= 192;
// Used by LEDMatrix
const uint8_t MATRIX_TILE_H = 1; // number of matrices arranged horizontally
const uint8_t MATRIX_TILE_V = 1; // number of matrices arranged vertically
// Used by NeoMatrix
const uint16_t mw = MATRIX_TILE_WIDTH * MATRIX_TILE_H;
const uint16_t mh = MATRIX_TILE_HEIGHT * MATRIX_TILE_V;
const uint32_t NUMMATRIX = mw*mh;
#ifdef LEDMATRIX
// cLEDMatrix defines
cLEDMatrix<MATRIX_TILE_WIDTH, -MATRIX_TILE_HEIGHT, HORIZONTAL_MATRIX,
MATRIX_TILE_H, MATRIX_TILE_V, HORIZONTAL_BLOCKS> ledmatrix(false);
#endif
CRGB *matrixleds;
FastLED_RPIRGBPanel_GFX *matrix = new FastLED_RPIRGBPanel_GFX(matrixleds, mw, mh);
//----------------------------------------------------------------------------
#elif defined(ST7735_128b128) || defined(ST7735_128b160)
#include <Adafruit_ST7735.h>
#include <FastLED_SPITFT_GFX.h>
uint8_t matrix_brightness = 255;
const uint16_t MATRIX_TILE_WIDTH = 128;
#ifdef ST7735_128b128
const uint16_t MATRIX_TILE_HEIGHT= 128;
#else
const uint16_t MATRIX_TILE_HEIGHT= 160;
#endif
// Used by LEDMatrix
const uint8_t MATRIX_TILE_H = 1; // number of matrices arranged horizontally
const uint8_t MATRIX_TILE_V = 1; // number of matrices arranged vertically
// Used by NeoMatrix
const uint16_t mw = MATRIX_TILE_WIDTH * MATRIX_TILE_H;
const uint16_t mh = MATRIX_TILE_HEIGHT * MATRIX_TILE_V;
const uint32_t NUMMATRIX = mw*mh;
#ifdef LEDMATRIX
// cLEDMatrix defines
cLEDMatrix<MATRIX_TILE_WIDTH, -MATRIX_TILE_HEIGHT, HORIZONTAL_MATRIX,
MATRIX_TILE_H, MATRIX_TILE_V, HORIZONTAL_BLOCKS> ledmatrix(false);
#endif
CRGB *matrixleds;
/*
Arduino ESP8266 ESP32 ESP32 rPi rPi
VSPI HSPI SPI0 SPI1
VCC
SCL/SCK/CLK/D0 13 GPIO14/D5 18 14 BC11/22 BC21/40
SDA/SDI/MOSI/D1 11 GPIO13/D7 23 13 BC10/19 BC20/38
RES/RST 9 GPIO15/D8 26 26 BC24
DC/A0/RS (data) 8 GPIO05/D1 27 27 BC23
CS 10 GPIO04/D2 25 25 BC08
MISO 12 GPIO12/D6 19 12 BM11/23 BC19/35
*/
#ifdef ESP32
#define TFT_CS 25
#define TFT_RST 26
#define TFT_DC 27
#elif defined(ESP8266)
#define TFT_CS 4
#define TFT_RST 15
#define TFT_DC 5
#else
#define TFT_CS 10
#define TFT_RST 9 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC 8
#endif
Adafruit_ST7735 *tft = new Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
FastLED_SPITFT_GFX *matrix = new FastLED_SPITFT_GFX(matrixleds, mw, mh, mw, mh, tft, 0);
//----------------------------------------------------------------------------
#elif defined(SSD1331)
#include <Adafruit_SSD1331.h>
#include <FastLED_SPITFT_GFX.h>
uint8_t matrix_brightness = 255;
#if SSD1331_ROTATE == 0
const uint16_t MATRIX_TILE_WIDTH = 96;
const uint16_t MATRIX_TILE_HEIGHT= 64;
#else
const uint16_t MATRIX_TILE_WIDTH = 64;
const uint16_t MATRIX_TILE_HEIGHT= 96;
#endif
//
// Used by LEDMatrix
const uint8_t MATRIX_TILE_H = 1; // number of matrices arranged horizontally
const uint8_t MATRIX_TILE_V = 1; // number of matrices arranged vertically
// Used by NeoMatrix
const uint16_t mw = MATRIX_TILE_WIDTH * MATRIX_TILE_H;
const uint16_t mh = MATRIX_TILE_HEIGHT * MATRIX_TILE_V;
const uint32_t NUMMATRIX = mw*mh;
#ifdef LEDMATRIX
// cLEDMatrix defines
cLEDMatrix<MATRIX_TILE_WIDTH, -MATRIX_TILE_HEIGHT, HORIZONTAL_MATRIX,
MATRIX_TILE_H, MATRIX_TILE_V, HORIZONTAL_BLOCKS> ledmatrix(false);
#endif
CRGB *matrixleds;
/* https://pinout.xyz/pinout/spi
SD1331 Pin Arduino ESP8266 ESP32 ESP32 rPi rPi
1 GND VSPI HSPI SPI0 SPI1
2 VCC
3 SCL/SCK/CLK/D0 13 GPIO14/D5 18 14 BC11/22 BC21/40
4 SDA/SDI/MOSI/D1 11 GPIO13/D7 23 13 BC10/19 BC20/38
5 RES/RST 9 GPIO15/D8 26 26 BC24
6 DC/A0/RS (data) 8 GPIO05/D1 27 27 BC23
7 CS 10 GPIO04/D2 25 25 BC08
MISO 12 GPIO12/D6 19 12 BM11/23 BC19/35
*/
#ifdef ESP32
#define sclk 18
#define mosi 23
#define rst 26
#define dc 27
#define cs 25
// Option 1: use any pins but a little slower
//#pragma message "Using SWSPI"
Adafruit_SSD1331 *tft = new Adafruit_SSD1331(cs, dc, mosi, sclk, rst);
#else
// You can use any (4 or) 5 pins
// hwspi hardcodes those pins, no need to redefine them
#define sclk 14
#define mosi 13
#define rst 15
#define cs 4
#define dc 5
// Option 2: must use the hardware SPI pins
// (for UNO thats sclk = 13 and sid = 11) and pin 10 must be
// an output. This is much faster - also required if you want
// to use the microSD card (see the image drawing example)
#pragma message "Using HWSPI"
Adafruit_SSD1331 *tft = new Adafruit_SSD1331(&SPI, cs, dc, rst);
#endif
#if SSD1331_ROTATE == 0
FastLED_SPITFT_GFX *matrix = new FastLED_SPITFT_GFX(matrixleds, mw, mh, 96, 64, tft, 0);
#else
FastLED_SPITFT_GFX *matrix = new FastLED_SPITFT_GFX(matrixleds, mw, mh, 96, 64, tft, 1);
#endif
//----------------------------------------------------------------------------
#elif defined(M32B8X3)
#include <FastLED_NeoMatrix.h>
uint8_t matrix_brightness = 64;
// Used by LEDMatrix
const uint16_t MATRIX_TILE_WIDTH = 8; // width of EACH NEOPIXEL MATRIX (not total display)
const uint16_t MATRIX_TILE_HEIGHT= 32; // height of each matrix
const uint8_t MATRIX_TILE_H = 3; // number of matrices arranged horizontally
const uint8_t MATRIX_TILE_V = 1; // number of matrices arranged vertically
// Used by NeoMatrix
const uint16_t mw = MATRIX_TILE_WIDTH * MATRIX_TILE_H;
const uint16_t mh = MATRIX_TILE_HEIGHT * MATRIX_TILE_V;
const uint32_t NUMMATRIX = mw*mh;
#ifdef LEDMATRIX
// cLEDMatrix defines
cLEDMatrix<-MATRIX_TILE_WIDTH, -MATRIX_TILE_HEIGHT, HORIZONTAL_ZIGZAG_MATRIX,
MATRIX_TILE_H, MATRIX_TILE_V, HORIZONTAL_BLOCKS> ledmatrix(false);
#endif
CRGB *matrixleds;
// MATRIX DECLARATION:
// Parameter 1 = width of EACH NEOPIXEL MATRIX (not total display)
// Parameter 2 = height of each matrix
// Parameter 3 = number of matrices arranged horizontally
// Parameter 4 = number of matrices arranged vertically
// Parameter 5 = pin number (most are valid)
// Parameter 6 = matrix layout flags, add together as needed:
// NEO_MATRIX_TOP, NEO_MATRIX_BOTTOM, NEO_MATRIX_LEFT, NEO_MATRIX_RIGHT:
// Position of the FIRST LED in the FIRST MATRIX; pick two, e.g.
// NEO_MATRIX_TOP + NEO_MATRIX_LEFT for the top-left corner.
// NEO_MATRIX_ROWS, NEO_MATRIX_COLUMNS: LEDs WITHIN EACH MATRIX are
// arranged in horizontal rows or in vertical columns, respectively;
// pick one or the other.
// NEO_MATRIX_PROGRESSIVE, NEO_MATRIX_ZIGZAG: all rows/columns WITHIN
// EACH MATRIX proceed in the same order, or alternate lines reverse
// direction; pick one.
// NEO_TILE_TOP, NEO_TILE_BOTTOM, NEO_TILE_LEFT, NEO_TILE_RIGHT:
// Position of the FIRST MATRIX (tile) in the OVERALL DISPLAY; pick
// two, e.g. NEO_TILE_TOP + NEO_TILE_LEFT for the top-left corner.
// NEO_TILE_ROWS, NEO_TILE_COLUMNS: the matrices in the OVERALL DISPLAY
// are arranged in horizontal rows or in vertical columns, respectively;
// pick one or the other.
// NEO_TILE_PROGRESSIVE, NEO_TILE_ZIGZAG: the ROWS/COLUMS OF MATRICES
// (tiles) in the OVERALL DISPLAY proceed in the same order for every
// line, or alternate lines reverse direction; pick one. When using
// zig-zag order, the orientation of the matrices in alternate rows
// will be rotated 180 degrees (this is normal -- simplifies wiring).
// See example below for these values in action.
FastLED_NeoMatrix *matrix = new FastLED_NeoMatrix(matrixleds, MATRIX_TILE_WIDTH, MATRIX_TILE_HEIGHT, MATRIX_TILE_H, MATRIX_TILE_V,
NEO_MATRIX_TOP + NEO_MATRIX_RIGHT +
NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG +
NEO_TILE_TOP + NEO_TILE_LEFT + NEO_TILE_PROGRESSIVE);
//----------------------------------------------------------------------------
#elif defined(M16BY16T4)
#include <FastLED_NeoMatrix.h>
uint8_t matrix_brightness = 64;
const uint16_t MATRIX_TILE_WIDTH = 16; // width of EACH NEOPIXEL MATRIX (not total display)
const uint16_t MATRIX_TILE_HEIGHT= 16; // height of each matrix
const uint8_t MATRIX_TILE_H = 2; // number of matrices arranged horizontally
const uint8_t MATRIX_TILE_V = 2; // number of matrices arranged vertically
// Used by NeoMatrix
const uint16_t mw = MATRIX_TILE_WIDTH * MATRIX_TILE_H;
const uint16_t mh = MATRIX_TILE_HEIGHT * MATRIX_TILE_V;
const uint32_t NUMMATRIX = mw*mh;
#ifdef LEDMATRIX
// cLEDMatrix defines
cLEDMatrix<-MATRIX_TILE_WIDTH, MATRIX_TILE_HEIGHT, HORIZONTAL_ZIGZAG_MATRIX,
MATRIX_TILE_H, MATRIX_TILE_V, VERTICAL_BLOCKS> ledmatrix(false);
#endif
CRGB *matrixleds;
FastLED_NeoMatrix *matrix = new FastLED_NeoMatrix(matrixleds, MATRIX_TILE_WIDTH, MATRIX_TILE_HEIGHT, MATRIX_TILE_H, MATRIX_TILE_V,
NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT +
NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG +
NEO_TILE_TOP + NEO_TILE_RIGHT + NEO_TILE_PROGRESSIVE);
const uint8_t MATRIXPIN = 13;
//----------------------------------------------------------------------------
#elif defined(M64BY64) || defined(FASTLED_SDL) // 64x64 straight connection (no matrices)
#include <FastLED_NeoMatrix.h>
// http://marc.merlins.org/perso/arduino/post_2018-07-30_Building-a-64x64-Neopixel-Neomatrix-_4096-pixels_-running-NeoMatrix-FastLED-IR.html
uint8_t matrix_brightness = 128;
//
// Used by LEDMatrix
const uint16_t MATRIX_TILE_WIDTH = 64; // width of EACH NEOPIXEL MATRIX (not total display)
const uint16_t MATRIX_TILE_HEIGHT= 64; // height of each matrix
const uint8_t MATRIX_TILE_H = 1; // number of matrices arranged horizontally
const uint8_t MATRIX_TILE_V = 1; // number of matrices arranged vertically
#define NUM_STRIPS 16
#define NUM_LEDS_PER_STRIP 256
// Used by NeoMatrix
const uint16_t mw = MATRIX_TILE_WIDTH * MATRIX_TILE_H;
const uint16_t mh = MATRIX_TILE_HEIGHT * MATRIX_TILE_V;
const uint32_t NUMMATRIX = mw*mh;
CRGB *matrixleds;
#if defined(M64BY64)
#ifdef LEDMATRIX
// cLEDMatrix defines
cLEDMatrix<MATRIX_TILE_WIDTH, MATRIX_TILE_HEIGHT, VERTICAL_ZIGZAG_MATRIX> ledmatrix(false);
#endif
FastLED_NeoMatrix *matrix = new FastLED_NeoMatrix(matrixleds, MATRIX_TILE_WIDTH, MATRIX_TILE_HEIGHT,
NEO_MATRIX_BOTTOM + NEO_MATRIX_LEFT +
NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG );
#else
#ifdef LEDMATRIX
// cLEDMatrix defines
cLEDMatrix<MATRIX_TILE_WIDTH, MATRIX_TILE_HEIGHT, VERTICAL_MATRIX> ledmatrix(false);
#endif
FastLED_NeoMatrix *matrix = new FastLED_NeoMatrix(matrixleds, MATRIX_TILE_WIDTH, MATRIX_TILE_HEIGHT,
NEO_MATRIX_TOP + NEO_MATRIX_LEFT + NEO_MATRIX_ROWS );
#endif
#else
#error "Please write a matrix config or choose one of the definitions above"
#endif // end Matrix resolution and display defines
//============================================================================
// Compat for some other demos
const uint32_t NUM_LEDS = NUMMATRIX;
const uint16_t MATRIX_HEIGHT = mh;
const uint16_t MATRIX_WIDTH = mw;
// Compat with SmartMatrix code that uses those variables
// (but don't redefine for SmartMatrix backend)
#ifndef SMARTMATRIX
const uint16_t kMatrixWidth = mw;
const uint16_t kMatrixHeight = mh;
#endif
#ifdef ESP8266
// Turn off Wifi in setup()
// https://www.hackster.io/rayburne/esp8266-turn-off-wifi-reduce-current-big-time-1df8ae
//
#include "ESP8266WiFi.h"
extern "C" {
#include "user_interface.h"
}
// min/max are broken by the ESP8266 include
#define min(a,b) (a<b)?(a):(b)
#define max(a,b) (a>b)?(a):(b)
#endif // ESP8266
uint8_t gHue = 0; // rotating "base color" used by many of the patterns
uint16_t speed = 255;
float matrix_gamma = 1; // higher number is darker, needed for Neomatrix more than SmartMatrix
// Like XY, but for a mirror image from the top (used by misconfigured code)
int XY2( int x, int y, bool wrap=false) {
wrap = wrap; // squelch compiler warning
return matrix->XY(x,MATRIX_HEIGHT-1-y);
}
uint16_t XY( uint8_t x, uint8_t y) {
return matrix->XY(x,y);
}
int wrapX(int x) {
if (x < 0 ) return 0;
if (x >= MATRIX_WIDTH) return (MATRIX_WIDTH-1);
return x;
}
void show_free_mem(const char *pre=NULL) {
Framebuffer_GFX::show_free_mem(pre);
}
void *mallocordie(const char *varname, uint32_t req) {
Serial.print("Malloc ");
Serial.print(varname);
Serial.print(" . Requested bytes: ");
Serial.println(req);
void *mem = malloc(req);
if (mem) {
return mem;
} else {
Serial.print("FATAL: malloc failed for ");
Serial.println(varname);
show_free_mem();
while (1);
}
return NULL;
}
void matrix_setup(int reservemem = 40000) {
reservemem = reservemem; // squelch compiler warning if var is unused.
if (init_done) {
Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> BUG: matrix_setup called twice");
return;
}
init_done = 1;
// Teensy takes a while to initialize serial port.
// Teensy 3.0, 3.1/3.2, 3.5, 3.6
#if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
delay(3000);
#endif
Serial.begin(115200);
Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Serial.begin");
show_free_mem("Memory after setup() starts");
// ESP32 has more memory available for allocation in setup than in global
// (if this were a global array), so we use malloc here.
matrixleds = (CRGB *) mallocordie("matrixleds", sizeof(CRGB) * NUMMATRIX);
// and then fix the until now NULL pointer in the object.
matrix->newLedsPtr(matrixleds);
show_free_mem("After matrixleds malloc");
#ifdef LEDMATRIX
ledmatrix.SetLEDArray(matrixleds);
#endif
matrix_gamma = 2.4; // higher number is darker, needed for Neomatrix more than SmartMatrix
#if defined(SMARTMATRIX)
matrix_gamma = 1; // SmartMatrix should be good by default.
matrixLayer.addLayer(&backgroundLayer);
// SmartMatrix takes all the RAM it can get its hands on. Get it to leave some
// free RAM so that other libraries can work too.
#ifdef ESP32
if (reservemem) matrixLayer.begin(reservemem); else matrixLayer.begin();
#else
matrixLayer.begin();
#endif
// This sets the neomatrix and LEDMatrix pointers
show_callback();
matrixLayer.setRefreshRate(240);
backgroundLayer.enableColorCorrection(true);
Serial.print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SmartMatrix GFX output, total LEDs: ");
Serial.println(NUMMATRIX);
// Quick hello world test
#ifndef DISABLE_MATRIX_TEST
Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SmartMatrix Grey Demo");
backgroundLayer.fillScreen( {0x80, 0x80, 0x80} );
// backgroundLayer.swapBuffers();
show_callback();
delay(1000);
#endif
Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> SmartMatrix Init Done");
#elif defined(ILI9341)
// Need to init the underlying TFT SPI engine
Serial.println("ILI9341 tft begin");
tft->begin(40000000);
#if ILI_ROTATE == 0
tft->setRotation(1);
// This is very important, or FastLED_SPITFT_GFX::show will not work.
// Size is hardcoded by TFT size.
// Doesn't seem to work, but fillscreen below takes care of it
//tft->setAddrWindow(0, 0, 320, 240);
#else
tft->setRotation(0);
//tft->setAddrWindow(0, 0, 240, 320);
#endif
// Seems that filllscreen further initializes the tft so that it works
tft->fillScreen(ILI9341_DARKGREY);
#elif defined(TFTWRAPPER)
// Need to init the underlying TFT SPI engine
Serial.println("ARDUINOONPC tft begin");
tft->begin();
matrix->fillScreen(LTDC_BLACK);
#elif defined(RPIRGBPANEL)
Serial.println("Using rpi-rgb-led-matrix");
rgb_matrix::RGBMatrix::Options defaults;
defaults.hardware_mapping = "regular"; // or e.g. "adafruit-hat"
defaults.rows = 64;
defaults.cols = 128;
defaults.chain_length = 1;
defaults.parallel = 3;
defaults.pwm_lsb_nanoseconds = 50;
defaults.pwm_bits = 7;
defaults.led_rgb_sequence = "RBG";
defaults.panel_type = "FM6126A";
rgb_matrix::RuntimeOptions ropt;
rgb_matrix::Canvas *canvas = rgb_matrix::CreateMatrixFromOptions(defaults, ropt);
while (canvas == NULL) Serial.println("Canvas did not initialize");
matrix->setCanvas(canvas);
Serial.println("RGBPanel Canvas initialized");
#elif defined(ST7735_128b128)
Serial.println("ST7735_128b128: For extra SPI speed, try spi.begin 80Mhz, but it may be less stable");
//tft->begin(80000000);
// fillScreen below does the job
//tft->setAddrWindow(0, 0, 128, 128);
tft->initR(INITR_144GREENTAB);
// This is reuqired for the screen to work
tft->fillScreen(ST77XX_GREEN);
#elif defined(ST7735_128b160)
Serial.println("ST7735_128b160: For extra SPI speed, try spi.begin 80Mhz, but it may be less stable");
//tft->begin(80000000);
// fillScreen below does the job
//tft->setAddrWindow(0, 0, 128, 160);
tft->initR(INITR_BLACKTAB);
// This is reuqired for the screen to work
tft->fillScreen(ST77XX_GREEN);
#elif defined(SSD1331)
// Need to init the underlying TFT SPI engine
tft->begin();
Serial.println("SSD1331: For extra SPI speed, try spi.begin 80Mhz, but it may be less stable");
//tft->begin(80000000);
// This is very important, or FastLED_SPITFT_GFX::show will not work.
// Size is hardcoded by TFT size.
tft->setAddrWindow(0, 0, 96, 64);
// Example of parallel output
#elif defined(M32B8X3)
// Init Matrix
// Serialized, 768 pixels takes 26 seconds for 1000 updates or 26ms per refresh
// FastLED.addLeds<NEOPIXEL,MATRIXPIN>(matrixleds, NUMMATRIX).setCorrection(TypicalLEDStrip);
// https://github.com/FastLED/FastLED/wiki/Parallel-Output
// WS2811_PORTA - pins 12, 13, 14 and 15 or pins 6,7,5 and 8 on the NodeMCU
// This is much faster 1000 updates in 10sec
// See https://github.com/FastLED/FastLED/wiki/Parallel-Output for pin definitions
#ifdef ESP8266
FastLED.addLeds<WS2811_PORTA,3>(matrixleds, NUMMATRIX/MATRIX_TILE_H).setCorrection(TypicalLEDStrip);
#else
FastLED.addLeds<WS2811_PORTD,3>(matrixleds, NUMMATRIX/MATRIX_TILE_H).setCorrection(TypicalLEDStrip);
#endif
Serial.print("Neomatrix parallel output, total LEDs: ");
Serial.println(NUMMATRIX);
// Serialized (slow-ish) output
#elif defined(M16BY16T4)
FastLED.addLeds<NEOPIXEL,MATRIXPIN>(matrixleds, NUMMATRIX).setCorrection(TypicalLEDStrip);
Serial.print("Neomatrix total LEDs: ");
Serial.print(NUMMATRIX);
Serial.print(" running on pin: ");
Serial.println(MATRIXPIN);
// 64x64 straight wiring
#elif defined(M64BY64) // 64x64 straight connection (no matrices)
// https://github.com/FastLED/FastLED/wiki/Multiple-Controller-Examples
FastLED.addLeds<WS2812B,23, GRB>(matrixleds, 0*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
#ifdef ESP32
FastLED.addLeds<WS2812B,22, GRB>(matrixleds, 1*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2812B,27, GRB>(matrixleds, 2*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP); // was 3
FastLED.addLeds<WS2812B,21, GRB>(matrixleds, 3*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2812B,19, GRB>(matrixleds, 4*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2812B,18, GRB>(matrixleds, 5*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2812B, 5, GRB>(matrixleds, 6*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2812B, 4, GRB>(matrixleds, 7*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2812B, 0, GRB>(matrixleds, 8*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2812B, 2, GRB>(matrixleds, 9*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2812B,15, GRB>(matrixleds,10*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2812B,25, GRB>(matrixleds,11*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2812B,26, GRB>(matrixleds,12*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2812B,14, GRB>(matrixleds,13*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2812B,12, GRB>(matrixleds,14*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
FastLED.addLeds<WS2812B,13, GRB>(matrixleds,15*NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
Serial.print("Neomatrix 16 pin via RMT/I2S 16 way parallel output, total LEDs: ");
Serial.println(NUMMATRIX);
#endif // ESP32
#elif defined(FASTLED_SDL) // 64x64 straight connection (no matrices)
FastLED.addLeds<SDL, mw, mh>(matrixleds, NUMMATRIX);
Serial.print("Neomatrix on top of SDL, total LEDs: ");
Serial.println(NUMMATRIX);
#else
#error "Undefined Matrix"
#endif
show_free_mem("Before matrix->begin");
matrix->begin();
//show_free_mem("After matrix->begin");
Serial.print("Setting Brightness: ");
Serial.println(matrix_brightness);
#if defined(SMARTMATRIX)
matrixLayer.setBrightness(matrix_brightness);
#else
FastLED.setBrightness(matrix_brightness);
#endif
Serial.print("Gamma Correction: ");
Serial.println(matrix_gamma);
// Gamma is used by AnimatedGIFs and others, as such:
// CRGB color = CRGB(matrix->gamma[red], matrix->gamma[green], matrix->gamma[blue]);
matrix->precal_gamma(matrix_gamma);
// LEDMatrix alignment is tricky, this test helps make sure things are aligned correctly
#ifndef DISABLE_MATRIX_TEST
#ifdef LEDMATRIX
Serial.println("LEDMatrix Test");
ledmatrix.DrawLine (0, 0, ledmatrix.Width() - 1, ledmatrix.Height() - 1, CRGB(0, 255, 0));
ledmatrix.DrawPixel(0, 0, CRGB(255, 0, 0));
ledmatrix.DrawPixel(ledmatrix.Width() - 1, ledmatrix.Height() - 1, CRGB(0, 0, 255));
ledmatrix.DrawLine (ledmatrix.Width() - 5, 4, 4, ledmatrix.Height() - 5, CRGB(128, 128, 128));
ledmatrix.DrawPixel(ledmatrix.Width() - 5, 4, CRGB(255, 64, 64));
ledmatrix.DrawPixel(4, ledmatrix.Height() - 5, CRGB(64, 64, 255));
matrix->show();
delay(1000);
#endif
#endif
Serial.println("neomatrix_config setup done");
// At least on teensy, due to some framework bug it seems, early
// serial output gets looped back into serial input
// Hence, flush input.
while(Serial.available() > 0) { char t = Serial.read(); t = t; }
}
#endif // neomatrix_config_h
//vim:sts=4:sw=4