-
Notifications
You must be signed in to change notification settings - Fork 6
/
midi_muppet_hx_2btn.ino
587 lines (514 loc) · 15.7 KB
/
midi_muppet_hx_2btn.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
/**************************************************************************
BSD 3-Clause License
Copyright (c) 2020, Matthias Wientapper
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Midi Foot Switch for HX Stomp
=============================
- Button Up/Down on pins D2, D3
- LED green/red D4, D5
- requires OneButton lib https://github.com/mathertel/OneButton
- requires JC_Button lib https://github.com/JChristensen/JC_Button
We are using two different button libraries for different purposes:
- OneButton to detect a long press without detecting a short press first.
This means the action will be evaluated on releasing the button,
which is ok for most cases but bad for timing critical actions
like when we are in looper mode.
- JC_Button to detect a short press as soon as the button is pressed down.
This button library is used in looper mode only.
SCROLL Mode: up/dn switches prog patches
long press dn: TUNER
long press up: SNAPSHOT mode
up + dn: FS mode
FS Mode: up/dn emulate FS4/FS5
long press dn: TUNER
long press up: SNAPSHOT mode
up + dn: SCROLL mode
SNAPSHOT Mode: up/dn switches snapshot
long press dn: TUNER
long press up: FS mode
TUNER Mode: up or dn back to prev mode
LOOPER Mode: dn toggles record/overdub
up toggles play/stop
long press up toggles undo/redo
**************************************************************************/
#include <Wire.h>
#include <OneButton.h>
#include <JC_Button.h>
#include <EEPROM.h>
// GPIO pins used
#define BTN_UP 2
#define BTN_DN 3
#define LED_GRN 4
#define LED_RED 5
// EEPROM addresses
#define OP_MODE_ADDR 0 // stores looper mode of operation
#define LOOPER_MODE_ADDR 1 // stores if looper control is enabled
#define MIDI_CHANNEL_ADDR 2 // stores the midi channel
// Adjust red LED brightness 0-255 (full on was way too bright for me)
#define LED_RED_BRIGHTNESS 25
// on/off delay when we flash a LED
#define LED_FLASH_DELAY 30
OneButton btnUp(BTN_UP, true);
OneButton btnDn(BTN_DN, true);
Button jc_btnUp(BTN_UP);
Button jc_btnDn(BTN_DN);
enum modes_t {SCROLL, SNAPSHOT, FS, LOOPER, TUNER, CHANNEL_CFG, LOOPER_CFG}; // modes of operation
static modes_t MODE; // current mode
static modes_t LAST_MODE; // last mode
static modes_t MODE_BEFORE_SNAPSHOT; // last mode before snap shot mode
enum lmodes_t {PLAY, RECORD, OVERDUB, STOP}; // Looper modes
static lmodes_t LPR_MODE;
bool with_looper = true;
uint8_t midi_channel = 0;
void (*Reset)(void) = 0;
void setup() {
// LEDs
pinMode(LED_RED, OUTPUT);
pinMode(LED_GRN, OUTPUT);
// Buttons:
btnUp.setClickTicks(50);
btnUp.attachClick(upClick);
btnUp.attachLongPressStart(upLongPressStart);
btnDn.setClickTicks(50);
btnDn.attachClick(dnClick);
btnDn.attachLongPressStart(dnLongPressStart);
// Set MIDI baud rate:
Serial.begin(31250);
// restore MODE from EEPROM
MODE = (modes_t) EEPROM.read(OP_MODE_ADDR);
if (MODE > 4){
// no valid value in eeprom found. (Maybe this is the first power up ever?)
MODE = SCROLL;
}
// restore looper config from adr. 1
with_looper = EEPROM.read(LOOPER_MODE_ADDR);
if (with_looper > 1)
with_looper = false;
// restore MIDI channel
midi_channel = EEPROM.read(MIDI_CHANNEL_ADDR);
if (midi_channel > 15){
// set channel to 0 if data not valid
EEPROM.update(MIDI_CHANNEL_ADDR, 0);
midi_channel = 0;
}
if (digitalRead(BTN_DN) == 0 && digitalRead(BTN_UP) == 1) {
// btn dn pressed: configure MIDI channel
MODE = CHANNEL_CFG;
flashLED(20, LED_GRN, LED_FLASH_DELAY);
// 'display' configure MIDI channel
delay(1000);
flashLED(midi_channel + 1, LED_GRN, 500);
}
if (digitalRead(BTN_DN) == 1 && digitalRead(BTN_UP) == 0) {
// btn up pressed: toggle looper ctrl enabled/disabled
MODE = LOOPER_CFG;
flashLED(20, LED_RED, LED_FLASH_DELAY);
delay(1000);
if (with_looper)
flashLED(1, LED_GRN, 500);
else
flashLED(1, LED_RED, 500);
}
// restore mode on HX Stomp as well
if (MODE == SNAPSHOT)
midiCtrlChange(71, 3); // set snapshot mode
else if (MODE == FS)
midiCtrlChange(71, 0); // set stomp mode
else if (MODE == SCROLL)
midiCtrlChange(71, 0); // set stomp mode
MODE_BEFORE_SNAPSHOT = MODE;
// indicate mode via LEDs
if (MODE == LOOPER) {
flashRedGreen(10);
// we are in looper mode, so we are using the jc_button class for action on button press
// (OneButton acts on button release)
jc_btnUp.begin();
jc_btnDn.begin();
}
else if (MODE == SCROLL)
flashLED(10, LED_RED, LED_FLASH_DELAY);
else if (MODE == FS)
flashLED(10, LED_GRN, LED_FLASH_DELAY);
// Looper default state
LPR_MODE = STOP;
}
void loop() {
if (MODE == LOOPER) {
jc_btnDn.read(); // DN Button handled by JC_Button
btnUp.tick(); // Up Button handled by OneButton
if (jc_btnDn.wasPressed() && digitalRead(BTN_UP) == 1) // attach handler
jc_dnClick();
} else {
btnUp.tick(); // both buttons handled by OneButton
btnDn.tick();
}
handle_leds();
}
/* ------------------------------------------------- */
/* --- OneButton Callback Routines ---*/
/* ------------------------------------------------- */
void dnClick() {
switch (MODE)
{
case SCROLL:
patchDown();
flashLED(2, LED_RED, LED_FLASH_DELAY);
break;
case TUNER:
midiCtrlChange(68, 0); // toggle tuner
flashLED(2, LED_RED, LED_FLASH_DELAY);
MODE = LAST_MODE;
break;
case SNAPSHOT:
midiCtrlChange(69, 9); // prev snapshot
flashLED(2, LED_RED, LED_FLASH_DELAY);
break;
case FS:
midiCtrlChange(52, 0); // emulate FS 4
flashLED(2, LED_RED, LED_FLASH_DELAY);
break;
case LOOPER:
switch (LPR_MODE) {
case STOP:
LPR_MODE = RECORD;
midiCtrlChange(60, 127); // Looper record
break;
case RECORD:
LPR_MODE = PLAY;
midiCtrlChange(61, 127); // Looper play
break;
case PLAY:
LPR_MODE = OVERDUB;
midiCtrlChange(60, 0); // Looper overdub
break;
case OVERDUB:
LPR_MODE = PLAY;
midiCtrlChange(61, 127); // Looper play
break;
}
break;
case CHANNEL_CFG:
if(midi_channel > 0)
midi_channel--;
flashLED(midi_channel + 1, LED_GRN, 500);
EEPROM.update(MIDI_CHANNEL_ADDR, midi_channel);
break;
case LOOPER_CFG:
with_looper = false;
flashLED(1, LED_RED, 500);
EEPROM.update(LOOPER_MODE_ADDR, with_looper);
break;
}
}
void upClick() {
switch (MODE)
{
case SCROLL:
patchUp();
flashLED(2, LED_RED, LED_FLASH_DELAY);
break;
case TUNER:
midiCtrlChange(68, 0); // toggle tuner
flashLED(2, LED_RED, LED_FLASH_DELAY);
MODE = LAST_MODE;
break;
case SNAPSHOT:
flashLED(2, LED_RED, LED_FLASH_DELAY);
midiCtrlChange(69, 8); // next snapshot
break;
case FS:
flashLED(2, LED_RED, LED_FLASH_DELAY);
midiCtrlChange(53, 0); // emulate FS 5
break;
case LOOPER:
switch (LPR_MODE) {
case STOP:
LPR_MODE = PLAY;
midiCtrlChange(61, 127); // Looper play
break;
case PLAY:
case RECORD:
case OVERDUB:
LPR_MODE = STOP;
midiCtrlChange(61, 0); // Looper stop
break;
}
break;
case CHANNEL_CFG:
if(midi_channel < 15)
midi_channel++;
flashLED(midi_channel + 1, LED_GRN, 500);
EEPROM.update(MIDI_CHANNEL_ADDR, midi_channel);
break;
case LOOPER_CFG:
with_looper = true;
flashLED(1, LED_GRN, 500);
EEPROM.update(LOOPER_MODE_ADDR, with_looper);
break;
}
}
void dnLongPressStart() {
if (digitalRead(BTN_UP) == 1) {
switch (MODE)
{
case TUNER:
break;
case SCROLL:
case SNAPSHOT:
case FS:
midiCtrlChange(68, 0); // toggle tuner
flashLED(2, LED_RED, LED_FLASH_DELAY);
LAST_MODE = MODE;
MODE = TUNER;
break;
case LOOPER:
break;
}
}
}
void upLongPressStart() {
if (digitalRead(BTN_DN) == 0) {
// yay, both buttons pressed!
// Toggle through modes
switch (MODE) {
case SCROLL:
MODE = FS;
break;
case FS:
if (with_looper)
MODE = LOOPER;
else
MODE = SCROLL;
break;
case LOOPER:
// make sure to switch off looper
midiCtrlChange(61, 0); // Looper stop
MODE = SCROLL;
break;
case SNAPSHOT:
if (with_looper)
MODE = LOOPER;
else
MODE = SCROLL;
break;
case TUNER:
break;
case CHANNEL_CFG:
MODE = LAST_MODE;
break;
}
EEPROM.update(OP_MODE_ADDR, MODE);
// reset the device to reboot in new mode
Reset();
} else {
// regular long press event:
switch (MODE)
{
case TUNER:
break;
case SCROLL:
// save mode where we entered snapshot mode from
MODE_BEFORE_SNAPSHOT = MODE;
midiCtrlChange(71, 3); // set snapshot mode
flashLED(5, LED_RED, LED_FLASH_DELAY);
MODE = SNAPSHOT;
EEPROM.update(0, MODE);
break;
case SNAPSHOT:
if (MODE_BEFORE_SNAPSHOT == FS) {
midiCtrlChange(71, 0); // set stomp mode
flashLED(5, LED_RED, LED_FLASH_DELAY);
MODE = FS;
} else {
if (MODE_BEFORE_SNAPSHOT == SCROLL) {
midiCtrlChange(71, 0); // set stomp mode
flashLED(5, LED_RED, LED_FLASH_DELAY);
MODE = SCROLL;
}
}
EEPROM.update(0, MODE);
break;
case FS:
// save mode where we entered snapshot mode from
MODE_BEFORE_SNAPSHOT = MODE;
midiCtrlChange(71, 3); // set snapshot mode
flashLED(5, LED_RED, LED_FLASH_DELAY);
MODE = SNAPSHOT;
EEPROM.update(0, MODE);
break;
case LOOPER:
switch (LPR_MODE) {
case PLAY:
case STOP:
midiCtrlChange(63, 127); // looper undo/redo
flashLED(3, LED_RED, LED_FLASH_DELAY);
break;
case RECORD:
case OVERDUB:
break;
}
break;
}
}
}
/* ------------------------------------------------- */
/* --- JC_Button Callback Routines ---*/
/* ------------------------------------------------- */
void jc_dnClick() {
switch (LPR_MODE) {
case STOP:
LPR_MODE = RECORD;
midiCtrlChange(60, 127); // Looper record
break;
case RECORD:
LPR_MODE = PLAY;
midiCtrlChange(61, 127); // Looper play
break;
case PLAY:
LPR_MODE = OVERDUB;
midiCtrlChange(60, 0); // Looper overdub
break;
case OVERDUB:
LPR_MODE = PLAY;
midiCtrlChange(61, 127); // Looper play
break;
}
}
/* ------------------------------------------------- */
/* --- Midi Routines ---*/
/* ------------------------------------------------- */
// Use these routines if you are using firmware 3.01 or older:
// HX stomp does not have a native patch up/dn midi command
// so we are switching to scroll mode and emulating a FS1/2
// button press.
/*
void patchUp() {
midiCtrlChange(71, 1); // HX scroll mode
delay(30);
midiCtrlChange(50, 127); // FS 2 (up)
midiCtrlChange(71, 0); // HX stomp mode
}
void patchDown() {
midiCtrlChange(71, 1); // HX scroll mode
delay(30);
midiCtrlChange(49, 127); // FS 1 (down)
midiCtrlChange(71, 0); // HX stomp mode
}
*/
// Added in Firmware 3.10:
//New MIDI message: CC 72 value 64-127 = next preset, value 0-63 = previous preset
void patchUp() {
midiCtrlChange(72, 127); // next preset
}
void patchDown() {
midiCtrlChange(72, 0); // prev preset
}
void midiProgChange(uint8_t p) {
Serial.write(0xc0 | midi_channel); // PC message
Serial.write(p); // prog
}
void midiCtrlChange(uint8_t c, uint8_t v) {
Serial.write(0xb0 | midi_channel); // CC message
Serial.write(c); // controller
Serial.write(v); // value
}
/* ------------------------------------------------- */
/* --- Misc Stuff ---*/
/* ------------------------------------------------- */
void flashLED(uint8_t i, uint8_t led, uint8_t del)
{
uint8_t last_state;
last_state = digitalRead(led);
for (uint8_t j = 0; j < i; j++) {
digitalWrite(led, HIGH);
delay(del);
digitalWrite(led, LOW);
delay(del);
}
digitalWrite(led, last_state);
}
void flashRedGreen(uint8_t i) {
uint8_t last_state_r;
uint8_t last_state_g;
last_state_r = digitalRead(LED_RED);
last_state_g = digitalRead(LED_GRN);
for (uint8_t j = 0; j < i; j++) {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GRN, HIGH);
delay(75);
analogWrite(LED_RED, LED_RED_BRIGHTNESS);
digitalWrite(LED_GRN, LOW);
delay(75);
}
digitalWrite(LED_RED, last_state_r);
digitalWrite(LED_GRN, last_state_g);
}
void handle_leds() {
static unsigned long next = millis();
switch (MODE) {
case SCROLL:
// solid red
digitalWrite(LED_GRN, LOW);
analogWrite(LED_RED, LED_RED_BRIGHTNESS);
//digitalWrite(LED_RED, HIGH);
break;
case SNAPSHOT:
// solid green
digitalWrite(LED_GRN, HIGH);
digitalWrite(LED_RED, LOW);
break;
case FS:
// solid green
digitalWrite(LED_RED, LOW);
digitalWrite(LED_GRN, HIGH);
break;
case TUNER:
// blink green
if (millis() - next > 500) {
next += 500;
digitalWrite(LED_GRN, !digitalRead(LED_GRN));
}
break;
case LOOPER:
switch (LPR_MODE) {
case STOP:
digitalWrite(LED_GRN, LOW);
digitalWrite(LED_RED, LOW);
break;
case PLAY:
digitalWrite(LED_GRN, HIGH);
digitalWrite(LED_RED, LOW);
break;
case RECORD:
digitalWrite(LED_GRN, LOW);
analogWrite(LED_RED, LED_RED_BRIGHTNESS);
break;
case OVERDUB:
// yellow
digitalWrite(LED_GRN, HIGH);
analogWrite(LED_RED, LED_RED_BRIGHTNESS);
break;
}
break;
}
}