Arduino Projekten

Sat 20-Apr-24
05:19:08


Midi Paneel

Datum: Sat 09 August 2014
Samenvatting: Midi Signalen zichtbaar gemaakt op een RGB led-Matrix - Het uiterste is wel uit een Arduino gehaald.


En toen is er het volgende idee ontstaan om de RGB Led Matrix Panelen voor te gebruuiken: "Laat Midi signalen er op zien." De foto's late zien dat dit wel succesvol is geweest.

Dit projekt is een combinatie geweest van Leds aansturen met schuifregisters, de Led-Panelen aansturing en de Midi Module. Ik had al een vermoeden dat de Arduino het geheel niet zou kunnen aansturen, maar zijn rekensnelheid is mij meegevallen.

Funkties op het Led Paneel.

De bovenste helft van de Panelen (die met de zwarte regels) is bedoeld voor Noot-afhankelijke zaken, zoals Note-On (regel 0-3) en Note-Off (ook regel 0-3), AfterTouch (regel 4-5), Controls (regel 6-7).

De onderste helft is bedoeld voor apparaat aansturingen, of te wel: Program Change (regel 8-9, blauw), Channel AfterTouch (regel 10-11, groen), PitchBend (regel 12-13, rood) en de Common Commando's (regel 14-15, blauw met paars).

  1. Note-On en Note-Off: Per noot worden 2 Led's boven elkaar gebruikt. Noot 0 ligt links onder, Noot 1 ligt erboven, Noot 2 naast Noot 0, Noot 3 boven Noot 2, en zo zigzaggend voort. De velocity van de Noten worden door kleuren voorgesteld, van zwart via donkere kleurtjes naar uiteindelijk fel wit. Hierbij is een deling gemaakt van 7 bits naar 5 bits (met een ... array).
  2. AfterTouch: Dit gaat tevens per noot (1 Led per noot) en gaat ook zigzaggend van links onder naar recht boven. De waarde zelf worden ook door kleuren voorgesteld.
  3. Controls: Ook deze gaat zigzaggend (1 Led per control nummer) via het hiervoor genoemde patroon. Oook deze waardes worden door kleuren aangegeven.
  4. Program Change: Dit is een enkele byte-waarde, zodat de Leds aan/uit gezet wordt (ook in een zigzag-patroon). Elke ProgramChange waarde heeft een andere Led, vandaar dat hiervoor ook een achtergrond-kleur (blauw) gebruikt kan worden. De Program-waarde zelf wordt in het paars getoond.
  5. Channel AfterTouch: Ook dit is een enkele byte-waarde met aan/uit (gele) Led's (op een groene achtergrond). Mijn apparatuur geeft deze waarde niet, zodat dit (nog) niet echt getest is.
  6. PitchBend: Van deze dubbele byte-waarde wordt alleen de hoogste getoond. Een (cyan) Led op zijn eigen (rode) achtergrond.

    Bij mijn vorige Midi-Keyboard genereerde de PitchBend erg veel data. Hier blijkt de Arduino prima met deze data om te kunnen gaan.

Common Commando's.

