Skip to content

Commit ca4cbe9

Browse files
committed
add MIDI isStatusByte helper
1 parent 9fb32b2 commit ca4cbe9

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

libraries/Bluefruit52Lib/src/services/BLEMidi.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,17 @@ void BLEMidi::_write_handler(uint8_t* data, uint16_t len)
176176
while (len)
177177
{
178178

179-
if ( bitRead(data[0], 7) && bitRead(data[1], 7) )
179+
// timestamp low byte followed by a MIDI status,
180+
// so we drop the timestamp low byte
181+
if ( isStatusByte(data[0]) && isStatusByte(data[1]) )
180182
{
181-
// timestamp low + MIDI status so we skip the timestamp
182183
data++;
183184
len--;
184185
}
185-
else if ( bitRead(data[0], 7) && ! bitRead(data[1], 7) )
186+
// timestamp low byte on it's own (running status),
187+
// so we drop the timestamp low byte
188+
else if ( isStatusByte(data[0]) && ! isStatusByte(data[1]) )
186189
{
187-
// timestamp low on its own, skip timestamp
188190
data++;
189191
len--;
190192
}
@@ -194,7 +196,6 @@ void BLEMidi::_write_handler(uint8_t* data, uint16_t len)
194196
len--;
195197

196198
if ( _write_cb ) _write_cb();
197-
198199
}
199200

200201
#ifdef MIDI_LIB_INCLUDED
@@ -234,7 +235,7 @@ size_t BLEMidi::write ( uint8_t b )
234235
{
235236
// send and clear the last of the existing buffer.
236237
// it should contain the final bytes in the sysex payload.
237-
if (bitRead(buf[0], 7))
238+
if (isStatusByte(buf[0]))
238239
send(buf, count);
239240
else
240241
sendSplit(buf, count);
@@ -259,12 +260,12 @@ size_t BLEMidi::write ( uint8_t b )
259260
count = 0;
260261
}
261262

262-
// if we have a full buffer at this point, we
263-
// need to send a full or split sysex message
264-
// and reset the buffer.
263+
// do we have a full buffer at this point?
265264
if(count == BLE_MIDI_TX_BUFFER_SIZE)
266265
{
267-
if (bitRead(buf[0], 7))
266+
// send a full or split message depending
267+
// on the type of the first byte in the buffer
268+
if (isStatusByte(buf[0]))
268269
send(buf, count);
269270
else
270271
sendSplit(buf, count);
@@ -275,7 +276,6 @@ size_t BLEMidi::write ( uint8_t b )
275276
}
276277

277278
return 1;
278-
279279
}
280280

281281
int BLEMidi::available ( void )
@@ -297,6 +297,15 @@ void BLEMidi::flush ( void )
297297
/*------------------------------------------------------------------*/
298298
/* Message Type Helpers
299299
*------------------------------------------------------------------*/
300+
bool BLEMidi::isStatusByte( uint8_t b )
301+
{
302+
// if the bit 7 is set, then it's a MIDI status message
303+
if (bitRead(b, 7))
304+
return true;
305+
else
306+
return false;
307+
}
308+
300309
bool BLEMidi::oneByteMessage( uint8_t status )
301310
{
302311
// system messages

libraries/Bluefruit52Lib/src/services/BLEMidi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class BLEMidi: public BLEService, public Stream
6363
err_t send(uint8_t data[], uint8_t len);
6464
err_t sendSplit(uint8_t data[], uint8_t len);
6565

66+
// message type helpers
67+
bool isStatusByte(uint8_t b);
6668
bool oneByteMessage(uint8_t status);
6769
bool twoByteMessage(uint8_t status);
6870
bool threeByteMessage(uint8_t status);

tools/midi_tests/rx_arduino/rx_arduino.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void setup()
3232
Bluefruit.autoConnLed(true);
3333

3434
Bluefruit.begin();
35-
Bluefruit.setName("Bluefruit52");
35+
Bluefruit.setName("Bluefruit52 MIDI");
3636

3737
MIDI.begin(MIDI_CHANNEL_OMNI);
3838
MIDI.setHandleNoteOn(handleNoteOn);

tools/midi_tests/tx_arduino/tx_arduino.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void setup()
3939
Bluefruit.autoConnLed(true);
4040

4141
Bluefruit.begin();
42-
Bluefruit.setName("Bluefruit52");
42+
Bluefruit.setName("Bluefruit52 MIDI");
4343

4444
MIDI.begin(MIDI_CHANNEL_OMNI);
4545

0 commit comments

Comments
 (0)