This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
forked from rogerlinndesign/linnstrument-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ls_handleTouches.ino
2072 lines (1801 loc) · 77.1 KB
/
ls_handleTouches.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_handleTouches: LinnStrument Handle Touch Events ***********************
This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/
or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
***************************************************************************************************
These routines handle the processing of new touch events, continuous updates of touch events and
released touch events
**************************************************************************************************/
void cellTouched(TouchState state) {
cellTouched(sensorCol, sensorRow, state);
};
void cellTouched(byte col, byte row, TouchState state) {
// turn on the bit that correspond to the column and row of this cell,
// this allows us to very quickly find other touched cells and detect
// phantom key presses without having to evaluate every cell on the board
if (state != untouchedCell &&
state != transferCell) {
// keep track of how many cells are currently touched
if (!(rowsInColsTouched[col] & (int32_t)(1 << row))) {
cellsTouched++;
}
// flip the bits to indicate that this cell is now touched
rowsInColsTouched[col] |= (int32_t)(1 << row);
colsInRowsTouched[row] |= (int32_t)(1 << col);
}
// if the state is untouched, turn off the appropriate bit in the
// bitmasks that track the touched cells
else {
// keep track of how many cells are currently touched
if ((rowsInColsTouched[col] & (int32_t)(1 << row))) {
cellsTouched--;
}
// flip the bits to indicate that this cell is now untouched
rowsInColsTouched[col] &= ~(int32_t)(1 << row);
colsInRowsTouched[row] &= ~(int32_t)(1 << col);
}
// save the touched state for each cell
cell(col, row).touched = state;
}
#define TRANSFER_SLIDE_PROXIMITY 100
byte countTouchesForMidiChannel(byte split, byte col, byte row) {
if (!cell(col, row).hasNote()) {
return 0;
}
return noteTouchMapping[split].getMusicalTouchCount(cell(col, row).channel);
}
const int32_t PENDING_RELEASE_RATE_X = FXD_FROM_INT(5);
boolean potentialSlideTransferCandidate(byte col) {
if (controlModeActive) return false;
if (col < 1) return false;
if (userFirmwareActive) {
if (!userFirmwareSlideMode[sensorRow]) return false;
}
else if (Split[Global.currentPerSplit].sequencer) {
if (!requiresSequencerSlideTracking()) return false;
}
else {
if (sensorSplit != getSplitOf(col)) return false;
if (!isLowRow() && // don't perform slide transfers
(!Split[sensorSplit].sendX || // if pitch slides are disabled
!isFocusedCell(col, sensorRow) || // if this is not a focused cell
countTouchesForMidiChannel(sensorSplit, col, sensorRow) > 1)) { // when there are several touches for the same MIDI channel
return false;
}
if (isLowRow() && !lowRowRequiresSlideTracking()) return false;
if (isStrummingSplit(sensorSplit)) return false;
}
if (cell(col, sensorRow).pendingReleaseCount && // if there's a pending release but not enough X change
cell(col, sensorRow).fxdRateX <= PENDING_RELEASE_RATE_X) {
return false;
}
return cell(col, sensorRow).touched != untouchedCell && // the sibling cell has an active touch
(cell(col, sensorRow).pendingReleaseCount || // either a release is pending to be performed, or
abs(sensorCell->calibratedX() - cell(col, sensorRow).currentCalibratedX) < TRANSFER_SLIDE_PROXIMITY); // both cells are touched simultaneously on the edges
}
boolean isReadyForSlideTransfer(byte col) {
return cell(col, sensorRow).pendingReleaseCount || // there's a pending release waiting
sensorCell->currentRawZ > cell(col, sensorRow).currentRawZ; // the cell pressure is higher
}
boolean hasImpossibleX() { // checks whether the calibrated X is outside of the possible bounds for the current cell
return Device.calibrated &&
(sensorCell->calibratedX() < FXD_TO_INT(Device.calRows[sensorCol][0].fxdReferenceX - FXD_CALX_PHANTOM_RANGE) ||
sensorCell->calibratedX() > FXD_TO_INT(Device.calRows[sensorCol][0].fxdReferenceX + FXD_CALX_PHANTOM_RANGE));
}
void transferFromSameRowCell(byte col) {
TouchInfo* fromCell = &cell(col, sensorRow);
sensorCell->lastTouch = fromCell->lastTouch;
sensorCell->didMove = fromCell->didMove;
sensorCell->initialX = fromCell->initialX;
sensorCell->initialColumn = fromCell->initialColumn;
sensorCell->quantizationOffsetX = 0; // as soon as we transfer to an adjacent cell, the pitch quantization is reset to play the absolute pitch position instead
sensorCell->lastMovedX = fromCell->lastMovedX;
sensorCell->fxdRateX = fromCell->fxdRateX;
sensorCell->fxdRateCountX = fromCell->fxdRateCountX;
sensorCell->slideTransfer = true;
sensorCell->rogueSweepX = fromCell->rogueSweepX;
sensorCell->initialY = fromCell->initialY;
sensorCell->note = fromCell->note;
sensorCell->channel = fromCell->channel;
sensorCell->octaveOffset = fromCell->octaveOffset;
sensorCell->fxdPrevPressure = fromCell->fxdPrevPressure;
sensorCell->fxdPrevTimbre = fromCell->fxdPrevTimbre;
sensorCell->velocity = fromCell->velocity;
sensorCell->vcount = fromCell->vcount;
noteTouchMapping[sensorSplit].changeCell(sensorCell->note, sensorCell->channel, sensorCol, sensorRow);
fromCell->lastTouch = 0;
fromCell->didMove = false;
fromCell->initialX = INVALID_DATA;
fromCell->initialColumn = -1;
fromCell->quantizationOffsetX = 0;
fromCell->lastMovedX = 0;
fromCell->fxdRateX = 0;
fromCell->fxdRateCountX = 0;
fromCell->slideTransfer = true;
fromCell->rogueSweepX = false;
fromCell->initialY = -1;
fromCell->pendingReleaseCount = 0;
fromCell->note = -1;
fromCell->channel = -1;
fromCell->octaveOffset = 0;
fromCell->fxdPrevPressure = 0;
fromCell->fxdPrevTimbre = FXD_CONST_255;
fromCell->velocity = 0;
// do not reset vcount!
signed char channel = sensorCell->channel;
if (channel > 0 && col == focus(sensorSplit, channel).col && sensorRow == focus(sensorSplit, channel).row) {
focus(sensorSplit, channel).col = sensorCol;
focus(sensorSplit, channel).row = sensorRow;
}
}
void transferToSameRowCell(byte col) {
TouchInfo* toCell = &cell(col, sensorRow);
toCell->lastTouch = sensorCell->lastTouch;
toCell->didMove = sensorCell->didMove;
toCell->initialX = sensorCell->initialX;
toCell->initialColumn = sensorCell->initialColumn;
toCell->quantizationOffsetX = 0; // as soon as we transfer to an adjacent cell, the pitch quantization is reset to play the absolute pitch position instead
toCell->lastMovedX = sensorCell->lastMovedX;
toCell->fxdRateX = sensorCell->fxdRateX;
toCell->fxdRateCountX = sensorCell->fxdRateCountX;
toCell->slideTransfer = true;
toCell->rogueSweepX = sensorCell->rogueSweepX;
toCell->initialY = sensorCell->initialY;
toCell->note = sensorCell->note;
toCell->channel = sensorCell->channel;
toCell->octaveOffset = sensorCell->octaveOffset;
toCell->fxdPrevPressure = sensorCell->fxdPrevPressure;
toCell->fxdPrevTimbre = sensorCell->fxdPrevTimbre;
toCell->velocity = sensorCell->velocity;
toCell->vcount = sensorCell->vcount;
noteTouchMapping[sensorSplit].changeCell(toCell->note, toCell->channel, col, sensorRow);
sensorCell->lastTouch = 0;
sensorCell->didMove = false;
sensorCell->initialX = INVALID_DATA;
sensorCell->initialColumn = -1;
sensorCell->quantizationOffsetX = 0;
sensorCell->lastMovedX = 0;
sensorCell->fxdRateX = 0;
sensorCell->fxdRateCountX = 0;
sensorCell->slideTransfer = true;
sensorCell->rogueSweepX = false;
sensorCell->initialY = -1;
sensorCell->pendingReleaseCount = 0;
sensorCell->note = -1;
sensorCell->channel = -1;
sensorCell->octaveOffset = 0;
sensorCell->fxdPrevPressure = 0;
sensorCell->fxdPrevTimbre = FXD_CONST_255;
sensorCell->velocity = 0;
// do not reset vcount!
signed char channel = toCell->channel;
if (channel > 0 && sensorCol == focus(sensorSplit, channel).col && sensorRow == focus(sensorSplit, channel).row) {
focus(sensorSplit, channel).col = col;
focus(sensorSplit, channel).row = sensorRow;
}
}
boolean isPhantomTouchIndividual() {
// when the device is calibrated we fully rely on the plausability of the X readings to determine
// if a touch is a phantom touch or not
if (Device.calibrated) {
if (hasImpossibleX()) {
sensorCell->setPhantoms(sensorCol, sensorCol, sensorRow, sensorRow);
return true;
}
}
return false;
}
boolean isPhantomTouchContextual() {
// check if this is a potential corner of a rectangle to filter out ghost notes, this first check matches
// any cells that have other cells on the same row and column, so it's not sufficient by itself, but it's fast
int32_t rowsInSensorColTouched = rowsInColsTouched[sensorCol] & ~(int32_t)(1 << sensorRow);
int32_t colsInSensorRowTouched = colsInRowsTouched[sensorRow] & ~(int32_t)(1 << sensorCol);
if (rowsInSensorColTouched && colsInSensorRowTouched) {
// now we check each touched row in the column of the current sensor
// we gradually flip the touched bits to zero until they're all turned off
// this allows us to loop only over the touched rows, and none other
while (rowsInSensorColTouched) {
// we use the ARM Cortex-M3 instruction that reports the leading bit zeros of any number
// we determine that the left-most bit is that is turned on by substracting the leading zero
// count from the bitdepth of a 32-bit int
byte touchedRow = 31 - __builtin_clz(rowsInSensorColTouched);
// for each touched row we also check each touched column in the row of the current sensor
int32_t colsInRowTouched = colsInSensorRowTouched;
// we use the same looping approach as explained for the rows
while (colsInRowTouched) {
// we use the same leading zeros approach to dermine the left-most active bit
byte touchedCol = 31 - __builtin_clz(colsInRowTouched);
// if we find a cell that has both the touched row and touched column set,
// then the current sensor completed a rectangle by being the fourth corner
if (rowsInColsTouched[touchedCol] & (int32_t)(1 << touchedRow)) {
// since we found four corners, we now have to determine which ones are
// real presses and which ones are phantom presses, so we're looking for
// the other corner that was scanned twice to determine which one has the
// lowest pressure, this is the most likely to be the phantom press
if ((cell(touchedCol, touchedRow).isHigherPhantomPressure(sensorCell->currentRawZ) &&
cell(sensorCol, touchedRow).isHigherPhantomPressure(sensorCell->currentRawZ) &&
cell(touchedCol, sensorRow).isHigherPhantomPressure(sensorCell->currentRawZ))) {
// store coordinates of the rectangle, which also serves as an indicator that we
// should stop looking for a phantom press
cell(sensorCol, sensorRow).setPhantoms(sensorCol, touchedCol, sensorRow, touchedRow);
cell(touchedCol, touchedRow).setPhantoms(sensorCol, touchedCol, sensorRow, touchedRow);
cell(sensorCol, touchedRow).setPhantoms(sensorCol, touchedCol, sensorRow, touchedRow);
cell(touchedCol, sensorRow).setPhantoms(sensorCol, touchedCol, sensorRow, touchedRow);
return true;
}
}
// turn the left-most active bit off, to continue the iteration over the touched columns
colsInRowTouched &= ~(1 << touchedCol);
}
// turn the left-most active bit off, to continue the iteration over the touched rows
rowsInSensorColTouched &= ~(1 << touchedRow);
}
}
return false;
}
byte countTouchesInColumn() {
byte count = 0;
int32_t rowsInSensorColTouched = rowsInColsTouched[sensorCol];
if (rowsInSensorColTouched) {
while (rowsInSensorColTouched) {
byte touchedRow = 31 - __builtin_clz(rowsInSensorColTouched);
count++;
// turn the left-most active bit off, to continue the iteration over the touched rows
rowsInSensorColTouched &= ~(int32_t)(1 << touchedRow);
}
}
return count;
}
boolean hasOtherTouchInSplit(byte split) {
for (int r = 0; r < NUMROWS; ++r) {
int32_t colsInRowTouchedAdapted = colsInRowsTouched[r];
if (r == sensorRow) {
colsInRowTouchedAdapted &= ~(int32_t)(1 << sensorCol);
}
if (colsInRowTouchedAdapted) {
// if split is not active and there's a touch on the row, it's obviously in the current split
if (!Global.splitActive) {
return true;
}
// determine which columns need to be active in the touched row for this to be considered
// part of either split
if (split == LEFT && (colsInRowTouchedAdapted & ((int32_t)(1 << Global.splitPoint) - 1))) {
return true;
}
if (split == RIGHT && (colsInRowTouchedAdapted & ~((int32_t)(1 << Global.splitPoint) - 1))) {
return true;
}
}
}
return false;
}
boolean hasTouchInSplitOnRow(byte split, byte row) {
if (colsInRowsTouched[row]) {
// if split is not active and there's a touch on the row, it's obviously in the current split
if (!Global.splitActive) {
return true;
}
// determine which columns need to be active in the touched row for this to be considered
// part of either split
if (split == LEFT && (colsInRowsTouched[row] & ((int32_t)(1 << Global.splitPoint) - 1))) {
return true;
}
if (split == RIGHT && (colsInRowsTouched[row] & ~((int32_t)(1 << Global.splitPoint) - 1))) {
return true;
}
}
return false;
}
void handleSlideTransferCandidate(byte siblingCol) {
// if the pressure gets higher than adjacent cell, the slide is transitioning over
if (isReadyForSlideTransfer(siblingCol)) {
transferFromSameRowCell(siblingCol);
// if a slide transfer happened, but the pitch hold was still quantized, reset the
// X rate and threshold exceed count so that the real X position will be used as soon as
// the transfer cell is active, this makes the onset of slides from a stationary position
// smoother when quantize hold is on
if (fxdRateXThreshold[sensorSplit] - sensorCell->fxdRateX > 0) {
sensorCell->fxdRateX = fxdRateXThreshold[sensorSplit];
sensorCell->fxdRateCountX = 0;
}
if (userFirmwareActive) {
// if user firmware is active, we implement a particular transition scheme to allow touches to be tracked over MIDI
sensorCell->note = sensorCol;
midiSendControlChange(119, siblingCol, sensorCell->channel, true);
midiSendNoteOn(LEFT, sensorCol, sensorCell->velocity, sensorCell->channel);
midiSendNoteOffWithVelocity(LEFT, siblingCol, sensorCol, sensorCell->channel);
}
else {
if (Split[sensorSplit].colorPlayed && Split[sensorSplit].playedTouchMode == playedCell) {
setLed(siblingCol, sensorRow, COLOR_OFF, cellOff, LED_LAYER_PLAYED);
if (cell(sensorCol, sensorRow).hasNote()) {
setLed(sensorCol, sensorRow, Split[sensorSplit].colorPlayed, cellOn, LED_LAYER_PLAYED);
}
}
}
if (cell(siblingCol, sensorRow).touched != untouchedCell) {
cellTouched(siblingCol, sensorRow, transferCell);
}
handleXYZupdate();
}
// otherwise act as if this new touch never happend
else {
cellTouched(transferCell);
}
}
boolean handleNewTouch() {
DEBUGPRINT((1,"handleNewTouch"));
DEBUGPRINT((1," col="));DEBUGPRINT((1,(int)sensorCol));
DEBUGPRINT((1," row="));DEBUGPRINT((1,(int)sensorRow));
DEBUGPRINT((1," velocityZ="));DEBUGPRINT((1,(int)sensorCell->velocityZ));
DEBUGPRINT((1," pressureZ="));DEBUGPRINT((1,(int)sensorCell->pressureZ));
DEBUGPRINT((1,"\n"));
lastTouchMoment = millis();
// if the touches are restricted to a particular row, any touch outside this row is ignored
if (restrictedRow != -1 && sensorRow != restrictedRow) {
cellTouched(ignoredCell);
return false;
}
// allow any new touch to cancel scrolling
if (animationActive) {
stopAnimation = true;
cellTouched(ignoredCell);
return false;
}
// any touch will wake up LinnStrument again, and should be ignored
if (displayMode == displaySleep) {
cellTouched(ignoredCell);
setDisplayMode(displayNormal);
updateDisplay();
return false;
}
boolean result = false;
cellTouched(touchedCell); // mark this cell as touched
// if it's a command button, handle it
if (sensorCol == 0) {
if (controlModeActive) {
switchSerialMode(false);
return false;
}
// check if we should activate sleep mode
if ((sensorRow == GLOBAL_SETTINGS_ROW && cell(0, PER_SPLIT_ROW).touched == touchedCell) ||
(sensorRow == PER_SPLIT_ROW && cell(0, GLOBAL_SETTINGS_ROW).touched == touchedCell)) {
activateSleepMode();
return false;
}
// user firmware mode only handles the global settings command button
if (!userFirmwareActive || sensorRow == GLOBAL_SETTINGS_ROW) {
if (sensorRow != SWITCH_1_ROW && // if commands buttons are pressed that are not the two switches
sensorRow != SWITCH_2_ROW) { // only activate them if there's note being played on the playing surface
for (int r = 0; r < NUMROWS; ++r) { // this prevents accidental settings modifications while playing
if ((colsInRowsTouched[r] & ~(int32_t)(1)) != 0) {
cellTouched(ignoredCell);
return false;
}
}
}
handleControlButtonNewTouch();
}
}
else { // or if it's in column 1-25...
switch (displayMode)
{
case displaySplitPoint: // if the Split button is held, this touch changes the split point
if (splitButtonDown) {
handleSplitPointNewTouch();
break;
}
// If we get here, we're displaying in displaySplitPoint mode, but we've just gotten a normal new touch.
// THE FALL THROUGH HERE (no break statement) IS PURPOSEFUL!
case displayNormal: // it's normal performance mode
case displayVolume: // it's a volume change
// check if the new touch could be an ongoing slide to the right
if (potentialSlideTransferCandidate(sensorCol-1)) {
handleSlideTransferCandidate(sensorCol-1);
}
// check if the new touch could be an ongoing slide to the left
else if (potentialSlideTransferCandidate(sensorCol+1)) {
handleSlideTransferCandidate(sensorCol+1);
}
// only allow a certain number of touches in a single column to prevent cross talk
else if (countTouchesInColumn() > MAX_TOUCHES_IN_COLUMN) {
cellTouched(ignoredCell);
}
// this is really a new touch without any relationship to an ongoing slide
// however, it could be the low row and in certain situations it doesn't allow new touches
else if (!isLowRow() || allowNewTouchOnLowRow()) {
initVelocity();
calcVelocity(sensorCell->velocityZ);
result = true;
}
else {
cellTouched(untouchedCell);
}
break;
default:
initVelocity();
calcVelocity(sensorCell->velocityZ);
result = true;
break;
}
}
return result;
}
// Calculate the transposed note number for the current cell by taken the transposition settings into account
short cellTransposedNote(byte split) {
return transposedNote(split, sensorCol, sensorRow);
}
short transposedNote(byte split, byte col, byte row) {
return getNoteNumber(split, col, row) + Split[split].transposePitch + Split[split].transposeOctave;
}
// Check if the currently scanned cell is a focused cell
boolean isFocusedCell() {
return isFocusedCell(sensorCol, sensorRow);
}
// Check if a specific cell is a focused cell
boolean isFocusedCell(byte col, byte row) {
if (cell(col, row).channel < 1) {
return false;
}
FocusCell& focused = focus(getSplitOf(col), cell(col, row).channel);
return col == focused.col && row == focused.row;
}
// Check if X expression should be sent for this cell
boolean isXExpressiveCell() {
return isFocusedCell();
}
boolean isXExpressiveCell(byte col, byte row) {
return isFocusedCell(col, row);
}
// Check if Y expression should be sent for this cell
boolean isYExpressiveCell() {
if (Split[sensorSplit].expressionForY == timbrePolyPressure) {
return true;
}
else {
return isFocusedCell();
}
}
// Check if Z expression should be sent for this cell
boolean isZExpressiveCell() {
if (Split[sensorSplit].expressionForZ == loudnessPolyPressure) {
return true;
}
else {
return isFocusedCell();
}
}
byte takeChannel(byte split, byte row) {
switch (Split[split].midiMode)
{
case channelPerNote:
{
return splitChannels[split].take();
}
case channelPerRow:
{
byte channel = Split[split].midiChanPerRow;
if (Split[split].midiChanPerRowReversed) {
channel += (NUMROWS - 1) - row;
}
else {
channel += row;
}
if (channel > 16) {
channel -= 16;
}
return channel;
}
case oneChannel:
default:
{
return Split[split].midiChanMain;
}
}
}
void handleNonPlayingTouch() {
switch (displayMode) {
case displayNormal:
case displaySplitPoint:
case displayVolume:
case displayReset:
case displayAnimation:
case displaySleep:
// handled elsewhere
break;
case displayPerSplit:
handlePerSplitSettingNewTouch();
break;
case displayPreset:
handlePresetNewTouch();
break;
case displayBendRange:
handleBendRangeNewTouch();
break;
case displayLimitsForY:
handleLimitsForYNewTouch();
break;
case displayCCForY:
handleCCForYNewTouch();
break;
case displayInitialForRelativeY:
handleInitialForRelativeYNewTouch();
break;
case displayLimitsForZ:
handleLimitsForZNewTouch();
break;
case displayCCForZ:
handleCCForZNewTouch();
break;
case displayPlayedTouchModeConfig:
handlePlayedTouchModeNewTouch();
break;
case displayCCForFader:
handleCCForFaderNewTouch();
break;
case displayLowRowCCXConfig:
handleLowRowCCXConfigNewTouch();
break;
case displayLowRowCCXYZConfig:
handleLowRowCCXYZConfigNewTouch();
break;
case displayCCForSwitchCC65:
handleCCForSwitchCC65ConfigNewTouch();
break;
case displayCCForSwitchSustain:
handleCCForSwitchSustainConfigNewTouch();
break;
case displayCustomSwitchAssignment:
handleCustomSwitchAssignmentConfigNewTouch();
break;
case displayLimitsForVelocity:
handleLimitsForVelocityNewTouch();
break;
case displayValueForFixedVelocity:
handleValueForFixedVelocityNewTouch();
break;
case displaySleepConfig:
handleSleepConfigNewTouch();
break;
case displaySplitHandedness:
handleSplitHandednessNewTouch();
break;
case displayRowOffset:
handleRowOffsetNewTouch();
break;
case displayGuitarTuning:
handleGuitarTuningNewTouch();
break;
case displayMinUSBMIDIInterval:
handleMinUSBMIDIIntervalNewTouch();
break;
case displayMIDIThrough:
handleMIDIThroughNewTouch();
break;
case displaySensorSensitivityZ:
handleSensorSensitivityZNewTouch();
break;
case displaySensorLoZ:
handleSensorLoZNewTouch();
break;
case displaySensorFeatherZ:
handleSensorFeatherZNewTouch();
break;
case displaySensorRangeZ:
handleSensorRangeZNewTouch();
break;
case displayOctaveTranspose:
handleOctaveTransposeNewTouch();
break;
case displayGlobal:
case displayGlobalWithTempo:
handleGlobalSettingNewTouch();
break;
case displayOsVersion:
setDisplayMode(displayOsVersionBuild);
updateDisplay();
break;
case displayOsVersionBuild:
setDisplayMode(displayOsVersion);
updateDisplay();
break;
case displayCalibration:
initVelocity();
break;
case displayEditAudienceMessage:
handleEditAudienceMessageNewTouch();
break;
case displaySequencerProjects:
handleSequencerProjectsNewTouch();
break;
case displaySequencerDrum0107:
handleSequencerDrum0107NewTouch();
break;
case displaySequencerDrum0814:
handleSequencerDrum0814NewTouch();
break;
case displaySequencerColors:
handleSequencerColorsNewTouch();
break;
case displayCustomLedsEditor:
handleCustomLedsEditorNewTouch();
break;
}
}
// handleXYZupdate:
// Called when a cell is held, in order to read X, Y or Z movements and send MIDI messages as appropriate
// Returns a flag to indicate if the performance loop can be short-circuited
boolean handleXYZupdate() {
// if the touch is in the control buttons column, ignore it
if (sensorCol == 0 &&
// except for user firmware mode where only the global settings button is ignored for continuous updates
(!userFirmwareActive || sensorRow == GLOBAL_SETTINGS_ROW)) return false;
// if this data point serves as a calibration sample, return immediately
if (handleCalibrationSample()) return false;
// some features need hold functionality
if (sensorCell->velocity) {
switch (displayMode) {
case displayPerSplit:
handlePerSplitSettingHold();
return false;
case displayPreset:
handlePresetHold();
return false;
case displayGlobal:
case displayGlobalWithTempo:
handleGlobalSettingHold();
return false;
case displaySequencerProjects:
handleSequencerProjectsHold();
break;
case displaySensorSensitivityZ:
handleSensorSensitivityZHold();
break;
case displayCustomLedsEditor:
handleCustomLedsEditorHold();
return false;
default:
// other displays don't need hold features
break;
}
}
VelocityState velState = calcVelocity(sensorCell->velocityZ);
// velocity calculation works in stages, handle each one
boolean newVelocity = false;
switch (velState) {
// when the velocity is being calculated, the performance loop can be short-circuited
case velocityCalculating:
return true;
case velocityNew:
if (isPhantomTouchIndividual() || isPhantomTouchContextual()) {
cellTouched(untouchedCell);
return false;
}
// mark this as a valid new velocity and process it as such further down the method
newVelocity = true;
break;
case velocityCalculated:
// velocity has been calculated, no need to short-circuit anymore and we can continue
// with the main touch logic
break;
}
// only continue if the active display modes require finger tracking
if (displayMode != displayNormal &&
displayMode != displayVolume &&
(displayMode != displaySplitPoint || splitButtonDown)) {
// check if this should be handled as a non-playing touch
if (newVelocity) {
handleNonPlayingTouch();
performContinuousTasks();
}
return false;
}
DEBUGPRINT((2,"handleXYZupdate"));
DEBUGPRINT((2," col="));DEBUGPRINT((2,(int)sensorCol));
DEBUGPRINT((2," row="));DEBUGPRINT((2,(int)sensorRow));
DEBUGPRINT((2," velocityZ="));DEBUGPRINT((2,(int)sensorCell->velocityZ));
DEBUGPRINT((2," pressureZ="));DEBUGPRINT((2,(int)sensorCell->pressureZ));
DEBUGPRINT((2,"\n"));
lastTouchMoment = millis();
// turn off note handling and note expression features for low row, volume, cc faders and strumming
boolean handleNotes = true;
// in user firmware mode, everything is always encoded as MIDI notes and information
if (userFirmwareActive) {
handleNotes = true;
}
// in regular firmware mode, some features need special non-MIDI note handling
else if (isLowRow() ||
displayMode == displayVolume ||
Split[sensorSplit].ccFaders ||
Split[Global.currentPerSplit].sequencer ||
isStrummingSplit(sensorSplit)) {
handleNotes = false;
}
// this cell corresponds to a playing note
if (newVelocity) {
sensorCell->lastTouch = millis();
sensorCell->didMove = false;
sensorCell->lastMovedX = 0;
sensorCell->lastValueX = INVALID_DATA;
sensorCell->shouldRefreshX = true;
sensorCell->initialX = INVALID_DATA;
sensorCell->quantizationOffsetX = 0;
sensorCell->fxdRateCountX = fxdPitchHoldSamples[sensorSplit];
if (userFirmwareActive) {
handleNewUserFirmwareTouch();
}
else if (controlModeActive) {
handleNewControlModeTouch();
}
// is this cell used for low row functionality
else if (isLowRow()) {
lowRowStart();
}
// Split strum only triggers notes in the other split
else if (isStrummingSplit(sensorSplit)) {
handleSplitStrum();
}
else if (handleNotes) {
short notenum = cellTransposedNote(sensorSplit);
// if there was a previous note and automatic octave switching is enabled,
// check if the conditions are met to change the octave up or down while playing
if (latestNoteNumberForAutoOctave != -1 && isSwitchAutoOctavePressed(sensorSplit)) {
short octaveChange = 0;
// if the previous note was at least a perfect fifth lower, transpose one octave down
// since the arpeggio would be in a downward movement
if (notenum - latestNoteNumberForAutoOctave >= 7) {
octaveChange = -12;
}
// if the previous note was at least a perfect fifth higher, transpose one octave up
// since the arpeggio would be in a upward movement
else if (notenum - latestNoteNumberForAutoOctave <= -7) {
octaveChange = 12;
}
// apply the automatic octave change and adapt the note number
if (octaveChange != 0) {
Split[sensorSplit].transposeOctave = constrain(Split[sensorSplit].transposeOctave + octaveChange, -60, 60);
notenum += octaveChange;
// switching octaves might turn off some note cells since they fall outside of the MIDI note range
updateDisplay();
}
}
// if the note number is outside of MIDI range, don't start it
if (notenum >= 0 && notenum <= 127) {
prepareNewNote(notenum);
}
}
}
// we don't need to handle any expression in control mode
if (controlModeActive && !newVelocity) {
return false;
}
// get the processed expression data
short valueX = INVALID_DATA;
short valueY = INVALID_DATA;
unsigned short valueZHi = handleZExpression();
byte valueZ = scale1016to127(valueZHi, true);
performContinuousTasks();
// Only process x and y data when there's meaningful pressure on the cell
if (sensorCell->isMeaningfulTouch() || (doQuantizeHold() && isQuantizeHoldStable())) {
valueX = handleXExpression();
if (valueX != INVALID_DATA && isLeftHandedSplit(sensorSplit)) {
valueX = -1 * valueX;
}
performContinuousTasks();
sensorCell->lastValueX = valueX;
}
short tempY = handleYExpression();;
if (tempY == 0 || tempY == 127 || sensorCell->isMeaningfulTouch()) {
valueY = tempY;
}
performContinuousTasks();
// update the low row state, but not for the low row cells themselves when there's a new velocity
// this is handled in lowRowStart, and immediately calling handleLowRowState will wrongly handle the
// low row state transitions
if ((!newVelocity || !isLowRow()) && !userFirmwareActive) {
handleLowRowState(newVelocity, valueX, valueY, valueZ);
}
// the volume fader has its own operation mode
if (displayMode == displayVolume) {
if (sensorCell->isMeaningfulTouch()) {
handleVolumeNewTouch(newVelocity);
}
}
else if (Split[sensorSplit].ccFaders && !userFirmwareActive) {
if (sensorCell->isMeaningfulTouch()) {
handleFaderTouch(newVelocity);
}
}
else if (Split[Global.currentPerSplit].sequencer && !userFirmwareActive) {
if (sensorCell->isMeaningfulTouch()) {
handleSequencerTouch(newVelocity);
}
}
else if (handleNotes && sensorCell->hasNote()) {
if (userFirmwareActive) {
// don't send expression data for the control switches
if (sensorCol != 0) {
// Z-axis movements are encoded using Poly Pressure with the note as the column and the channel as the row
if (userFirmwareZActive[sensorRow]) {
midiSendPolyPressure(sensorCell->note, valueZ, sensorCell->channel);
}
// X-axis movements are encoded in 14-bit with MIDI CC 0-25 / 32-57 as the column and the channel as the row
if (userFirmwareXActive[sensorRow] && valueX != INVALID_DATA) {
short positionX = valueX + FXD_TO_INT(sensorCell->fxdInitialReferenceX());
// compensate for the -85 offset at the left side since 0 is positioned at the center of the left-most cell
positionX = positionX + 85;
midiSendControlChange14BitUserFirmware(sensorCol, sensorCol+32, positionX, sensorCell->channel);
}
// Y-axis movements are encoded using MIDI CC 64-89 as the column and the channel as the row
if (userFirmwareYActive[sensorRow] && valueY != INVALID_DATA) {
midiSendControlChange(sensorCol+64, valueY, sensorCell->channel);
}
}
}
else {
// if X-axis movements are enabled and it's a candidate for
// X/Y expression based on the MIDI mode and the currently held down cells
if (valueX != INVALID_DATA &&
Split[sensorSplit].sendX && isXExpressiveCell() && !isLowRowBendActive(sensorSplit)) {
int pitch = valueX;
// if there are several touches for the same MIDI channel (for instance in one channel mode)
// we average the X values to have only one global X value for those touches
if (countTouchesForMidiChannel(sensorSplit, sensorCol, sensorRow) > 2) {
// start with the current sensor's pitch and note
int highestNotePitch = valueX;
signed char highestNote = sensorCell->note;
// start with the current sensor's X value
long averagePitch = valueX;
byte averageDivider = 1;
// iterate over all the rows
for (byte row = 0; row < NUMROWS; ++row) {
// exclude the current sensor for the rest of the logic, we already
// took it into account
int32_t colsInRowTouched = colsInRowsTouched[row];
if (row == sensorRow) {
colsInRowTouched = colsInRowTouched & ~(1 << sensorCol);
}
// continue while there are touched columns in the row
while (colsInRowTouched) {
byte touchedCol = 31 - __builtin_clz(colsInRowTouched);
// add the X value of the cell to the average that's being calculated if the cell
// is on the same channel
if (cell(touchedCol, row).touched == touchedCell &&
cell(touchedCol, row).lastValueX != INVALID_DATA &&
cell(touchedCol, row).channel == sensorCell->channel) {
if (cell(touchedCol, row).note >= highestNote) {
highestNote = cell(touchedCol, row).note;
highestNotePitch = cell(touchedCol, row).lastValueX;
}
averagePitch += cell(touchedCol, row).lastValueX;
averageDivider++;
}
// exclude the cell we just processed by flipping its bit
colsInRowTouched &= ~(1 << touchedCol);
}
}