De onderste twee regels zijn aan de Common Commando's toegewezen. Deze regels zijn in 3 groepen verdeeld.

  1. Links: De Midi-kanalen worden met (paarse) Leds aangegeven (op blauw). Er is een Omni-voorziening ingebouwd.
  2. Midden: Diverse Common Ongedefinieerde Waardes ($F4, $F5, $F9, $FD) en Common Commando's, zoals de Active Sensing ($FE),
  3. Rechts: Het Midi Clock gebied, zoals de Tijdsklok (24 stuks in een maat) (24 cyan Leds op blauw - regel 14), de Maat-telling (16 cyan Leds op blauw) - regel 15), Start (rode Leds) en Stop (paarse Leds).
  4. Rechts onder: Hier is een "activity"-looplicht ingebouwd (van witte Led's op zwart), speciaal bedoeld om er zeker van te zijjn dat de Arduino zijn programma Loop() wel doorloopt, maar dat er geen Midi signaal binnen komt.

Het Programma.

Het is een monster-programma geworden van maar liefst 950 regels (inclusief commentaar-regels, maar exclusief de regels van het Letter-Font). De Led-Paneel aansturing was al groot (en is rechtstreeks overgenomen van het Font-Functies experiment), het Midi-deel is ook niet klein te noemen. En op een gegeven moment wilde het programma niet runnen na een uitbreiding. 4 Midi funkties konden toen niet meer worden verwerkt. Uiteindelijk heb ik geconcludeerd dat het door het variabelen geheugen (van maar 2kb) komt. Het definiëren van veel variabelen als constanten lijkt dit probleem hier opgelost te hebben - immers tot regel 190 is 1 en al variabelen (en constanten_ declaraties (en definities).

Veel constanten hebben te maken met kleur-geving en positie op de panelen. Veel variabelen hebben te maken met het correct weer uitzetten van de Led's.

In Pascal kunnen veel van deze variabelen in een record-type worden opgegeven. In C zal ook wel zo'n soort constructie bestaan, maar of dat veel variabelen geheugen zal besparen, geen idee.

De Setup-functie.

Zoals te verwachten doet deze functie diverse initialisatie dingen. Paneel voorbereiden, Front-Led's als output merken. Een Intro-schermpje, Midi balken "tekenen" en de seriele-poort op de juiste snelheid (van 31250 baud) instellen.

De Loop-functie.

Deze doet in het kort het volgende. Lees de waardes van de (enige gebruikte) potmeter uit. Lees een Midi-commando in. Doe de activity (die witte Led rechts onder).

Bij het inlezen van de potmeter is er ook een teller constructie gebruikt, zodat deze niet in elke lus een (vertragende) inlees-aktie hoeft uit te voeren.

De ReadMidi-functie en Decodeer-functies.

De structuur van de ReadMidi is bijna hetzelfde als van het Midi-In projekt. Er zijn twee aanroepen erin geplaatst, elk naar een decodeer-functie (DecodeComCom en DecodeCommand). Ook het Midi-kanaal checken is erbij geschreven.

De DecodeComCom is 1 groot switch-case statement. Precies waar deze voor bedoeld is. Elk Common Commando roept hier zijn eigen functie aan. Hier wordt iets gedaan met eventuele parameters van Common Commando's.

Ook de DecodeCommand is 1 groot switch-case statement, waar ook hier elk midi-commando zijn eigen functie aanroept.

Het Midi-kanaal.

Links onder op het paneel is te zien van welk Midi-kanalen er signalen binnen komen. Elk kanaal heeft zijn eigen (paarse) Led. Komen er signalen binnen van meerdere kanalen, dan knipperen hier ook meerdere (paarse) Led's (zie foto's).

Het idee van de Channel-functie is ook: Doe de Led aan (SetPoint(..)), onthou deze Led (in Old_Channel) en zet hem uit vlak voordat een nieuw kanaal moet worden aangezet (SetPoint(..)).

Wie de paarse Led's goed telt, ziet dat de kanalen 1, 2 en 10 aan zijn (vanwege een lange sluitertijd branden er meerdere tegelijk). Deze kanalen komen van (1) een Midi-keyboard, (2) de Bass Synthesizer (de TB-3 staat van de fabriek af op kanaal 2 ingesteld) en (10) een Drum-computer (kanaal 10 is (bij afspraak) bedoeld voor drum-computers en andere soorten ritme machines). Ik heb dus meer Midi-apparatuur aan de ingang hangen dan de foto's laat zien.

Midi Commando-functies

Elk commando is recht-toe recht-aan uitgewerkt in het programma. Elke groep Led's heeft zijn eigen start-regel. De Led op deze regel zelf wordt berekend uit de parameters (zoals voor de noot en snelheid).

Diverse Undef-functies ($F0 .. $F7).

Ik weet het, ik maak mij het er wat makkelijk vanaf als het aankomt op de Systeem Exclusive Commando (en de commando's ertussen). Dat komt ook omdat mijn apparatuur deze commando's niet ondersteunen en ik er dus geen ervaring mee kan opdoen.

Elke Undef-functie heeft een looplicht functie, 4 Led's die na elkaar aan en uit gaan. Meerdere leds geven beter een wisselend signaal aan dan maar 1 Led dat sneller aan kan gaan dan het uit is.

De Midi Rhythm- en Midi Tempo-functie.

Drum-computers en Sequencers kunnen hun eigen snelheid bepalen en kunnen deze ook doorsturen om andere Midi-apparatuur mee te synchroniseren. Dit wordt gedaan via het Common Commando $F8. De MidiRhythm-functie telt eenvoudig deze $F8-commando's en laat hiermee een looplicht van 24 Leds voortgaan. Deze is rechts onder op het paneel.

Na elke 6 commando's roept de MidiRhythm-functie de MidiTempo-functie aan, die een looplicht van 16 Leds laat voortgaan, onder die van het Rhythm-looplicht.

Waarom deze waardes 24 en 16? Midi schrijft voor dat er 24 $F8 commando's in een maat zitten. Bij veel hedendaagse muziek is een maat verdeeld in 2/8 (van elk 12 * $F8) of 4/16 stukjes (van elk 6 * $F8). Een 4-kwarts maat heeft dus 16/16 stukjes.

De deling 24 op 6 is vast in dit programma ingebouwd en dus niet via een potmeter veranderbaar gemaakt.

De Start en Stop functies.

Drumcomputers en Sequencers moeten te starten en te stoppen zijn. Hiervoor zijn aparte Midi commando's (resp. $FA en $FC). Hoewel de Tijdklok gewoon door blijft lopen, resulteert het opnieuw Starten wel in het opnieuw synchroniseren van deze tijdklok.

Een ontvangend apparaat gebruikt de Start ook om zijn eigen klok te "resetten". Hier wordt dat ook gedaan, door beide looplichten (Rhythm en Tempo) op NUL te laten beginnen.

Bij een Stop zal de Rhythm-looplicht wel door blijven wandelen, maar het Tempo-looplicht zal stoppen.

Bij het Starten worden de balkjes om het Tempo-looplicht rood gemaakt. Bij het Stoppen worden deze balkjes paars.

Bij het Midi-Commando Continue ($FB) worden deze Balkjes voor de helft rood. En hoewel het Rhythm-looplicht door blijft wandelen na een Stop, gaat het Tempo-looplicht verder vanaf het punt waar het gestopt is.

Active-Sensing.

Zoals in het Midi-In projekt al is geschreven, heb ik bij mijn Midi-Keyboard een "storend" signaal geconstateerd, namelijk Active Sensing ($FE). Ook dit commando is op het paneel zichtbaar gemaakt in de vorm van een paars looplicht, links van het Tempo-looplicht. Vanwege een lange sluitertijd zijn ze (op de foto's) alle 4 aan.

Overigens wordt deze Active Sensing ook door mijn TB-3 gegenereerd.

Het Led Matrix Paneel gedeelte.

Deze (laatse 300 regels van het programma) is al uitgebreid beschreven bij de Paneel-projekten (Font-Functies). Hier is ook niets aan veranderd.

De Front Leds.

De 16 Leds op de voorzijde van de experimenteer-behuizing laten (met de functie Status) de binaire waardes zien van de Midi-parameters, die met de Commando's worden verstuurd.

Projekt Conclusie.

Na de vorige projekten (Led-Paneel uitzoeken, Midi-In en Midi-Out uitzoeken), is dit projekt relatief snel uitgevoerd. Misschien omdat er niets extra gesoldeerd hoefde te worden, misschien omdat de overige programma-delen recht-toe recht-aan zijn en misschien omdat het idee al weken in mijn hoofd zat te borrelen.

Al met al is dit projekt 1 groot succes te noemen en rest er eigenlijk nog 1 ding ... namelijk dit projekt zijn eigen Arduino en zijn eigen behuizing te geven, zodat het altjd gebruikt klaar is om Midi signalen uit te zoeken.

Ik heb nu al veel zin om de Sid Synthesizer te ontwerpen, te bouwen en te programmeren. Misschien met de Arduino Mega, want die heeft lekker veel in- en uitgangen.


Broncode: [1: Midi Paneel]

1: De broncode van Midi Paneel

(Sat 09 August 2014) Midi Signalen op een RGB Led-Matrix (dit programma is ruim 950 regels lang - bijna 9kb aan programma code wordt er gegenereerd)

Deze broncode heeft 944 regels met programma-code.
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
//     ---------------------------------------------------------
//     | Midi and RGBMatrix : Shows Midi data on the RGBMatrix |
//     ---------------------------------------------------------

//--------[ Needed Include Files ] -------------------------------------------
// Include the beautiful Font
#include "<drive>\<directory>\FontFunc\cbmfont.c"
// Next line is needed for PROGMEM and pgm_read_byte_near-function
#include <avr/pgmspace.h>

//=========================================================
// Some Pot-vars
const byte Pot4        = A3; // Potmeter 4
const byte Pot5        = A4; // Potmeter 5
const byte ChannelPot  = A5; // Potmeter 6
byte       PickChannel =  0; // The Midi Channel to listen to
// Activity Vars
byte       Old_Activity      =  0;
byte       Activity_Count    =  0;
word       Activity_TimeOut  =  0;
const byte ActivityX         = 60;
const byte ActivityY         = 15;
const word ActivityColor     = 0x0111;
const word ActivityBGColor   = 0x0000;
const word Activity_CountOut = 5000;
//=========================================================
// Some Midi Vars
byte       SerialByte  = 0;
byte       MidiChannel = 0;
byte       MidiCommand = 0;
byte       MidiComCom  = 0;
byte       MidiParm1   = 0;
byte       MidiParm2   = 0;
byte       AantalParms = 0;
byte       ParmCount   = 0;
// Array with Number of Parameters of a Command
const byte AantalMidiParms [16] = {0,0,0,0,0,0,0,0,2,2,2,2,1,1,2,0};
// Note On Colors
const word NoteColors [32] = {0x0000,0x0001,0x0002,0x0003,
                              0x0004,0x0005,0x0006,0x0007,
                              0x0010,0x0030,0x0050,0x0070,
                              0x0011,0x0033,0x0055,0x0077,
                              0x0100,0x0300,0x0500,0x0700,
                              0x0101,0x0303,0x0505,0x0707,
                              0x0110,0x0330,0x0550,0x0770,
                              0x0111,0x0333,0x0555,0x0777
                             };
//=========================================================
// Midi Counters, Coordinations & Colors
// Channel
byte       Old_Channel     =  0;
word       Pot_Count       =  0;
const word Pot_TimeOut     = 1000;
const byte ChannelX        =  0;
const byte ChannelY        = 14;
const byte ChannelPotY     = 15;
const word ChannelColor    = 0x0707;
const word ChannelPotColor = 0x0737;
const word ChannelBGColor  = 0x0001;
// $80 & $90 Note On & Note Off ==> 4 lines
const byte NoteY =  0;
// $A0 AfterTouch ==> 2 lines
const byte AfterY =  4;
// $B0 Control Change ==> 2 lines
const byte ControlY =  6;
// $C0 ProgramChange ==> 2 lines
byte       Old_ProgChangeX   =  0;
byte       Old_ProgChangeY   =  0;
const byte ProgChangeY       =  8;
const word ProgChangeColor   = 0x0707;
const word ProgChangeBGColor = 0x0001;
// $D0 Channel AfterTouch ==> 2 lines
byte       Old_ChAfterX   =  0;
byte       Old_ChAfterY   =  0;
const byte ChAfterY       = 10;
const word ChAfterColor   = 0x0770;
const word ChAfterBGColor = 0x0010;
// $E0 PitchBend ==> 2 lines
byte       Old_PitchBendX   =  0;
byte       Old_PitchBendY   =  0;
const byte PitchBendY       = 12;
const word PitchBendColor   = 0x0077;
const word PitchBendBGColor = 0x0100;
//=========================================================
// Common Commands
// $F0 System Exclusive Start
byte       Old_UndefF0_Count =  0;
byte       UndefF0_Count     =  0;
const byte UndefF0X          = 16;
const byte UndefF0Y          = 14;
const word UndefF0Color      = 0x0100;
const word UndefF0BGColor    = 0x0000;
// $F1 Time Code
byte       Old_UndefF1_Count =  0;
byte       UndefF1_Count     =  0;
const byte UndefF1X          = 20;
const byte UndefF1Y          = 14;
const word UndefF1Color      = 0x0100;
const word UndefF1BGColor    = 0x0000;
// $F2 Song Position
byte       Old_UndefF2_Count =  0;
byte       UndefF2_Count     =  0;
const byte UndefF2X          = 24;
const byte UndefF2Y          = 14;
const word UndefF2Color      = 0x0100;
const word UndefF2BGColor    = 0x0000;
// $F3 Choose Song
byte       Old_UndefF3_Count =  0;
byte       UndefF3_Count     =  0;
const byte UndefF3X          = 28;
const byte UndefF3Y          = 14;
const word UndefF3Color      = 0x0100;
const word UndefF3BGColor    = 0x0000;
// $F4 UndefF4
byte       Old_UndefF4_Count =  0;
byte       UndefF4_Count     =  0;
const byte UndefF4X          = 16;
const byte UndefF4Y          = 15;
const word UndefF4Color      = 0x0100;
const word UndefF4BGColor    = 0x0000;
// $F5 UndefF5
byte       Old_UndefF5_Count =  0;
byte       UndefF5_Count     =  0;
const byte UndefF5X          = 20;
const byte UndefF5Y          = 15;
const word UndefF5Color      = 0x0100;
const word UndefF5BGColor    = 0x0000;
// $F6 Tune Request
byte       Old_UndefF6_Count =  0;
byte       UndefF6_Count     =  0;
const byte UndefF6X          = 24;
const byte UndefF6Y          = 15;
const word UndefF6Color      = 0x0100;
const word UndefF6BGColor    = 0x0000;
// $F7 System Exclusive End
byte       Old_UndefF7_Count =  0;
byte       UndefF7_Count     =  0;
const byte UndefF7X          = 28;
const byte UndefF7Y          = 15;
const word UndefF7Color      = 0x0100;
const word UndefF7BGColor    = 0x0000;
//=========================================================
// $F8 Basic Tempo Indications (24 beats per beat)
byte       Old_Rhythm_Count =  0;
byte       Rhythm_Count     =  0;
const byte RhythmX          = 40;
const byte RhythmY          = 14;
const word RhythmColor      = 0x0047;
const word RhythmBGColor    = 0x0001;
// $F8 Extended Tempo Indications
byte       Old_Tempo_Count =  0;
byte       Tempo_Count     =  0;
byte       Tempo_Walk      =  0;
const byte TempoX          = 44;
const byte TempoY          = 15;
const word TempoColor      = 0x0272;
const word TempoBGColor    = 0x0001;
// $F9 UndefF9
byte       Old_UndefF9_Count =  0;
byte       UndefF9_Count     =  0;
const byte UndefF9X          = 32;
const byte UndefF9Y          = 14;
const word UndefF9Color      = 0x0100;
const word UndefF9BGColor    = 0x0000;
// $FA & $FB & $FC Start & Continue & Stop
const word StartColor      = 0x0700;
const word StopColor       = 0x0101;
// $FD UndefFD
byte       Old_UndefFD_Count =  0;
byte       UndefFD_Count     =  0;
const byte UndefFDX          = 32;
const byte UndefFDY          = 15;
const word UndefFDColor      = 0x0100;
const word UndefFDBGColor    = 0x0000;
// $FE Active Sensing
byte       Old_Active_Count =  0;
byte       Active_Count     =  0;
const byte ActiveX          = 36;
const byte ActiveY          = 15;
const word ActiveColor      = 0x0303;
const word ActiveBGColor    = 0x0000;
// $FF Reset
byte       Old_Reset_Count =  0;
byte       Reset_Count     =  0;
const byte ResetX          = 12;
const byte ResetY          = 15;
const word ResetColor      = 0x0777;
const word ResetBGColor    = 0x0000;

//==============================================================================
//== Setup and Loop                                                           ==
//==============================================================================

void setup()
{ InitPan(0x0000);
  Init_Led();
  font_intro();
  ClrPan();
  Init_Midi();
  Serial.begin(31250);
}

void loop()
{ ReadPots();
  ReadMidi();
  if ( Activity_TimeOut >= Activity_CountOut )
     { // Running Light for Activity
       SetPoint(ActivityX + Old_Activity  , ActivityY, ActivityBGColor);
       SetPoint(ActivityX + Activity_Count, ActivityY, ActivityColor  );
       Old_Activity = Activity_Count;
       Activity_Count = (Activity_Count + 1) & 0x03;
       Activity_TimeOut = 0;
     }
  else
     { Activity_TimeOut = Activity_TimeOut + 1;
     }
}

void ReadPots()
{ if (Pot_Count < Pot_TimeOut)
     { Pot_Count = Pot_Count + 1;
     }
  else
     { Pot_Count = 0;
       SetPoint(ChannelX+PickChannel, ChannelPotY, ChannelBGColor);
       PickChannel = analogRead(ChannelPot) / 61;
       SetPoint(ChannelX+PickChannel, ChannelPotY, ChannelPotColor);
     }
}

//==============================================================================
//==  Midi Panel Introduction                                                 ==
//==============================================================================

void font_intro()
{ // return;
  // Write "Midi" & Panel"
  Status(0xF0,0x0F);
  CharColor (0x0013);
  GotoXY (4,0);
  PrintChar ("M i d i");
  GotoXY (4,8);
  PrintChar (" Panel ");
  delay( 1000);
  Status(0x0F,0xF0);
  GotoXY (0,0);
  PrintChar ("  Midi  ");
  delay( 1000);
  Status(0x00,0x00);
}

//==============================================================================
//==  Midi Read Functions                                                     ==
//==============================================================================

void Init_Led()
{ pinMode(11,OUTPUT);  // Clock Shift Register
  pinMode(12,OUTPUT);  // Data  Shift Register
  pinMode(13,OUTPUT);  // Latch Shift Register
}

void Status(byte S1, byte S2)
{ digitalWrite(13, LOW);            // Hold Data
  shiftOut( 11, 12, MSBFIRST, S1);  // Send first byte
  shiftOut( 11, 12, MSBFIRST, S2);  // Send second byte
  digitalWrite(13, HIGH);           // Show Data
}

void Init_Midi()
{ // Some Background Lines
  for (byte qq=0; qq<64; qq=qq+1)
    { // Program Change BGColor
      SetPoint(0 + qq, 0 + ProgChangeY, ProgChangeBGColor);
      SetPoint(0 + qq, 1 + ProgChangeY, ProgChangeBGColor);
      // Channel AfterTouch BGColor
      SetPoint(0 + qq, 0 + ChAfterY, ChAfterBGColor);
      SetPoint(0 + qq, 1 + ChAfterY, ChAfterBGColor);
      // Pitchbend BGColor
      SetPoint(0 + qq, 0 + PitchBendY, PitchBendBGColor);
      SetPoint(0 + qq, 1 + PitchBendY, PitchBendBGColor);
    }
  // Channel BGColor
  for (byte qq=0; qq<16; qq=qq+1)
    { SetPoint(ChannelX + qq, ChannelY   , ChannelBGColor);
      SetPoint(ChannelX + qq, ChannelPotY, ChannelBGColor);
    }
  // Rhythm Leds BGColor
  for (byte qq=0; qq<24; qq=qq+1)
    { SetPoint(RhythmX + qq, RhythmY ,RhythmBGColor);
    }
  // Tempo Leds BGColor
  for (byte qq=0; qq<16; qq=qq+1)
    { SetPoint(TempoX + qq, TempoY ,TempoBGColor);
    }
}

void ReadMidi()
{ if (Serial.available() > 0)
     {  Activity_TimeOut = 0;
        SerialByte = Serial.read();
        if (SerialByte & 0x80) != 0)
           { // Its a Command Byte
             if ( SerialByte >= 0xF0 )  // System Common Commando
                { MidiComCom = SerialByte;
                  DecodeComCom(MidiComCom);
                }
             else
                { MidiComCom = 0;  // Its No Common Commando, So Clear it
                  ParmCount=0;
                  // Split Channel and Command
                  MidiChannel = ( SerialByte & 0x0F );
                  MidiCommand = ( SerialByte & 0xF0 );
                  // Get the Number of Parms of the Command.
                  AantalParms = AantalMidiParms[ MidiCommand >> 4 ];
                }
           }
        else
           { // Its a Parameter Byte
             MidiComCom = 0;
             if (ParmCount < AantalParms)
                { // Count the number of Parameters
                  ParmCount = ParmCount + 1;
                  if (ParmCount == 1)
                     { MidiParm1 = SerialByte;  // Read First Parameter
                       MidiParm2 = 0;
                     }
                  if (ParmCount == 2)
                     { MidiParm2 = SerialByte;  // Read Second Parameter
                     }
                }
             if (ParmCount == AantalParms)
                { // When All Parms are read, set the counter to NUL.
                  // Maybe only Parms of the next (but same) Command is comming.
                  ParmCount = 0;
                  Status(MidiParm1,MidiParm2);  // Sent (both) Parms to Front-Leds
                  Channel (MidiChannel);        // Do Channel Leds

                  // Check the Midi Channel (>=$10 is Omni)
                  if ( PickChannel >=0x10 ) || ( MidiChannel == PickChannel ) )
                     { // The Channel does match
                       DecodeCommand (MidiCommand, MidiParm1, MidiParm2);
                     }
                }
           }
     }
}

void DecodeComCom(byte ComCom)
{ switch (ComCom)
    { case 0xF0 : // System Exclusive Start
                  MidiUndefF0();
                  break;
      case 0xF1 : // Time Code
                  MidiUndefF1();
                  break;
      case 0xF2 : // Song Position
                  MidiUndefF2();
                  break;
      case 0xF3 : // Choose Song
                  MidiUndefF3();
                  break;
      case 0xF4 : // Undef
                  MidiUndefF4();
                  break;
      case 0xF5 : // Undef
                  MidiUndefF5();
                  break;
      case 0xF6 : // Tune Request
                  MidiUndefF6();
                  break;
      case 0xF7 : // System Exclusive End
                  MidiUndefF7();
                  break;
      case 0xF8 : // Timeclock
                  MidiRhythm();
                  break;
      case 0xF9 : // Undef
                  MidiUndefF9();
                  break;
      case 0xFA : // Start
                  MidiStart();
                  break;
      case 0xFB : // Continue
                  MidiContinue();
                  break;
      case 0xFC : // Stop
                  MidiStop();
                  break;
      case 0xFD : // Undef
                  MidiUndefFD();
                  break;
      case 0xFE : // Active Sensing
                  MidiActive();
                  break;
      case 0xFF : // Reset
                  MidiReset();
                  break;
    }
}

void DecodeCommand (byte Com, byte Parm1, byte Parm2)
{ switch (Com)
    { case 0x80 : // Note Off
                  NoteOff (Parm1, Parm2);
                  break;
      case 0x90 : // Note On
                  NoteOn (Parm1, Parm2);
                  break;
      case 0xA0 : // AfterTouch
                  AfterTouch (Parm1, Parm2);
                  break;
      case 0xB0 : // Control Change
                  Control (Parm1, Parm2);
                  break;
      case 0xC0 : // Program Change
                  ProgChange (Parm1, Parm2);
                  break;
      case 0xD0 : // Channel AfterTouch
                  ChAfter (Parm1, Parm2);
                  break;
      case 0xE0 : // PitchBend
                  PitchBend (Parm1, Parm2);
                  break;
    }
}

//==============================================================================
//==  Midi Note/ Music Functions                                              ==
//==============================================================================

void Channel(byte Ch)
{ // Channel
  SetPoint(ChannelX + Old_Channel, ChannelY, ChannelBGColor);
  SetPoint(ChannelX + Ch         , ChannelY, ChannelColor);
  Old_Channel = Ch;
}

void NoteOn (byte P1, byte P2)
{ // $80 Note On
  NoteSmall (P1,P2,NoteColors[ P2 >> 2 ]);
}

void NoteOff (byte P1, byte P2)
{ // $90 Note Off
  NoteSmall (P1,P2,0x0000);
}

void NoteSmall (byte P1, byte P2, word CC)
{ // $80 / $90 Note On/Off - but small 2x1 lines.
  byte P1x = P1 >> 1;
  byte P1y = 2 * (( P1 & 1 ) xor 1);
  SetPoint(0 + P1x, NoteY + 0 + P1y, CC);
  SetPoint(0 + P1x, NoteY + 1 + P1y, CC);
}

void AfterTouch (byte P1, byte P2)
{ // $A0 AfterTouch
  byte P1x = P1 >> 1;
  byte P1y = (( P1 & 1 ) xor 1);
  SetPoint(0 + P1x, AfterY + 0 + P1y, NoteColors[ P2 >> 2 ] );
}

void Control (byte P1, byte P2)
{ // $B0 Control Change
  // Check for Special Control Messages
  if (P1 == 0x78) || (P1 == 0x7A) || (P1 == 0x7B) )
     { // All Notes Off
       for ( byte qq=0; qq<=63; qq=qq+1)
           { SetPoint(qq, NoteY + 0, 0x0000);
             SetPoint(qq, NoteY + 1, 0x0000);
             SetPoint(qq, NoteY + 2, 0x0000);
             SetPoint(qq, NoteY + 3, 0x0000);
           }
     }
  if (P1 == 0x79)
     { // All Notes Off
       for ( byte qq=0; qq<=63; qq=qq+1)
           { SetPoint(qq, ControlY + 0, 0x0000);
             SetPoint(qq, ControlY + 1, 0x0000);
           }
     }
  byte P1x = P1 >> 1;
  byte P1y = (( P1 & 1 ) xor 1);
  SetPoint(0 + P1x, ControlY + 0 + P1y, NoteColors[ P2 >> 2 ] );
}

