-
Notifications
You must be signed in to change notification settings - Fork 0
/
Feather_M4_Seq.ino
1711 lines (1638 loc) · 58.2 KB
/
Feather_M4_Seq.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
/**
* Feather M4 Seq -- An 8 track 32 step MIDI (& Analog) Gate (& CV) Sequencer for Feather M4 Express & Neotrellis 8x8, with probability, looping, swing, velocity, arpeggiator and mutes
* 04 Nov 2023 - @apatchworkboy / Marci
* 28 Apr 2023 - original via @todbot / Tod Kurt https://github.com/todbot/picostepseq/
*
* Libraries needed (all available via Library Manager):
* - Adafruit SPIFlash -- https://github.com/adafruit/Adafruit_SPIFlash
* - Adafruit_TinyUSB -- https://github.com/adafruit/Adafruit_TinyUSB_Arduino
* - Adafruit Seesaw -- https://github.com/adafruit/Adafruit_Seesaw
* - Adafruit Neopixel -- https://github.com/adafruit/Adafruit_Neopixel
* - MIDI -- https://github.com/FortySevenEffects/arduino_midi_library
* - ArduinoJson -- https://arduinojson.org/
*
* To upload:
* - Use Arduino IDE 1.8.19+
* - In "Tools", set "Tools / USB Stack: Adafruit TinyUSB"
* - Program the sketch to the Feather M4 Express with "Upload"
*
**/
#include <SPI.h>
#include <SdFat.h>
#include <Adafruit_SPIFlash.h>
#include <Adafruit_TinyUSB.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_NeoTrellis.h>
#include <MIDI.h>
#include <ArduinoJson.h>
#include <algorithm>
#include <vector>
#include "wiring_digital.h"
#include "flash_config.h"
// Serial Console logging...
const bool midi_out_debug = false;
const bool midi_in_debug = false;
const bool marci_debug = false;
const bool serial_midi = true;
const bool analog_feats = true;
// HOW BIG IS YOUR TRELLIS?
#define Y_DIM 8 //number of rows of key
#define X_DIM 8 //number of columns of keys
Adafruit_NeoPixel strip = Adafruit_NeoPixel(2, 8, NEO_GRB + NEO_KHZ800);
Adafruit_SPIFlash flash(&flashTransport);
Adafruit_USBD_MIDI usb_midi; // USB MIDI object
Adafruit_NeoTrellis t_array[Y_DIM / 4][X_DIM / 4] = {
{ Adafruit_NeoTrellis(0x30), Adafruit_NeoTrellis(0x2E) },
{ Adafruit_NeoTrellis(0x2F), Adafruit_NeoTrellis(0x31) }
};
Adafruit_MultiTrellis trellis((Adafruit_NeoTrellis*)t_array, Y_DIM / 4, X_DIM / 4);
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDIusb); // USB MIDI
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, serialmidi); // Serial MIDI
FatVolume fatfs;
#define D_FLASH "/M4SEQ32"
#define t_size Y_DIM * X_DIM
#define t_length t_size - 1
// Color defs for Trellis...
#include "color_defs.h"
const uint8_t numtracks = X_DIM;
const uint8_t num_steps = t_size / 2;
const uint8_t numpresets = X_DIM * 2;
const uint16_t dacrange = 4095; // A0 & A1 = 12bit DAC on Feather M4 Express
const byte numdacs = 2;
const byte cvpins[2] = { 14, 15 };
const byte gatepins[numtracks] = { 4, 5, 6, 9, 10, 11, 12, 13 };
uint8_t length = t_size / 2; // same as num_steps at boot!
uint8_t brightness = 50;
uint8_t sel_track;
uint8_t lastsel;
uint8_t selstep;
uint8_t shifted; // (SHIFT)
uint8_t transpose;
bool hzv[2] = { 0, 0 };
bool chanedit;
bool divedit;
bool gateedit;
bool lenedit;
bool m4init;
bool notesedit;
bool offedit;
bool patedit;
bool presetmode;
bool probedit;
bool sure;
bool swingedit;
bool veledit;
bool write;
bool resetflag;
#include "multisequencer.h"
#include "save_locations.h"
typedef struct {
int step_size;
bool midi_send_clock;
bool midi_forward_usb;
} Config;
Config cfg = {
.step_size = SIXTEENTH_NOTE,
.midi_send_clock = true,
.midi_forward_usb = true
};
float tempo = 120;
MultiStepSequencer<numtracks, numpresets, num_steps, numdacs, numarps> seqr;
// end hardware definitions
uint8_t midiclk_cnt = 0;
uint32_t midiclk_last_micros = 0;
//
// -- MIDI sending & receiving functions
//
// callback used by Sequencer to send song position in beats since start
void send_song_pos(int beat) {
MIDIusb.sendSongPosition(beat);
if (serial_midi) serialmidi.sendSongPosition(beat);
}
// callback used by Sequencer to trigger note on
void send_note_on(uint8_t note, uint8_t vel, uint8_t gate, bool on, uint8_t chan) {
if (on) {
MIDIusb.sendNoteOn(note, vel, chan);
if (serial_midi) serialmidi.sendNoteOn(note, vel, chan);
}
if (midi_out_debug) { Serial.printf("noteOn: %d %d %d %d\n", note, vel, gate, on); }
}
// callback used by Sequencer to trigger note off
void send_note_off(uint8_t note, uint8_t vel, uint8_t gate, bool on, uint8_t chan) {
if (on) {
MIDIusb.sendNoteOff(note, vel, chan);
if (serial_midi) serialmidi.sendNoteOff(note, vel, chan);
}
if (midi_out_debug) { Serial.printf("noteOff: %d %d %d %d\n", note, vel, gate, on); }
}
// callback used by Sequencer to send controlchange messages
void send_cc(uint8_t cc, uint8_t val, bool on, uint8_t chan) {
if (on) {
MIDIusb.sendControlChange(cc, val, chan);
if (serial_midi) serialmidi.sendControlChange(cc, val, chan);
}
if (midi_out_debug) { Serial.printf("controlChange: %d %d %d %d\n", cc, val, on, chan); }
}
// callback used by Sequencer to send midi clock when internally triggered
void send_clock_start_stop(clock_type_t type) {
if (type == START) {
MIDIusb.sendStart();
if (serial_midi) serialmidi.sendStart();
} else if (type == STOP) {
MIDIusb.sendStop();
if (serial_midi) serialmidi.sendStop();
} else if (type == CLOCK) {
MIDIusb.sendClock();
if (serial_midi) serialmidi.sendClock();
}
if (midi_out_debug) { Serial.printf("\tclk:%d\n", type); }
}
void handle_midi_in_start() {
seqr.play();
midiclk_cnt = 0;
if (midi_in_debug) { Serial.println("midi in start"); }
}
void handle_midi_in_stop() {
seqr.stop();
if (midi_in_debug) { Serial.println("midi in stop"); }
}
// FIXME: midi continue?
void handle_midi_in_clock() {
uint32_t now_micros = micros();
// once every ticks_per_step, play note (24 ticks per quarter note => 6 ticks per 16th note)
if (midiclk_cnt % seqr.ticks_per_step == 0) { // ticks_per_step = 6 for 16th note
seqr.trigger_ext(now_micros);
}
midiclk_cnt++;
// once every quarter note, calculate new BPM, but be a little cautious about it
if (midiclk_cnt == ticks_per_quarternote) {
uint32_t new_tick_micros = (now_micros - midiclk_last_micros) / ticks_per_quarternote;
if (new_tick_micros > seqr.tick_micros / 2 && new_tick_micros < seqr.tick_micros * 2) {
seqr.tick_micros = new_tick_micros;
}
midiclk_last_micros = now_micros;
midiclk_cnt = 0;
}
}
void handle_midi_in_NoteOff(uint8_t channel, uint8_t note, uint8_t vel) {
uint8_t trk_arr = sel_track - 1;
if (marci_debug) Serial.println("Note Stop");
if (seqr.mutes[trk_arr] == 1) {
if (seqr.modes[trk_arr] == TRIGATE) {
note = seqr.track_notes[trk_arr];
}
if (serial_midi) serialmidi.sendNoteOff(note, vel, seqr.track_chan[trk_arr]);
MIDIusb.sendNoteOff(note, vel, seqr.track_chan[trk_arr]);
} else {
switch (seqr.modes[trk_arr]) {
case TRIGATE:
MIDIusb.sendNoteOff(seqr.track_notes[trk_arr], vel, seqr.track_chan[trk_arr]);
if (serial_midi) serialmidi.sendNoteOff(seqr.track_notes[trk_arr], vel, seqr.track_chan[trk_arr]);
break;
case NOTE:
MIDIusb.sendNoteOff(note, vel, seqr.track_chan[trk_arr]);
if (serial_midi) serialmidi.sendNoteOff(note, vel, seqr.track_chan[trk_arr]);
break;
case ARP:
if (marci_debug) Serial.println("Firing NoteOff to arp for");
if (marci_debug) Serial.println(note);
arps[trk_arr - numarps].NoteOff(note);
break;
default: break;
}
}
}
void handle_midi_in_NoteOn(uint8_t channel, uint8_t note, uint8_t vel) {
if (marci_debug) Serial.println("Note Start");
uint8_t trk_arr = sel_track - 1;
if (seqr.mutes[trk_arr] == 1) {
if (seqr.modes[trk_arr] == TRIGATE) {
note = seqr.track_notes[trk_arr];
}
if (serial_midi) serialmidi.sendNoteOn(note, vel, seqr.track_chan[trk_arr]);
MIDIusb.sendNoteOn(note, vel, seqr.track_chan[trk_arr]);
} else {
uint8_t _s = constrain(seqr.ticki > 3 ? seqr.multistepi[trk_arr] + 1 : seqr.multistepi[trk_arr], 0 , num_steps - 1);
switch (seqr.modes[trk_arr]) {
case CC:
if (marci_debug) Serial.println("CC");
if (veledit == 1) {
switch (shifted) {
case 0:
seqr.vels[seqr.presets[trk_arr]][trk_arr][selstep] = note;
seqr.seqs[seqr.presets[trk_arr]][trk_arr][selstep] = 1;
break;
case 1:
// LIVE ENTRY
seqr.vels[seqr.presets[trk_arr]][trk_arr][_s] = note;
seqr.seqs[seqr.presets[trk_arr]][trk_arr][_s] = 1;
break;
default: break;
}
}
break;
case NOTE:
if (marci_debug) Serial.println("Note");
if (veledit == 1 || patedit == 1) {
switch (shifted) {
case 0:
send_note_off(seqr.notes[seqr.presets[trk_arr]][trk_arr][selstep], 0, 0, 1, seqr.track_chan[trk_arr]);
seqr.notes[seqr.presets[trk_arr]][trk_arr][selstep] = note;
seqr.vels[seqr.presets[trk_arr]][trk_arr][selstep] = vel;
seqr.seqs[seqr.presets[trk_arr]][trk_arr][selstep] = 1;
break;
case 1:
// LIVE ENTRY
send_note_off(seqr.notes[seqr.presets[trk_arr]][trk_arr][_s], 0, 0, 1, seqr.track_chan[trk_arr]);
MIDIusb.sendNoteOn(note, vel, seqr.track_chan[trk_arr]);
if (serial_midi) serialmidi.sendNoteOn(note, vel, seqr.track_chan[trk_arr]);
seqr.notes[seqr.presets[trk_arr]][trk_arr][_s] = note;
seqr.vels[seqr.presets[trk_arr]][trk_arr][_s] = vel;
seqr.seqs[seqr.presets[trk_arr]][trk_arr][_s] = 1;
break;
default: break;
}
}
break;
case TRIGATE:
if (marci_debug) Serial.println("Trigate");
switch (shifted) {
case 0:
seqr.vels[seqr.presets[trk_arr]][trk_arr][selstep] = note;
seqr.seqs[seqr.presets[trk_arr]][trk_arr][selstep] = 1;
break;
case 1:
// LIVE ENTRY
MIDIusb.sendNoteOn(seqr.track_notes[trk_arr], vel, seqr.track_chan[trk_arr]);
if (serial_midi) serialmidi.sendNoteOn(seqr.track_notes[trk_arr], vel, seqr.track_chan[trk_arr]);
seqr.vels[seqr.presets[trk_arr]][trk_arr][_s] = note;
seqr.seqs[seqr.presets[trk_arr]][trk_arr][_s] = 1;
trellis.setPixelColor(_s, W100);
break;
default: break;
}
break;
case ARP: {
if (marci_debug) Serial.println("Arp");
switch (shifted) {
case 0:
if (marci_debug) Serial.println("Unshifted - ignore");
break;
case 1:
if (marci_debug) Serial.println("Sending to Arp");
arps[trk_arr - numarps].NoteOn(note);
break;
default: break;
}
break;
default: break;
}
}
}
}
void handle_midi_in_CC(uint8_t channel, uint8_t cc, uint8_t val) {
Serial.println("CC Incoming");
uint8_t trk_arr = sel_track - 1;
if (seqr.mutes[trk_arr] == 1) {
if (serial_midi) serialmidi.sendControlChange(cc, val, seqr.track_chan[trk_arr]);
MIDIusb.sendControlChange(cc, val, seqr.track_chan[trk_arr]);
} else {
switch (seqr.modes[trk_arr]) {
case CC:
switch (shifted) {
case 0:
seqr.track_notes[trk_arr] = cc;
seqr.vels[seqr.presets[trk_arr]][trk_arr][selstep] = val;
break;
case 1:
seqr.track_notes[trk_arr] = cc;
seqr.vels[seqr.presets[trk_arr]][trk_arr][seqr.multistepi[trk_arr]] = val;
break;
default: break;
}
break;
case NOTE:
switch (shifted) {
case 0:
seqr.vels[seqr.presets[trk_arr]][trk_arr][selstep] = val;
break;
case 1:
seqr.vels[seqr.presets[trk_arr]][trk_arr][seqr.multistepi[trk_arr]] = val;
break;
default: break;
}
break;
default: break;
}
}
}
//
void midi_read_and_forward() {
if (MIDIusb.read()) {
midi::MidiType t = MIDIusb.getType();
switch (t) {
case midi::Start:
handle_midi_in_start();
break;
case midi::Stop:
handle_midi_in_stop();
break;
case midi::Clock:
handle_midi_in_clock();
break;
default: break;
}
}
}
// Analog IO
void analog_gate(uint8_t pin, uint8_t direction) {
if (analog_feats) digitalWrite(pin, direction == 1 ? HIGH : LOW);
}
void analog_cv(uint8_t pin, uint16_t value) {
if (analog_feats) analogWrite(pin, value);
}
void update_display() {
if (presetmode == 1 || chanedit == 1 || sure == 1 || divedit == 1) {
return;
}
uint32_t color = 0;
uint32_t hit = 0;
uint8_t trk_arr = sel_track - 1;
//active step ticker
if (gateedit == 1) {
hit = seqr.gates[seqr.presets[trk_arr]][trk_arr][seqr.multistepi[trk_arr]] > 0 ? PURPLE : W100;
color = seqr.gates[seqr.presets[trk_arr]][trk_arr][seqr.laststeps[trk_arr]] < 15 ? Wheel(seqr.gates[seqr.presets[trk_arr]][trk_arr][seqr.laststeps[trk_arr]] * 5) : seq_col(sel_track);
} else if (probedit == 1) {
hit = seqr.seqs[seqr.presets[trk_arr]][trk_arr][seqr.multistepi[trk_arr]] > 0 ? PURPLE : W100;
color = seqr.probs[seqr.presets[trk_arr]][trk_arr][seqr.laststeps[trk_arr]] < 10 ? Wheel(seqr.probs[seqr.presets[trk_arr]][trk_arr][seqr.laststeps[trk_arr]] * 10) : seq_col(sel_track);
} else if (veledit == 1) {
hit = seqr.multistepi[trk_arr] != selstep ? seqr.seqs[seqr.presets[trk_arr]][trk_arr][seqr.multistepi[trk_arr]] > 0 ? PURPLE : W100 : W100;
if (seqr.modes[trk_arr] == CC || seqr.modes[trk_arr] == NOTE) {
color = Wheel(seqr.vels[seqr.presets[trk_arr]][trk_arr][seqr.laststeps[trk_arr]]);
} else {
color = seq_dim(sel_track, seqr.vels[seqr.presets[trk_arr]][trk_arr][seqr.laststeps[trk_arr]]);
}
} else if (notesedit == 1) {
hit = seqr.multistepi[trk_arr] != selstep ? seqr.seqs[seqr.presets[trk_arr]][trk_arr][seqr.multistepi[trk_arr]] > 0 ? PURPLE : W100 : W100;
if (seqr.modes[trk_arr] == CC || seqr.modes[trk_arr] == NOTE) {
color = Wheel(seqr.notes[seqr.presets[trk_arr]][trk_arr][seqr.laststeps[trk_arr]]);
} else {
color = seq_dim(sel_track, seqr.notes[seqr.presets[trk_arr]][trk_arr][seqr.laststeps[trk_arr]]);
}
} else {
hit = seqr.seqs[seqr.presets[trk_arr]][trk_arr][seqr.multistepi[trk_arr]] > 0 ? PURPLE : W100;
color = seqr.seqs[seqr.presets[trk_arr]][trk_arr][seqr.laststeps[trk_arr]] > 0 ? seq_col(sel_track) : 0;
}
trellis.setPixelColor(seqr.multistepi[trk_arr], hit);
strip.setPixelColor(0, seqr.seqs[seqr.presets[trk_arr]][trk_arr][seqr.multistepi[trk_arr]] > 0 ? seq_col(sel_track) : (seqr.pulse == 1 ? W40 : 0));
if (seqr.modes[trk_arr] == CC && veledit == 1) {
trellis.setPixelColor(seqr.laststeps[trk_arr], seqr.laststeps[trk_arr] != selstep ? color : W100);
} else {
trellis.setPixelColor(seqr.laststeps[trk_arr], color);
}
if (seqr.offsets[trk_arr] - 1 > 0) trellis.setPixelColor(seqr.offsets[trk_arr] - 1, trk_arr > 1 ? R127 : B127);
if (seqr.lengths[trk_arr] < num_steps) trellis.setPixelColor(seqr.lengths[trk_arr] - 1, trk_arr != 4 ? C127 : G127);
if (seqr.resetflag == 1) {
if (gateedit == 1) {
show_gates(sel_track);
} else if (veledit == 1) {
show_accents(sel_track);
} else if (probedit == 1) {
show_probabilities(sel_track);
} else {
show_sequence(sel_track);
}
seqr.resetflag = 0;
}
trellis.show();
strip.show();
}
void reset_display() {
uint32_t hit = 0;
uint8_t trk_arr = sel_track - 1;
if (patedit == 1) {
hit = seqr.seqs[seqr.presets[trk_arr]][trk_arr][seqr.multistepi[trk_arr]] > 0 ? seq_col(sel_track) : W10;
} else if (gateedit == 1) {
hit = seqr.gates[seqr.presets[trk_arr]][trk_arr][seqr.multistepi[trk_arr]] > 0 ? seq_col(sel_track) : W10;
} else if (veledit == 1) {
hit = seqr.vels[seqr.presets[trk_arr]][trk_arr][seqr.multistepi[trk_arr]] > 0 ? seq_col(sel_track) : W10;
} else if (probedit == 1) {
hit = seqr.probs[seqr.presets[trk_arr]][trk_arr][seqr.multistepi[trk_arr]] > 0 ? seq_col(sel_track) : W10;
} else {
hit = W10;
}
trellis.setPixelColor(seqr.multistepi[trk_arr], hit);
strip.setPixelColor(0, seq_col(sel_track));
strip.show();
}
void toggle_selected(uint8_t keyId) {
trellis.setPixelColor(lastsel + (num_steps - 1), seq_dim(lastsel, 40));
trellis.setPixelColor(keyId, seq_col(sel_track));
if (!seqr.playing) {
strip.setPixelColor(0, seq_col(sel_track));
if (!seqr.playing) { strip.show(); }
}
}
void show_sequence(uint8_t& seq) {
patedit = 1;
for (uint8_t i = 0; i < num_steps; ++i) {
trellis.setPixelColor(i, seqr.seqs[seqr.presets[seq - 1]][seq - 1][i] > 0 ? seq_col(sel_track) : 0);
}
if (!seqr.playing) { trellis.show(); }
}
void set_gate(uint8_t gid, uint8_t stp, int c) {
uint32_t col = seqr.gates[seqr.presets[gid]][gid][stp] >= 15 ? c : Wheel(seqr.gates[seqr.presets[gid]][gid][stp] * 5);
trellis.setPixelColor(stp, col);
if (!seqr.playing) { trellis.show(); }
}
void show_gates(uint8_t& seq) {
gateedit = 1;
uint8_t gateId = seq - 1;
uint32_t col = 0;
for (uint8_t i = 0; i < num_steps; ++i) {
set_gate(gateId, i, seq_col(seq));
}
if (!seqr.playing) { trellis.show(); }
}
void show_probabilities(uint8_t& seq) {
probedit = 1;
uint32_t col = 0;
for (uint8_t i = 0; i < num_steps; ++i) {
col = seqr.probs[seqr.presets[seq - 1]][seq - 1][i] == 10 ? seq_col(seq) : Wheel(seqr.probs[seqr.presets[seq - 1]][seq - 1][i] * 10);
trellis.setPixelColor(i, col);
}
if (!seqr.playing) { trellis.show(); }
}
void show_accents(uint8_t& seq) { // velocities
uint8_t trk_arr = sel_track - 1;
veledit = 1;
uint32_t col = 0;
if (seqr.modes[seq - 1] == CC) {
for (uint8_t i = 0; i < num_steps; ++i) {
trellis.setPixelColor(i, Wheel(seqr.vels[seqr.presets[trk_arr]][seq - 1][i]));
}
} else {
for (uint8_t i = 0; i < num_steps; ++i) {
trellis.setPixelColor(i, seq_dim(seq, seqr.vels[seqr.presets[trk_arr]][seq - 1][i]));
}
}
if (!seqr.playing) { trellis.show(); }
}
void show_notes(uint8_t& seq) {
uint8_t trk_arr = sel_track - 1;
notesedit = 1;
uint32_t col = 0;
if (seqr.modes[seq - 1] == NOTE) {
for (uint8_t i = 0; i < num_steps; ++i) {
trellis.setPixelColor(i, Wheel(seqr.notes[seqr.presets[trk_arr]][seq - 1][i]));
}
} else {
for (uint8_t i = 0; i < num_steps; ++i) {
trellis.setPixelColor(i, seq_dim(seq, seqr.notes[seqr.presets[trk_arr]][seq - 1][i]));
}
}
if (!seqr.playing) { trellis.show(); }
}
void show_presets() {
uint8_t trk_arr = sel_track - 1;
for (uint8_t i = 0; i < num_steps; ++i) {
trellis.setPixelColor(i, i < (num_steps / 2) ? 0 : W10);
}
trellis.setPixelColor(seqr.presets[trk_arr], W100);
trellis.show();
}
void show_divisions() {
uint8_t trk_arr = sel_track - 1;
for (uint8_t i = 0; i < num_steps; ++i) {
trellis.setPixelColor(i, 0);
}
trellis.setPixelColor(seqr.divs[trk_arr], seq_col(sel_track));
trellis.setPixelColor(53, O80);
trellis.setPixelColor(52, W100);
trellis.show();
}
// Update Transpose Key
void transpose_led() {
switch (transpose) {
case 0:
trellis.setPixelColor(53, B40);
break;
case 12:
trellis.setPixelColor(53, B80);
break;
case 24:
trellis.setPixelColor(53, B127);
break;
default:
break;
}
if (!seqr.playing) { trellis.show(); }
}
// Update Channel Config MODE buttons
void mode_leds(uint8_t& track) {
for (uint8_t i = X_DIM * 3; i < num_steps; ++i) {
trellis.setPixelColor(i, 0);
}
switch (seqr.modes[track - 1]) {
case TRIGATE:
trellis.setPixelColor(24, W100);
break;
case CC:
trellis.setPixelColor(25, W100);
if (track - 1 == 6 || track - 1 == 7) {
if (hzv[(track - 1) - 6] == 1) {
trellis.setPixelColor(31, seq_dim(track, 100)); //hzv
} else {
trellis.setPixelColor(31, W100); //voct
};
}
break;
case NOTE:
trellis.setPixelColor(26, W100);
if (track - 1 == 6 || track - 1 == 7) {
if (hzv[(track - 1) - 6] == 1) {
trellis.setPixelColor(31, seq_dim(track, 100)); //hzv
} else {
trellis.setPixelColor(31, W100); //voct
};
}
break;
case ARP:
if (track - 1 >= 4) trellis.setPixelColor(27, W100);
break;
case CHORD:
trellis.setPixelColor(28, W100);
break;
default: break;
}
trellis.show();
}
// Initialise Channel Config display...
void init_chan_conf(uint8_t& track) {
for (uint8_t i = 0; i < num_steps; ++i) {
trellis.setPixelColor(i, 0);
}
trellis.setPixelColor(seqr.track_chan[track - 1] - 1, seq_col(track));
switch (seqr.modes[track - 1]) {
case TRIGATE:
trellis.setPixelColor(24, W100);
break;
case CC:
trellis.setPixelColor(25, W100);
if (track - 1 == 6 || track - 1 == 7) {
if (hzv[(track - 1) - 6] == 1) {
trellis.setPixelColor(31, seq_dim(track, 100)); //hzv
} else {
trellis.setPixelColor(31, W100); //voct
};
}
break;
case NOTE:
trellis.setPixelColor(26, W100);
if (track - 1 == 6 || track - 1 == 7) {
if (hzv[(track - 1) - 6] == 1) {
trellis.setPixelColor(31, seq_dim(track, 100)); //hzv
} else {
trellis.setPixelColor(31, W100); //voct
};
}
break;
case ARP:
if (track - 1 >= 4) trellis.setPixelColor(27, W100);
break;
case CHORD:
if (track - 1 >= 4) trellis.setPixelColor(28, W100);
break;
default: break;
}
trellis.show();
}
// Show "Are you sure, Y/N" display...
void sure_pane() {
for (uint8_t i = 0; i < t_size; ++i) {
trellis.setPixelColor(i, i == 26 ? GREEN : i == 29 ? RED
: 0);
}
sure = 1;
trellis.setPixelColor(60, R40);
trellis.show();
}
// Show via button color that we're writing to flash...
void toggle_write() {
write = write == 0 ? 1 : 0;
trellis.setPixelColor(59, write == 0 ? R40 : R127);
trellis.show();
}
// Set Brightness of Neotrellis...
void set_brightness() {
for (uint8_t x = 0; x < X_DIM; ++x) {
for (uint8_t y = 0; y < Y_DIM; ++y) {
t_array[y][x].pixels.setBrightness(brightness);
}
}
strip.setBrightness(brightness * 2);
trellis.show();
strip.show();
}
// Initialise Neotrellis interactions and static control rows...
void init_interface() {
if (m4init == 0) {
for (int y = 0; y < Y_DIM; y++) {
for (int x = 0; x < X_DIM; x++) {
trellis.activateKey(x, y, SEESAW_KEYPAD_EDGE_RISING, true);
//trellis.activateKey(x, y, SEESAW_KEYPAD_EDGE_FALLING, true);
trellis.registerCallback(x, y, onKey);
}
}
m4init = 1;
}
set_brightness();
//Seq 1 > 8
trellis.setPixelColor(32, sel_track == 1 ? seq_col(sel_track) : seq_dim(1, 40));
trellis.setPixelColor(33, sel_track == 2 ? seq_col(sel_track) : seq_dim(2, 40));
trellis.setPixelColor(34, sel_track == 3 ? seq_col(sel_track) : seq_dim(3, 40));
trellis.setPixelColor(35, sel_track == 4 ? seq_col(sel_track) : seq_dim(4, 40));
trellis.setPixelColor(36, sel_track == 5 ? seq_col(sel_track) : seq_dim(5, 40));
trellis.setPixelColor(37, sel_track == 6 ? seq_col(sel_track) : seq_dim(6, 40));
trellis.setPixelColor(38, sel_track == 7 ? seq_col(sel_track) : seq_dim(7, 40));
trellis.setPixelColor(39, sel_track == 8 ? seq_col(sel_track) : seq_dim(8, 40));
//seqr.mutes
trellis.setPixelColor(40, seqr.mutes[0] == 1 ? R80 : G40);
trellis.setPixelColor(41, seqr.mutes[1] == 1 ? R80 : G40);
trellis.setPixelColor(42, seqr.mutes[2] == 1 ? R80 : G40);
trellis.setPixelColor(43, seqr.mutes[3] == 1 ? R80 : G40);
trellis.setPixelColor(44, seqr.mutes[4] == 1 ? R80 : G40);
trellis.setPixelColor(45, seqr.mutes[5] == 1 ? R80 : G40);
trellis.setPixelColor(46, seqr.mutes[6] == 1 ? R80 : G40);
trellis.setPixelColor(47, seqr.mutes[7] == 1 ? R80 : G40);
//Panes
trellis.setPixelColor(48, patedit == 1 ? R127 : R40);
trellis.setPixelColor(49, veledit == 1 ? Y127 : Y40);
trellis.setPixelColor(50, probedit == 1 ? P127 : P40);
trellis.setPixelColor(51, gateedit == 1 ? B127 : B40);
//Globals
trellis.setPixelColor(52, shifted == 1 ? W100 : PK40);
if (chanedit == 0) {
switch (transpose) {
case 0:
trellis.setPixelColor(53, B40);
break;
case 12:
trellis.setPixelColor(53, B80);
break;
case 24:
trellis.setPixelColor(53, B127);
break;
default:
break;
}
} else {
trellis.setPixelColor(53, R127);
}
trellis.setPixelColor(54, G40);
switch (cfg.step_size) {
case SIXTEENTH_NOTE:
trellis.setPixelColor(55, O127);
break;
case QUARTER_NOTE:
trellis.setPixelColor(55, O40);
break;
case EIGHTH_NOTE:
trellis.setPixelColor(55, O80);
break;
default:
break;
}
trellis.setPixelColor(55, O127);
//Control Row
trellis.setPixelColor(56, GREEN);
trellis.setPixelColor(57, RED);
trellis.setPixelColor(58, ORANGE);
trellis.setPixelColor(59, B80);
trellis.setPixelColor(60, C40);
trellis.setPixelColor(61, YELLOW);
trellis.setPixelColor(62, RED);
trellis.setPixelColor(63, GREEN);
trellis.show();
}
#include "saveload.h" /// FIXME:
// in vast need of improvements for efficiency:
TrellisCallback onKey(keyEvent evt) {
auto const now = millis();
auto const keyId = evt.bit.NUM;
if (marci_debug) { Serial.println(keyId); }
uint8_t trk_arr = sel_track - 1;
switch (evt.bit.EDGE) {
case SEESAW_KEYPAD_EDGE_RISING:
uint32_t col;
if (sure == 1) { // SURE Y/N STEP EDIT
if (keyId == 26) {
sure = 1;
trellis.setPixelColor(60, R127);
trellis.setPixelColor(29, 0);
pattern_reset();
} else if (keyId == 29) {
sure = 0;
shifted = 0;
init_interface();
show_sequence(sel_track);
} else {
break;
}
} else if (divedit == 1) { // TRACK CLOCK DIVIDER STEP EDIT
if (keyId < (num_steps)) {
seqr.divs[trk_arr] = keyId;
seqr.divcounts[trk_arr] = -1;
show_divisions();
} else if (keyId == 52) {
divedit = 0;
shifted = 0;
trellis.setPixelColor(52, PK40);
trellis.setPixelColor(53, B40);
show_sequence(sel_track);
} else if (keyId == 53) {
divedit = 0;
trellis.setPixelColor(53, B40);
show_sequence(sel_track);
} else if (keyId == 58) {
seqr.reset();
} else if (keyId < 40 && keyId >= num_steps) {
divedit = 1;
lastsel = sel_track;
sel_track = keyId - (num_steps -1);
toggle_selected(keyId);
show_divisions();
}
} else if (presetmode == 1 && (keyId < num_steps || keyId > 39 && keyId < 56)) { // PRESET STEP EDIT
if (keyId < (numpresets)) {
for (uint8_t i = 0; i < numpresets; ++i) {
trellis.setPixelColor(i, 0);
}
trellis.setPixelColor(seqr.presets[trk_arr], 0);
trellis.setPixelColor(keyId, W100);
seqr.presets[trk_arr] = keyId;
// seqr.reset();
} else if (keyId > ((numpresets - 1)) && keyId < (numpresets * 2)) {
trellis.setPixelColor(seqr.presets[trk_arr], 0);
trellis.setPixelColor(keyId - (X_DIM * 2), W100);
for (uint8_t i = 0; i < numpresets; ++i) {
seqr.presets[i] = (keyId - (X_DIM * 2));
// seqr.reset();
}
} else if (keyId > 39 && keyId < 48) {
if (seqr.mutes[keyId - 40] == 0) {
seqr.mutes[keyId - 40] = 1;
trellis.setPixelColor(keyId, R80);
} else {
seqr.mutes[keyId - 40] = 0;
trellis.setPixelColor(keyId, G80);
}
}
show_presets();
} else if (chanedit == 1 && keyId < num_steps) { // CHANNEL STEP EDIT
if (keyId < 16) {
uint8_t chan = keyId + 1;
seqr.track_chan[trk_arr] = chan;
for (uint8_t i = 0; i < 16; ++i) {
trellis.setPixelColor(i, 0);
}
trellis.setPixelColor(keyId, seq_col(sel_track));
} else {
switch (keyId) {
case 24:
seqr.modes[trk_arr] = TRIGATE;
break;
case 25:
seqr.modes[trk_arr] = CC;
break;
case 26:
seqr.modes[trk_arr] = NOTE;
break;
case 27:
if (trk_arr >= 4) {
seqr.modes[sel_track-1] = ARP;
}
break;
case 28:
//seqr.modes[sel_track-1] = CHORD;
break;
case 31:
if ((seqr.modes[trk_arr] == CC || seqr.modes[trk_arr] == NOTE) && (trk_arr > 5)) {
hzv[(trk_arr) - 6] = hzv[(trk_arr) - 6] == 0 ? 1 : 0;
}
break;
default: break;
}
mode_leds(sel_track);
}
} else if (chanedit == 1 && ((keyId > (num_steps - 1) + X_DIM && keyId < (num_steps) + X_DIM + 12) || (keyId > (num_steps) + X_DIM + 13 && keyId < (t_size - 2)))) {
// do nothin'
} else if (lenedit == 1 && keyId < num_steps) { // LENGTH EDIT
if (keyId + 1 >= seqr.offsets[trk_arr]) {
seqr.lengths[trk_arr] = keyId + 1;
//length = keyId + 1;
lenedit = 0;
trellis.setPixelColor(keyId, C127);
trellis.setPixelColor(54, G40);
configure_sequencer();
}
} else if (offedit == 1 && keyId < num_steps) { // OFFSET EDIT
if (keyId + 1 <= seqr.lengths[trk_arr]) {
seqr.offsets[trk_arr] = keyId + 1;
//length = keyId + 1;
offedit = 0;
trellis.setPixelColor(keyId, B127);
trellis.setPixelColor(54, B40);
configure_sequencer();
}
} else if (gateedit == 1 && keyId < num_steps) { // GATE STEP EDIT
uint8_t gateId = trk_arr;
if (seqr.gates[seqr.presets[trk_arr]][gateId][keyId] >= 15) {
seqr.gates[seqr.presets[trk_arr]][gateId][keyId] = 0;
}
seqr.gates[seqr.presets[trk_arr]][gateId][keyId] += 3;
set_gate(gateId, keyId, seq_col(sel_track));
} else if (probedit == 1 && keyId < num_steps) { // PROBABILITY STEP EDIT
if (seqr.probs[seqr.presets[trk_arr]][trk_arr][keyId] == 10) {
seqr.probs[seqr.presets[trk_arr]][trk_arr][keyId] = 0;
}
seqr.probs[seqr.presets[trk_arr]][trk_arr][keyId] += 1;
col = seqr.probs[seqr.presets[trk_arr]][trk_arr][keyId] == 10 ? seq_col(sel_track) : Wheel(seqr.probs[seqr.presets[trk_arr]][trk_arr][keyId] * 10);
trellis.setPixelColor(keyId, col);
if (!seqr.playing) { trellis.show(); }
} else if (notesedit == 1 & keyId < num_steps) { // NOTES STEP EDIT
if (seqr.modes[trk_arr] == CC || seqr.modes[trk_arr] == NOTE) {
uint8_t prev_selstep = selstep;
selstep = keyId;
trellis.setPixelColor(prev_selstep, Wheel(seqr.notes[seqr.presets[trk_arr]][trk_arr][prev_selstep]));
trellis.setPixelColor(selstep, W100);
}
} else if (veledit == 1 & keyId < num_steps) { // VELOCITY STEP EDIT
if (seqr.modes[trk_arr] == CC || seqr.modes[trk_arr] == NOTE) {
uint8_t prev_selstep = selstep;
selstep = keyId;
trellis.setPixelColor(prev_selstep, Wheel(seqr.vels[seqr.presets[trk_arr]][trk_arr][prev_selstep]));
trellis.setPixelColor(selstep, W100);
} else {
switch (sel_track) {
case 0:
break;
case 1:
switch (seqr.vels[seqr.presets[trk_arr]][trk_arr][keyId]) {
case 127:
trellis.setPixelColor(keyId, R40);
seqr.vels[seqr.presets[trk_arr]][trk_arr][keyId] = 40;
break;
case 80:
trellis.setPixelColor(keyId, R127);
seqr.vels[seqr.presets[trk_arr]][trk_arr][keyId] = 127;
break;
case 40:
trellis.setPixelColor(keyId, R80);
seqr.vels[seqr.presets[trk_arr]][trk_arr][keyId] = 80;
break;
default:
break;
}
break;
case 2:
switch (seqr.vels[seqr.presets[trk_arr]][trk_arr][keyId]) {
case 127:
trellis.setPixelColor(keyId, O40);
seqr.vels[seqr.presets[trk_arr]][trk_arr][keyId] = 40;
break;
case 80:
trellis.setPixelColor(keyId, O127);
seqr.vels[seqr.presets[trk_arr]][trk_arr][keyId] = 127;
break;
case 40:
trellis.setPixelColor(keyId, O80);
seqr.vels[seqr.presets[trk_arr]][trk_arr][keyId] = 80;
break;
default:
break;
}
break;
case 3:
switch (seqr.vels[seqr.presets[trk_arr]][trk_arr][keyId]) {
case 127:
trellis.setPixelColor(keyId, Y40);
seqr.vels[seqr.presets[trk_arr]][trk_arr][keyId] = 40;
break;
case 80:
trellis.setPixelColor(keyId, Y127);
seqr.vels[seqr.presets[trk_arr]][trk_arr][keyId] = 127;
break;
case 40:
trellis.setPixelColor(keyId, Y80);
seqr.vels[seqr.presets[trk_arr]][trk_arr][keyId] = 80;
break;
default:
break;
}
break;
case 4:
switch (seqr.vels[seqr.presets[trk_arr]][trk_arr][keyId]) {
case 127:
trellis.setPixelColor(keyId, G40);
seqr.vels[seqr.presets[trk_arr]][trk_arr][keyId] = 40;
break;
case 80:
trellis.setPixelColor(keyId, G127);
seqr.vels[seqr.presets[trk_arr]][trk_arr][keyId] = 127;
break;
case 40:
trellis.setPixelColor(keyId, G80);
seqr.vels[seqr.presets[trk_arr]][trk_arr][keyId] = 80;