Skip to content

Commit 2c9dfea

Browse files
committed
simplify BLE MIDI input & support all message types
1 parent a7329b9 commit 2c9dfea

File tree

2 files changed

+17
-77
lines changed

2 files changed

+17
-77
lines changed

libraries/Bluefruit52Lib/src/services/BLEMidi.cpp

Lines changed: 17 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
#endif
4545
#endif
4646

47-
#define SYSEX_START 0xF0
48-
#define SYSEX_END 0xF7
4947

5048
/* MIDI Service: 03B80E5A-EDE8-4B33-A751-6CE34EC4C700
5149
* MIDI I/O : 7772E5DB-3868-4112-A1A9-F2669D106BF3
@@ -110,7 +108,6 @@ BLEMidi::BLEMidi(uint16_t fifo_depth)
110108
{
111109
_write_cb = NULL;
112110
_midilib_obj = NULL;
113-
_receiving_sysex = false;
114111
}
115112

116113
bool BLEMidi::notifyEnabled(void)
@@ -164,89 +161,32 @@ void blemidi_write_cb(BLECharacteristic& chr, uint8_t* data, uint16_t len, uint1
164161

165162
void BLEMidi::_write_handler(uint8_t* data, uint16_t len)
166163
{
167-
// First 3 bytes is always : Header + Timestamp + Status
168-
midi_header_t header;
169-
uint16_t tstamp = 0;
170-
uint8_t status = 0;
171-
172-
header.byte = *data++;
164+
// drop the BLE MIDI header byte
165+
data++;
173166
len--;
174167

175-
/* SysEx message
176-
* Header | Timestamp | SysEx Start (0xF0) | SysEx Data ...... | timestamp | SysEx End (0xF7)
177-
* For each MTU packet, SysEx Data is preceded by Header
178-
*/
179-
180-
// Start packet of SysEx
181-
if ( !_receiving_sysex && (data[1] == SYSEX_START) )
168+
while (len)
182169
{
183-
// skip timestamp
184-
data++;
185-
len--;
186-
187-
_receiving_sysex = true;
188-
}
189170

190-
// Receiving SysEx (including first packet above)
191-
if (_receiving_sysex)
192-
{
193-
// If final packet --> skip timestamp
194-
if ( (data[len-1] == SYSEX_END) && bitRead(data[len-2],7) )
195-
{
196-
_rxd_fifo.write(data, len-2);
197-
_rxd_fifo.write(&data[len-1]); // SYSEX_END
198-
199-
_receiving_sysex = false; // done with SysEx
200-
}else
171+
if ( bitRead(data[0], 7) && bitRead(data[1], 7) )
201172
{
202-
_rxd_fifo.write(data, len);
173+
// timestamp low + MIDI status so we skip the timestamp
174+
data++;
175+
len--;
203176
}
204-
}else
205-
{
206-
/* Normal Event data
207-
* event : 0x00 - 0x7F
208-
* status: 0x80 - 0xEF
209-
*/
210-
211-
while (len)
177+
else if ( bitRead(data[0], 7) && ! bitRead(data[1], 7) )
212178
{
213-
if ( bitRead(data[0], 7) )
214-
{
215-
// Start of new full event
216-
midi_timestamp_t timestamp;
217-
218-
timestamp.byte = *data++;
219-
len--;
220-
221-
tstamp = (header.timestamp_hi << 7) | timestamp.timestamp_low;
222-
(void) tstamp;
223-
224-
status = *data++;
225-
len--;
226-
227-
// Status must have 7th-bit set, otherwise something is wrong !!!
228-
if ( !bitRead(status, 7) ) return;
229-
230-
uint8_t tempbuf[3] = { status, data[0], data[1] };
231-
232-
_rxd_fifo.write(tempbuf, 3);
233-
if ( _write_cb ) _write_cb();
179+
// timestamp low on its own, skip timestamp
180+
data++;
181+
len--;
182+
}
234183

235-
len -= 2;
236-
data += 2;
237-
}
238-
else
239-
{
240-
// Running event
241-
uint8_t tempbuf[3] = { status, data[0], data[1] };
184+
// write the status or MIDI data to the FIFO
185+
_rxd_fifo.write(data++, 1);
186+
len--;
242187

243-
_rxd_fifo.write(tempbuf, 3);
244-
if ( _write_cb ) _write_cb();
188+
if ( _write_cb ) _write_cb();
245189

246-
len -= 2;
247-
data += 2;
248-
}
249-
}
250190
}
251191

252192
#ifdef MIDI_LIB_INCLUDED
@@ -256,6 +196,7 @@ void BLEMidi::_write_handler(uint8_t* data, uint16_t len)
256196
while( ((midi::MidiInterface<BLEMidi>*)_midilib_obj)->read() ) { }
257197
}
258198
#endif
199+
259200
}
260201

261202
/*------------------------------------------------------------------*/

libraries/Bluefruit52Lib/src/services/BLEMidi.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ class BLEMidi: public BLEService, public Stream
8282
midi_write_cb_t _write_cb;
8383

8484
void* _midilib_obj;
85-
bool _receiving_sysex;
8685

8786
void _write_handler(uint8_t* data, uint16_t len);
8887

0 commit comments

Comments
 (0)