void ProgChange (byte P1, byte PQ)
{ // $C0 ProgramChange
  byte P1x = P1 >> 1;
  byte P1y = (( P1 & 1 ) xor 1);
  SetPoint(Old_ProgChangeX, ProgChangeY + Old_ProgChangeY , ProgChangeBGColor);
  SetPoint(0 + P1x        , ProgChangeY + P1y             , ProgChangeColor  );
  Old_ProgChangeX = P1x;
  Old_ProgChangeY = P1y;
}

void ChAfter (byte P1, byte PQ)
{ // $D0 Channel AfterTouch
  byte P1x = P1 >> 1;
  byte P1y = (( P1 & 1 ) xor 1);
  SetPoint(Old_ChAfterX, ChAfterY + Old_ChAfterY , ChAfterBGColor);
  SetPoint(0 + P1x     , ChAfterY + P1y          , ChAfterColor  );
  Old_ChAfterX = P1x;
  Old_ChAfterY = P1y;
}

void PitchBend (byte P1, byte P2)
{ // $E0 PitchBend
  byte P2x = P2 >> 1;
  byte P2y = (( P2 & 1 ) xor 1);
  SetPoint(Old_PitchBendX, PitchBendY + Old_PitchBendY , PitchBendBGColor);
  SetPoint(0 + P2x       , PitchBendY + P2y            , PitchBendColor  );
  Old_PitchBendX = P2x;
  Old_PitchBendY = P2y;
}

//==============================================================================
//==  Midi Rhythm Functions                                                   ==
//==============================================================================

void MidiUndefF0()
{ // $F0
  SetPoint(UndefF0X + Old_UndefF0_Count, UndefF0Y ,UndefF0BGColor);
  SetPoint(UndefF0X + UndefF0_Count    , UndefF0Y ,UndefF0Color  );
  Old_UndefF0_Count =  UndefF0_Count;
  if ( UndefF0_Count < 3 )  { UndefF0_Count = UndefF0_Count + 1; }
  else                      { UndefF0_Count = 0;                 }
}

void MidiUndefF1()
{ // $F1
  SetPoint(UndefF1X + Old_UndefF1_Count, UndefF1Y ,UndefF1BGColor);
  SetPoint(UndefF1X + UndefF1_Count    , UndefF1Y ,UndefF1Color  );
  Old_UndefF1_Count =  UndefF1_Count;
  if ( UndefF1_Count < 3 )  { UndefF1_Count = UndefF1_Count + 1; }
  else                      { UndefF1_Count = 0;                 }
}

void MidiUndefF2()
{ // $F2
  SetPoint(UndefF2X + Old_UndefF2_Count, UndefF2Y ,UndefF2BGColor);
  SetPoint(UndefF2X + UndefF2_Count    , UndefF2Y ,UndefF2Color  );
  Old_UndefF2_Count =  UndefF2_Count;
  if ( UndefF2_Count < 3 )  { UndefF2_Count = UndefF2_Count + 1; }
  else                      { UndefF2_Count = 0;                 }
}

