Bug Fix and support for multipart SysEx
This commit is contained in:
parent
aa0f6bc44a
commit
5cfcf20262
|
|
@ -10,6 +10,8 @@
|
|||
#include "BLEMIDI_Defs.h"
|
||||
#include "BLEMIDI_Namespace.h"
|
||||
|
||||
#define CHECK_BIT(var,pos) (!!((var) & (1<<(pos))))
|
||||
|
||||
BEGIN_BLEMIDI_NAMESPACE
|
||||
|
||||
template <class T, class _Settings = DefaultSettings>
|
||||
|
|
@ -104,7 +106,8 @@ public:
|
|||
{
|
||||
uint8_t byte;
|
||||
auto success = mBleClass.available(&byte);
|
||||
if (!success) return mRxIndex;
|
||||
if (!success)
|
||||
return mRxIndex;
|
||||
|
||||
mRxBuffer[mRxIndex++] = byte;
|
||||
|
||||
|
|
@ -175,11 +178,13 @@ public:
|
|||
void (*_disconnectedCallback)() = nullptr;
|
||||
|
||||
public:
|
||||
void setHandleConnected(void(*fptr)()) {
|
||||
void setHandleConnected(void (*fptr)())
|
||||
{
|
||||
_connectedCallback = fptr;
|
||||
}
|
||||
|
||||
void setHandleDisconnected(void(*fptr)()) {
|
||||
void setHandleDisconnected(void (*fptr)())
|
||||
{
|
||||
_disconnectedCallback = fptr;
|
||||
}
|
||||
|
||||
|
|
@ -218,40 +223,74 @@ public:
|
|||
// Pointers used to search through payload.
|
||||
byte lPtr = 0;
|
||||
byte rPtr = 0;
|
||||
|
||||
// lastStatus used to capture runningStatus
|
||||
byte lastStatus;
|
||||
// Decode first packet -- SHALL be "Full MIDI message"
|
||||
lPtr = 2; //Start at first MIDI status -- SHALL be "MIDI status"
|
||||
|
||||
byte headerByte = buffer[lPtr++];
|
||||
auto signatureIs1 = CHECK_BIT(headerByte, 7 - 1);
|
||||
auto reservedIs0 = !CHECK_BIT(headerByte, 6 - 1);
|
||||
auto timestampHigh = 0x3f & headerByte;
|
||||
|
||||
byte timestampByte = buffer[lPtr++];
|
||||
signatureIs1 = CHECK_BIT(timestampByte, 7 - 1);
|
||||
|
||||
uint16_t timestamp = 0;
|
||||
|
||||
bool sysExContinuation = false;
|
||||
|
||||
if (signatureIs1)
|
||||
{
|
||||
auto timestampLow = 0x7f & timestampByte;
|
||||
timestamp = 0;
|
||||
timestampLow + (timestampHigh << 7);
|
||||
}
|
||||
else
|
||||
{
|
||||
sysExContinuation = true;
|
||||
lPtr--;
|
||||
}
|
||||
|
||||
//While statement contains incrementing pointers and breaks when buffer size exceeded.
|
||||
while (true)
|
||||
{
|
||||
lastStatus = buffer[lPtr];
|
||||
|
||||
if( (buffer[lPtr] < 0x80))
|
||||
if ((buffer[lPtr] < 0x80) && !sysExContinuation)
|
||||
return; // Status message not present, bail
|
||||
|
||||
// Point to next non-data byte
|
||||
rPtr = lPtr;
|
||||
while ((buffer[rPtr + 1] < 0x80) && (rPtr < (length - 1)))
|
||||
rPtr++;
|
||||
if (buffer[rPtr + 1] == 0xF7) rPtr++;
|
||||
|
||||
// look at l and r pointers and decode by size.
|
||||
if( rPtr - lPtr < 1 ) {
|
||||
if (rPtr - lPtr < 1)
|
||||
{
|
||||
// Time code or system
|
||||
mBleClass.add(lastStatus);
|
||||
} else if( rPtr - lPtr < 2 ) {
|
||||
}
|
||||
else if (rPtr - lPtr < 2)
|
||||
{
|
||||
mBleClass.add(lastStatus);
|
||||
mBleClass.add(buffer[lPtr + 1]);
|
||||
} else if( rPtr - lPtr < 3 ) {
|
||||
}
|
||||
else if (rPtr - lPtr < 3)
|
||||
{
|
||||
mBleClass.add(lastStatus);
|
||||
mBleClass.add(buffer[lPtr + 1]);
|
||||
mBleClass.add(buffer[lPtr + 2]);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
auto midiType = buffer[lPtr] & 0xF0;
|
||||
if (sysExContinuation)
|
||||
midiType = 0xF0;
|
||||
|
||||
// Too much data
|
||||
// If not System Common or System Real-Time, send it as running status
|
||||
switch(buffer[lPtr] & 0xF0)
|
||||
switch (midiType)
|
||||
{
|
||||
case 0x80:
|
||||
case 0x90:
|
||||
|
|
@ -277,19 +316,53 @@ public:
|
|||
mBleClass.add(buffer[lPtr]);
|
||||
for (auto i = lPtr; i < rPtr; i++)
|
||||
mBleClass.add(buffer[i + 1]);
|
||||
|
||||
rPtr++;
|
||||
if (rPtr >= length)
|
||||
return; // end of packet
|
||||
|
||||
timestampByte = buffer[rPtr++];
|
||||
signatureIs1 = CHECK_BIT(timestampByte, 7 - 1);
|
||||
if (signatureIs1)
|
||||
{
|
||||
auto timestampLow = 0x7f & timestampByte;
|
||||
timestamp = timestampLow + (timestampHigh << 7);
|
||||
|
||||
// std::cout << "timestamp low: 0x" << std::hex << (int)timestampByte << std::endl;
|
||||
}
|
||||
|
||||
rPtr++;
|
||||
// std::cout << "end of SysEx: 0x" << std::hex << (int)buffer[rPtr] << std::endl;
|
||||
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
rPtr++;
|
||||
|
||||
if (rPtr >= length)
|
||||
return; // end of packet
|
||||
|
||||
timestampByte = buffer[rPtr++];
|
||||
signatureIs1 = CHECK_BIT(timestampByte, 7 - 1);
|
||||
if (signatureIs1)
|
||||
{
|
||||
auto timestampLow = 0x7f & timestampByte;
|
||||
timestamp = timestampLow + (timestampHigh << 7);
|
||||
|
||||
// std::cout << "timestamp low is 0x" << std::hex << (int)timestampByte << std::endl;
|
||||
}
|
||||
|
||||
// std::cout << "first" << std::hex << (int)buffer[lPtr] << std::endl;
|
||||
|
||||
// Point to next status
|
||||
lPtr = rPtr + 2;
|
||||
lPtr = rPtr;
|
||||
if (lPtr >= length)
|
||||
return; //end of packet
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
struct MySettings : public MIDI_NAMESPACE::DefaultSettings
|
||||
|
|
@ -298,4 +371,3 @@ struct MySettings : public MIDI_NAMESPACE::DefaultSettings
|
|||
};
|
||||
|
||||
END_BLEMIDI_NAMESPACE
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue