-
Notifications
You must be signed in to change notification settings - Fork 0
/
SteinhartMidi.ino
487 lines (436 loc) · 13 KB
/
SteinhartMidi.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
#include <midi_Settings.h>
#include <midi_Namespace.h>
#include <midi_Message.h>
#include <midi_Defs.h>
#include <MIDI.h>
#include "State.h"
#include "SequencerEngine.h"
#include "DrumRackEngine.h"
#include "ArpeggiatorEngine.h"
#include "ActionButtonEngine.h"
USING_NAMESPACE_MIDI
MIDI_CREATE_DEFAULT_INSTANCE();
#define ENCODER_PIN_A 2
#define ENCODER_PIN_B 4
#define LED_SRCLK 5
#define LED_RCLK 6
#define LED_DATA 7
#define MUX_SELECT_PIN_0 8
#define MUX_SELECT_PIN_1 9
#define MUX_SELECT_PIN_2 10
// CC MUXES
#define MUX_INPUT_PIN_1 A0
#define MUX_INPUT_PIN_2 A1
#define MUX_INPUT_PIN_3 A4
#define MUX_INPUT_PIN_4 A3
// buttons and playground
#define MUX_INPUT_PIN_5 A7
#define MUX_INPUT_PIN_6 A5
#define MUX_INPUT_PIN_7 A6
#define MUX_INPUT_PIN_8 A2
#define SEQUENCER_MIDI_CHANNEL 5
#define DRUM_RACK_MIDI_CHANNEL 5
#define CC_MIDI_CHANNEL 4
#define C1_OFFSET 36
#define LED 13 // LED pin on Arduino Uno
#define RANGE_1_LED_INDEX 4
#define RANGE_2_LED_INDEX 5
#define RANGE_3_LED_INDEX 6
#define RANGE_4_LED_INDEX 7
#define ENCODER_R_LED_INDEX 8
#define ENCODER_G_LED_INDEX 9
#define ENCODER_B_LED_INDEX 10
#define SHIFT_LED_INDEX 11
#define PLAY_LED_INDEX 12
/* LED SHIFT PINS { 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 };
Pins 0-7
*/
const char _ccNumberByGlobalMuxPinID[32] = { 17, 25, 26, 18, 27, 19, 28, 20, 21, 29, 30, 22, 31, 23, 32, 24, 1, 9, 10, 2, 11, 3, 12, 4, 5, 13, 14, 6, 15, 7, 16, 8 };
const char _buttonIDByGlobalMuxPinID[32] = { 8, 0, 1, 9, 2, 10, 3, 11, 12, 4, 5, 13, 6, 14, 7, 15, 40, 40, 40, 40, 16, 18, 19, 17, 20, 21, 40, 40, 40, 40, 40, 40 }; // todo enter correct ids
//const char _ledIndexByID[32] = { 7, 5, 2, 1, 8, 13, 12, 9, 6, 4, 3, 0, 10, 15, 14, 11, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 }; // todo update after Romans workover
const char _ledIndexByID[32] = { 23, 21, 18, 17, 24, 29, 28, 25, 22, 20, 19, 16, 26, 31, 30, 27, 0, 1, 2, 3, ENCODER_R_LED_INDEX, ENCODER_G_LED_INDEX, ENCODER_B_LED_INDEX, RANGE_1_LED_INDEX, RANGE_2_LED_INDEX, RANGE_3_LED_INDEX, RANGE_4_LED_INDEX, SHIFT_LED_INDEX, PLAY_LED_INDEX, 13, 14, 15 };
char _ledStates[32] = { 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 };
unsigned long _buttonLongPressStartTime[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
unsigned int _selectedMuxChannel = 0;
unsigned int _selectedEngine = 0;
State _muxStates[8];
SequencerEngine _sequencer;
DrumRackEngine _drumRack;
ArpeggiatorEngine _arpEngine;
ActionButtonEngine _actionEngine;
bool _playing = false;
int _midiClockPosition = 0;
long _tempTime = 0;
void setup()
{
pinMode(LED_SRCLK, OUTPUT);
pinMode(LED_RCLK, OUTPUT);
pinMode(LED_DATA, OUTPUT);
pinMode(MUX_SELECT_PIN_0, OUTPUT);
pinMode(MUX_SELECT_PIN_1, OUTPUT);
pinMode(MUX_SELECT_PIN_2, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(ENCODER_PIN_A, INPUT);
pinMode(ENCODER_PIN_B, INPUT);
digitalWrite(ENCODER_PIN_A, HIGH); // turn on pullup resistor
digitalWrite(ENCODER_PIN_B, HIGH); // turn on pullup resistor
attachInterrupt(0, updateRotaryEncoder, CHANGE); // encoder pin on interrupt 0 - pin 2
_selectedMuxChannel = 0;
selectMuxChannel(_selectedMuxChannel);
flushLedStates();
_muxStates[0].pinID = MUX_INPUT_PIN_1;
_muxStates[0].setBinary(true, 1); // set the switches to binary {0,127}
_muxStates[0].setBinary(true, 2);
_muxStates[0].setBinary(true, 4);
_muxStates[0].setBinary(true, 6);
_muxStates[1].pinID = MUX_INPUT_PIN_2;
_muxStates[1].setBinary(true, 1); // set the switches to binary {0,127}
_muxStates[1].setBinary(true, 2);
_muxStates[1].setBinary(true, 4);
_muxStates[1].setBinary(true, 6);
_muxStates[2].pinID = MUX_INPUT_PIN_3;
_muxStates[3].pinID = MUX_INPUT_PIN_4;
_muxStates[4].pinID = MUX_INPUT_PIN_5;
_muxStates[4].setBinary(true); // make button mux binary
_muxStates[5].pinID = MUX_INPUT_PIN_6;
_muxStates[5].setBinary(true); // make button mux binary
_muxStates[6].pinID = MUX_INPUT_PIN_7;
_muxStates[6].setBinary(true); // make button mux binary
_muxStates[6].setBinary(false, 1); // make actionButton-Potentiometers non-binary
_muxStates[6].setBinary(false, 2); // make actionButton-Potentiometers non-binary
_muxStates[7].pinID = MUX_INPUT_PIN_8;
_muxStates[7].setBinary(true); // make shiftbutton and playbutton mux binary
MIDI.begin();
MIDI.setHandleClock(clockCallback);
MIDI.setHandleStart(startCallback);
MIDI.setHandleStop(stopCallback);
MIDI.setHandleContinue(continueCallback);
}
void loop()
{
MIDI.read();
processCCValues();
processButtonEventsAndStateSettings();
processActionEngine();
_selectedMuxChannel = (_selectedMuxChannel + 1) % 8;
selectMuxChannel(_selectedMuxChannel);
/*if (millis() - _tempTime > 20)
{
clockCallback();
_tempTime = millis();
}*/
if (_selectedMuxChannel == 0)
{
bool shiftDown = (_muxStates[7].values[0] > 60);
_sequencer.shiftDown = shiftDown;
_drumRack.shiftDown = shiftDown;
//_arpeggiator.shiftDown = shiftDown;
char tempEngine = _selectedEngine;
if (_muxStates[6].values[0] > 60)
{
_selectedEngine = 0;
_sequencer.updateLedStates(_ledStates, _ledIndexByID);
}
else if (_muxStates[6].values[3] > 60)
{
_selectedEngine = 2;
//_arpeggiator.updateLedStates(_ledStates);
}
else
{
_selectedEngine = 1;
_drumRack.updateLedStates(_ledStates, _ledIndexByID);
}
if (tempEngine != _selectedEngine)
{
switch (tempEngine)
{
case 0:
break;
case 1:
{
releaseDrumRackNotes(_drumRack.releaseAll());
break;
}
case 2:
break;
default:
break;
}
/*Serial.print("new engine selected :");
Serial.print((int)_selectedEngine);
Serial.println();
*/
}
updateGeneralLeds();
flushLedStates();
}
}
void updateGeneralLeds()
{
_actionEngine.updateLedStates(_ledStates, _ledIndexByID);
_ledStates[ENCODER_R_LED_INDEX] = 0;
_ledStates[ENCODER_G_LED_INDEX] = 0;
_ledStates[ENCODER_B_LED_INDEX] = 0;
_ledStates[SHIFT_LED_INDEX] = 0;
switch (_selectedEngine)
{
case 0:
_ledStates[ENCODER_R_LED_INDEX] = 1;
if (_sequencer.shiftDown)
{
_ledStates[ENCODER_B_LED_INDEX] = 2;
_ledStates[SHIFT_LED_INDEX] = 1;
}
break;
case 1:
_ledStates[ENCODER_G_LED_INDEX] = 1;
if (_drumRack.shiftDown)
{
_ledStates[ENCODER_R_LED_INDEX] = 2;
_ledStates[SHIFT_LED_INDEX] = 1;
}
break;
case 2:
_ledStates[ENCODER_B_LED_INDEX] = 1;
if (_drumRack.shiftDown) // //if (_arpeggiator.shiftDown)
{
_ledStates[ENCODER_G_LED_INDEX] = 2;
_ledStates[SHIFT_LED_INDEX] = 1;
}
break;
default:
break;
}
_ledStates[PLAY_LED_INDEX] = (_playing) ? 1 : 0;
}
void processButtonEventsAndStateSettings()
{
for (int i = 4; i <= 7; i++) // parse mux 4-7 (binary muxes: Buttons and Tristate)
{
int globalButtonId = _buttonIDByGlobalMuxPinID[(i - 4) * 8 + _selectedMuxChannel];
int buttonAction = _muxStates[i].updateValue(_selectedMuxChannel);
if (buttonAction != -1) // buttonAction -1: nothing, 0: buttonUp, 127: buttonDown
{
if (i == 6)
{
// action button engine Potis
if(_selectedMuxChannel == 1)
{
_actionEngine.value1Change(127 - buttonAction); // hack 127-buttonAction, apparently potis are flipped
continue;
} else if (_selectedMuxChannel == 2)
{
_actionEngine.value2Change(127 - buttonAction); // hack 127-buttonAction, apparently potis are flipped
continue;
}
}
if (buttonAction == 0)
{
//Serial.print("buttonUp : "); Serial.print(globalButtonId); Serial.print(" mux"); Serial.print(i); Serial.print(" channel"); Serial.println((int) _selectedMuxChannel);
if (globalButtonId < 16) // sequencer-button
{
switch (_selectedEngine)
{
case 0: // Sequencer
//_sequencer.buttonUp(globalButtonId);
break;
case 1: // Drumrack
{
releaseDrumRackNotes(_drumRack.buttonUp(globalButtonId));
break;
}
case 2: // Arpeggiator
break;
default:
break;
}
_buttonLongPressStartTime[globalButtonId] = 0;
}else
{
}
}
else if (buttonAction > 0)
{
/*Serial.print("buttonDown : "); Serial.print(globalButtonId); Serial.print(" mux"); Serial.print(i); Serial.print(" channel"); Serial.println((int) _selectedMuxChannel);*/
if (globalButtonId < 16) // sequencer-button
{
switch (_selectedEngine)
{
case 0: // Sequencer
_sequencer.buttonDown(globalButtonId);
break;
case 1: // Drumrack
{
char drumRackPressMessage = _drumRack.buttonDown(globalButtonId);
MIDI.sendNoteOn((midi::DataByte) drumRackPressMessage, (midi::DataByte) _drumRack.velocity, (midi::Channel) DRUM_RACK_MIDI_CHANNEL);
break;
}
case 2: // Arpeggiator
break;
default:
break;
}
_buttonLongPressStartTime[globalButtonId] = millis();
}
else
{
//Serial.println(globalButtonId);
if (globalButtonId == 21)
{
_playing = !_playing;
if (_playing)
MIDI.sendRealTime((midi::MidiType) midi::Start);
else
MIDI.sendRealTime((midi::MidiType) midi::Stop);
}
else
{
_actionEngine.buttonDown(globalButtonId);
//Serial.println(globalButtonId);
}
}
}
}
if ((globalButtonId < 16) && (_buttonLongPressStartTime[globalButtonId] > 0)) // sequencer-button is down and we have to detect a longpress event
{
if ((unsigned long)(millis() - _buttonLongPressStartTime[globalButtonId]) >= 1500)
{
_buttonLongPressStartTime[globalButtonId] = 0;
// button longPressEvent
if(_selectedEngine == 0)
{
_sequencer.buttonLongPress(globalButtonId); // clear sequence
}
//Serial.print("buttonLongPress : "); Serial.println(globalButtonId);
}
}
}
}
void releaseDrumRackNotes(char* releaseMessages)
{
int i = 0;
while (releaseMessages[i] >= 0)
{
MIDI.sendNoteOff((midi::DataByte) releaseMessages[i], (midi::DataByte) _drumRack.velocity, (midi::Channel) DRUM_RACK_MIDI_CHANNEL);
++i;
}
}
void processCCValues()
{
for (int i = 0; i <= 3; i++) // parse mux 0-3
{
int newValue = _muxStates[i].updateValue(_selectedMuxChannel);
if (newValue != -1)
{
//Serial.print("midicc "); Serial.println((int) newValue);
midi::DataByte ccNumber = (midi::DataByte) (_ccNumberByGlobalMuxPinID[i * 8 + _selectedMuxChannel]);
MIDI.sendControlChange(ccNumber, (midi::DataByte) newValue, (midi::Channel) CC_MIDI_CHANNEL);
//Serial.print("mux"); Serial.print(i); Serial.print(" pin"); Serial.print(int(_selectedMuxChannel)); Serial.print(": "); Serial.println(ccNumber);
}
}
}
void processActionEngine()
{
if (_actionEngine.active)
{
char actionCC[4];
_actionEngine.getCCValues(actionCC);
for (size_t i = 0; i < 4; i++)
{
if (actionCC[i] != -1)
{
//Serial.print(millis()); Serial.print(";"); Serial.println((int)actionCC[i]);
MIDI.sendControlChange((midi::DataByte) (33 + i), (midi::DataByte) actionCC[i], (midi::Channel) CC_MIDI_CHANNEL);
}
}
}
}
void updateRotaryEncoder()
{
int val = (digitalRead(ENCODER_PIN_A) == digitalRead(ENCODER_PIN_B)) ? -1 : 1;
switch (_selectedEngine)
{
case 0:
_sequencer.encoderValueChange(val);
break;
case 1:
_drumRack.encoderValueChange(val);
break;
case 2:
break;
default:
break;
}
}
void selectMuxChannel(int channelNumber)
{
digitalWrite(MUX_SELECT_PIN_0, channelNumber & 1);
digitalWrite(MUX_SELECT_PIN_1, channelNumber & 2);
digitalWrite(MUX_SELECT_PIN_2, channelNumber & 4);
}
void flushLedStates()
{
digitalWrite(LED_RCLK, LOW);
for (int outputIndex = 31; outputIndex >= 0; outputIndex--)
{
bool val = _ledStates[outputIndex];
if (_ledStates[outputIndex] >= 2)
{
val = ((int)(millis()*(int)_ledStates[outputIndex] / 1000) % 2);
}
if ((8 <= outputIndex) && (outputIndex <= 10))
digitalWrite(LED_DATA, !val);
else
digitalWrite(LED_DATA, val);
digitalWrite(LED_SRCLK, HIGH);
digitalWrite(LED_SRCLK, LOW);
}
digitalWrite(LED_RCLK, HIGH);
}
void noteOnCallback(byte channel, byte note, byte velocity)
{
/*digitalWrite(LED, HIGH);
delay(10*note);
digitalWrite(LED, LOW);*/
//MIDI.sendNoteOn(40, 127, 1); // Send a Note (pitch 42, velo 127 on channel 1)
//delay(1000);
//MIDI.sendNoteOff(40, 0, 1); // Stop the note
}
void clockCallback()
{
_midiClockPosition++;
if(_midiClockPosition % 3 == 0)
{
// todo make some tuple class for the "message" with note value, and message
char msg[16];
_sequencer.setSequencePosition(_midiClockPosition / 3);
_sequencer.getMessagesAtCurrentPosition(msg);
for (size_t i = 0; i < 16; i++)
{
if ((msg[i] == Sequence::NOTE_PLAY) || (msg[i] == Sequence::NOTE_REPLAY))
{
MIDI.sendNoteOn(i + C1_OFFSET, 127, (midi::Channel) SEQUENCER_MIDI_CHANNEL);
}
else if (msg[i] == Sequence::NOTE_STOP)
{
MIDI.sendNoteOff(i + C1_OFFSET, 127, (midi::Channel) SEQUENCER_MIDI_CHANNEL);
}
}
}
}
void startCallback()
{
_midiClockPosition = -1;
_playing = true;
}
void continueCallback()
{
_playing = true;
}
void stopCallback()
{
_playing = false;
_midiClockPosition = 0;
_sequencer.setSequencePosition(-1);
}