void MidiUndefF3()
{ // $F3
  SetPoint(UndefF3X + Old_UndefF3_Count, UndefF3Y ,UndefF3BGColor);
  SetPoint(UndefF3X + UndefF3_Count    , UndefF3Y ,UndefF3Color  );
  Old_UndefF3_Count =  UndefF3_Count;
  if ( UndefF3_Count < 3 )  { UndefF3_Count = UndefF3_Count + 1; }
  else                      { UndefF3_Count = 0;                 }
}

void MidiUndefF4()
{ // $F4
  SetPoint(UndefF4X + Old_UndefF4_Count, UndefF4Y ,UndefF4BGColor);
  SetPoint(UndefF4X + UndefF4_Count    , UndefF4Y ,UndefF4Color  );
  Old_UndefF4_Count =  UndefF4_Count;
  if ( UndefF4_Count < 3 )  { UndefF4_Count = UndefF4_Count + 1; }
  else                      { UndefF4_Count = 0;                 }
}

void MidiUndefF5()
{ // $F5
  SetPoint(UndefF5X + Old_UndefF5_Count, UndefF5Y ,UndefF5BGColor);
  SetPoint(UndefF5X + UndefF5_Count    , UndefF5Y ,UndefF5Color  );
  Old_UndefF5_Count =  UndefF5_Count;
  if ( UndefF5_Count < 3 )  { UndefF5_Count = UndefF5_Count + 1; }
  else                      { UndefF5_Count = 0;                 }
}

