diff --git a/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp b/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp index 11a64c63..258631b0 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp +++ b/libraries/Bluefruit52Lib/src/services/BLEMidi.cpp @@ -176,15 +176,17 @@ void BLEMidi::_write_handler(uint8_t* data, uint16_t len) while (len) { - if ( bitRead(data[0], 7) && bitRead(data[1], 7) ) + // timestamp low byte followed by a MIDI status, + // so we drop the timestamp low byte + if ( isStatusByte(data[0]) && isStatusByte(data[1]) ) { - // timestamp low + MIDI status so we skip the timestamp data++; len--; } - else if ( bitRead(data[0], 7) && ! bitRead(data[1], 7) ) + // timestamp low byte on it's own (running status), + // so we drop the timestamp low byte + else if ( isStatusByte(data[0]) && ! isStatusByte(data[1]) ) { - // timestamp low on its own, skip timestamp data++; len--; } @@ -194,7 +196,6 @@ void BLEMidi::_write_handler(uint8_t* data, uint16_t len) len--; if ( _write_cb ) _write_cb(); - } #ifdef MIDI_LIB_INCLUDED @@ -234,7 +235,7 @@ size_t BLEMidi::write ( uint8_t b ) { // send and clear the last of the existing buffer. // it should contain the final bytes in the sysex payload. - if (bitRead(buf[0], 7)) + if (isStatusByte(buf[0])) send(buf, count); else sendSplit(buf, count); @@ -259,12 +260,12 @@ size_t BLEMidi::write ( uint8_t b ) count = 0; } - // if we have a full buffer at this point, we - // need to send a full or split sysex message - // and reset the buffer. + // do we have a full buffer at this point? if(count == BLE_MIDI_TX_BUFFER_SIZE) { - if (bitRead(buf[0], 7)) + // send a full or split message depending + // on the type of the first byte in the buffer + if (isStatusByte(buf[0])) send(buf, count); else sendSplit(buf, count); @@ -275,7 +276,6 @@ size_t BLEMidi::write ( uint8_t b ) } return 1; - } int BLEMidi::available ( void ) @@ -297,6 +297,15 @@ void BLEMidi::flush ( void ) /*------------------------------------------------------------------*/ /* Message Type Helpers *------------------------------------------------------------------*/ +bool BLEMidi::isStatusByte( uint8_t b ) +{ + // if the bit 7 is set, then it's a MIDI status message + if (bitRead(b, 7)) + return true; + else + return false; +} + bool BLEMidi::oneByteMessage( uint8_t status ) { // system messages diff --git a/libraries/Bluefruit52Lib/src/services/BLEMidi.h b/libraries/Bluefruit52Lib/src/services/BLEMidi.h index 2f3cb7e1..f0c39e9a 100644 --- a/libraries/Bluefruit52Lib/src/services/BLEMidi.h +++ b/libraries/Bluefruit52Lib/src/services/BLEMidi.h @@ -63,6 +63,8 @@ class BLEMidi: public BLEService, public Stream err_t send(uint8_t data[], uint8_t len); err_t sendSplit(uint8_t data[], uint8_t len); + // message type helpers + bool isStatusByte(uint8_t b); bool oneByteMessage(uint8_t status); bool twoByteMessage(uint8_t status); bool threeByteMessage(uint8_t status); diff --git a/tools/midi_tests/rx_arduino/rx_arduino.ino b/tools/midi_tests/rx_arduino/rx_arduino.ino index a3629076..9984beae 100644 --- a/tools/midi_tests/rx_arduino/rx_arduino.ino +++ b/tools/midi_tests/rx_arduino/rx_arduino.ino @@ -32,7 +32,7 @@ void setup() Bluefruit.autoConnLed(true); Bluefruit.begin(); - Bluefruit.setName("Bluefruit52"); + Bluefruit.setName("Bluefruit52 MIDI"); MIDI.begin(MIDI_CHANNEL_OMNI); MIDI.setHandleNoteOn(handleNoteOn); diff --git a/tools/midi_tests/tx_arduino/tx_arduino.ino b/tools/midi_tests/tx_arduino/tx_arduino.ino index 399dc76e..0601467f 100644 --- a/tools/midi_tests/tx_arduino/tx_arduino.ino +++ b/tools/midi_tests/tx_arduino/tx_arduino.ino @@ -39,7 +39,7 @@ void setup() Bluefruit.autoConnLed(true); Bluefruit.begin(); - Bluefruit.setName("Bluefruit52"); + Bluefruit.setName("Bluefruit52 MIDI"); MIDI.begin(MIDI_CHANNEL_OMNI);