Skip to content

Commit 950ff4e

Browse files
committed
updated parser for MultiPart SysEx
Thanks to @RobertoHE
1 parent e0afefb commit 950ff4e

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed

src/BLEMIDI_Transport.h

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212

1313
BEGIN_BLEMIDI_NAMESPACE
1414

15+
using namespace MIDI_NAMESPACE;
16+
17+
#define MIDI_TYPE 0x80
18+
1519
template <class T, class _Settings = DefaultSettings>
1620
class BLEMIDI_Transport
1721
{
@@ -46,7 +50,7 @@ class BLEMIDI_Transport
4650
void begin()
4751
{
4852
mBleClass.begin(mDeviceName, this);
49-
}
53+
}
5054

5155
void end()
5256
{
@@ -75,7 +79,7 @@ class BLEMIDI_Transport
7579

7680
void endTransmission()
7781
{
78-
if (mTxBuffer[mTxIndex - 1] == 0xF7)
82+
if (mTxBuffer[mTxIndex - 1] == SystemExclusiveEnd)
7983
{
8084
if (mTxIndex >= sizeof(mTxBuffer))
8185
{
@@ -88,7 +92,7 @@ class BLEMIDI_Transport
8892
{
8993
mTxBuffer[mTxIndex - 1] = mTimestampLow; // or generate new ?
9094
}
91-
mTxBuffer[mTxIndex++] = 0xF7;
95+
mTxBuffer[mTxIndex++] = SystemExclusiveEnd;
9296
}
9397

9498
mBleClass.write(mTxBuffer, mTxIndex);
@@ -180,7 +184,7 @@ class BLEMIDI_Transport
180184
{
181185
strncpy(mDeviceName, deviceName, sizeof(mDeviceName));
182186
};
183-
187+
184188
public:
185189
void setHandleConnected(void (*fptr)())
186190
{
@@ -223,12 +227,12 @@ class BLEMIDI_Transport
223227
from other messages – except for System Exclusive messages.
224228
*/
225229

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.
228232
* Else, it will put in the buffer the same info that it had received (runningStatus will be not transformated).
229233
* 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
232236

233237
void receive(byte *buffer, size_t length)
234238
{
@@ -239,7 +243,7 @@ class BLEMIDI_Transport
239243
// lastStatus used to capture runningStatus
240244
byte lastStatus;
241245
// previousStatus used to continue a runningStatus interrupted by a timeStamp or a System Message.
242-
byte previousStatus = 0x00;
246+
byte previousStatus = InvalidType;
243247

244248
byte headerByte = buffer[lPtr++];
245249

@@ -251,10 +255,10 @@ class BLEMIDI_Transport
251255
bool sysExContinuation = false;
252256
bool runningStatusContinuation = false;
253257

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
255259
{
256-
auto timestampLow = 0x7f & timestampByte;
257-
timestamp = timestampLow + (timestampHigh << 7);
260+
timestamp = setMidiTimestamp(headerByte, timestampByte);
261+
// what do to with the timestamp?
258262
}
259263
else // if bit 7 is 0, it's the Continuation of a previous SysEx
260264
{
@@ -267,20 +271,20 @@ class BLEMIDI_Transport
267271
{
268272
lastStatus = buffer[lPtr];
269273

270-
if (previousStatus == 0x00)
274+
if (previousStatus == InvalidType)
271275
{
272-
if ((lastStatus < 0x80) && !sysExContinuation)
276+
if ((lastStatus < MIDI_TYPE) && !sysExContinuation)
273277
return; // Status message not present and it is not a runningStatus continuation, bail
274278
}
275-
else if (lastStatus < 0x80)
279+
else if (lastStatus < MIDI_TYPE)
276280
{
277281
lastStatus = previousStatus;
278282
runningStatusContinuation = true;
279283
}
280284

281285
// Point to next non-data byte
282286
rPtr = lPtr;
283-
while ((buffer[rPtr + 1] < 0x80) && (rPtr < (length - 1)))
287+
while ((buffer[rPtr + 1] < MIDI_TYPE) && (rPtr < (length - 1)))
284288
rPtr++;
285289

286290
if (!runningStatusContinuation)
@@ -289,15 +293,15 @@ class BLEMIDI_Transport
289293

290294
auto midiType = lastStatus & 0xF0;
291295
if (sysExContinuation)
292-
midiType = 0xF0;
296+
midiType = SystemExclusive;
293297

294298
switch (midiType)
295299
{
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:
301305
#ifdef RUNNING_ENABLE
302306
mBleClass.add(lastStatus);
303307
#endif
@@ -310,8 +314,8 @@ class BLEMIDI_Transport
310314
mBleClass.add(buffer[i + 2]);
311315
}
312316
break;
313-
case 0xC0:
314-
case 0xD0:
317+
case ProgramChange:
318+
case AfterTouchChannel:
315319
#ifdef RUNNING_ENABLE
316320
mBleClass.add(lastStatus);
317321
#endif
@@ -323,7 +327,7 @@ class BLEMIDI_Transport
323327
mBleClass.add(buffer[i + 1]);
324328
}
325329
break;
326-
case 0xF0:
330+
case SystemExclusive:
327331
mBleClass.add(lastStatus);
328332
for (auto i = lPtr; i < rPtr; i++)
329333
mBleClass.add(buffer[i + 1]);
@@ -338,16 +342,13 @@ class BLEMIDI_Transport
338342
{
339343
#ifndef RUNNING_ENABLE
340344
auto midiType = lastStatus & 0xF0;
341-
if (sysExContinuation)
342-
midiType = 0xF0;
343-
344345
switch (midiType)
345346
{
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:
351352
//3 bytes full Midi -> 2 bytes runningStatus
352353
for (auto i = lPtr; i <= rPtr; i = i + 2)
353354
{
@@ -356,8 +357,8 @@ class BLEMIDI_Transport
356357
mBleClass.add(buffer[i + 1]);
357358
}
358359
break;
359-
case 0xC0:
360-
case 0xD0:
360+
case ProgramChange:
361+
case AfterTouchChannel:
361362
//2 bytes full Midi -> 1 byte runningStatus
362363
for (auto i = lPtr; i <= rPtr; i = i + 1)
363364
{
@@ -380,16 +381,16 @@ class BLEMIDI_Transport
380381
if (++rPtr >= length)
381382
return; // end of packet
382383

383-
if (lastStatus < 0xf0) //exclude System Message. They must not be RunningStatus
384+
if (lastStatus < SystemExclusive) //exclude System Message. They must not be RunningStatus
384385
{
385386
previousStatus = lastStatus;
386387
}
387388

388389
timestampByte = buffer[rPtr++];
389-
if (timestampByte >= 80) // is bit 7 set?
390+
if (timestampByte >= MIDI_TYPE) // is bit 7 set?
390391
{
391-
auto timestampLow = 0x7f & timestampByte;
392-
timestamp = timestampLow + (timestampHigh << 7);
392+
timestamp = setMidiTimestamp(headerByte, timestampByte);
393+
// what do to with the timestamp?
393394
}
394395

395396
// Point to next status

0 commit comments

Comments
 (0)