void MidiUndefF6()
{ // $F6
  SetPoint(UndefF6X + Old_UndefF6_Count, UndefF6Y ,UndefF6BGColor);
  SetPoint(UndefF6X + UndefF6_Count    , UndefF6Y ,UndefF6Color  );
  Old_UndefF6_Count =  UndefF6_Count;
  if ( UndefF6_Count < 3 )  { UndefF6_Count = UndefF6_Count + 1; }
  else                      { UndefF6_Count = 0;                 }
}

void MidiUndefF7()
{ // $F7
  SetPoint(UndefF7X + Old_UndefF7_Count, UndefF7Y ,UndefF7BGColor);
  SetPoint(UndefF7X + UndefF7_Count    , UndefF7Y ,UndefF7Color  );
  Old_UndefF7_Count =  UndefF7_Count;
  if ( UndefF7_Count < 3 )  { UndefF7_Count = UndefF7_Count + 1; }
  else                      { UndefF7_Count = 0;                 }
}

void MidiRhythm()
{ // $F8 Basic Clock
  if (Rhythm_Count % 6 ) == 0)         {  MidiTempo();       }
  SetPoint(RhythmX + Old_Rhythm_Count, RhythmY ,RhythmBGColor);
  SetPoint(RhythmX + Rhythm_Count    , RhythmY ,RhythmColor  );
  Old_Rhythm_Count =  Rhythm_Count;
  if ( Rhythm_Count < 23 )  { Rhythm_Count = Rhythm_Count + 1; }
  else                      { Rhythm_Count = 0;                }
}

void MidiTempo()
{ // $F8 Extended Clock
  SetPoint(TempoX + Old_Tempo_Count, TempoY ,TempoBGColor);
  SetPoint(TempoX + Tempo_Count    , TempoY ,TempoColor  );
  Old_Tempo_Count =  Tempo_Count;
  if ( Tempo_Count < 15 )  { Tempo_Count = Tempo_Count + Tempo_Walk; }
  else                     { Tempo_Count = 0;                        }
}

void MidiUndefF9()
{ // $F9
  SetPoint(UndefF9X + Old_UndefF9_Count, UndefF9Y ,UndefF9BGColor);
  SetPoint(UndefF9X + UndefF9_Count    , UndefF9Y ,UndefF9Color  );
  Old_UndefF9_Count =  UndefF9_Count;
  if ( UndefF9_Count < 3 )  { UndefF9_Count = UndefF9_Count + 1; }
  else                      { UndefF9_Count = 0;                 }
}

void MidiStart()
{ // $FA
  for ( byte qq=0; qq<4; qq=qq+1)
    { SetPoint(TempoX -  4 + qq, TempoY ,StartColor);
      SetPoint(TempoX + 16 + qq, TempoY ,StartColor);
    }
  Rhythm_Count = 0;
  Tempo_Count  = 0;
  Tempo_Walk   = 1;
}

void MidiContinue()
{ // $FB
  for ( byte qq=0; qq<2; qq=qq+1)
    { SetPoint(TempoX -  4 + qq, TempoY ,StartColor);
      SetPoint(TempoX + 18 + qq, TempoY ,StartColor);
    }
  Tempo_Walk   = 1;
}

void MidiStop()
{ // $FC
  for ( byte qq=0; qq<4; qq=qq+1)
    { SetPoint(TempoX -  4 + qq, TempoY ,StopColor);
      SetPoint(TempoX + 16 + qq, TempoY ,StopColor);
    }
  Tempo_Walk   = 0;
}

void MidiUndefFD()
{ // $FD
  SetPoint(UndefFDX + Old_UndefFD_Count, UndefFDY ,UndefFDBGColor);
  SetPoint(UndefFDX + UndefFD_Count    , UndefFDY ,UndefFDColor  );
  Old_UndefFD_Count =  UndefFD_Count;
  if ( UndefFD_Count < 3 )  { UndefFD_Count = UndefFD_Count + 1; }
  else                      { UndefFD_Count = 0;                 }
}

void MidiActive()
{ // $FE
  SetPoint(ActiveX + Old_Active_Count, ActiveY ,ActiveBGColor);
  SetPoint(ActiveX + Active_Count    , ActiveY ,ActiveColor  );
  Old_Active_Count =  Active_Count;
  if ( Active_Count < 3 )  { Active_Count = Active_Count + 1; }
  else                     { Active_Count = 0;                }
}

void MidiReset()
{ // $FF
  SetPoint(ResetX + Old_Reset_Count, ResetY ,ResetBGColor);
  SetPoint(ResetX + Reset_Count    , ResetY ,ResetColor  );
  Old_Reset_Count =  Reset_Count;
  if ( Reset_Count < 3 )  { Reset_Count = Reset_Count + 1; }
  else                    { Reset_Count = 0;               }
}

