-
Notifications
You must be signed in to change notification settings - Fork 2
/
remi_3.ino
1746 lines (1510 loc) · 57.6 KB
/
remi_3.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
/*
FileName: remi_3.ino
Overview: Main source file for REMI_3 "All-in-One" EWI
Processor: Freescale MK20DX256 ARM Cortex M4 (F_CPU = 72 MHz)
Platform: Teensy3.2 microcontroller dev board (PJRC)
Toolchain: Arduino IDE with Teensyduino support package
Originated: Nov. 2021, M.J.Bauer [www.mjbauer.biz]
*/
#include <EEPROM.h> // Teensy EEPROM support
#include <Wire.h> // For IIC (I2C, TWI) library
#include "remi_3.h" // App-specific def's
// -------------- Global data --------------------------------------------------------
//
EepromBlock_t g_Config; // structure holding configuration param's
//------------------- PRESET: 8 1 2 3 4 5 6 7
uint8 defaultSynthPatch[] = { 43, 10, 11, 21, 26, 45, 41, 42 };
// Array of touch readings corresponding to touch-pad pins (for diagnostics only):
uint16 touchReadings[12];
extern int32 g_debugValue;
// -------------- Operational variables ----------------------
//
static bool USB_powered;
static bool MIDI_enabled;
static bool DiagnosticModeEnabled;
static bool DisplayEnabled;
static bool ShutdownFlag;
static bool LowBatteryFlag;
static bool ButtonHitFlag;
static uint8 LastDisplayItem;
static uint8 SpeakerEnabled;
static short OctaveShift; // multiple of 12 semitones
static short NoteTranspose;
static uint32 ButtonPressTime_ms;
static uint32 UserInactiveTimer_sec;
static uint16 StartupErrorCode;
static uint16 TouchPadStates;
static uint8 NoteOnOffState;
static uint32 TimeSinceLastNoteOff_ms;
static uint16 PressureSensorReading;
static uint16 PressureQuiescent;
static uint16 PressureThresholdNoteOn;
static uint16 PressureThresholdNoteOff;
static uint16 ModulationSensorReading;
//static uint16 PitchBendSensorReading;
//static uint16 PitchBendZeroLevel;
static uint16 BatteryVoltage_mV;
static uint16 TouchPadScanTime_ms; // (diagnostic usage only)
//=================================================================================================
void setup(void)
{
analogReadRes(12); // set analogRead() resolution to 12 bits
analogWriteRes(12); // set DAC resolution to 12 bits
//Wire1.setSCL(29); // set IIC(1) SCL = pin 29 (SCL1) [redundant]
//Wire1.setSDA(30); // set IIC(1) SDA = pin 30 (SDA1) [redundant]
Wire1.begin(); // set IIC(1) as master device
Wire1.setClock(400*1000); // set IIC(1) clock rate to 400kHz
// Low-level I/O macros defined in "remi_3.h" ...
BATT_LED_CONFIG();
HEARTBEAT_LED_CONFIG();
USB_VBUS_DET_CONFIG();
SET_BUTTON_CONFIG();
SPKR_CTRL_CONFIG();
HEADPHONE_DET_CONFIG();
TESTPOINT_CONFIG();
HEARTBEAT_LED_ON();
BATT_LED_OFF();
// Diagnostic Mode is activated by holding the 'SET' button pressed during power-up.
if (SET_BUTTON_PRESSED) DiagnosticModeEnabled = TRUE;
CheckPowerSource(); // Keep battery ON if no USB_VBUS power
delay(100);
if (!USB_powered) BATT_LED_ON(); // Indicate power is on
if (CheckConfigData() == FALSE) // Read Config data from EEPROM
{
StartupErrorCode |= ERROR_EEPROM_DATA_CHECK;
DefaultConfigData();
}
if (CalibrateSensors() == FALSE) // Calibrate pressure sensor, etc
{
StartupErrorCode |= ERROR_CALIBRATING_SENSOR;
}
g_Config.MidiOutChannel = 1; // Not settable via UI in this version (temp.)
g_Config.MidiLegatoModeEnabled = TRUE; // .. .. ..
g_Config.VelocitySenseEnabled = FALSE; // .. .. ..
// Restore synth settings to Preset last selected and start synth engine
SynthAudioInit();
SynthReverbLevelSet(g_Config.ReverbMix_pc);
SynthVibratoModeSet(g_Config.VibratoMode);
PresetSelect(g_Config.PresetLastSelected);
OLED_Display_Init();
delay(100); // for OLED on-board power to stabilize
Disp_ClearScreen();
DisplayStartupScreen();
delay(1000);
if (StartupErrorCode)
{
DisplayStartupError();
delay(2000);
}
delay(1000); // wait to view start-up/error screen
Disp_ClearScreen();
OLED_Display_Sleep();
DisplayEnabled = FALSE;
LastDisplayItem = DISPLAY_UNDEF;
SPEAKER_ENABLE(); // Done after POR delay to avoid audio "pop"
SpeakerEnabled = 1; // (while headphone plug not inserted)
}
// Main background loop -- called continuously
//
void loop(void)
{
static uint32 taskIntervalStart50us;
static uint32 taskPeriodStart1ms;
static uint32 taskPeriodStart5ms;
static uint32 taskPeriodStart50ms;
static uint8 ledFlashTimer;
static uint8 taskTimer200ms;
// A time interval of 50 microsec (minimum) is interposed between successive
// calls to the TouchPad Monitor routine. The interval may be longer than 50us,
// depending on other tasks' execution times.
if ((micros() - taskIntervalStart50us) >= 50)
{
TouchPad_Monitor();
taskIntervalStart50us = micros();
}
if ((micros() - taskPeriodStart1ms) >= 1000) // Do 1ms (1000us) periodic task
{
taskPeriodStart1ms = micros();
SynthProcess();
}
if ((millis() - taskPeriodStart5ms) >= 5) // Do 5ms periodic tasks...
{
taskPeriodStart5ms = millis();
ButtonScan();
PressureSensorReading = analogRead(BREATH_SENSOR_PIN);
ModulationSensorReading = analogRead(MODN_PAD_PIN);
if (!DiagnosticModeEnabled) NoteOnOffStateTask();
}
if ((millis() - taskPeriodStart50ms) >= 50) // Do 50ms periodic tasks...
{
taskPeriodStart50ms = millis();
if (TouchPadStates != 0) UserInactiveTimer_sec = 0; // Reset UI time-out
if (!DiagnosticModeEnabled) Player_UI_Service();
if (++taskTimer200ms >= 4) // Do 200ms periodic tasks...
{
taskTimer200ms = 0;
CheckPowerSource();
CheckHeadphonePlug();
if (DiagnosticModeEnabled) DiagnosticService();
else DisplayUpdate();
}
HEARTBEAT_LED_OFF();
if (LowBatteryFlag) BATT_LED_OFF(); // Pulse duty = 50ms
if (++ledFlashTimer >= 40) // Every 2 seconds...
{
ledFlashTimer = 0;
// HEARTBEAT_LED_ON(); // LED period = 2 sec, duty = 50ms (debug usage)
if (!USB_powered && LowBatteryFlag) BATT_LED_ON();
UserInactiveTimer_sec += 2;
}
}
if (MIDI_enabled) usbMIDI.read(); // Read and discard MIDI IN messages
if (ButtonPressTime_ms > 5000) ShutdownFlag = TRUE; // Force shut-down
}
/*
Function performs initialization and calibration of player controls and sensors.
Called once at power-on/reset and when user requests sensor recalibration.
*/
bool CalibrateSensors()
{
static uint32 startupTime;
static uint16 lastReading;
bool status = TRUE;
// Measure the pressure sensor signal level
PressureSensorReading = analogRead(BREATH_SENSOR_PIN);
lastReading = PressureSensorReading;
startupTime = millis();
// Take several readings; apply 1st-order IIR filter
while ((millis() - startupTime) < 200)
{
if ((millis() % 5) == 0) // every 5 ms ...
{
PressureSensorReading -= lastReading / 4;
PressureSensorReading += analogRead(BREATH_SENSOR_PIN) / 4;
lastReading = PressureSensorReading;
}
}
// Determine the quiescent pressure level and note on/off thresholds
PressureQuiescent = PressureSensorReading;
PressureThresholdNoteOn = PressureQuiescent + 32;
PressureThresholdNoteOff = PressureQuiescent + 24;
if (PressureQuiescent < 60 || PressureQuiescent > 1200) // sensor fault
status = FALSE;
return status;
}
void CheckPowerSource()
{
int battRawADC;
int threshold_mV = 2700; // low-voltage warning threshold (Alkaline)
battRawADC = analogRead(VBATT_PIN);
BatteryVoltage_mV = (3300 * battRawADC) / 4096;
if (USB_VBUS_DETECTED) // running on USB VBUS power
{
USB_powered = TRUE;
MIDI_enabled = TRUE;
BATT_LED_OFF();
BATT_EN_NEGATE(); // switch off battery power supply
LowBatteryFlag = FALSE;
ShutdownFlag = FALSE;
}
else // USB VBUS not detected...
{
if (ShutdownFlag || USB_powered) // VBUS high on last check (USB just disconnected)
{
BATT_EN_NEGATE(); // shut down
delay(5000);
}
else // running on battery power
{
USB_powered = FALSE;
MIDI_enabled = FALSE;
BATT_EN_ASSERT(); // battery DC-DC converter enabled
if (!LowBatteryFlag) BATT_LED_SET_DUTY(5); // 5% duty => battery OK
}
// Check battery voltage; if low, set low-voltage warning flag
if (g_Config.BatteryType != ALKALINE_BATTERY) threshold_mV = 2200; // NiMH (2 x 1.2V nominal)
else threshold_mV = 2700; // Alkaline (2 x 1.5V nominal)
LowBatteryFlag = FALSE;
if (BatteryVoltage_mV < threshold_mV) LowBatteryFlag = TRUE;
threshold_mV = (threshold_mV * 90) / 100; // 90% of low-voltage warning -> shut down
if (BatteryVoltage_mV < threshold_mV) ShutdownFlag = TRUE;
}
// Check auto-shutdown timer (only when battery-powered)
if (!DiagnosticModeEnabled && !USB_powered
&& (UserInactiveTimer_sec > AUTO_SHUTDOWN_TIMEOUT))
{
BATT_LED_OFF();
BATT_EN_NEGATE(); // Switch off battery power supply
delay(5000);
}
// Check sensibility of battery type (config. param.)
if (g_Config.BatteryType != ALKALINE_BATTERY && BatteryVoltage_mV > 2900)
{
// Voltage too high for NiMH -- change config setting to Alkaline (0)
g_Config.BatteryType = ALKALINE_BATTERY;
StoreConfigData();
}
}
// Function to monitor headphone jack...
// If headphone plug is inserted, mute the internal speaker;
// else if headphone plug removed, restore last state of speaker
//
void CheckHeadphonePlug()
{
if (HEADPHONE_PLUGGED_IN) SPEAKER_DISABLE();
else if (SpeakerEnabled) SPEAKER_ENABLE();
else SPEAKER_DISABLE();
}
/*````````````````````````````````````````````````````````````````````````````````````````````````
Task to read touch-pad signal levels, hence to determine binary (on/off) states.
Readings are updated as fast as possible. The time taken to read each touch-pad varies
according to the applied capacitance, i.e. whether touched or not.
Global variable 'TouchPadStates' holds the de-glitched logic states of the touch-pads.
bit: 9 8 7 6 5 4 3 2 1 0
pad: OCT+ OCT- LH1 LH2 LH3 RH1 RH2 RH3 RH4 LH4
A time-out is imposed on the wait time to read each input, because if a pad is touched,
the effective capacitance to ground may be quite high (> 1000pF) resulting in excessive
time to determine the precise value. If the time taken waiting for a touch reading is
higher than 2ms (40 calls), it is assumed that the corresponding pad is touched.
*/
void TouchPad_Monitor()
{
// Array of 10 touch-pad inputs (pin numbers) to be read...
// in the order: LH4, RH4, RH3, RH2, RH1, LH3, LH2, LH1, OCT-, OCT+
static const uint8 touchPadPins[] = { 19, 18, 17, 16, 15, 33, 1, 0, 22, 23 };
static bool initialized;
static short pindex; // index into array, touchPadPins[]
static uint32 timeoutCount; // call count (per pad)
static uint32 scanStartTime; // for diagnostics only
static uint32 stableReadingCount;
static uint16 padStatesLastCycle;
static uint16 padStatesThisCycle;
int touchReading = 0;
if (!initialized) // one-time initialization at startup
{
touchSenseInit(touchPadPins[0]);
pindex = 0;
timeoutCount = 0;
scanStartTime = millis();
initialized = TRUE;
return;
}
if (TOUCH_SENSE_DONE || ++timeoutCount >= 40)
{
if (timeoutCount >= 40)
{
touchReading = touchSenseRead(); // discard reading
SET_BIT(padStatesThisCycle, pindex);
touchReadings[pindex] = 9999; // capped value
}
else
{
touchReading = touchSenseRead();
touchReadings[pindex] = touchReading;
if (touchReading >= TOUCH_SENSE_THRESHOLD)
SET_BIT(padStatesThisCycle, pindex);
else CLEAR_BIT(padStatesThisCycle, pindex);
}
if (++pindex >= NUMBER_OF_TOUCH_INPUTS) // next pin/pad
{
pindex = 0; // Start new reading cycle and apply de-glitch filter
if (padStatesThisCycle == padStatesLastCycle) stableReadingCount++;
else stableReadingCount = 0;
if (stableReadingCount >= 3) TouchPadStates = padStatesLastCycle; // update
padStatesLastCycle = padStatesThisCycle;
TouchPadScanTime_ms = millis() - scanStartTime; // (diagnostic use only)
scanStartTime = millis();
}
touchSenseInit(touchPadPins[pindex]);
timeoutCount = 0;
}
}
/*````````````````````````````````````````````````````````````````````````````````````````````````
Function: Find MIDI note number from key/fingering pattern,
adjusted by the "Octave Shift" and "Note Transpose" operational variables.
Entry arg: (uint16) fingerPattern = Touch-pad electrode bits, incl. octave pads.
Return val: (uint8) MIDI note number between 23 (B0) and 108 (C8).
````````````````````````````````````````````````````````````````````````````````````````
Touch-pad configuration on REMI 3:
| octave | ----------- s e m i t o n e ----------- | # |
Finger position -> | OCT+ | OCT- | LH1 | LH2 | LH3 | RH1 | RH2 | RH3 | RH4 | LH4 |
Touch pads bit -> | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
````````````````````````````````````````````````````````````````````````````````````````
*/
uint8 NoteNumberFromKeyPattern(uint16 fingerPattern)
{
// Array gives MIDI note number from finger pattern, without LH4 and octave selection.
static uint8 baseNoteNumberLUT[] =
{
// RH1..RH4 finger pattern (4 LS bits of table index) =
// 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, // LH = 000
48, 48, 48, 48, 48, 48, 48, 48, 47, 46, 46, 46, 45, 44, 44, 44, // LH = 001
36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, // LH = 010
43, 42, 42, 42, 42, 42, 42, 42, 41, 41, 41, 41, 40, 39, 38, 36, // LH = 011
35, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, // LH = 100
34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, // LH = 101
33, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, // LH = 110
31, 30, 30, 30, 30, 30, 30, 30, 29, 29, 29, 29, 28, 27, 26, 24 // LH = 111
};
uint8 noteNumber; // return value
uint8 baseNote, octave, padLH4;
uint8 octavePads, top7Fingers;
short transposedNote;
octavePads = (fingerPattern >> 8) & 3;
if (octavePads == 2) octave = 2; // Upper pad => highest octave
else if (octavePads == 3) octave = 1; // Both pads => middle octave
else octave = 0; // Bottom pad => lowest octave
top7Fingers = (fingerPattern >> 1) & 0x7F; // Drop LH4 and strip octave pads
padLH4 = fingerPattern & 1;
baseNote = baseNoteNumberLUT[top7Fingers]; // Note number before octave applied
noteNumber = baseNote + (octave * 12) + 24; // Normal range is 48 (C3) to 96 (C7)
// Check notes which must be sharpened if LH4 is touched (C and F only)
if (baseNote == 24 || baseNote == 29 || baseNote == 36 || baseNote == 41 || baseNote == 48)
{
if (padLH4) noteNumber++; // Sharpen the note
}
// Adjust noteNumber according to player 'octave shift' and 'note transpose' settings
transposedNote = (short) noteNumber + OctaveShift; // +/- 0..24 semitones
if (transposedNote >= 12 && transposedNote <= 96) noteNumber = transposedNote;
transposedNote = (short) noteNumber + NoteTranspose; // +/- 0..12 semitones
if (transposedNote >= 12 && transposedNote <= 96) noteNumber = transposedNote;
return noteNumber;
}
/*````````````````````````````````````````````````````````````````````````````````````````````````
Function: NoteOnOffStateTask()
Background task (state machine) executed every 5 milliseconds.
The function monitors data acquired by the touch-pad sensor routine, looking for
any change in status that signals an "event", e.g. Note-On, Note-Off, Note-Change,
and sends MIDI commands accordingly.
The task also sends MIDI Expression (Pressure), Modulation and Pitch-Bend messages
at the time intervals configured.
*/
void NoteOnOffStateTask()
{
static uint32 stateTimer_ms;
static uint32 controllerUpdateTimer_ms;
static uint8 noteNumPlaying;
static uint8 velocity;
uint16 pressure_14b, modulation_14b;
uint8 pressure_Hi, modulation_Hi;
int16 pitch_bend_14b;
uint8 midiChan = g_Config.MidiOutChannel;
uint8 noteNumber = NoteNumberFromKeyPattern(TouchPadStates);
uint8 top3Fingers = (TouchPadStates >> 5) & 7; // Isolate LH1, LH2 & LH3 pads
uint8 octavePads = (TouchPadStates >> 8) & 3; // Isolate OCT+ & OCT- pads
bool isValidNote = (octavePads != 0) && (top3Fingers != 0);
bool doSendNoteOn = FALSE;
switch (NoteOnOffState)
{
case NOTE_OFF_IDLE:
{
// A new note is triggered when the pressure sensor signal rises above the
// NoteOnPressureThreshold (raw ADC count).
if (isValidNote && (PressureSensorReading >= PressureThresholdNoteOn))
{
if (DisplayEnabled) // Blank the OLED display while note playing
{
OLED_Display_Sleep();
DisplayEnabled = FALSE;
}
stateTimer_ms = 0; // start delay for velocity acquisition
NoteOnOffState = NOTE_ON_PENDING;
}
controllerUpdateTimer_ms = 0;
TimeSinceLastNoteOff_ms += 5;
break;
}
case NOTE_ON_PENDING:
{
TimeSinceLastNoteOff_ms = 0;
controllerUpdateTimer_ms = 0;
if (!g_Config.VelocitySenseEnabled) // use fixed velocity value
{
velocity = 64;
doSendNoteOn = TRUE;
}
else if (stateTimer_ms >= NOTE_ON_VELOCITY_DELAY) // ready to acquire velocity
{
pressure_14b = GetBreathPressureLevel(); // change since note trigger
velocity = pressure_14b >> 7;
doSendNoteOn = TRUE;
}
// else... Don't send Note-On; remain in this state.
if (doSendNoteOn)
{
SynthNoteOn(noteNumber, velocity);
if (MIDI_enabled) usbMIDI.sendNoteOn(noteNumber, velocity, midiChan);
noteNumPlaying = noteNumber;
NoteOnOffState = NOTE_ON_PLAYING;
}
break;
}
case NOTE_ON_PLAYING:
{
// A Note-off is sent when the pressure sensor signal falls below the Note-Off
// pressure threshold.
if (PressureSensorReading < PressureThresholdNoteOff)
{
SynthNoteOff();
if (MIDI_enabled) usbMIDI.sendNoteOff(noteNumPlaying, 0, midiChan);
NoteOnOffState = NOTE_OFF_IDLE;
break;
}
// Look for a change in fingering pattern with any valid note selected;
// this signals a "Legato" note change. Remain in this state.
if (isValidNote && (noteNumber != noteNumPlaying))
{
SynthNoteOn(noteNumber, velocity); // REMI synth: legato always enabled
if (MIDI_enabled && g_Config.MidiLegatoModeEnabled)
{
usbMIDI.sendNoteOn(noteNumber, velocity, midiChan); // New note ON
usbMIDI.sendNoteOff(noteNumPlaying, 0, midiChan); // Old note OFF
}
else if (MIDI_enabled)
{
usbMIDI.sendNoteOff(noteNumPlaying, 0, midiChan); // Old note OFF
usbMIDI.sendNoteOn(noteNumber, velocity, midiChan); // New note ON
}
noteNumPlaying = noteNumber; // update
}
pressure_14b = GetBreathPressureLevel();
pressure_Hi = pressure_14b >> 7;
modulation_14b = GetModulationPadForce();
modulation_Hi = modulation_14b >> 7;
pitch_bend_14b = GetPitchBendData();
// Update synth expression (pressure), modulation and pitch-bend signals
SynthExpression(pressure_14b);
SynthModulation(modulation_14b);
SynthPitchBend(pitch_bend_14b);
// Send MIDI expression (breath pressure) message on CC02 (every 5ms)
if (MIDI_enabled) usbMIDI.sendControlChange(MIDI_EXPRN_CC, pressure_Hi, midiChan);
// Send other MIDI control-change messages (when pending)
controllerUpdateTimer_ms += 5;
if (MIDI_enabled && controllerUpdateTimer_ms >= MIDI_MODN_MSG_INTERVAL)
{
usbMIDI.sendControlChange(1, modulation_Hi, midiChan);
usbMIDI.sendPitchBend(pitch_bend_14b, midiChan);
controllerUpdateTimer_ms = 0;
}
break;
}
default:
NoteOnOffState = NOTE_OFF_IDLE;
break;
} // end switch
stateTimer_ms += 5;
}
/*
Function: Select an Instrument Preset.
Entry args: preset = PRESET number (0..7)
NB: The user interface displays preset 0 as preset number 8.
*/
void PresetSelect(uint8 preset)
{
preset = preset & 7; // preset index must be 0..7
// Load and activate the REMI synth patch assigned to this Preset...
SynthPatchSelect(g_Config.PresetPatchNum[preset]);
g_Config.PresetLastSelected = preset; // Save this Preset for next power-on
StoreConfigData();
}
/*````````````````````````````````````````````````````````````````````````````````````````````````
Player User Interface (UI) service routine.
Periodic Background task executed every 50 milliseconds.
The Player UI comprises the 'SET' button, OLED graphics display (128 x 64 px)
and the 10 touch-pads. The touch-pads select a "menu" item for view/setting.
Refer to "REMI 3 User Guide" for details of operation.
*/
void Player_UI_Service()
{
static uint8 reverbStep[] = { 0, 5, 10, 15, 20, 25, 35, 50 };
int idx;
//uint8 channel = g_Config.MidiOutChannel;
short preset = g_Config.PresetLastSelected;
if (NoteOnOffState != NOTE_OFF_IDLE) return; // A note is playing now
if (DiagnosticModeEnabled) return; // Diag. mode is a conflicting UI
if (ButtonHitFlag)
{
ButtonHitFlag = 0;
UserInactiveTimer_sec = 0; // Reset UI time-out
if (!DisplayEnabled)
{
OLED_Display_Wake();
DisplayEnabled = TRUE;
}
if (TouchPadStates == 0) // No pad touched
{
SynthAudioInit(); // Disable synth -- All sound off
LastDisplayItem = DISPLAY_UNDEF; // trigger idle/menu screen
SPEAKER_DISABLE(); // mute speaker (OLED IIC bus activity causes audio noise)
}
else if (TouchPadStates == (OCT_UP + OCT_DN)) OctaveShift = 0; // clear octave shift
else if (TouchPadStates == OCT_UP)
{
if (OctaveShift <= 12) OctaveShift += 12; // shift up 12 semitones
}
else if (TouchPadStates == OCT_DN)
{
if (OctaveShift >= -12) OctaveShift -= 12; // shift down 12 semitones
}
else if (TouchPadStates == (LH1 + LH2)) NoteTranspose = 0; // clear transpose
else if (TouchPadStates == LH1)
{
if (NoteTranspose < 11) NoteTranspose++; // shift up 1 semitone
}
else if (TouchPadStates == LH2)
{
if (NoteTranspose > 1) NoteTranspose--; // shift down 1 semitone
}
else if (TouchPadStates == RH1)
{
if (++preset > 7) preset = 0; // Increment Preset
PresetSelect(preset); // Save and activate
}
else if (TouchPadStates == RH2)
{
if (--preset < 0) preset = 7; // Decrement Preset
PresetSelect(preset); // Save and activate
}
else if (TouchPadStates == LH4)
{
// Set vibrato mode -- Scroll thru modes 0-1-2-0 (OFF, Mod.Pad, Auto)
if (g_Config.VibratoMode == 0) g_Config.VibratoMode = 1;
else if (g_Config.VibratoMode == 1) g_Config.VibratoMode = 2;
else g_Config.VibratoMode = 0; // OFF
StoreConfigData();
SynthVibratoModeSet(g_Config.VibratoMode);
}
else if (TouchPadStates == LH3)
{
// Set Pitch Bend -- Toggle on/off (Other synth PB modes not supported)
g_Config.PitchBendEnabled = !g_Config.PitchBendEnabled;
StoreConfigData();
if (g_Config.PitchBendEnabled) SynthPitchBendModeSet(1);
else SynthPitchBendModeSet(0);
}
else if (TouchPadStates == RH3)
{
// Set reverb level (scroll up thru 8 fixed steps)
for (idx = 0; idx < (int)sizeof(reverbStep); idx++)
{
if (g_Config.ReverbMix_pc == reverbStep[idx]) break;
}
if (++idx >= (int)sizeof(reverbStep)) idx = 0;
g_Config.ReverbMix_pc = reverbStep[idx];
StoreConfigData();
SynthReverbLevelSet(g_Config.ReverbMix_pc);
}
else if (TouchPadStates == RH4 && !HEADPHONE_PLUGGED_IN)
{
SpeakerEnabled = !SpeakerEnabled; // toggle speaker on/off
if (SpeakerEnabled) SPEAKER_ENABLE(); // update SPKR_EN output
else SPEAKER_DISABLE();
}
else if (TouchPadStates == (RH3 + RH4)) // toggle battery type
{
if (g_Config.BatteryType == ALKALINE_BATTERY) g_Config.BatteryType = 1; // change to NiMH
else g_Config.BatteryType = ALKALINE_BATTERY; // change to Alkaline
StoreConfigData();
}
else if (TouchPadStates == (LH3 + RH1))
{
g_Config.PressureFullScale = PressureSensorReading; // set max. pressure
StoreConfigData();
}
else if (TouchPadStates == (LH3 + LH4)) ShutdownFlag = TRUE; // shut down
} // end if (ButtonHitFlag())
}
/*
Routine to update the graphical OLED display during normal operating mode.
Background task called at 200 millisec intervals (5 times/sec).
The display is normally blanked out; it is activated by pressing the SET button;
also momentarily for device start-up and battery power-off sequences.
While activated, the display shows the "menu" item selected by the touch-pad(s) that
are currently touched, if any, with the current value of the respective parameter
or operational variable. It also shows what will happen if the SET button is hit.
*/
void DisplayUpdate()
{
uint8 displayItem;
bool isNewScreen = FALSE;
if (NoteOnOffState != NOTE_OFF_IDLE) return; // A note is playing now
if (UserInactiveTimer_sec >= 5) // Time-out... no UI activity
{
Disp_ClearScreen();
OLED_Display_Sleep();
DisplayEnabled = FALSE;
}
if (TouchPadStates == 0) displayItem = DISPLAY_PROMPT;
else if (TouchPadStates == OCT_UP || TouchPadStates == OCT_DN || TouchPadStates == (OCT_UP + OCT_DN))
displayItem = DISPLAY_OCTAVE;
else if (TouchPadStates == (LH1 + LH2) || TouchPadStates == LH1 || TouchPadStates == LH2)
displayItem = DISPLAY_TRANSPOSE;
else if (TouchPadStates == (RH1 + RH2) || TouchPadStates == RH1 || TouchPadStates == RH2)
displayItem = DISPLAY_PRESET;
else if (TouchPadStates == LH3) displayItem = DISPLAY_PITCHBEND; // todo ***************
else if (TouchPadStates == LH4) displayItem = DISPLAY_VIBRATO;
else if (TouchPadStates == RH3) displayItem = DISPLAY_REVERB;
else if (TouchPadStates == RH4) displayItem = DISPLAY_SPEAKER;
else if (TouchPadStates == (LH3 + LH4)) displayItem = DISPLAY_SHUTDOWN;
else if (TouchPadStates == (RH3 + RH4)) displayItem = DISPLAY_BATTERY;
else if (TouchPadStates == (LH3 + RH3)) displayItem = DISPLAY_SYSINFO;
else if (TouchPadStates == (LH3 + RH1)) displayItem = DISPLAY_PRESSURE;
else displayItem = DISPLAY_IDLE; // No valid touch-pad combo selected
if (displayItem != LastDisplayItem)
{
isNewScreen = TRUE;
Disp_ClearScreen(); // prepare new screen
Disp_PosXY(0, 13);
Disp_DrawLineHoriz(128);
LastDisplayItem = displayItem;
}
switch (displayItem)
{
case DISPLAY_PROMPT:
DisplayPrompt(isNewScreen);
break;
case DISPLAY_OCTAVE:
DisplayOctaveShift(isNewScreen);
break;
case DISPLAY_TRANSPOSE:
DisplayTranspose(isNewScreen);
break;
case DISPLAY_PRESET:
DisplayPreset(isNewScreen);
break;
case DISPLAY_VIBRATO:
DisplayVibrato(isNewScreen);
break;
case DISPLAY_REVERB:
DisplayReverb(isNewScreen);
break;
case DISPLAY_SPEAKER:
DisplaySpeaker(isNewScreen);
break;
case DISPLAY_BATTERY:
DisplayBattery(isNewScreen);
break;
case DISPLAY_SHUTDOWN:
DisplayShutdown(isNewScreen);
break;
case DISPLAY_SYSINFO:
DisplaySystemInfo(isNewScreen);
break;
case DISPLAY_PRESSURE:
DisplayPressure(isNewScreen);
break;
default: break;
} // end switch
}
void DisplayStartupError()
{
DisplayTextCentered12p(0, "Self-test Error"); // title bar
Disp_PosXY(0, 13);
Disp_DrawLineHoriz(128);
Disp_SetFont(MONO_8_NORM);
if (StartupErrorCode & ERROR_CALIBRATING_SENSOR)
{
Disp_PosXY(0, 16);
Disp_PutText("Pressure sensor");
Disp_PosXY(0, 26);
Disp_PutText(" calibration.");
}
if (StartupErrorCode & ERROR_EEPROM_DATA_CHECK)
{
Disp_PosXY(0, 36);
Disp_PutText("EEPROM defaulted.");
}
}
void DisplayStartupScreen()
{
Disp_Mode(SET_PIXELS);
Disp_SetFont(PROP_12_BOLD);
Disp_PosXY(32, 0);
Disp_PutText("Bauer");
Disp_PosXY(12, 12);
Disp_PutImage(remi_logo_85x30, 85, 30);
Disp_SetFont(PROP_12_BOLD);
Disp_PosXY(102, 22);
Disp_PutText("3");
Disp_SetFont(PROP_8_NORM);
Disp_PosXY(32, 48);
Disp_PutText("Initializing...");
}
void DisplayPrompt(bool prep)
{
if (prep) // once only
{
DisplayTextCentered12p(0, "- Menu -"); // title bar
Disp_SetFont(PROP_12_NORM);
Disp_PosXY(0, 20);
Disp_PutText("Select menu item");
Disp_PosXY(0, 34);
Disp_PutText("using touch pads");
}
}
void DisplayOctaveShift(bool prep)
{
static int lastValueShown;
static int previousTouchPadStates;
if (prep) // prepare new screen
{
DisplayTextCentered12p(0, "Octave Shift"); // title bar
Disp_SetFont(PROP_8_NORM);
Disp_PosXY(84, 28);
Disp_PutText("octave");
lastValueShown = 999; // force data refresh
}
if (OctaveShift != lastValueShown || TouchPadStates != previousTouchPadStates)
{
Disp_PosXY(48, 18);
Disp_ClearArea(32, 20);
Disp_SetFont(PROP_24_NORM);
if (OctaveShift > 0) Disp_PutChar('+');
else if (OctaveShift == 0) Disp_PutChar(' ');
Disp_PutDecimal(OctaveShift/12, 1); // OctaveShift = semitones
lastValueShown = OctaveShift;
// Show 'SET' button action (none if both pads touched)
Disp_PosXY(16, 48);
Disp_ClearArea(96, 16);
if (TouchPadStates == OCT_UP && OctaveShift < 1)
DisplayTextCenteredInBox(48, "Up (+1)");
if (TouchPadStates == OCT_DN && OctaveShift > -1)
DisplayTextCenteredInBox(48, "Down (-1)");
}
previousTouchPadStates = TouchPadStates;
}
void DisplayTranspose(bool prep)
{
static int lastValueShown;
static uint16 previousTouchPadStates;
if (prep) // prepare new screen
{
DisplayTextCentered12p(0, "Transpose"); // title bar
Disp_SetFont(PROP_8_NORM);
Disp_PosXY(84, 20);
Disp_PutText("semi-");
Disp_PosXY(84, 30);
Disp_PutText("tone(s)");
lastValueShown = 999; // force data refresh
}
// Update variable data, but only if it has changed...
if (NoteTranspose != lastValueShown || TouchPadStates != previousTouchPadStates)
{
Disp_PosXY(48, 18);
Disp_ClearArea(30, 20);
Disp_SetFont(PROP_24_NORM);
if (NoteTranspose > 0) Disp_PutChar('+');
if (NoteTranspose == 0) Disp_PutChar(' ');
Disp_PutDecimal(NoteTranspose, 1);
lastValueShown = NoteTranspose;
// Show 'SET' button action
Disp_PosXY(16, 48);
Disp_ClearArea(96, 16);
if (TouchPadStates == (LH1 + LH2) && NoteTranspose != 0)
DisplayTextCenteredInBox(48, "Clear (0)");
else if (TouchPadStates == LH1 && NoteTranspose < 12)
DisplayTextCenteredInBox(48, "Up (+1)");
else if (TouchPadStates == LH2 && NoteTranspose > -12)
DisplayTextCenteredInBox(48, "Down (-1)");
}
previousTouchPadStates = TouchPadStates;
}
void DisplayPreset(bool prep)
{
static int lastValueShown;
static uint16 previousTouchPadStates;
const char *patchName;
int idx;
int preset = g_Config.PresetLastSelected;
int patchID = g_Config.PresetPatchNum[preset];
idx = GetPatchTableIndex(patchID);
if (idx < 0) idx = 0; // error
patchName = g_PatchProgram[idx].PatchName;
if (prep)
{
DisplayTextCentered12p(0, "PRESET"); // title bar
lastValueShown = 999;
}
// Update variable data, but only if it has changed...
if (preset != lastValueShown || TouchPadStates != previousTouchPadStates)
{
Disp_PosXY(58, 18);
Disp_ClearArea(16, 20);
Disp_SetFont(PROP_24_NORM);
if (preset == 0) Disp_PutText("8"); // Preset 0 is displayed as '8'
else Disp_PutDecimal(preset, 1);
lastValueShown = preset;
Disp_PosXY(0, 48);
Disp_ClearArea(128, 16);
// If both pads RH1 & RH2 touched, show the preset patch name
if (TouchPadStates == (RH1 + RH2))
{
Disp_PosXY(0, 50);
Disp_SetFont(PROP_12_NORM);
if (strlen(patchName) > 15) Disp_PutText(patchName);
else DisplayTextCentered12p(50, patchName);
}
// Otherwise, show 'SET' button action
if (TouchPadStates == RH1) DisplayTextCenteredInBox(48, "Up (+1)");
if (TouchPadStates == RH2) DisplayTextCenteredInBox(48, "Down (-1)");
}
previousTouchPadStates = TouchPadStates;
}
void DisplayVibrato(bool prep)
{
static int lastValueShown;
int vibMode = g_Config.VibratoMode;
if (prep)
{
DisplayTextCentered12p(0, "Vibrato"); // title bar
Disp_SetFont(MONO_8_NORM);
Disp_PosXY(28, 18);
Disp_PutText("Control Mode");
// Show 'SET' button action (constant)
DisplayTextCenteredInBox(48, "Change");
lastValueShown = 999; // force refresh
}
// Update variable data, but only if it has changed...
if (vibMode != lastValueShown)
{
Disp_PosXY(24, 32);