12
12
13
13
BEGIN_BLEMIDI_NAMESPACE
14
14
15
+ using namespace MIDI_NAMESPACE ;
16
+
17
+ #define MIDI_TYPE 0x80
18
+
15
19
template <class T , class _Settings = DefaultSettings>
16
20
class BLEMIDI_Transport
17
21
{
@@ -46,7 +50,7 @@ class BLEMIDI_Transport
46
50
void begin ()
47
51
{
48
52
mBleClass .begin (mDeviceName , this );
49
- }
53
+ }
50
54
51
55
void end ()
52
56
{
@@ -75,7 +79,7 @@ class BLEMIDI_Transport
75
79
76
80
void endTransmission ()
77
81
{
78
- if (mTxBuffer [mTxIndex - 1 ] == 0xF7 )
82
+ if (mTxBuffer [mTxIndex - 1 ] == SystemExclusiveEnd )
79
83
{
80
84
if (mTxIndex >= sizeof (mTxBuffer ))
81
85
{
@@ -88,7 +92,7 @@ class BLEMIDI_Transport
88
92
{
89
93
mTxBuffer [mTxIndex - 1 ] = mTimestampLow ; // or generate new ?
90
94
}
91
- mTxBuffer [mTxIndex ++] = 0xF7 ;
95
+ mTxBuffer [mTxIndex ++] = SystemExclusiveEnd ;
92
96
}
93
97
94
98
mBleClass .write (mTxBuffer , mTxIndex );
@@ -180,7 +184,7 @@ class BLEMIDI_Transport
180
184
{
181
185
strncpy (mDeviceName , deviceName, sizeof (mDeviceName ));
182
186
};
183
-
187
+
184
188
public:
185
189
void setHandleConnected (void (*fptr)())
186
190
{
@@ -223,12 +227,12 @@ class BLEMIDI_Transport
223
227
from other messages – except for System Exclusive messages.
224
228
*/
225
229
226
- /* *
227
- * If #define RUNNING_ENABLE is commented, it will transform all incoming runningStatus messages in full midi messages.
230
+ /* *
231
+ * If #define RUNNING_ENABLE is commented/disabled , it will transform all incoming runningStatus messages in full midi messages.
228
232
* Else, it will put in the buffer the same info that it had received (runningStatus will be not transformated).
229
233
* It recommend not use runningStatus by default. Only use if parser accepts runningStatus and your application has a so high transmission rate.
230
- */
231
- // #define RUNNING_ENABLE
234
+ */
235
+ #define RUNNING_ENABLE
232
236
233
237
void receive (byte *buffer, size_t length)
234
238
{
@@ -239,7 +243,7 @@ class BLEMIDI_Transport
239
243
// lastStatus used to capture runningStatus
240
244
byte lastStatus;
241
245
// previousStatus used to continue a runningStatus interrupted by a timeStamp or a System Message.
242
- byte previousStatus = 0x00 ;
246
+ byte previousStatus = InvalidType ;
243
247
244
248
byte headerByte = buffer[lPtr++];
245
249
@@ -251,10 +255,10 @@ class BLEMIDI_Transport
251
255
bool sysExContinuation = false ;
252
256
bool runningStatusContinuation = false ;
253
257
254
- if (timestampByte >= 80 ) // if bit 7 is 1, it's a timestampByte
258
+ if (timestampByte >= MIDI_TYPE ) // if bit 7 is 1, it's a timestampByte
255
259
{
256
- auto timestampLow = 0x7f & timestampByte;
257
- timestamp = timestampLow + (timestampHigh << 7 );
260
+ timestamp = setMidiTimestamp (headerByte, timestampByte) ;
261
+ // what do to with the timestamp?
258
262
}
259
263
else // if bit 7 is 0, it's the Continuation of a previous SysEx
260
264
{
@@ -267,20 +271,20 @@ class BLEMIDI_Transport
267
271
{
268
272
lastStatus = buffer[lPtr];
269
273
270
- if (previousStatus == 0x00 )
274
+ if (previousStatus == InvalidType )
271
275
{
272
- if ((lastStatus < 0x80 ) && !sysExContinuation)
276
+ if ((lastStatus < MIDI_TYPE ) && !sysExContinuation)
273
277
return ; // Status message not present and it is not a runningStatus continuation, bail
274
278
}
275
- else if (lastStatus < 0x80 )
279
+ else if (lastStatus < MIDI_TYPE )
276
280
{
277
281
lastStatus = previousStatus;
278
282
runningStatusContinuation = true ;
279
283
}
280
284
281
285
// Point to next non-data byte
282
286
rPtr = lPtr;
283
- while ((buffer[rPtr + 1 ] < 0x80 ) && (rPtr < (length - 1 )))
287
+ while ((buffer[rPtr + 1 ] < MIDI_TYPE ) && (rPtr < (length - 1 )))
284
288
rPtr++;
285
289
286
290
if (!runningStatusContinuation)
@@ -289,15 +293,15 @@ class BLEMIDI_Transport
289
293
290
294
auto midiType = lastStatus & 0xF0 ;
291
295
if (sysExContinuation)
292
- midiType = 0xF0 ;
296
+ midiType = SystemExclusive ;
293
297
294
298
switch (midiType)
295
299
{
296
- case 0x80 :
297
- case 0x90 :
298
- case 0xA0 :
299
- case 0xB0 :
300
- case 0xE0 :
300
+ case NoteOff :
301
+ case NoteOn :
302
+ case AfterTouchPoly :
303
+ case ControlChange :
304
+ case PitchBend :
301
305
#ifdef RUNNING_ENABLE
302
306
mBleClass .add (lastStatus);
303
307
#endif
@@ -310,8 +314,8 @@ class BLEMIDI_Transport
310
314
mBleClass .add (buffer[i + 2 ]);
311
315
}
312
316
break ;
313
- case 0xC0 :
314
- case 0xD0 :
317
+ case ProgramChange :
318
+ case AfterTouchChannel :
315
319
#ifdef RUNNING_ENABLE
316
320
mBleClass .add (lastStatus);
317
321
#endif
@@ -323,7 +327,7 @@ class BLEMIDI_Transport
323
327
mBleClass .add (buffer[i + 1 ]);
324
328
}
325
329
break ;
326
- case 0xF0 :
330
+ case SystemExclusive :
327
331
mBleClass .add (lastStatus);
328
332
for (auto i = lPtr; i < rPtr; i++)
329
333
mBleClass .add (buffer[i + 1 ]);
@@ -338,16 +342,13 @@ class BLEMIDI_Transport
338
342
{
339
343
#ifndef RUNNING_ENABLE
340
344
auto midiType = lastStatus & 0xF0 ;
341
- if (sysExContinuation)
342
- midiType = 0xF0 ;
343
-
344
345
switch (midiType)
345
346
{
346
- case 0x80 :
347
- case 0x90 :
348
- case 0xA0 :
349
- case 0xB0 :
350
- case 0xE0 :
347
+ case NoteOff :
348
+ case NoteOn :
349
+ case AfterTouchPoly :
350
+ case ControlChange :
351
+ case PitchBend :
351
352
// 3 bytes full Midi -> 2 bytes runningStatus
352
353
for (auto i = lPtr; i <= rPtr; i = i + 2 )
353
354
{
@@ -356,8 +357,8 @@ class BLEMIDI_Transport
356
357
mBleClass .add (buffer[i + 1 ]);
357
358
}
358
359
break ;
359
- case 0xC0 :
360
- case 0xD0 :
360
+ case ProgramChange :
361
+ case AfterTouchChannel :
361
362
// 2 bytes full Midi -> 1 byte runningStatus
362
363
for (auto i = lPtr; i <= rPtr; i = i + 1 )
363
364
{
@@ -380,16 +381,16 @@ class BLEMIDI_Transport
380
381
if (++rPtr >= length)
381
382
return ; // end of packet
382
383
383
- if (lastStatus < 0xf0 ) // exclude System Message. They must not be RunningStatus
384
+ if (lastStatus < SystemExclusive ) // exclude System Message. They must not be RunningStatus
384
385
{
385
386
previousStatus = lastStatus;
386
387
}
387
388
388
389
timestampByte = buffer[rPtr++];
389
- if (timestampByte >= 80 ) // is bit 7 set?
390
+ if (timestampByte >= MIDI_TYPE ) // is bit 7 set?
390
391
{
391
- auto timestampLow = 0x7f & timestampByte;
392
- timestamp = timestampLow + (timestampHigh << 7 );
392
+ timestamp = setMidiTimestamp (headerByte, timestampByte) ;
393
+ // what do to with the timestamp?
393
394
}
394
395
395
396
// Point to next status
0 commit comments