//==============================================================================
//==  Matrix Panel Vars and Functions                                         ==
//==============================================================================
// Paneel Vars.
const byte MaxCol = 64;
const byte MaxRow = 16;
volatile byte PanelData [MaxCol][8][3];   // = [00..63],[0..7],[0..2] of (Rl Gl Bl Ru Gu Bu 0 0)
volatile byte RowCount = 0;
// Brightness Vars.
const byte MaxBrightCount = 9;    // Count 1..MaxBrightCount
const byte BrightRow [MaxBrightCount]  = { 0,2,2,1,2,2,1,2,2}; // Counter Conversion for Binairy Code Modulation.
volatile byte BrightCount =  0;
// BackGround-Color (format: 0x0RGB = B 0000 RRRR GGGG BBBB)
word _bgcolor = 0x0000;
// Font Vars
const byte _bitjes[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
word _fontcolor = 0x0333;
byte _fontreverse = 0;
byte _fonttransparant = 0;
int _font_x = 0;
int _font_y = 0;

// RGB Pin Definitions - All Pins are hardcoded to speed things up.
// const int a   = A0;  // Address A (A0)
// const int b   = A1;  // Address B (A1)
// const int c   = A2;  // Address C (A2)
// const int b1  =  2;  // Blue 1
// const int g1  =  3;  // Green 1
// const int r1  =  4;  // Red 1
// const int b2  =  5;  // Blue 2
// const int g2  =  6;  // Green 2
// const int r2  =  7;  // Red 2
// const int clk =  8;  // Clock
// const int oe  =  9;  // outEnable
// const int lat = 10;  // Latch

void InitPan(word _k)
// Initializing the Panel
// Input: _k = BackGround-Color (format: 0x0RGB = B 0000 0RRR 0GGG 0BBB)
{ // Put Pins to Output (A0 A1 A2 - D2 D3 D4 - D5 D6 D7 - D8 D9 D10)
  DDRC = DDRC | B00000111;    // pinMode(a  , OUTPUT); pinMode(b  , OUTPUT); pinMode(c  , OUTPUT);
  DDRD = DDRD | B11111100;    // pinMode(r1 , OUTPUT); pinMode(g1 , OUTPUT); pinMode(b1 , OUTPUT);
  // pinMode(r2 , OUTPUT); pinMode(g2 , OUTPUT); pinMode(b2 , OUTPUT);
  DDRB = DDRB | B00000111;    // pinMode(clk, OUTPUT); pinMode(oe , OUTPUT); pinMode(lat, OUTPUT);
  Setbgcolor(_k);
  ClrPan();
  InitTimer();
}

void InitTimer()
{ // Initialize Timer1
  cli();                        // Disable Interrupts
  // Timer Counter Control Registers 1A & 1B
  TCCR1A = 0;                   // Make all bits of this register to 0
  TCCR1B = 0;                   // Make all bits of this register to 0

  OCR1A = 4400;                 // set Output Compare Register 1 to the desired timer count
  TCCR1B = TCCR1B | B00001000;  // turn on CTC mode (WGM12 = bit 3 - Waveform Generation Mode)
  TCCR1B = TCCR1B | B00000001;  // Set the Prescaler (CS12 CS11 CS10 = bit 2..0 - Clock Select)

  TIMSK1 = TIMSK1 | B00000010;  // set Timer Interupt MaSK 1 to compare interrupt
  // (OCIE1A = bit 1 - Output Compare match Interrupt Enable 1A)
  sei();                        // Enable Interrupts
}

void Setbgcolor(word _k)
// Set the BackGround-Color (format: 0x0RGB = B 0000 0RRR 0GGG 0BBB)
{ _bgcolor = _k & 0x0777;
}

word bgcolor(void)
{ return _bgcolor;
}

void ClrPan()
// Clear the Panel with bgcolor
{ byte _lc = (  LowColor(bgcolor()) * 9 ) << 2;
  byte _mc = (  MidColor(bgcolor()) * 9 ) << 2;
  byte _hc = ( HighColor(bgcolor()) * 9 ) << 2;
  for (byte ww=0; ww<8; ww=ww+1)
      { for (byte qq=0; qq<MaxCol; qq=qq+1)
            { PanelData [qq][ww][0] = _lc;
              PanelData [qq][ww][1] = _mc;
              PanelData [qq][ww][2] = _hc;
            }
      }
_fontreverse = 0;
_fonttransparant = 0;
_font_x = 0;
_font_y = 0;
}

byte LowColor (word _k)
// Get the LowColor rgb from 0000 0RRR 0GGG 0BBB
{ return (( _k & 0x0100 ) >> 6 ) + (( _k & 0x0010 ) >> 3 ) + (( _k & 0x0001 )      );
}

byte MidColor (word _k)
// Get the MidColor RGB from 0000 0RRR 0GGG 0BBB
{ return (( _k & 0x0200 ) >> 7 ) + (( _k & 0x0020 ) >> 4 ) + (( _k & 0x0002 ) >> 1 );
}

byte HighColor (word _k)
// Get the HighColor RGB from 0000 0RRR 0GGG 0BBB
{ return (( _k & 0x0400 ) >> 8 ) + (( _k & 0x0040 ) >> 5 ) + (( _k & 0x0004 ) >> 2 );
}

word Dec2RGB (word _k)
// Convert a decimal color into a hex RGB-color 0000 0RRR 0GGG 0BBB
{ return (( _k & 0x01C0 ) << 2 ) + (( _k & 0x0038 ) << 1 ) + ( _k & 0x0007 );
}

void SetPoint (int _x, int _y, word _k)
// Let one Led lit at the (x;y) position
{ if (( _x < 0 ) || ( _x >= MaxCol ))   { return; }   // _x is outside the panel
  if (( _y < 0 ) || ( _y >= MaxRow ))   { return; }   // _y is outside the panel
  _y = _y ^ 0x0F;
  byte _y7 = _y & 0x07;
  _k = _k & 0x0777;
  byte _lc =  LowColor(_k);
  byte _mc =  MidColor(_k);
  byte _hc = HighColor(_k);
  // Put The Pixel On The Right Spot.
  if (( _y & 0x08 ) == 0 )
     { // This is the Upper-half
       PanelData[_x][_y7][0] = ( PanelData[_x][_y7][0] & B11100000 ) | ( _lc << 2 );
       PanelData[_x][_y7][1] = ( PanelData[_x][_y7][1] & B11100000 ) | ( _mc << 2 );
       PanelData[_x][_y7][2] = ( PanelData[_x][_y7][2] & B11100000 ) | ( _hc << 2 );
     }
  else
     { // This is the Lower half.
       PanelData[_x][_y7][0] = ( PanelData[_x][_y7][0] & B00011100 ) | ( _lc << 5 );
       PanelData[_x][_y7][1] = ( PanelData[_x][_y7][1] & B00011100 ) | ( _mc << 5 );
       PanelData[_x][_y7][2] = ( PanelData[_x][_y7][2] & B00011100 ) | ( _hc << 5 );
     }
}

//--------[ Font Functions ]---------------------------------------------------

void GotoXY (int _x, int _y)
{ _font_x = _x;
  _font_y = _y;
}

void NextXY ()
{ // Set the (_font_x;_font_y) to the next char-position.
  // Do an <LF><CR> at the end of the line;
  _font_x = _font_x + 8;
  if ( _font_x >= MaxCol )
     { _font_x = _font_x - MaxCol;
       _font_y = _font_y + charbytes;
       if (_font_y >= MaxRow)
          { _font_y = _font_y - MaxRow;
          }
     }
}

void ReverseChar ()
{ _fontreverse = 0xFF;
}

void NormalChar ()
{ _fontreverse = 0;
}

void SetReverse (byte _bits)
{ _fontreverse = (_bits & 0xFF);
}

void TransparantChar ()
{ _fonttransparant = 0xFF;
}

void SolidChar ()
{ _fonttransparant = 0;
}

void SetTransparance (byte _bits)
{ _fonttransparant = (_bits & 0xFF);
}

void CharColor (word _kk)
{ _fontcolor = _kk;
}

void WriteChar (word ch)
{ // Writes 1 character ch at position (_font_x;_font_y) in the color _fontcolor
  // If nescesary, xor it with _fontreverse
  // move _font_x to 8 point to the right
  if ( ch>=fontchars )
     { return; }    // Char does not exist in font.
  word _ch=ch*charbytes;
  for (byte qy=0; qy<charbytes; qy=qy+1)
      { word charbyte=pgm_read_byte_near(font+_ch+qy)  xor _fontreverse;
        for (byte qx=0; qx<8; qx=qx+1)
            { if ((charbyte & 128)>0)        // Look at MSB-point of charbyte
                 { // if (1) then draw pixel
                   SetPoint (_font_x+qx,_font_y+qy,_fontcolor);
                 }
              else
                 { // if (0) then draw bgcolor
                   if (_fonttransparant & _bitjes[qx]) == 0)
                      { // But only if not Transparant (= 0)
                        SetPoint (_font_x+qx,_font_y+qy,_bgcolor);
                      }
                 }
              charbyte=charbyte<<1;
            }
      }
  NextXY();
}

