-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmourino.cpp
More file actions
408 lines (347 loc) · 10.6 KB
/
mourino.cpp
File metadata and controls
408 lines (347 loc) · 10.6 KB
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
/*
Mourino: a MIDI to MIDI-BLE adapter using Arduino 101, based on MIDIBLE.ino example
Copyright (C) 2017 Daniel Moura <oxe@oxesoft.com>
This test simulates the normal operation of a MIDI device connected to the
Arduino 101, in both ways (IN, OUT)
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/>.
*/
#if EXECUTE_FROM_COMMAND_LINE
#define byte unsigned char
#define BLE_MAX_ATTR_DATA_LEN 20
#define MINIMUM_CONN_INTERVAL 11250
#define OUTPUT 1
#define HIGH 1
#define BLEWrite 1
#define BLEWriteWithoutResponse 2
#define BLENotify 4
#define BLERead 8
#define BLEWritten 1
#define BLEConnected 1
#define BLEDisconnected 2
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <time.h>
#define SERIAL_MIDI_BYTES_COUNT 200
byte testStream[SERIAL_MIDI_BYTES_COUNT] = {
0x90, 0x01, 0x7F,
0x90, 0x02, 0x01,
0x90, 0x02, 0x7F,
0x90, 0x03, 0x01,
0x90, 0x03, 0x02,
0x90, 0x03, 0x03,
0x90, 0x03, 0x04,
0x90, 0x03, 0x05,
0x90, 0x03, 0x06,
0x90, 0x03, 0x07,
0x90, 0x03, 0x7F,
0x90, 0x03, 0x01,
0x91, 0x03, 0x02,
0x90, 0x03, 0x03,
0x90, 0x03, 0x04,
0x90, 0x03, 0x05,
0x90, 0x03, 0x7F,
0xC0, 0x7F,
0xC0, 0x01,
0xD0, 0x7F,
0xC0, 0x01,
0xC0, 0x02,
0xD0, 0x01,
0xD0, 0x7F,
0xC0, 0x01,
0xC0, 0x02,
0xD1, 0x01,
0xD0, 0x7F,
0x90, 0x01, 0x01,
0x91, 0x02, 0x02,
0x93, 0x03, 0x03,
0x94, 0x04, 0x04,
0x95, 0x05, 0x05,
0x96, 0x01, 0x7F,
0x90, 0x01, 0x01,
0xC0, 0x01,
0xC0, 0x02,
0x91, 0x01, 0x7F,
0xC0, 0x01,
0xC0, 0x02,
0xC0, 0x03,
0xC0, 0x04,
0xC0, 0x05,
0xC0, 0x06,
0xC0, 0x07,
0xC0, 0x08,
0xC0, 0x09,
0xC0, 0x0A,
0xC0, 0x0B,
0xC0, 0x0C,
0xC0, 0x0D,
0xC0, 0x0E,
0xC0, 0x0F,
0xC0, 0x10,
0xC0, 0x11,
0xC0, 0x7F,
0xF0, 0x01, 0x02, 0x03, 0x04, 0xF7,
0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xF7,
0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0xF7,
0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0xF7
};
#define BLE_MIDI_PACKETS_COUNT 19
byte packets[BLE_MIDI_PACKETS_COUNT][BLE_MAX_ATTR_DATA_LEN] = {
{0x80, 0x80, 0x90, 0x01, 0x7F},
{0x80, 0x80, 0x90, 0x02, 0x01, 0x02, 0x7F},
{0x80, 0x80, 0x90, 0x03, 0x01, 0x03, 0x02, 0x03, 0x03, 0x03, 0x04, 0x03, 0x05, 0x03, 0x06, 0x03, 0x07, 0x03, 0x7F},
{0x80, 0x80, 0x90, 0x03, 0x01, 0x80, 0x91, 0x03, 0x02, 0x80, 0x90, 0x03, 0x03, 0x03, 0x04, 0x03, 0x05, 0x03, 0x7F},
{0x80, 0x80, 0xC0, 0x7F},
{0x80, 0x80, 0xC0, 0x01, 0x80, 0xD0, 0x7F},
{0x80, 0x80, 0xC0, 0x01, 0x02, 0x80, 0xD0, 0x01, 0x7F},
{0x80, 0x80, 0xC0, 0x01, 0x02, 0x80, 0xD1, 0x01, 0x80, 0xD0, 0x7F},
{0x80, 0x80, 0x90, 0x01, 0x01, 0x80, 0x91, 0x02, 0x02, 0x80, 0x93, 0x03, 0x03, 0x80, 0x94, 0x04, 0x04},
{0x80, 0x80, 0x95, 0x05, 0x05, 0x80, 0x96, 0x01, 0x7F},
{0x80, 0x80, 0x90, 0x01, 0x01, 0x80, 0xC0, 0x01, 0x02, 0x80, 0x91, 0x01, 0x7F},
{0x80, 0x80, 0xC0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11},
{0x80, 0x80, 0xC0, 0x7F},
{0x80, 0x80, 0xF0, 0x01, 0x02, 0x03, 0x04, 0x80, 0xF7},
{0x80, 0x80, 0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x80, 0xF7},
{0x80, 0x80, 0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11},
{0x80, 0x80, 0xF7},
{0x80, 0x80, 0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11},
{0x80, 0x12, 0x13, 0x80, 0xF7},
};
int packetsLength[BLE_MIDI_PACKETS_COUNT] = {
5,
7,
19,
19,
4,
7,
9,
11,
17,
9,
13,
20,
4,
9,
20,
20,
3,
20,
5
};
int packetsCount = 0;
int bytesCount = 0;
int step = 1;
class BLEService;
class BLEDevice {
public:
BLEDevice();
void begin();
void setDeviceName(const char *name);
void setLocalName(const char *name);
void advertise();
void addService(BLEService &service);
void setAdvertisedServiceUuid(int uuid);
void setEventHandler(int type, void (*handler)(BLEDevice central));
void poll();
private:
BLEService *service;
int packetsPosition;
};
class BLECharacteristic {
public:
BLECharacteristic(const char *uuid, int pars, int len);
void setValue(byte *buffer, int len);
void setEventHandler(int type, void (*callbackFunction)(BLEDevice, BLECharacteristic));
int valueLength();
byte* value();
void fireEvent(BLEDevice &device, byte *buffer, int len);
private:
void (*callbackFunction)(BLEDevice d, BLECharacteristic c);
byte *buffer;
int len;
};
class BLEService {
public:
BLEService(const char *uuid);
void addCharacteristic(BLECharacteristic &characteristic);
int uuid();
void fireEvent(BLEDevice &device, byte *buffer, int len);
private:
BLECharacteristic *characteristic;
};
// -----------------------------------------------------------------------------
BLEDevice::BLEDevice() {
this->service = NULL;
this->packetsPosition = 0;
}
void BLEDevice::begin() {
}
void BLEDevice::setDeviceName(const char *name) {
}
void BLEDevice::setLocalName(const char *name) {
}
void BLEDevice::advertise() {
}
void BLEDevice::addService(BLEService &service) {
this->service = &service;
}
void BLEDevice::setAdvertisedServiceUuid(int uuid) {
}
void BLEDevice::setEventHandler(int type, void (*handler)(BLEDevice central)) {
}
void BLEDevice::poll() {
if (packetsPosition < BLE_MIDI_PACKETS_COUNT) {
this->service->fireEvent(*this, packets[this->packetsPosition], packetsLength[this->packetsPosition]);
this->packetsPosition++;
}
}
// -----------------------------------------------------------------------------
BLECharacteristic::BLECharacteristic(const char *uuid, int pars, int len) {
this->callbackFunction = NULL;
}
void BLECharacteristic::setValue(byte *buffer, int len) {
switch (step) {
case 1:
printf("packet (%02i bytes):", len);
for (int i = 0; i < len; i++) {
printf(" %02X", buffer[i]);
}
printf("\n");
if (memcmp(buffer, packets[packetsCount], len)) {
printf("--------------------\n");
printf("UNEXPECTED PACKET!!!\n");
printf("--------------------\n");
exit(1);
}
packetsCount++;
break;
default:
break;
}
}
void BLECharacteristic::setEventHandler(int type, void (*callbackFunction)(BLEDevice, BLECharacteristic)) {
this->callbackFunction = callbackFunction;
}
int BLECharacteristic::valueLength() {
return this->len;
}
byte* BLECharacteristic::value() {
return this->buffer;
}
void BLECharacteristic::fireEvent(BLEDevice &device, byte *buffer, int len) {
this->buffer = buffer;
this->len = len;
this->callbackFunction(device, *this);
}
// -----------------------------------------------------------------------------
BLEService::BLEService(const char *uuid) {
this->characteristic = NULL;
}
void BLEService::addCharacteristic(BLECharacteristic &characteristic) {
this->characteristic = &characteristic;
}
int BLEService::uuid() {
return 0;
}
void BLEService::fireEvent(BLEDevice &device, byte *buffer, int len) {
this->characteristic->fireEvent(device, buffer, len);
}
// -----------------------------------------------------------------------------
static unsigned long timeMicros = 0;
unsigned long micros() {
return timeMicros;
}
unsigned long millis() {
return 0;
}
void pinMode(int pin, int mode) {
}
void digitalWrite(int pin, int value) {
}
class Serial {
public:
unsigned int pointer;
Serial() {
pointer = 0;
}
void begin(int baudrate) {
}
int available() {
switch (step) {
case 1: {
int length = SERIAL_MIDI_BYTES_COUNT - pointer;
if (!length) {
timeMicros += MINIMUM_CONN_INTERVAL;
}
return length;
}
default:
return 1;
}
}
byte read() {
switch (step) {
case 1: {
byte b = testStream[pointer++];
if (b == 0x7F || b == 0xF7) {
timeMicros += MINIMUM_CONN_INTERVAL;
}
return b;
}
default:
return rand() % 0xFF;
}
}
void write(byte b) {
if (b != testStream[bytesCount] || bytesCount >= SERIAL_MIDI_BYTES_COUNT) {
printf("--------------------------------------\n");
printf("UNEXPECTED BYTE 0x%02X (expected 0x%02X)!!!\n", b, testStream[bytesCount]);
printf("--------------------------------------\n");
exit(1);
}
bytesCount++;
}
};
// -----------------------------------------------------------------------------
BLEDevice BLE;
Serial Serial1;
#include "mourino.ino"
int main() {
unsigned int count = 0;
setup();
// -------------------------------------------------------------------------
step = 1;
while (packetsCount < BLE_MIDI_PACKETS_COUNT || bytesCount < SERIAL_MIDI_BYTES_COUNT) {
loop();
count++;
if (count > SERIAL_MIDI_BYTES_COUNT + 4) {
printf("--------------------------------------\n");
if (packetsCount < BLE_MIDI_PACKETS_COUNT) {
printf("PACKET NOT RECEIVED!!!\n");
} else {
printf("BYTE NOT SENT!!!\n");
}
printf("--------------------------------------\n");
break;
}
}
// -------------------------------------------------------------------------
step = 2;
count = 1000000;
while (count--) {
loop();
}
// -------------------------------------------------------------------------
return EXIT_SUCCESS;
}
#endif