-
Notifications
You must be signed in to change notification settings - Fork 2
/
LED_Marquee.ino
1111 lines (925 loc) · 34.8 KB
/
LED_Marquee.ino
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define DEBUG false
#define uint8_t uint
#include <Arduino.h>
#include "Color.h"
#include "Gamma.h"
#include "Fonts.h"
#include "Icons.h"
#include "PixelBitBanging.h"
#include "ArduinoJson-v6.18.3.h"
// Used to receive data on a virtual RX pin instead of the usual pin 0
#include "SoftwareSerial/SoftwareSerial.h"
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
#define diagnosticLed 13
void diagnosticLedOn() {
digitalWrite(diagnosticLed, HIGH); // turn the LED on (HIGH is the voltage level)
}
void diagnosticLedOff() {
digitalWrite(diagnosticLed, LOW); // turn the LED on (HIGH is the voltage level)
}
void diagnosticBlink() {
if (DEBUG) {
diagnosticLedOn();
delay(5);
diagnosticLedOff();
delay(5);
diagnosticLedOn();
// delay(5);
// diagnosticLedOff();
// delay(300);
}
}
volatile unsigned long timeOfLastBeat = 0;
const unsigned int FADE_MILLIS = 250; // number of ms beat flash takes to fade out
// http://www.gammon.com.au/interrupts
ISR (PCINT1_vect) {
// handle pin change interrupt for A0 to A5 here
if (PINC & bit (0)) { // if it was high
timeOfLastBeat = millis();
diagnosticLedOn();
} else {
diagnosticLedOff();
}
}
#define rxPin 10
#define txPin 11
SoftwareSerial softSerial(rxPin, txPin); // RX, TX
const char ASOT[]
PROGMEM = " ";
/*"I had a dream last night... A vision! I saw a world full of people. "
"Everybody was dancing! And screaming loud! They were just there to listen to "
"the music. Some even had their eyes closed. "
"Everybody was just smiling. "
"It was deep. "
"It was underground. "
"Tranceparent. "
"It was magical. "
"It was a happy place... "
"celebrating music! "
"Celebrating life! "
"Men and women - free - without a worry! "
"Then, when I woke up, I realized: "
"I WANNA BE IN THAT MOMENT! "
"The very essence of my existence is looking for that emotion! "
"And when the weekend comes... "
"I L I V E F O R T H A T E N E R G Y . "; //598 chars
*/
//How often to advertise "MSG ME!!!", e.g. every 5 marquee scrolls
#define ADVERTISE_EVERY 5
//Speed of invader sequence, lower is faster
#define INVADER_DELAY 55
//Speed of scrolling text marquee, lower is faster
#define MARQUEE_DELAY 25
//Affects how long the all your base style text stays on screen. Higher is faster
#define ALLYOURBASE_DELAY 10
//Speed of chars spelled out one by one effect, lower is faster
#define CHARS_ONEBYONE_DELAY 50
//Speed (delay between frames) of flashy countdown animation, lower is faster
#define COUNTDOWN_DELAY 500
// Change this to be at least as long as your pixel string (too long will work fine, just be a little slower)
#define NUM_PANELS 2 // Number of panels. There are 2.
#define COLUMNS_PER_PANEL 60 // Number of columns per panel, in my case, the # of pixels in each LED string. I am using 60LED/M
#define PIXELS NUM_PANELS*COLUMNS_PER_PANEL // Length of pixels total. I am using 2 meters of 60LED/M
#define INTERCHAR_SPACE 1
// The length of the buffer used to read a new string from the serial port
#define MAX_BUFFER_LEN 600
#define STRINGBUFFER_LEN (MAX_BUFFER_LEN + 1)
#define PADDED_STRINGBUFFER_LEN (STD_STRING_PADDING + STRINGBUFFER_LEN)
// Pad this amount so that scrolling starts nicely off the end, for FontStd
#define STRING_PADDING(font_width) (NUM_PANELS * COLUMNS_PER_PANEL / ((font_width) + INTERCHAR_SPACE))
const int json_capacity = STRINGBUFFER_LEN + JSON_OBJECT_SIZE(8);
StaticJsonDocument <json_capacity> json;
// This is what style the current text will be shown in
const char MSGTYPE_CHONKY_SLIDE = 'C';
const char MSGTYPE_ONE_BY_ONE = 'O';
const char MSGTYPE_FLASHY = 'F';
const char MSGTYPE_COUNTDOWN = 'W';
const char MSGTYPE_STARFIELD = 'S';
const char MSGTYPE_UTILITY = 'U';
const char MSGTYPE_KEYBOARD = 'K';
const char MSGTYPE_CHOOSER = 'H';
const char MSGTYPE_ICON = 'I';
const char MSGTYPE_TRACKID = 'T';
const char MSGTYPE_CHONKYMARQUEE = 'N';
const char MSGTYPE_DEFAULT = 'D';
// Modes to show keyboard mode in
const char KEYBOARD_MODE_WARNING = 'W';
//vertical tab or \v; single column
const char VT = '\u000B';
const char HEART = '\u007F';
/* Calculates how many empty columns to pad this string on its start and on its end,
* and stores the results in startPad and endPad. Helps get it to be in the middle of the panel.
* E.g.
* (columns - total string width) = startPad, endPad
* (60-6) = 27, 27
* (60-5) = 27, 28
*/
void getColumnsToPadForString(const char* str, int fontWidth, uint* startPad, uint* endPad) {
uint stringColumns = 0;
while (*str++) {
if (*str != VT) {
stringColumns += (fontWidth + INTERCHAR_SPACE);
}
}
//Subtract 1 because we don't append an interchar space at the end of the string
stringColumns -= 1;
uint usedCols = COLUMNS_PER_PANEL - stringColumns;
*startPad = usedCols / 2;
if (usedCols % 2 == 0) {
*endPad = usedCols / 2;
} else {
*endPad = usedCols / 2 + 1;
}
}
// Just wait long enough without sending any bits to cause the pixels to latch and display the last sent frame
void show() {
delayMicroseconds((RES / 1000UL) +
1); // Round up since the delay must be _at_least_ this long (too short might not work, too long not a problem)
}
uint BRIGHTNESS_SHIFT = 0;
// Send 3 bytes of color data (R,G,B) for a signle pixel down all the connected stringsat the same time
// A 1 bit in "row" means send the color, a 0 bit means send black.
static inline void sendColumnRGB(uint row, uint r, uint g, uint b) {
sendBitx8(row, g >> BRIGHTNESS_SHIFT, onBits); // WS2812 takes colors in GRB order
sendBitx8(row, r >> BRIGHTNESS_SHIFT, onBits); // WS2812 takes colors in GRB order
sendBitx8(row, b >> BRIGHTNESS_SHIFT, onBits); // WS2812 takes colors in GRB order
}
// Turn off all pixels
static inline void clear() {
cli();
for (unsigned int i = 0; i < PIXELS; i++) {
sendColumnRGB(0, 0, 0, 0);
}
sei();
show();
}
// Send the pixels to form the specified char, not including interchar space
// skip is the number of pixels to skip at the begining to enable sub-char smooth scrolling
// TODO: Subtract the offset from the char before starting the send sequence to save time if necessary
// TODO: Also could pad the beginning of the font table to aovid the offset subtraction at the cost of 20*8 bytes of progmem
// TODO: Could pad all chars out to 8 bytes wide to turn the the multiply by FONTSTD_WIDTH into a shift
static void sendChar(
const uint ch,
uint skip,
uint r, uint g, uint b,
boolean sendTrailingSpace = true,
const Font& font = fontStd) {
// For the VT char, we only send a single column (so just the interchar space)
if (ch != VT) {
const uint* charbase = font.data + ((ch - ' ') * font.width);
uint col = font.width;
if (ch == HEART) {
col = HEART_WIDTH;
}
while (skip--) {
charbase++;
col--;
}
while (col--) {
sendColumnRGB(pgm_read_byte_near(charbase++), r, g, b);
}
// TODO: FLexible interchar spacing
}
if (sendTrailingSpace) {
sendColumnRGB(0, r, g, b); // Interchar space
}
}
static void sendString(const char* str, const Font& font, uint skip, const uint r, const uint g, const uint b) {
unsigned int l = PIXELS / (font.width + INTERCHAR_SPACE);
sendChar(*str, skip, r, g, b, true, font); // First char is special case because it can be stepped for smooth scrolling
// Send rest of string
while (*(++str) && l--) {
sendChar(*str, 0, r, g, b, *(str + 1) != '\0'); //dont send interchar space on last char
}
}
// Show the passed string. The last letter of the string will be in the rightmost pixels of the display.
// Skip is how many cols of the 1st char to skip for smooth scrolling
static inline void
sendPaddedString(const char* padding, const char* str, const Font& font, uint skip, const uint r, const uint g, const uint b) {
unsigned int l = PIXELS / (font.width + INTERCHAR_SPACE);
if (*padding) {
sendChar(*padding, skip, r, g, b, true, font); // First char is special case because it can be stepped for smooth scrolling
while (*(++padding) && l--) {
sendChar(*padding, 0, r, g, b, true, font);
}
sendChar(*str, 0, r, g, b, true, font); // First char is special case because it can be stepped for smooth scrolling
} else {
sendChar(*str, skip, r, g, b, true, font); // First char is special case because it can be stepped for smooth scrolling
}
// Send rest of string
while (*(++str) && l--) {
sendChar(*str, 0, r, g, b, true, font);
}
}
void sendEmptyColumns(int columns) {
while (columns--) {
sendColumnRGB(0, 0, 0, 0);
}
}
static inline void
sendStringJustified(const char* s, const Font& font, uint skip, const uint r, const uint g, const uint b) {
uint startPad, endPad;
getColumnsToPadForString(s, font.width, &startPad, &endPad);
sendEmptyColumns(startPad);
sendString(s, font, skip, r, g, b);
sendEmptyColumns(endPad);
}
// Keep track of where we are in the color cycle between chars
uint* cyclingColor = (uint * )(calloc(1, sizeof(uint)));
// Send a char with a column-based color cycle
static inline void sendCharColorCycle(const Font& font, uint c,
uint skip,
uint* r, uint* g, uint* b) {
const uint* charbase = font.data + ((c - ' ') * font.width);
uint col = font.width;
while (skip--) {
charbase++;
col--;
}
while (col--) {
sendColumnRGB(pgm_read_byte_near(charbase++), *r, *g, *b);
*cyclingColor += 10;
}
// if (sendTrailingSpace) {
sendColumnRGB(0, 0, 0, 0); // Interchar space
// }
*cyclingColor += 10;
}
// Show the passed string with the arcade font and a nice vertical color cycle effect
// columnsPrefix: the # of empty columns to add before the string, useful for spacing
static inline void
sendStringColorCycle(const Font& font, const char* str, int columnsPrefix,
uint skip,
uint* r, uint* g, uint* b) {
unsigned int l = PIXELS / (font.width + INTERCHAR_SPACE);
while (columnsPrefix--) {
sendColumnRGB(0, 0x00, 0x00, 0x00);
}
sendCharColorCycle(font, *str, skip, r, g,
b); // First char is special case because it can be stepped for smooth scrolling
// Send rest of string
while (*(++str) && l--) {
sendCharColorCycle(font, *str, 0, r, g, b);
}
}
void setupDiagnosticLed() {
pinMode(diagnosticLed, OUTPUT);
}
// Set the specified pins up as digital out
void setupLeds() {
PIXEL_DDR |= onBits; // Set all used pins to output
}
void startSerial() {
// set the data rate for the SoftwareSerial port
softSerial.begin(14400);
softSerial.flush();
// Serial.begin(19200);
}
void stopSerial() {
softSerial.end();
}
void setupSerial() {
// define pin modes for tx, rx:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
}
// TODO idxToBlink that isn't always last char
void showAsInputStyle(const char* str, int idxToBlink, const char mode) {
// str = str + STRING_PADDING(FONTSTD_WIDTH) + 1; // drop initial padding
int length = strlen(str);
int shiftBy = constrain(length - MAX_CHARS_PER_PANEL, 0, length);
// Shift string from the start if it is longer than the max chars we can show,
// so that last MAX_CHARS_PER_PANEL chars are always visible (plus _)
str = str + shiftBy;
unsigned int count = 2;
clear();
idxToBlink = strlen(str) - 1;
// cli();
// // Send string, except for last char (which is '_')
// for (uint i = 0; i < length; i++) {
// int r = 0xff;
// int g = 0xff;
// int b = 0xff;
//// if (i == idxToBlink ) {
//// sendChar(str[idxToBlink], 0, r, brightness, brightness);
//// } else {
// sendChar(str[i], 0, GAMMA(r), GAMMA(g), GAMMA(b));
//// }
// }
while (count > 0) {
count--;
uint brightness = ((count % 100) * 256) / 100;
cli();
// Send string, except for last char (which is '_')
for (uint i = 0; i < length; i++) {
int r = 0xff;
int g = 0xff;
int b = 0xff;
// Blink idxToBlink char, or all chars if we're in input warning mode
if (i == idxToBlink || mode == KEYBOARD_MODE_WARNING) {
sendChar(str[i], 0, 0xff, brightness, brightness);
} else {
sendChar(str[i], 0, GAMMA(r), GAMMA(g), GAMMA(b));
}
}
sendColumnRGB(0x00, 0, 0, 0xff);
sei();
show();
}
}
void showAsChooser(const char* blinkyStr, const char* countyStr) {
// int maxStrLen = 10; // max number of chars to show //TODO or, could scroll the whole thing
// blinkyStr[maxStrLen] = 0x00; //chop here
int blinkyLen = strlen(blinkyStr);
int shiftBy = constrain(blinkyLen - MAX_CHARS_PER_PANEL - strlen(countyStr), 0, blinkyLen);
// Shift string from the start if it is longer than the max chars we can show,
// so that last MAX_CHARS_PER_PANEL chars are always visible (plus _)
// blinkyStr = blinkyStr + shiftBy;
unsigned int count = 58;
clear();
while (count > 0) {
count--;
uint brightness = GAMMA(((count % 100) * 256) / 100);
cli();
// County part
sendString(countyStr, fontStd, 0, 0x80, 0, 0);
sendColumnRGB(0x00, 0, 0, 0xff);
// Blinky part
// while (*blinkyStr) {
// for (int s = 0; s <= shiftBy; s++) {
// if (s == blinkyLen) {
// break;
// }
// for (uint step = 0; step < FONTSTD_WIDTH +
// INTERCHAR_SPACE; step++) {
// // step though each column of the 1st char for smooth scrolling
// sendString(blinkyStr, step, brightness, brightness, brightness);
sendString(blinkyStr, fontStd, 0, brightness, brightness, brightness);
// }
// blinkyStr = blinkyStr + s;
// }
sei();
// delay(MARQUEE_DELAY); // speed. higher = slower
show();
}
}
void showAsCountdownStyle(const char* countdownstr, unsigned int count = 600, boolean showCount = true) {
clear();
while (count > 0) {
count--;
uint digit1 = count / 100;
uint digit2 = (count - (digit1 * 100)) / 10;
uint digit3 = (count - (digit1 * 100) - (digit2 * 10));
uint char1 = digit1 + '0';
uint char2 = digit2 + '0';
uint char3 = digit3 + '0';
uint brightness = GAMMA(((count % 100) * 256) / 100);
cli();
if (showCount) {
sendString(countdownstr, fontStd, 0, brightness, brightness, brightness);
} else {
// align string to middle if we're not showing the countdown.
sendStringJustified(countdownstr, fontStd, 0, brightness, brightness, brightness);
}
// sendChar( '0' , 0 , 0x80, 0 , 0 );
if (showCount) {
// sendColumnRGB(0x00, 0, 0, 0xff);
//Panel A:
sendChar(char1, 0, 0x80, 0, 0);
sendChar('.', 0, 0x80, 0, 0);
sendChar(char2, 0, 0x80, 0, 0);
sendChar(char3, 0, 0x80, 0, 0, false);
//Other panel:
sendString(countdownstr, fontStd, 0, brightness, brightness, brightness);
sendChar(char1, 0, 0x80, 0, 0);
sendChar('.', 0, 0x80, 0, 0);
sendChar(char2, 0, 0x80, 0, 0);
sendChar(char3, 0, 0x80, 0, 0, false);
} else {
// show the string on the other panel too if we're not showing the count
sendStringJustified(countdownstr, fontStd, 0, brightness, brightness, brightness);
}
sei();
delayMicroseconds(COUNTDOWN_DELAY);
show();
}
count = 100;
// One last farewell blink
while (count > 0) {
count--;
uint brightness = GAMMA(((count % 100) * 256) / 100);
cli();
if (showCount) {
sendString(countdownstr, fontStd, 0, brightness, brightness, brightness);
} else {
// align string to middle if we're not showing the countdown.
sendStringJustified(countdownstr, fontStd, 0, brightness, brightness, brightness);
}
// sendColumnRGB(0x00, 0, 0, 0xff);
if (showCount) {
// sendColumnRGB(0x00, 0, 0,
// 0xff); // We need to quickly send a blank byte just to keep from missing our deadlne.
sendChar('0', 0, brightness, 0, 0);
sendChar('.', 0, brightness, 0, 0);
sendChar('0', 0, brightness, 0, 0);
sendChar('0', 0, brightness, 0, 0, false);
//Other panel:
sendString(countdownstr, fontStd, 0, brightness, brightness, brightness);
sendChar('0', 0, brightness, 0, 0);
sendChar('.', 0, brightness, 0, 0);
sendChar('0', 0, brightness, 0, 0);
sendChar('0', 0, brightness, 0, 0, false);
} else {
// show the string on the other panel too if we're not showing the count
sendStringJustified(countdownstr, fontStd, 0, brightness, brightness, brightness);
}
sei();
delayMicroseconds(COUNTDOWN_DELAY);
show();
}
}
void showAsFlashyStyle(const char* countdownstr, unsigned int time = 200) {
showAsCountdownStyle(countdownstr, time, false);
}
void showstarfieldcustom(int stars) {
const uint field = 40; // Good size for a field, must be less than 256 so counters fit in a byte
uint sectors = (PIXELS / field); // Repeating sectors makes for more stars and faster update
for (unsigned int i = 0; i < stars; i++) {
unsigned int r = random(PIXELS * 8); // Random slow, so grab one big number and we will break it down.
unsigned int x = r / 8;
uint y = r & 0x07; // We use 7 rows
uint bitmask = (2 << y); // Start at bit #1 since we never use the bottom bit
cli();
unsigned int l = x;
while (l--) {
sendColumnRGB(0, 0x00, 0x00, 0x00);
}
sendColumnRGB(bitmask, 0x40, 0x40, 0xff); // Starlight blue
l = PIXELS - x;
while (l--) {
sendColumnRGB(0, 0x00, 0x00, 0x00);
}
sei();
// show(); // Not needed - random is always slow enough to trigger a reset
}
}
void showstarfield() {
showstarfieldcustom(300);
}
static inline void sendIcon(const uint* fontbase, uint which, int8_t shift, uint width, uint r, uint g, uint b) {
const uint* charbase = fontbase + (which * width);
if (shift < 0) {
uint shiftabs = -1 * shift;
while (width--) {
uint col = pgm_read_byte_near(charbase++);
sendColumnRGB(col << shiftabs, r, g, b);
}
} else {
while (width--) {
sendColumnRGB((pgm_read_byte_near(charbase++) >> shift) & onBits, r, g, b);
}
}
}
void showCharsOneByOneOnBothPanels(const char* str, Color textColor, int delayMs = 500) {
Color tc = textColor;
clear();
uint startPad, endPad;
getColumnsToPadForString(str, FONTSTD_WIDTH, &startPad, &endPad);
for (uint p = 0; p < strlen(str); p++) {
cli();
sendEmptyColumns(startPad);
for (uint i = 0; i <= p; i++) {
sendChar(*(str + i), 0, GAMMA(tc.r), GAMMA(tc.g), GAMMA(tc.b));
}
int usedCols = startPad + (p + 1) * (FONTSTD_WIDTH + INTERCHAR_SPACE);
sendEmptyColumns(COLUMNS_PER_PANEL - usedCols);
sendEmptyColumns(startPad);
for (uint i = 0; i <= p; i++) {
sendChar(*(str + i), 0, GAMMA(tc.r), GAMMA(tc.g), GAMMA(tc.b));
}
sendEmptyColumns(COLUMNS_PER_PANEL - usedCols);
sei();
delay(CHARS_ONEBYONE_DELAY);
}
delay(delayMs);
}
void showStringColorCycleOnBothPanels(const Font& font, const char* str, int delayMs, bool alignMiddle, uint skip,
uint* r, uint* g, uint* b) { //, uint slide) {
//TODO cycle color without slide
uint slide = delayMs;
// clear();
uint startPad = 0;
uint endPad = 0;
getColumnsToPadForString(str, font.width, &startPad, &endPad);
cli();
for (slide; slide; slide -= 10) {
*cyclingColor = (slide & 0xff);
if (alignMiddle) {
sendEmptyColumns(startPad);
}
sendStringColorCycle(font, str, 0, skip, r, g, b);
if (alignMiddle) {
sendEmptyColumns(endPad);
sendEmptyColumns(startPad);
} else {
sendEmptyColumns(startPad + endPad + font.width);
}
sendStringColorCycle(font, str, 0, skip, r, g, b);
if (alignMiddle) {
sendEmptyColumns(endPad);
} else {
sendEmptyColumns(startPad + endPad + font.width);
}
show();
delay(delayMs / 200);
}
sei();
}
void showChonkySlideStyleOnBothPanels(const char* str, int delayMs = ALLYOURBASE_DELAY,
Color colorFrom = Color{0, 0, 0x80}, bool scrollToLastChar = false) {
// const char *allyourbase = "CAT: ALL YOUR BASE ARE BELONG TO US !!!";
// TODO colorFrom, colorTo support
uint g = colorFrom.g;
uint b = colorFrom.b;
// delayMs = ALLYOURBASE_DELAY + 10; //FIXME rmove
if (scrollToLastChar) {
uint charWidth = fontChonk.width;
int l = 1; //or infinite loop to end of str
while (l--) {
for (uint skip = 0; skip <= charWidth; skip++) {
showStringColorCycleOnBothPanels(fontChonk, str, delayMs, false, skip, cyclingColor, &g, &b);
}
str++;
if (*(str + 1) == '|') {
break;
}
}
} else {
showStringColorCycleOnBothPanels(fontChonk, str, delayMs, false, 0, cyclingColor, &g, &b);//1250);
}
}
void showIconInvaders(const Icon* icon, Color iconColor) {
Color ic = iconColor;
int iconWidth = pgm_read_byte_near(icon);
// char blah[4];
// itoa(iconWidth, blah, 10);
// showAsFlashyStyle(blah);
uint acount = PIXELS / (iconWidth + FONTSTD_WIDTH); // How many aliens do we have room for?
for (int8_t row = -7; row < 7; row++) { // Walk down the rows
// Walk them 6 pixels per row
// ALternate direction on each row
uint start, end, step;
if (row & 1) {
start = 1;
end = 8;
step = 1;
} else {
start = 7;
end = 0;
step = -1;
}
for (char p = start; p != end; p += step) {
// Now slowly move aliens
// work our way though the aliens moving each one to the left
cli();
// Start with margin
uint margin = p;
while (margin--) {
sendColumnRGB(0, 0x00, 0x00, 0x00);
}
for (uint l = 0; l < acount; l++) {
sendIcon(icon->data,
p & 1,
row,
iconWidth,
GAMMA(ic.r), GAMMA(ic.g), GAMMA(ic.b));
// sendChar(' ', 0, 0x00, 0x00, 0x00); // No over crowding
sendEmptyColumns(4);
}
sei();
delay(INVADER_DELAY);
}
}
// delay(200);
}
#define JAB_MAX_BRIGHTNESS 0xff //(255 (100%))
//#define JAB_MAX_BRIGHTNESS 0x7f //(127)
#define JAB_MIN_BRIGHTNESS 0x00
#define JAB_STEPS (JAB_MAX_BRIGHTNESS-JAB_MIN_BRIGHTNESS)
// Keep color step between marquee calls, but still bump the sector by 1 every call so each new message starts with a
// noticeably different color
uint sector = 1;
uint colorStep = 0;
void marquee(const char* marqueePtr, const Font& font = fontStd, bool pad = true, uint marqueeDelay = MARQUEE_DELAY) {
// Dynamically allocate memory for paddingString based on the font width
char* paddingString = (char*) malloc(STRING_PADDING(font.width) + 1);
if (!paddingString) {
// Handle memory allocation failure if necessary
return;
}
memset(paddingString, ' ', STRING_PADDING(font.width));
paddingString[STRING_PADDING(font.width)] = '\0'; // null terminate the string
const char* paddingPtr = paddingString;
if (pad) {
paddingPtr = paddingString;
} else {
paddingPtr += STRING_PADDING(font.width) / 2;
}
// Text foreground color cycle effect
sector++;
if (sector == 3) {
sector = 0;
}
float beatPct = 0.0f;
unsigned long delta;
uint charWidth;
while (*marqueePtr) {
if (colorStep == JAB_STEPS) {
colorStep = 0;
sector++;
if (sector == 3) {
sector = 0;
}
} else {
colorStep++;
}
uint rampup = GAMMA(JAB_MIN_BRIGHTNESS + colorStep);
uint rampdown = GAMMA(JAB_MIN_BRIGHTNESS + (JAB_STEPS - colorStep));
uint r0, g0, b0, r, g, b;
switch (sector) {
case 0:
r0 = rampup;
g0 = rampdown;
b0 = JAB_MIN_BRIGHTNESS;
break;
case 1:
r0 = rampdown;
g0 = JAB_MIN_BRIGHTNESS;
b0 = rampup;
break;
case 2:
r0 = JAB_MIN_BRIGHTNESS;
g0 = rampup;
b0 = rampdown;
break;
};
if (!*paddingPtr && *marqueePtr == HEART) {
charWidth = HEART_WIDTH;
} else {
charWidth = font.width;
}
for (uint step = 0; step < charWidth + INTERCHAR_SPACE; step++) {
// step though each column of the 1st char for smooth scrolling
delta = millis() - timeOfLastBeat;
// beat proportion
beatPct = constrain((float) delta / (float) FADE_MILLIS, 0.0f, 1.0f);
if (beatPct >= 1.0f) {
r = r0;
g = g0;
b = b0;
} else if (beatPct > 0.0f) {
// Fade between white and rgb depending on how long ago the beat was
r = (uint)((1.0f - beatPct) * (float) JAB_MAX_BRIGHTNESS + beatPct * (float) r0);
g = (uint)((1.0f - beatPct) * (float) JAB_MAX_BRIGHTNESS + beatPct * (float) g0);
b = (uint)((1.0f - beatPct) * (float) JAB_MAX_BRIGHTNESS + beatPct * (float) b0);
} else {
// beatPct == 0.0, meaning it just happened.
// Flash on beats
// Full white to save doing the computation cycles
r = JAB_MAX_BRIGHTNESS;
g = JAB_MAX_BRIGHTNESS;
b = JAB_MAX_BRIGHTNESS;
}
cli();
sendPaddedString(paddingPtr, marqueePtr, font, step, r, g, b);
sei();
delay(marqueeDelay); // speed. higher = slower
PORTB |= 0x01;
delay(1);
PORTB &= ~0x01;
}
if (*paddingPtr) {
paddingPtr++;
} else {
marqueePtr++;
}
}
free(paddingString);
}
// Notifies the IMX that we're ready to retrieve custom message data
bool readSerialData() {
startSerial();
// Flushing Serial input buffer
// while (softSerial.available())
// softSerial.read();
// while (isspace(Serial.peek())) {
// Serial.read();
// }
// send special symbol so IMX knows to respond with custom message data
softSerial.print('~');
// int firstByte = softSerial.read();
//
// switch (firstByte) {
//
// }
// Read the JSON document from the serial port
DeserializationError err = deserializeJson(json, softSerial);
if (err == DeserializationError::Ok) {
stopSerial();
diagnosticBlink();
return true;
} else if (err == DeserializationError::EmptyInput) {
return false;
} else {
softSerial.print(err.c_str());
// char bytes[10] = {'\0'};
// softSerial.readBytes(bytes, 1);
// softSerial.print(":");
// softSerial.print(bytes);
//
// // do I need to clear out the rest of the unread serial stream?
// while(softSerial.available()) {
// softSerial.read();
// }
stopSerial();
showAsFlashyStyle((char*) err.c_str());
diagnosticBlink();
diagnosticBlink();
diagnosticBlink();
diagnosticBlink();
return false;
}
}
int loopcount = 0;
void setup() {
// strcpy_P(currentBuffer, ASOT);
// if (DEBUG) {
// currentBuffer[10] = 0x00; //chop off intro when debugging
// }
delay(100); //Try to introduce a bit of a delay to clear out serial noise on boot
setupDiagnosticLed();
setupLeds();
showAsFlashyStyle("I", 60);
showAsFlashyStyle("LIVE", 60);
showAsFlashyStyle("FOR", 60);
showAsFlashyStyle("THAT", 60);
showAsFlashyStyle("ENERGY!", 60);
// showAsCountdownStyle("NEW\u000BMSG\u000B", 600);
showstarfield();
setupSerial();
// A0 pin change interrupt
digitalWrite(A0, HIGH); // enable pull-up
PCMSK1 |= bit(PCINT8); // want pin A0
PCIFR |= bit(PCIF1); // clear any outstanding interrupts
PCICR |= bit(PCIE1); // enable pin change interrupts for A0 to A5
loopcount = 0;
}
void loop() {
diagnosticLedOn();
bool readSuccessful = readSerialData();
diagnosticLedOff();
if (readSuccessful) {
const char* str = json["str"];
const char msgType = json["type"].as<const char*>()[0];
switch (msgType) {
case MSGTYPE_ONE_BY_ONE: {
showCharsOneByOneOnBothPanels(str, {json["r"], json["g"], json["b"]}, json["dly"]);
break;
}
case MSGTYPE_CHONKY_SLIDE: {
showChonkySlideStyleOnBothPanels(str, json["dly"], {json["r"], json["g"], json["b"]}, json["scroll"]);
break;
}
case MSGTYPE_CHOOSER: {
showAsChooser(str, json["flashy"]);
break;
}
case MSGTYPE_UTILITY: { // Utility messages
char subtype = json["subtype"].as<const char*>()[0];
switch (subtype) {
case 'M': { // Microphone enable/disable
if (*str == 'E') {
PCICR |= bit(PCIE1); // enable pin change interrupts for A0 to A5
showAsFlashyStyle("MIC ON", 200);
} else if (*str == 'D') {
PCICR &= ~bit(PCIE1); // disable pin change interrupts for A0 to A5
showAsFlashyStyle("MIC OFF", 200);
}
break;
}
case 'B': { // Brightness shift
uint shift;
if (strlen(str) == 0) {
shift = BRIGHTNESS_SHIFT;
} else {
shift = atoi(str);
}
BRIGHTNESS_SHIFT = shift;