void PrintChar(char* ss)
{ // Print a character, after conversing it from ascii to font-character
  byte qq = 0;
  while ( ss[qq] != '\0' )
    { // chech every char if it is in the table.
      char cc=ss[qq];
      if (( cc>=asctableshift ) &&         // Char is above the bottom
          ( cc< asctablechars+asctableshift ) // Char is below the top
         )

         { // Find the char in the ascii-table to get the right number in the font.
           word _cc=pgm_read_byte_near(asc2font+cc - asctableshift);
           WriteChar (_cc);
         }
      qq=qq+1;
    }
}

//--------[ The Interupt Function ]--------------------------------------------

ISR(TIMER1_COMPA_vect)
// De Interrupt Service Routine
// The main interupt-routine. Do One Row (at each Interupt)
{
  byte rc = MaxCol;
  byte BrightLayer = BrightRow[BrightCount];
  byte _clock_rise = PORTB | B00000001;  // digitalWrite(clk, HIGH);
  byte _clock_fall = PORTB & B11111110;  // digitalWrite(clk, LOW );
  byte _data_serial = ( PORTD & B00000011 );
  do { rc=rc-1;
       // Send the RGB to the RGB-lines
       PORTD = _data_serial | PanelData[rc][RowCount][BrightLayer];
       // Clock the RGB
       PORTB = _clock_rise;
       PORTB = _clock_fall;
     }
  while (rc>0);
  // Make it Invisible
  PORTB = PORTB | B00000010;             // digitalWrite(oe , HIGH);
  // Select the Row on the panels.
  PORTC = ( PORTC & B11111000 ) | RowCount;
  // Latch the Data
  PORTB = PORTB | B00000100;             // digitalWrite(lat, HIGH);
  PORTB = PORTB & B11111011;             // digitalWrite(lat, LOW );
  // Make it Visible again
  PORTB = PORTB & B11111101;             // digitalWrite(oe , LOW );
  // Next Row
  RowCount = RowCount + 1;     // Count the Row
  if ( RowCount >= 8 )
     { RowCount = 0;
       BrightCount = BrightCount + 1;    // Inc the Brightness counter
       if ( BrightCount >= MaxBrightCount )
          { BrightCount = 0;
          }
     }
}


Broncode: [1: Midi Paneel]

Afbeeldingen

midipanel_01.jpg
1/10: midipanel_01.jpg.
midipanel_02.jpg
2/10: midipanel_02.jpg.
midipanel_03.jpg
3/10: midipanel_03.jpg.
midipanel_04.jpg
4/10: midipanel_04.jpg.
midipanel_05.jpg
5/10: midipanel_05.jpg.
midipanel_06.jpg
6/10: midipanel_06.jpg.
midipanel_07.jpg
7/10: midipanel_07.jpg.
midipanel_08.jpg
8/10: midipanel_08.jpg.
midipanel_09.jpg
9/10: midipanel_09.jpg.
midipanel_10.jpg
10/10: midipanel_10.jpg.

De aansluitingen van Midi Paneel

Midi Signalen op een RGB led-Matrix

Arduino Uno

Pwr USB ?
 
?
aref
gnd Gnd's (4x)
? d13 Latch (Front Leds)
ioreff d12 Clock (Front Leds)
reset d11 Data  (Front Leds)
+3.3v d10 Latch
+5v d9 Oe
Gnd's (4x) gnd d8 Clock
Gnd's (4x) gnd
Vin d7 R2
d6 G2
(Decoder Leds) A  (A0) a0 d5 B2
(Decoder Leds) B  (A1) a1 d4 R1
(Decoder Leds) C  (A2) a2 d3 G1
(Potmeter 4) a3 d2 B1
(Potmeter 5) a4 d1
Midi Kanaal (Potmeter 6) a5 d0 Rx Midi in