-
Notifications
You must be signed in to change notification settings - Fork 2
/
mourino.ino
256 lines (228 loc) · 7.34 KB
/
mourino.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
/*
Mourino: a MIDI to MIDI-BLE adapter using Arduino 101, based on MIDIBLE.ino example
Copyright (C) 2017 Daniel Moura <oxe@oxesoft.com>
This sketch implements the conversion between standard serial MIDI messages and
Bluetooth Low Energy MIDI Specification as detailed in the docucument at
https://developer.apple.com/bluetooth/Apple-Bluetooth-Low-Energy-MIDI-Specification.pdf
To connect a MIDI keyboard to the Arduino 101 RX pin you should make a small circuit
using an optocoupler as largely shown on internet (make a search of images with the words
"MIDI optocoupler arduino"). To connect the Arduino 101 TX pin to your MIDI device just
use a 220R resistor.
This code is originally hosted at https://github.com/oxesoft/mourino
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <CurieBLE.h>
#define DEVICE_NAME "Mourino"
#define MINIMUM_CONNECTION_INTERVAL 11250 // 11.25 ms
#define SINGLE_BLE_PACKET_LENGTH BLE_MAX_ATTR_DATA_LEN
#define MIDI_EVENT_MAX_LENGTH SINGLE_BLE_PACKET_LENGTH - 1 /* minus the header size */
#define EXTERNAL_LED_PIN 7
BLEService midiService("03B80E5A-EDE8-4B33-A751-6CE34EC4C700");
BLECharacteristic midiChar("7772E5DB-3868-4112-A1A9-F2669D106BF3", BLEWrite | BLEWriteWithoutResponse | BLENotify | BLERead, SINGLE_BLE_PACKET_LENGTH);
// variables used in SERIAL to BLE only
uint8_t midiData[SINGLE_BLE_PACKET_LENGTH];
uint8_t midiEvent[MIDI_EVENT_MAX_LENGTH];
int midiDataPosition;
int midiEventPosition;
byte prevStatus;
bool isConnected;
void sendPacket() {
midiChar.setValue(midiData, midiDataPosition);
midiDataPosition = 1;
prevStatus = 0;
}
void appendEvent(byte *event, int len) {
if (midiDataPosition + len > SINGLE_BLE_PACKET_LENGTH) {
sendPacket();
}
memcpy(midiData + midiDataPosition, event, len);
midiDataPosition += len;
midiEventPosition = 0;
}
void addEventByte(byte b) {
midiEvent[midiEventPosition++] = b;
if (midiEventPosition >= MIDI_EVENT_MAX_LENGTH) { // sysex data
appendEvent(midiEvent, midiEventPosition);
}
}
int getEventSize(byte b) {
/*
* https://www.midi.org/specifications/item/table-1-summary-of-midi-message
*/
byte eventType = b & 0xF0;
switch (eventType) {
case 0x80:
case 0x90:
case 0xA0:
case 0xB0:
case 0xE0:
return 2;
case 0xC0:
case 0xD0:
return 1;
case 0xF0:
switch (b) {
case 0xF0:
return -1;
case 0xF1:
return 1;
case 0xF2:
return 2;
case 0xF3:
return 1;
}
}
return 0;
}
void processByte(byte b) {
static int midiEventCount = 0;
bool newEvent = b & 0x80;
if (newEvent) {
if (b == 0xF7) {
appendEvent(midiEvent, midiEventPosition);
}
midiEventPosition = 0;
midiEventCount = getEventSize(b);
if (prevStatus != b || midiDataPosition + midiEventCount > SINGLE_BLE_PACKET_LENGTH) {
addEventByte(0x80); // empty timestamp
addEventByte(b);
}
if (midiEventCount > 0) {
prevStatus = b;
} else {
prevStatus = 0;
}
} else {
if (midiEventCount > 0) {
midiEventCount--;
}
addEventByte(b);
}
if (midiEventCount == 0) {
appendEvent(midiEvent, midiEventPosition);
}
}
void midiCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
static char outputState = 1;
static char eventBytesCount = 0;
static byte currentStatus = 0;
int len = characteristic.valueLength();
byte *buffer = (byte*)characteristic.value();
if (len < 0 || buffer == NULL) {
return;
}
len--; buffer++; // ignore header byte
while (len--) {
byte b = *(buffer++);
switch (outputState) {
case 1: // timestamp byte
if (b & 0x80) {
outputState = 2;
} else {
eventBytesCount = getEventSize(currentStatus);
Serial1.write(currentStatus);
Serial1.write(b);
eventBytesCount--;
if (eventBytesCount) {
outputState = 3;
} else {
outputState = 1;
}
}
break;
case 2: // event start
if (b & 0x80) {
eventBytesCount = getEventSize(b);
currentStatus = b;
Serial1.write(b);
} else {
eventBytesCount = getEventSize(currentStatus);
Serial1.write(currentStatus);
Serial1.write(b);
eventBytesCount--;
}
if (eventBytesCount) {
outputState = 3;
} else {
outputState = 1;
}
break;
case 3: // rest of event
if (b & 0x80) { // timestamp byte
outputState = 2;
} else {
Serial1.write(b);
if (eventBytesCount >= 0) {
eventBytesCount--;
}
if (eventBytesCount == 0) {
outputState = 1;
}
}
break;
}
}
}
void midiDeviceConnectHandler(BLEDevice central) {
isConnected = true;
}
void midiDeviceDisconnectHandler(BLEDevice central) {
isConnected = false;
}
void ledTick() {
static unsigned long ledTime = 0;
static byte ledValue = HIGH;
const int LED_BLINK_TIME = 500;
if (isConnected) {
digitalWrite(EXTERNAL_LED_PIN, HIGH);
return;
}
if (millis() - ledTime > LED_BLINK_TIME) {
ledValue = !ledValue;
digitalWrite(EXTERNAL_LED_PIN, ledValue);
ledTime = millis();
}
}
void setup() {
midiData[0] = 0x80; // header with empty timestamp
midiDataPosition = 1;
prevStatus = 0x00;
isConnected = false;
pinMode(EXTERNAL_LED_PIN, OUTPUT);
digitalWrite(EXTERNAL_LED_PIN, HIGH);
Serial1.begin(31250);
BLE.begin();
BLE.setLocalName(DEVICE_NAME);
BLE.setDeviceName(DEVICE_NAME);
BLE.setAdvertisedServiceUuid(midiService.uuid());
midiService.addCharacteristic(midiChar);
BLE.addService(midiService);
BLE.setEventHandler(BLEConnected, midiDeviceConnectHandler);
BLE.setEventHandler(BLEDisconnected, midiDeviceDisconnectHandler);
midiChar.setEventHandler(BLEWritten, midiCharacteristicWritten);
BLE.advertise();
}
void loop() {
static unsigned long time = 0;
if (Serial1.available()) {
byte b = Serial1.read();
processByte(b);
}
if (micros() - time >= MINIMUM_CONNECTION_INTERVAL) {
if (midiDataPosition > 1) {
sendPacket();
}
time = micros();
BLE.poll();
ledTick();
}
}