-
Notifications
You must be signed in to change notification settings - Fork 82
/
ls_settings.ino
3318 lines (2935 loc) · 101 KB
/
ls_settings.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
/****************************** ls_settings: LinnStrument Settings ********************************
Copyright 2023 Roger Linn Design (https://www.rogerlinndesign.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************************************
These functions handle the changing of any of LinnStrument's panel settings.
**************************************************************************************************/
// These messages correspond to the scrolling texts that will be displayed by default when pressing
// the top-most row in global settings. Only the first 30 characters will be used.
const char* defaultAudienceMessages[16] = {
"LINNSTRUMENT",
"APPLAUSE",
"HA HA HA",
"SINGER SUCKS",
"WRONG NOTE",
"SMELLY NIGHTCLUB",
"HELLO",
"HELLO NEW YORK",
"HELLO LOS ANGELES",
"HELLO SAN FRANCISCO",
"HELLO LONDON",
"HELLO MUNICH",
"HELLO BRUSSELS",
"HELLO PARIS",
"HELLO TOKYO",
"HELLO BARCELONA"
};
// These arrays use the setLed encoding scheme where the color is bitshifted << 3 and ORed with the CellDisplay value
const byte CUSTOM_LEDS_PATTERN1[LED_LAYER_SIZE] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25,
0, 25, 0, 0, 25, 0, 25, 0, 25, 0, 25, 0, 0, 25, 0, 0, 25, 0, 25, 0, 25, 0, 25, 0, 0, 25,
0, 25, 0, 0, 25, 0, 25, 0, 25, 0, 25, 0, 0, 25, 0, 0, 25, 0, 25, 0, 25, 0, 25, 0, 0, 25,
0, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
const byte CUSTOM_LEDS_PATTERN2[LED_LAYER_SIZE] = {
0, 0, 41, 0, 9, 0, 17, 33, 0, 49, 0, 73, 25, 0, 41, 0, 9, 0, 17, 33, 0, 49, 0, 73, 25, 0,
0, 17, 33, 0, 49, 0, 73, 25, 0, 41, 0, 9, 0, 17, 33, 0, 49, 0, 73, 25, 0, 41, 0, 9, 0, 17,
0, 73, 25, 0, 41, 0, 9, 0, 17, 33, 0, 49, 0, 73, 25, 0, 41, 0, 9, 0, 17, 33, 0, 49, 0, 73,
0, 9, 0, 17, 33, 0, 49, 0, 73, 25, 0, 41, 0, 9, 0, 17, 33, 0, 49, 0, 73, 25, 0, 41, 0, 9,
0, 49, 0, 73, 25, 0, 41, 0, 9, 0, 17, 33, 0, 49, 0, 73, 25, 0, 41, 0, 9, 0, 17, 33, 0, 49,
0, 41, 0, 9, 0, 17, 33, 0, 49, 0, 73, 25, 0, 41, 0, 9, 0, 17, 33, 0, 49, 0, 73, 25, 0, 41,
0, 33, 0, 49, 0, 73, 25, 0, 41, 0, 9, 0, 17, 33, 0, 49, 0, 73, 25, 0, 41, 0, 9, 0, 17, 33,
0, 25, 0, 41, 0, 9, 0, 17, 33, 0, 49, 0, 73, 25, 0, 41, 0, 9, 0, 17, 33, 0, 49, 0, 73, 25
};
unsigned long tempoChangeTime = 0; // time of last touch for tempo change
void GlobalSettings::setSwitchAssignment(byte whichSwitch, byte assignment, boolean disableSame) {
if (Global.switchAssignment[whichSwitch] == assignment) {
if (disableSame) {
Global.switchAssignment[whichSwitch] = ASSIGNED_DISABLED;
}
}
else {
resetSwitchStates(whichSwitch);
Global.switchAssignment[whichSwitch] = assignment;
}
}
void switchSerialMode(boolean flag) {
if (controlModeActive) {
controlModeActive = false;
clearDisplay();
updateDisplay();
}
if (Device.operatingLowPower) {
Device.operatingLowPower = false;
applyLedInterval();
applyMidiInterval();
}
Device.serialMode = flag;
applySerialMode();
}
void applySerialMode() {
if (Device.serialMode) {
digitalWrite(35, HIGH);
digitalWrite(36, HIGH);
Serial.begin(115200);
Serial.flush();
}
else {
digitalWrite(35, LOW);
applyMidiIo();
}
}
void initializeStorage() {
byte bootblock = dueFlashStorage.read(0);
if (bootblock != 0) { // See if we need to boot from scratch
if (bootblock == 255) { // When a new firmware is uploaded, the first flash byte will be 255
switchSerialMode(true); // Start in serial mode after OS upgrade to be able to receive the settings
Device.serialMode = true;
firstTimeBoot = true;
}
else {
switchSerialMode(false); // Start in MIDI mode for all other bootblock values
Device.serialMode = false;
}
writeInitialProjectSettings();
writeSettingsToFlash(); // Store the initial default settings
dueFlashStorage.write(0, 0); // Zero out the firstTime location.
setDisplayMode(displayCalibration); // Automatically start calibration after firmware update.
initializeCalibrationSamples();
setLed(0, GLOBAL_SETTINGS_ROW, globalColor, cellOn);
controlButton = GLOBAL_SETTINGS_ROW;
}
else {
loadSettings(); // On subsequent startups, load settings from Flash
if (Device.calibrated) {
// if calibration data is not a plausible series of values, clear out
// all the calibration data and reset everything to defaults
// the validation is needed together with the CRC to weed out bad calibration
// data that could have been lingering from previous firmware versions
if (!validateAndHealCalibrationData()) {
initializeCalibrationData();
}
else if (!Device.calibrationHealed) {
uint32_t crc = calculateCalibrationCRC();
if (Device.calCrcCalculated) {
// if the calculated CRC doesn't match the stored one, clear out
// all the calibration data and reset everything to defaults
if (Device.calCrc != crc) {
initializeCalibrationData();
}
}
// calculate the CRC the first time the device starts up from a firmware
// that didn't calculate the CRC
else {
Device.calCrc = crc;
}
}
}
}
}
void storeSettings() {
if (!sequencerIsRunning()) {
Project.tempo = FXD4_TO_INT(fxd4CurrentTempo);
writeSettingsToFlash();
}
}
void writeAdaptivelyToFlash(uint32_t offset, byte* source, int length) {
// batch and slow down the flash storage in low power mode
if (Device.operatingLowPower) {
unsigned long now = millis();
// ensure that there's at least 50 milliseconds between refreshing the display lights and writing to flash
unsigned long displayModeDelta = calcTimeDelta(now, displayModeStart);
if (displayModeDelta < 50) {
delayUsec((50 - displayModeDelta) * 1000);
}
// write the configuration data
byte batchsize = 128;
int total = length;
int i = 0;
while (i+batchsize < total) {
dueFlashStorage.write(offset+i, source+i, batchsize);
i += batchsize;
delayUsec(100);
}
int remaining = total - i;
if (remaining > 0) {
dueFlashStorage.write(offset+i, source+i, remaining);
}
delayUsec(100);
}
// do the faster possible flash storage in regular power mode
else {
dueFlashStorage.write(offset, source, length);
}
}
void writeSettingsToFlash() {
DEBUGPRINT((2,"writeSettingsToFlash size="));
DEBUGPRINT((2,sizeof(Configuration)));
DEBUGPRINT((2," bytes"));
DEBUGPRINT((2,"\n"));
disableLedDisplay();
// read the marker to know which configuration version was last written successfully
byte marker = dueFlashStorage.read(SETTINGS_OFFSET);
// update the marker and the flash memory offset to now write to the other configuration version
// ensuring that the previous one remains coherent
uint32_t configOffset;
if (marker == 0) {
marker = 1;
configOffset = sizeof(Configuration);
}
else {
marker = 0;
configOffset = 0;
}
// write to flash, taking low power mode into account
writeAdaptivelyToFlash(SETTINGS_OFFSET+sizeof(unsigned long)+configOffset, (byte*)&config, sizeof(Configuration));
// write the marker after the configuration data so that this version becomes to latest coherent one
dueFlashStorage.write(SETTINGS_OFFSET, marker);
clearFullDisplay();
completelyRefreshLeds();
updateDisplay();
enableLedDisplay();
}
void loadSettings() {
// read the marker to know which configuration version was last written successfully
byte marker = dueFlashStorage.read(SETTINGS_OFFSET);
uint32_t configOffset = 0;
if (marker != 0) {
configOffset = sizeof(Configuration);
}
memcpy(&config, dueFlashStorage.readAddress(SETTINGS_OFFSET+sizeof(unsigned long)+configOffset), sizeof(Configuration));
}
void writeInitialProjectSettings() {
dueFlashStorage.write(PROJECTS_OFFSET, 0);
for (byte i = 0; i < PROJECT_INDEXES_COUNT; ++i) {
dueFlashStorage.write(PROJECT_INDEX_OFFSET(0, i), i);
dueFlashStorage.write(PROJECT_INDEX_OFFSET(1, i), i);
}
for (byte p = 0; p <= MAX_PROJECTS; ++p) {
writeProjectToFlashRaw(p);
}
}
void writeProjectToFlashRaw(byte project) {
// write to flash, taking low power mode into account
uint32_t projectOffset = PROJECTS_OFFSET + PROJECTS_MARKERS_SIZE + project * SINGLE_PROJECT_SIZE;
Project.tempo = FXD4_TO_INT(fxd4CurrentTempo);
writeAdaptivelyToFlash(projectOffset, (byte*)&Project, sizeof(SequencerProject));
}
void writeProjectToFlash(byte project) {
DEBUGPRINT((2,"writeProjectToFlash size="));
DEBUGPRINT((2,sizeof(SequencerProject)));
DEBUGPRINT((2," bytes"));
DEBUGPRINT((2,"\n"));
clearDisplayImmediately();
clearFullDisplay();
completelyRefreshLeds();
// read marker of the current index marker
byte marker = dueFlashStorage.read(PROJECTS_OFFSET);
// read the location of the temporary project storage
byte previousIndexes[PROJECT_INDEXES_COUNT];
memcpy(&previousIndexes, dueFlashStorage.readAddress(PROJECT_INDEX_OFFSET(marker, 0)), PROJECT_INDEXES_COUNT);
byte tmpIndex = previousIndexes[MAX_PROJECTS];
byte prjIndex = previousIndexes[project];
writeProjectToFlashRaw(tmpIndex);
// write the marker after the project data so that this version becomes to latest coherent one
byte newMarker = 1 - marker;
previousIndexes[project] = tmpIndex;
previousIndexes[MAX_PROJECTS] = prjIndex;
dueFlashStorage.write(PROJECT_INDEX_OFFSET(newMarker, 0), previousIndexes, PROJECT_INDEXES_COUNT);
dueFlashStorage.write(PROJECTS_OFFSET, newMarker);
updateDisplay();
}
void loadProject(byte project) {
// read the marker to know which configuration version was last written successfully
byte marker = dueFlashStorage.read(PROJECTS_OFFSET);
byte prjIndex = dueFlashStorage.read(PROJECT_INDEX_OFFSET(marker, project));
uint32_t projectOffset = PROJECTS_OFFSET + PROJECTS_MARKERS_SIZE + prjIndex * SINGLE_PROJECT_SIZE;
memcpy(&Project, dueFlashStorage.readAddress(projectOffset), sizeof(SequencerProject));
fxd4CurrentTempo = FXD4_FROM_INT(Project.tempo);
}
void applyPresetSettings() {
applyPitchCorrectHold();
applyLimitsForY();
applyLimitsForZ();
applyLimitsForVelocity();
applyMidiIo();
updateSplitMidiChannels(LEFT);
updateSplitMidiChannels(RIGHT);
}
void applyConfiguration() {
applyPresetSettings();
applySequencerSettings();
loadCustomLedLayer(getActiveCustomLedPattern());
}
void applySystemState() {
applyConfiguration();
applySerialMode();
}
void loadSettingsFromPreset(byte p) {
Device.lastLoadedPreset = p;
memcpy(&Global, &config.preset[p].global, sizeof(GlobalSettings));
memcpy(&Split[LEFT], &config.preset[p].split[LEFT], sizeof(SplitSettings));
memcpy(&Split[RIGHT], &config.preset[p].split[RIGHT], sizeof(SplitSettings));
applyPresetSettings();
}
void storeSettingsToPreset(byte p) {
memcpy(&config.preset[p].global, &Global, sizeof(GlobalSettings));
memcpy(&config.preset[p].split[LEFT], &Split[LEFT], sizeof(SplitSettings));
memcpy(&config.preset[p].split[RIGHT], &Split[RIGHT], sizeof(SplitSettings));
}
// The first time after new code is loaded into the Linnstrument, this sets the initial defaults of all settings.
// On subsequent startups, these values are overwritten by loading the settings stored in flash.
void initializeDeviceSettings() {
Device.version = 16;
Device.serialMode = false;
Device.sleepAnimationActive = false;
Device.sleepActive = false;
Device.sleepDelay = 0;
Device.sleepAnimationType = animationNone;
Device.operatingLowPower = false;
Device.otherHanded = false;
Device.splitHandedness = reversedBoth;
Device.minUSBMIDIInterval = DEFAULT_MIN_USB_MIDI_INTERVAL;
Device.midiThrough = false;
Device.lastLoadedPreset = -1;
Device.lastLoadedProject = -1;
Global.splitActive = false;
initializeAudienceMessages();
memcpy(&Device.customLeds[0][0], &CUSTOM_LEDS_PATTERN1[0], LED_LAYER_SIZE);
memcpy(&Device.customLeds[1][0], &CUSTOM_LEDS_PATTERN2[0], LED_LAYER_SIZE);
memset(&Device.customLeds[2][0], 0, LED_LAYER_SIZE);
}
void initializeAudienceMessages() {
for (byte msg = 0; msg < 16; ++msg) {
memset(Device.audienceMessages[msg], '\0', sizeof(Device.audienceMessages[msg]));
strncpy(Device.audienceMessages[msg], defaultAudienceMessages[msg], 30);
Device.audienceMessages[msg][30] = '\0';
}
}
void initializeNoteLights(GlobalSettings& g) {
g.activeNotes = 0;
// initialize accentNotes array. Starting with only C within each octave highlighted
for (byte count = 0; count < 12; ++count) {
g.accentNotes[count] = 1;
}
// initialize mainNotes array (all off).
for (byte count = 0; count < 12; ++count) {
g.mainNotes[count] = 0;
}
// Major
g.mainNotes[0] |= 1 << 0;
g.mainNotes[0] |= 1 << 2;
g.mainNotes[0] |= 1 << 4;
g.mainNotes[0] |= 1 << 5;
g.mainNotes[0] |= 1 << 7;
g.mainNotes[0] |= 1 << 9;
g.mainNotes[0] |= 1 << 11;
// Natural minor
g.mainNotes[1] |= 1 << 0;
g.mainNotes[1] |= 1 << 2;
g.mainNotes[1] |= 1 << 3;
g.mainNotes[1] |= 1 << 5;
g.mainNotes[1] |= 1 << 7;
g.mainNotes[1] |= 1 << 8;
g.mainNotes[1] |= 1 << 10;
// Harmonic minor
g.mainNotes[2] |= 1 << 0;
g.mainNotes[2] |= 1 << 2;
g.mainNotes[2] |= 1 << 3;
g.mainNotes[2] |= 1 << 5;
g.mainNotes[2] |= 1 << 7;
g.mainNotes[2] |= 1 << 8;
g.mainNotes[2] |= 1 << 11;
// Major Pentatonic
g.mainNotes[3] |= 1 << 0;
g.mainNotes[3] |= 1 << 2;
g.mainNotes[3] |= 1 << 4;
g.mainNotes[3] |= 1 << 7;
g.mainNotes[3] |= 1 << 9;
// Minor Pentatonic
g.mainNotes[4] |= 1 << 0;
g.mainNotes[4] |= 1 << 3;
g.mainNotes[4] |= 1 << 5;
g.mainNotes[4] |= 1 << 7;
g.mainNotes[4] |= 1 << 10;
// Major Blues
g.mainNotes[5] |= 1 << 0;
g.mainNotes[5] |= 1 << 3;
g.mainNotes[5] |= 1 << 4;
g.mainNotes[5] |= 1 << 7;
g.mainNotes[5] |= 1 << 9;
g.mainNotes[5] |= 1 << 10;
// Minor Blues
g.mainNotes[6] |= 1 << 0;
g.mainNotes[6] |= 1 << 3;
g.mainNotes[6] |= 1 << 5;
g.mainNotes[6] |= 1 << 6;
g.mainNotes[6] |= 1 << 7;
g.mainNotes[6] |= 1 << 10;
// Diminished
g.mainNotes[7] |= 1 << 0;
g.mainNotes[7] |= 1 << 2;
g.mainNotes[7] |= 1 << 3;
g.mainNotes[7] |= 1 << 5;
g.mainNotes[7] |= 1 << 6;
g.mainNotes[7] |= 1 << 8;
g.mainNotes[7] |= 1 << 9;
g.mainNotes[7] |= 1 << 11;
// Whole Tone
g.mainNotes[8] |= 1 << 0;
g.mainNotes[8] |= 1 << 2;
g.mainNotes[8] |= 1 << 4;
g.mainNotes[8] |= 1 << 6;
g.mainNotes[8] |= 1 << 8;
g.mainNotes[8] |= 1 << 10;
// Spanish (Phrygian Dominant)
g.mainNotes[9] |= 1 << 0;
g.mainNotes[9] |= 1 << 1;
g.mainNotes[9] |= 1 << 4;
g.mainNotes[9] |= 1 << 5;
g.mainNotes[9] |= 1 << 7;
g.mainNotes[9] |= 1 << 8;
g.mainNotes[9] |= 1 << 10;
// Gypsy (Hungarian Minor)
g.mainNotes[10] |= 1 << 0;
g.mainNotes[10] |= 1 << 2;
g.mainNotes[10] |= 1 << 3;
g.mainNotes[10] |= 1 << 6;
g.mainNotes[10] |= 1 << 7;
g.mainNotes[10] |= 1 << 8;
g.mainNotes[10] |= 1 << 10;
// Arabic (Major Locrian)
g.mainNotes[11] |= 1 << 0;
g.mainNotes[11] |= 1 << 2;
g.mainNotes[11] |= 1 << 4;
g.mainNotes[11] |= 1 << 5;
g.mainNotes[11] |= 1 << 6;
g.mainNotes[11] |= 1 << 8;
g.mainNotes[11] |= 1 << 10;
}
void initializeGuitarTuning(GlobalSettings& g) {
g.guitarTuning[0] = 30;
g.guitarTuning[1] = 35;
g.guitarTuning[2] = 40;
g.guitarTuning[3] = 45;
g.guitarTuning[4] = 50;
g.guitarTuning[5] = 55;
g.guitarTuning[6] = 59;
g.guitarTuning[7] = 64;
}
void initializeMidiSettings(byte split, PresetSettings& p) {
for (byte chan = 0; chan < 16; ++chan) {
focusCell[split][chan].col = 0;
focusCell[split][chan].row = 0;
}
p.split[split].midiMode = oneChannel;
p.split[split].midiChanPerRowReversed = false;
p.split[split].expressionForY = timbreCC74;
p.split[split].customCCForY = 74;
p.split[split].expressionForZ = loudnessPolyPressure;
p.split[split].bendRangeOption = bendRange2;
p.split[split].customBendRange = 24;
p.split[split].mpe = false;
// initialize values that differ between the keyboard splits
if (split == LEFT) {
p.split[LEFT].midiChanMain = 1;
p.split[LEFT].midiChanMainEnabled = true;
p.split[LEFT].midiChanSet[0] = false;
for (byte chan = 1; chan < 8; ++chan) {
p.split[LEFT].midiChanSet[chan] = true;
}
for (byte chan = 8; chan < 16; ++chan) {
p.split[LEFT].midiChanSet[chan] = false;
}
p.split[LEFT].midiChanPerRow = 1;
}
else if (split == RIGHT) {
p.split[RIGHT].midiChanMain = 16;
p.split[RIGHT].midiChanMainEnabled = true;
for (byte chan = 0; chan < 8; ++chan) {
p.split[RIGHT].midiChanSet[chan] = false;
}
for (byte chan = 8; chan < 15; ++chan) {
p.split[RIGHT].midiChanSet[chan] = true;
}
p.split[RIGHT].midiChanSet[15] = false;
p.split[RIGHT].midiChanPerRow = 9;
}
}
void initializePresetSettings() {
Global.splitActive = false;
for (byte n = 0; n < NUMPRESETS; ++n) {
presetBlinkStart[n] = 0;
PresetSettings& p = config.preset[n];
GlobalSettings& g = p.global;
if (LINNMODEL == 200) {
g.splitPoint = 12;
}
else if (LINNMODEL == 128) {
g.splitPoint = 9;
}
g.currentPerSplit = LEFT;
g.rowOffset = 5;
g.customRowOffset = 12;
g.velocitySensitivity = velocityMedium;
g.minForVelocity = DEFAULT_MIN_VELOCITY;
g.maxForVelocity = DEFAULT_MAX_VELOCITY;
g.valueForFixedVelocity = DEFAULT_FIXED_VELOCITY;
g.pressureSensitivity = pressureMedium;
g.pressureAftertouch = false;
g.midiIO = 1; // set to 1 for USB jacks (not MIDI jacks)
// initialize switch settings
g.switchAssignment[SWITCH_FOOT_L] = ASSIGNED_ARPEGGIATOR;
g.switchAssignment[SWITCH_FOOT_R] = ASSIGNED_SUSTAIN;
g.switchAssignment[SWITCH_SWITCH_1] = ASSIGNED_SUSTAIN;
g.switchAssignment[SWITCH_SWITCH_2] = ASSIGNED_ARPEGGIATOR;
g.switchAssignment[SWITCH_FOOT_B] = ASSIGNED_DISABLED;
g.switchBothSplits[SWITCH_FOOT_L] = false;
g.switchBothSplits[SWITCH_FOOT_R] = false;
g.switchBothSplits[SWITCH_SWITCH_1] = false;
g.switchBothSplits[SWITCH_SWITCH_2] = false;
g.switchBothSplits[SWITCH_FOOT_B] = false;
g.ccForSwitchCC65[SWITCH_FOOT_L] = 65;
g.ccForSwitchCC65[SWITCH_FOOT_R] = 65;
g.ccForSwitchCC65[SWITCH_SWITCH_1] = 65;
g.ccForSwitchCC65[SWITCH_SWITCH_2] = 65;
g.ccForSwitchCC65[SWITCH_FOOT_B] = 65;
g.ccForSwitchSustain[SWITCH_FOOT_L] = 64;
g.ccForSwitchSustain[SWITCH_FOOT_R] = 64;
g.ccForSwitchSustain[SWITCH_SWITCH_1] = 64;
g.ccForSwitchSustain[SWITCH_SWITCH_2] = 64;
g.ccForSwitchSustain[SWITCH_FOOT_B] = 64;
g.customSwitchAssignment[SWITCH_FOOT_L] = ASSIGNED_TAP_TEMPO;
g.customSwitchAssignment[SWITCH_FOOT_R] = ASSIGNED_TAP_TEMPO;
g.customSwitchAssignment[SWITCH_SWITCH_1] = ASSIGNED_TAP_TEMPO;
g.customSwitchAssignment[SWITCH_SWITCH_2] = ASSIGNED_TAP_TEMPO;
g.customSwitchAssignment[SWITCH_FOOT_B] = ASSIGNED_SEQUENCER_PLAY;
initializeNoteLights(g);
initializeGuitarTuning(g);
g.arpDirection = ArpReplayAll;
g.arpTempo = ArpSixteenthSwing;
g.arpOctave = 0;
g.sustainBehavior = sustainHold;
// initialize all identical values in the keyboard split data
for (byte s = 0; s < NUMSPLITS; ++s) {
p.split[s].sendX = true;
p.split[s].sendY = true;
p.split[s].sendZ = true;
p.split[s].pitchCorrectQuantize = true;
p.split[s].pitchCorrectHold = true;
p.split[s].pitchResetOnRelease = false;
p.split[s].minForY = 0;
p.split[s].maxForY = 127;
p.split[s].relativeY = false;
p.split[s].initialRelativeY = 64;
p.split[s].minForZ = 0;
p.split[s].maxForZ = 127;
p.split[s].customCCForZ = 11;
p.split[s].ccForZ14Bit = false;
memcpy(&p.split[s].ccForFader, ccFaderDefaults, sizeof(unsigned short)*8);
p.split[s].colorAccent = COLOR_CYAN;
p.split[s].colorLowRow = COLOR_YELLOW;
p.split[s].colorSequencerEmpty = COLOR_YELLOW;
p.split[s].colorSequencerEvent = COLOR_ORANGE;
p.split[s].colorSequencerDisabled = COLOR_LIME;
p.split[s].playedTouchMode = playedCell;
p.split[s].lowRowCCXBehavior = lowRowCCHold;
p.split[s].ccForLowRow = 1;
p.split[s].lowRowCCXYZBehavior = lowRowCCHold;
p.split[s].ccForLowRowX = 16;
p.split[s].ccForLowRowY = 17;
p.split[s].ccForLowRowZ = 18;
p.split[s].transposeOctave = 0;
p.split[s].transposePitch = 0;
p.split[s].transposeLights = 0;
p.split[s].arpeggiator = false;
p.split[s].ccFaders = false;
p.split[s].strum = false;
p.split[s].sequencer = false;
}
// initialize values that differ between the keyboard splits
initializeMidiSettings(LEFT, p);
p.split[LEFT].colorMain = COLOR_GREEN;
p.split[LEFT].colorPlayed = COLOR_RED;
p.split[LEFT].lowRowMode = lowRowNormal;
p.split[LEFT].sequencerView = sequencerScales;
initializeMidiSettings(RIGHT, p);
p.split[RIGHT].colorMain = COLOR_BLUE;
p.split[RIGHT].colorPlayed = COLOR_MAGENTA;
p.split[RIGHT].lowRowMode = lowRowNormal;
p.split[RIGHT].sequencerView = sequencerScales;
}
// we're initializing the current settings with preset 0
memcpy(&config.settings, &config.preset[0], sizeof(PresetSettings));
// preset 0 is pre-programmed for one channel sounds from our Logic example file
config.preset[0].split[LEFT].midiMode = oneChannel;
config.preset[0].split[RIGHT].midiMode = oneChannel;
config.preset[0].split[LEFT].bendRangeOption = bendRange12;
config.preset[0].split[RIGHT].bendRangeOption = bendRange12;
config.preset[0].split[LEFT].expressionForZ = loudnessPolyPressure;
config.preset[0].split[RIGHT].expressionForZ = loudnessPolyPressure;
// preset 1 is pre-programmed for channel per note sounds from our Logic example file
config.preset[1].split[LEFT].midiMode = channelPerNote;
config.preset[1].split[RIGHT].midiMode = channelPerNote;
config.preset[1].split[LEFT].bendRangeOption = bendRange24;
config.preset[1].split[RIGHT].bendRangeOption = bendRange24;
config.preset[1].split[LEFT].customBendRange = 48;
config.preset[1].split[RIGHT].customBendRange = 48;
config.preset[1].split[LEFT].expressionForZ = loudnessChannelPressure;
config.preset[1].split[RIGHT].expressionForZ = loudnessChannelPressure;
config.preset[1].split[LEFT].midiChanMain = 1;
config.preset[1].split[LEFT].midiChanSet[0] = false;
config.preset[1].split[RIGHT].midiChanMain = 16;
config.preset[1].split[RIGHT].midiChanSet[15] = false;
// preset 3 is pre-programmed for making drumbeats
config.preset[3].split[LEFT].midiMode = channelPerNote;
config.preset[3].split[RIGHT].midiMode = oneChannel;
config.preset[3].split[LEFT].bendRangeOption = bendRange2;
config.preset[3].split[RIGHT].bendRangeOption = bendRange24;
config.preset[3].split[LEFT].pitchCorrectHold = pitchCorrectHoldOff;
config.preset[3].split[RIGHT].pitchCorrectHold = pitchCorrectHoldOff;
config.preset[3].split[LEFT].expressionForZ = loudnessChannelPressure;
config.preset[3].split[RIGHT].expressionForZ = loudnessChannelPressure;
config.preset[3].split[LEFT].lowRowMode = lowRowArpeggiator;
config.preset[3].split[RIGHT].lowRowMode = lowRowArpeggiator;
config.preset[3].split[LEFT].arpeggiator = true;
config.preset[3].split[RIGHT].arpeggiator = true;
config.preset[3].global.arpDirection = ArpReplayAll;
config.preset[3].global.arpTempo = ArpSixteenthSwing;
config.preset[3].global.splitActive = true;
// initialize runtime data
applyPitchCorrectHold();
applyLimitsForY();
applyLimitsForZ();
applyLimitsForVelocity();
for (byte s = 0; s < NUMSPLITS; ++s) {
for (byte c = 0; c < 129; ++c) {
ccFaderValues[s][c] = 0;
}
ccFaderValues[s][7] = 63;
currentEditedCCFader[s] = 0;
midiPreset[0] = 0;
arpTempoDelta[s] = 0;
splitChannels[s].clear();
}
}
void applyPitchCorrectHold() {
for (byte sp = 0; sp < NUMSPLITS; ++sp) {
switch (Split[sp].pitchCorrectHold) {
case pitchCorrectHoldOff:
{
fxdPitchHoldSamples[sp] = FXD_MAKE(PITCH_CORRECT_HOLD_SAMPLES_DEFAULT);
fxdRateXThreshold[sp] = FXD_MAKE(RATEX_THRESHOLD_DEFAULT);
break;
}
case pitchCorrectHoldFast:
{
fxdPitchHoldSamples[sp] = FXD_MAKE(PITCH_CORRECT_HOLD_SAMPLES_FAST);
fxdRateXThreshold[sp] = FXD_MAKE(RATEX_THRESHOLD_FAST);
break;
}
case pitchCorrectHoldMedium:
{
fxdPitchHoldSamples[sp] = FXD_MAKE(PITCH_CORRECT_HOLD_SAMPLES_MEDIUM);
fxdRateXThreshold[sp] = FXD_MAKE(RATEX_THRESHOLD_MEDIUM);
break;
}
case pitchCorrectHoldSlow:
{
fxdPitchHoldSamples[sp] = FXD_MAKE(PITCH_CORRECT_HOLD_SAMPLES_SLOW);
fxdRateXThreshold[sp] = FXD_MAKE(RATEX_THRESHOLD_SLOW);
break;
}
}
}
}
void setBendRange(byte split, byte bendRange) {
applyBendRange(Split[split], bendRange);
midiSendMpePitchBendRange(split);
}
void applyBendRange(SplitSettings& target, byte bendRange) {
switch (bendRange) {
case 2:
target.bendRangeOption = bendRange2;
break;
case 3:
target.bendRangeOption = bendRange3;
break;
case 12:
target.bendRangeOption = bendRange12;
break;
default:
target.bendRangeOption = bendRange24;
target.customBendRange = bendRange;
break;
}
}
void applyLimitsForY() {
for (byte sp = 0; sp < NUMSPLITS; ++sp) {
int32_t fxd_range = FXD_FROM_INT(Split[sp].maxForY - Split[sp].minForY);
fxdLimitsForYRatio[sp] = FXD_DIV(fxd_range, FXD_CONST_127);
}
}
void applyLimitsForZ() {
for (byte sp = 0; sp < NUMSPLITS; ++sp) {
int32_t fxd_range = FXD_FROM_INT(Split[sp].maxForZ - Split[sp].minForZ);
fxdLimitsForZRatio[sp] = FXD_DIV(fxd_range, FXD_CONST_127);
}
}
void applyLimitsForVelocity() {
fxdMinVelOffset = FXD_FROM_INT(Global.minForVelocity * 8);
int32_t fxd_maxVelOffset = FXD_CONST_1016 - FXD_FROM_INT(Global.maxForVelocity * 8);
fxdVelRatio = FXD_DIV(FXD_CONST_1016 - fxdMinVelOffset - fxd_maxVelOffset, FXD_CONST_1016);
}
// Called to handle press events of the 8 control buttons
void handleControlButtonNewTouch() {
// if we're in the startup phase after a global reset
// a new press on a control button terminates the global reset state
// and makes sure that startup control button combination is reset
if (globalReset) {
globalReset = false;
cellTouched(0, 0, untouchedCell);
cellTouched(0, 2, untouchedCell);
}
// allow the sequencer to short-circuit the control button new touch
if (handleSequencerControlButtonNewTouch()) {
lastControlPress[sensorRow] = millis();
return;
}
// only allow one control button to be pressed at the same time
// this prevents phantom presses to occur for the control buttons
// this is not detectable with the regular phantom press algorithm
if ((rowsInColsTouched[0] & ~(1 << sensorRow)) != 0) {
return;
}
if (sensorRow != SWITCH_1_ROW &&
sensorRow != SWITCH_2_ROW) { // handle non-switch control buttons
if (sensorRow == SPLIT_ROW) { // the split control has custom toggle / hold behavior
if (controlButton != -1) {
return;
}
}
else if (controlButton == sensorRow) { // detect whether this is the toggle off of a previous control press
lastControlPress[sensorRow] = 0;
handleControlButtonRelease(); // in that case act as if it was a button release
return;
}
else if (controlButton != -1) { // automatically turn off the led of another previously pressed control button
clearLed(0, controlButton);
}
controlButton = sensorRow; // keep track of which control button we're handling
}
// determine whether a double-tap happened on the switch (ie. second tap within 500 ms)
boolean doubleTap = (calcTimeDelta(millis(), lastControlPress[sensorRow]) < 500);
lastControlPress[sensorRow] = millis(); // keep track of the last press
switch (sensorRow) { // which control button is it?
case GLOBAL_SETTINGS_ROW: // global settings button presssed
resetAllTouches();
lightLed(0, 0); // light the button
setDisplayMode(displayGlobal); // change to global settings display mode
resetNumericDataChange();
updateDisplay();
break;
case SPLIT_ROW: // SPLIT button pressed
resetAllTouches();
splitButtonDown = true;
changedSplitPoint = false;
setDisplayMode(displaySplitPoint);
// handle double-tap
if (doubleTap) {
Global.currentPerSplit = otherSplit(Global.currentPerSplit);
}
updateDisplay();
break;
case SWITCH_2_ROW: // SWITCH 2 pressed
doSwitchPressed(SWITCH_SWITCH_2);
updateSwitchLeds();
break;
case SWITCH_1_ROW: // SWITCH 1 pressed
if (isSequencerSettingsDisplayMode()) {
setDisplayMode(displayNormal);
cellTouched(ignoredCell);
updateDisplay();
updateSwitchLeds();
}
else if (displayMode == displayCustomLedsEditor) {
customLedColor = colorCycle(customLedColor, false);
updateDisplay();
}
else {
doSwitchPressed(SWITCH_SWITCH_1);
updateSwitchLeds();
}
break;
case OCTAVE_ROW: // OCTAVE button pressed
resetAllTouches();
setLed(0, OCTAVE_ROW, globalColor, cellOn);
setDisplayMode(displayOctaveTranspose);
updateDisplay();
break;
case VOLUME_ROW: // displayVolume button pressed
resetAllTouches();
setLed(0, VOLUME_ROW, globalColor, cellOn);
setDisplayMode(displayVolume);
updateDisplay();
break;
case PRESET_ROW: // displayPreset button pressed
resetAllTouches();
setLed(0, PRESET_ROW, globalColor, cellOn);
for (byte p = 0; p < NUMPRESETS; ++p) {
presetBlinkStart[p] = 0;
}
setDisplayMode(displayPreset);
resetNumericDataChange();
updateDisplay();
break;
case PER_SPLIT_ROW: // PER SPLIT SETTINGs buttons pressed
resetAllTouches();
setLed(0, PER_SPLIT_ROW, globalColor, cellOn);
setDisplayMode(displayPerSplit);
resetNumericDataChange();
updateDisplay();
break;
}
}
// Called to handle release events of the 8 control buttons
void handleControlButtonRelease() {
// unless we pressed a new control button, no control button releases in global reset
// phase will be taken into account, this is needed to allow users to release the
// control button startup combination without leaving calibration mode
if (globalReset) {
return;
}
// allow the sequencer to short-circuit the control button touch release
if (handleSequencerControlButtonRelease()) {
return;
}
if (sensorRow != SWITCH_1_ROW &&
sensorRow != SWITCH_2_ROW) { // don't allow simultaneous control buttons except for the switches
if (controlButton != sensorRow || // only handle the release of the control button that's currently pressed
(calcTimeDelta(millis(), lastControlPress[sensorRow]) <= SWITCH_HOLD_DELAY && // however if this was not a hold press, don't process the release either
controlButton != SPLIT_ROW)) { // except for the split row, who has its own hold behavior
return;
}
controlButton = -1; // keep track of which control button we're handling
}
switch (sensorRow) {
// Most of the buttons, when released, revert the display to normal
// and save the global settings which may have been changed.
case GLOBAL_SETTINGS_ROW: // global settings button released
if (displayMode == displayReset) {
// ensure that MPE is actively disabled before resetting
disableMpe(LEFT);
disableMpe(RIGHT);
// reset all values to default
reset();
}
// fallthrough is on purpose
case PER_SPLIT_ROW:
case OCTAVE_ROW: // octave button released
case VOLUME_ROW: // volume button released
case PRESET_ROW: // preset button released
clearLed(0, sensorRow);
setDisplayMode(displayNormal);
storeSettings();
updateDisplay();
break;
case SPLIT_ROW: // SPLIT button released
if (Split[otherSplit(Global.currentPerSplit)].sequencer) {
Global.currentPerSplit = otherSplit(Global.currentPerSplit);
setLed(0, SPLIT_ROW, globalColor, Global.splitActive ? cellOn : cellOff);
updateDisplay();
}
else if (splitButtonDown) {
splitButtonDown = false;
if (changedSplitPoint) {
storeSettings();
}
else {
Global.splitActive = !Global.splitActive;
}
setLed(0, SPLIT_ROW, globalColor, Global.splitActive ? cellOn : cellOff);
setDisplayMode(displayNormal);
updateDisplay();
}