|
44 | 44 | #endif
|
45 | 45 | #endif
|
46 | 46 |
|
| 47 | +#define SYSEX_START 0xF0 |
| 48 | +#define SYSEX_END 0xF7 |
| 49 | + |
47 | 50 | /* MIDI Service: 03B80E5A-EDE8-4B33-A751-6CE34EC4C700
|
48 | 51 | * MIDI I/O : 7772E5DB-3868-4112-A1A9-F2669D106BF3
|
49 | 52 | */
|
@@ -107,6 +110,7 @@ BLEMidi::BLEMidi(uint16_t fifo_depth)
|
107 | 110 | {
|
108 | 111 | _write_cb = NULL;
|
109 | 112 | _midilib_obj = NULL;
|
| 113 | + _receiving_sysex = false; |
110 | 114 | }
|
111 | 115 |
|
112 | 116 | bool BLEMidi::notifyEnabled(void)
|
@@ -168,51 +172,80 @@ void BLEMidi::_write_handler(uint8_t* data, uint16_t len)
|
168 | 172 | header.byte = *data++;
|
169 | 173 | len--;
|
170 | 174 |
|
171 |
| - // not support SysEx |
172 |
| - if ( data[0] == 0xF0 ) return; |
| 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 | + */ |
173 | 179 |
|
174 |
| - while (len) |
| 180 | + // Start packet of SysEx |
| 181 | + if ( !_receiving_sysex && (data[1] == SYSEX_START) ) |
175 | 182 | {
|
176 |
| - /* event : 0x00 - 0x7F |
177 |
| - status: 0x80 - 0xEF |
178 |
| - sysex : 0xF0 - 0xFF |
179 |
| - */ |
| 183 | + // skip timestamp |
| 184 | + data++; |
| 185 | + len--; |
| 186 | + |
| 187 | + _receiving_sysex = true; |
| 188 | + } |
180 | 189 |
|
181 |
| - if ( bitRead(data[0], 7) ) |
| 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) ) |
182 | 195 | {
|
183 |
| - // Start of new full event |
184 |
| - midi_timestamp_t timestamp; |
| 196 | + _rxd_fifo.write(data, len-2); |
| 197 | + _rxd_fifo.write(&data[len-1]); // SYSEX_END |
185 | 198 |
|
186 |
| - timestamp.byte = *data++; |
187 |
| - len--; |
| 199 | + _receiving_sysex = false; // done with SysEx |
| 200 | + }else |
| 201 | + { |
| 202 | + _rxd_fifo.write(data, len); |
| 203 | + } |
| 204 | + }else |
| 205 | + { |
| 206 | + while (len) |
| 207 | + { |
| 208 | + /* event : 0x00 - 0x7F |
| 209 | + status: 0x80 - 0xEF |
| 210 | + sysex : 0xF0 - 0xFF |
| 211 | + */ |
188 | 212 |
|
189 |
| - tstamp = (header.timestamp_hi << 7) | timestamp.timestamp_low; |
190 |
| - (void) tstamp; |
| 213 | + if ( bitRead(data[0], 7) ) |
| 214 | + { |
| 215 | + // Start of new full event |
| 216 | + midi_timestamp_t timestamp; |
191 | 217 |
|
192 |
| - status = *data++; |
193 |
| - len--; |
| 218 | + timestamp.byte = *data++; |
| 219 | + len--; |
194 | 220 |
|
195 |
| - // Status must have 7th-bit set, otherwise something is wrong !!! |
196 |
| - if ( !bitRead(status, 7) ) return; |
| 221 | + tstamp = (header.timestamp_hi << 7) | timestamp.timestamp_low; |
| 222 | + (void) tstamp; |
197 | 223 |
|
198 |
| - uint8_t tempbuf[3] = { status, data[0], data[1] }; |
| 224 | + status = *data++; |
| 225 | + len--; |
199 | 226 |
|
200 |
| - _rxd_fifo.write(tempbuf, 3); |
201 |
| - if ( _write_cb ) _write_cb(); |
| 227 | + // Status must have 7th-bit set, otherwise something is wrong !!! |
| 228 | + if ( !bitRead(status, 7) ) return; |
202 | 229 |
|
203 |
| - len -= 2; |
204 |
| - data += 2; |
205 |
| - } |
206 |
| - else |
207 |
| - { |
208 |
| - // Running event |
209 |
| - uint8_t tempbuf[3] = { status, data[0], data[1] }; |
| 230 | + uint8_t tempbuf[3] = { status, data[0], data[1] }; |
| 231 | + |
| 232 | + _rxd_fifo.write(tempbuf, 3); |
| 233 | + if ( _write_cb ) _write_cb(); |
| 234 | + |
| 235 | + len -= 2; |
| 236 | + data += 2; |
| 237 | + } |
| 238 | + else |
| 239 | + { |
| 240 | + // Running event |
| 241 | + uint8_t tempbuf[3] = { status, data[0], data[1] }; |
210 | 242 |
|
211 |
| - _rxd_fifo.write(tempbuf, 3); |
212 |
| - if ( _write_cb ) _write_cb(); |
| 243 | + _rxd_fifo.write(tempbuf, 3); |
| 244 | + if ( _write_cb ) _write_cb(); |
213 | 245 |
|
214 |
| - len -= 2; |
215 |
| - data += 2; |
| 246 | + len -= 2; |
| 247 | + data += 2; |
| 248 | + } |
216 | 249 | }
|
217 | 250 | }
|
218 | 251 |
|
|
0 commit comments