-
Notifications
You must be signed in to change notification settings - Fork 0
/
MidiCentral.ino
285 lines (254 loc) · 7.2 KB
/
MidiCentral.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
#include <MIDI.h>
#include <Encoder.h>
#include <TimerOne.h>
#include <Adafruit_LEDBackpack.h>
// TODO:
// option for active-sensing (send at <300ms)
// select clock source
// pin definitions
#define PIN_BTN_START 11
#define PIN_BTN_TAP 12
#define PIN_ENCODER_A 21
#define PIN_ENCODER_B 20
#define PIN_ENCODER_BTN 6
#define PIN_CLK_IN 2 // not used yet
#define PIN_CLK_OUT 3
#define PIN_STST_IN 4
#define PIN_STST_OUT 5
#define PIN_LED_STARTED 22
#define PIN_LED_STOPPED 23
// I2C address of 7-segment display
#define DISP_I2C_ADDR 0x70
// MIDI definitions
#define CLOCKS_PER_BEAT 24
#define CLOCK_FROM_BPM(b) (60000000UL/(CLOCKS_PER_BEAT*b))
#define BPM_FROM_US(b) (60000000UL/(b))
// others
#define MINIMUM_US BPM_FROM_US(MAXIMUM_BPM)
#define MAXIMUM_US BPM_FROM_US(MINIMUM_BPM)
// minimal/maximal BPM
#define MINIMUM_BPM 30
#define MAXIMUM_BPM 300
// debounce time for start/stop button
#define BTN_START_DEBOUNCE 200
// global variables
static Adafruit_7segment disp;
static Encoder encoder(PIN_ENCODER_A, PIN_ENCODER_B);
static MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, midiA);
static MIDI_CREATE_INSTANCE(HardwareSerial, Serial2, midiB);
static volatile uint16_t bpm = 120;
static volatile bool started = false;
static volatile uint8_t clockCount = 0;
static volatile uint32_t btnStartPressedTime = 0;
static void sendMidi(midi::MidiType type, midi::DataByte data1 = 0, midi::DataByte data2 = 0, midi::Channel channel = 0) {
if (type <= midi::MidiType::PitchBend) {
midiA.send(type, data1, data2, channel);
midiB.send(type, data1, data2, channel);
usbMIDI.send(type, data1, data2, channel, 0);
}
else {
midiA.sendRealTime(type);
midiB.sendRealTime(type);
usbMIDI.sendRealTime(type);
}
}
void interruptSendMidiClock() {
if (started) {
if (clockCount == 0) {
digitalWrite(PIN_CLK_OUT, LOW);
digitalWrite(PIN_LED_STARTED, HIGH);
} else if (clockCount == CLOCKS_PER_BEAT/2) {
digitalWrite(PIN_CLK_OUT, HIGH);
digitalWrite(PIN_LED_STARTED, LOW);
}
sendMidi(midi::MidiType::Clock);
} else {
if (clockCount == 0) {
digitalWrite(PIN_LED_STOPPED, HIGH);
} else if (clockCount == CLOCKS_PER_BEAT/2) {
digitalWrite(PIN_LED_STOPPED, LOW);
}
}
clockCount = (clockCount + 1) % CLOCKS_PER_BEAT;
}
static void start(midi::MidiType type) {
if (!started) {
noInterrupts();
started = true;
clockCount = 0;
digitalWrite(PIN_LED_STOPPED, LOW);
digitalWrite(PIN_STST_OUT, LOW);
digitalWrite(PIN_CLK_OUT, HIGH);
sendMidi(type);
interrupts();
}
}
static void stop() {
if (started) {
noInterrupts();
started = false;
digitalWrite(PIN_LED_STARTED, LOW);
digitalWrite(PIN_STST_OUT, HIGH);
digitalWrite(PIN_CLK_OUT, HIGH);
sendMidi(midi::MidiType::Stop);
interrupts();
}
}
static void interruptStartButton() {
if (!digitalRead(PIN_BTN_START)) {
static uint32_t lastTime = 0;
uint32_t now = millis();
if (now - lastTime < BTN_START_DEBOUNCE)
return;
if (started)
stop();
else
start(midi::MidiType::Start);
lastTime = now;
btnStartPressedTime = now;
} else{
btnStartPressedTime = 0;
}
}
static void checkSystemReset() {
uint32_t now = millis();
if (btnStartPressedTime > 0 && now - btnStartPressedTime > 2000) {
btnStartPressedTime = 0;
disp.printError();
disp.writeDisplay();
stop();
sendMidi(midi::MidiType::SystemReset);
delay(500);
disp.print(bpm, DEC);
disp.writeDisplay();
}
}
static void interruptStartExtern() {
if (digitalRead(PIN_STST_IN))
stop();
else
start(midi::MidiType::Start);
}
static void interruptTapButton() {
static uint32_t tapInterval[2];
static uint32_t lastTime = 0;
noInterrupts();
uint32_t now = micros();
uint32_t dt = now - lastTime;
if (dt < MINIMUM_US || dt > MAXIMUM_US) {
tapInterval[0] = 0;
tapInterval[1] = 0;
lastTime = now;
} else {
tapInterval[1] = tapInterval[0];
tapInterval[0] = dt;
lastTime = now;
if (tapInterval[0] != 0 && tapInterval[1] != 0) {
uint32_t interval = (tapInterval[0] + tapInterval[1]) / 2;
bpm = BPM_FROM_US(interval);
}
}
interrupts();
}
static void interruptEncoderButton() {
static uint32_t lastTime = 0;
uint32_t now = millis();
if (now - lastTime < BTN_START_DEBOUNCE)
return;
if (started)
stop();
else
start(midi::MidiType::Continue);
lastTime = now;
}
static inline void readEncoder() {
static int32_t lastEncoderValue = 0;
int32_t encoderValue = encoder.read();
if (encoderValue - lastEncoderValue <= -4) {
bpm--;
lastEncoderValue = encoderValue;
} else if (encoderValue - lastEncoderValue >= 4) {
bpm++;
lastEncoderValue = encoderValue;
}
}
static inline void updateBpm() {
static int lastBpm = 0;
bpm = constrain(bpm, MINIMUM_BPM, MAXIMUM_BPM);
if (bpm != lastBpm) {
Timer1.setPeriod(CLOCK_FROM_BPM(bpm));
disp.print(bpm, DEC);
disp.writeDisplay();
lastBpm = bpm;
}
}
static inline bool filterMidiInput(midi::MidiType type) {
switch (type) {
case midi::MidiType::Start:
case midi::MidiType::Continue:
start(type);
return false;
case midi::MidiType::Stop:
stop();
return false;
case midi::MidiType::Clock:
return false;
default:
return true;
}
}
static inline void forwardMidi() {
if (midiA.read()) {
midi::MidiType type = midiA.getType();
if (filterMidiInput(type))
sendMidi(type, midiA.getData1(), midiA.getData2(), midiA.getChannel());
}
if (midiB.read()) {
midi::MidiType type = midiB.getType();
if (filterMidiInput(type))
sendMidi(type, midiB.getData1(), midiB.getData2(), midiB.getChannel());
}
if (usbMIDI.read()) {
midi::MidiType type = (midi::MidiType)usbMIDI.getType();
if (filterMidiInput(type))
sendMidi(type, usbMIDI.getData1(), usbMIDI.getData2(), usbMIDI.getChannel());
}
}
void setup() {
// configure pins
pinMode(PIN_BTN_START, INPUT_PULLUP);
pinMode(PIN_BTN_TAP, INPUT_PULLUP);
pinMode(PIN_ENCODER_BTN, INPUT_PULLUP);
pinMode(PIN_CLK_IN, INPUT);
pinMode(PIN_CLK_OUT, OUTPUT);
pinMode(PIN_STST_IN, INPUT);
pinMode(PIN_STST_OUT, OUTPUT);
pinMode(PIN_LED_STARTED, OUTPUT);
pinMode(PIN_LED_STOPPED, OUTPUT);
// attach interrupts
attachInterrupt(digitalPinToInterrupt(PIN_BTN_START), interruptStartButton, CHANGE);
attachInterrupt(digitalPinToInterrupt(PIN_BTN_TAP), interruptTapButton, FALLING);
attachInterrupt(digitalPinToInterrupt(PIN_ENCODER_BTN), interruptEncoderButton, FALLING);
attachInterrupt(digitalPinToInterrupt(PIN_STST_IN), interruptStartExtern, CHANGE);
// initialize display
disp.begin(DISP_I2C_ADDR);
// initialize MIDI A
midiA.begin(MIDI_CHANNEL_OMNI);
midiA.turnThruOff();
// initialize MIDI B
midiB.begin(MIDI_CHANNEL_OMNI);
midiB.turnThruOff();
// initialize timer to send the MIDI clock
Timer1.initialize(CLOCK_FROM_BPM(bpm));
Timer1.attachInterrupt(interruptSendMidiClock);
}
void loop() {
// check for system reset
checkSystemReset();
// read encoder
readEncoder();
// update bpm
updateBpm();
// forward MIDI messages
forwardMidi();
}