Skip to content

Commit 5f04c13

Browse files
committed
clean up blemidi. Add auto invoke MIDI.read() behavior
1 parent 9775c90 commit 5f04c13

File tree

3 files changed

+60
-68
lines changed

3 files changed

+60
-68
lines changed

libraries/Bluefruit52Lib/examples/Peripheral/blemidi/blemidi.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ void setup()
5252
bledis.begin();
5353

5454
// Initialize MIDI, and listen to all MIDI channels
55+
// This will also call blemidi service's begin()
5556
MIDI.begin(MIDI_CHANNEL_OMNI);
57+
blemidi.autoMIDIread(&MIDI);
5658

5759
// Attach the handleNoteOn function to the MIDI Library. It will
5860
// be called whenever the Bluefruit receives MIDI Note On messages.
@@ -106,7 +108,7 @@ void loop()
106108
}
107109

108110
// read any new MIDI messages
109-
MIDI.read();
111+
//MIDI.read();
110112

111113
// Store the current time
112114
unsigned long now = millis();

libraries/Bluefruit52Lib/src/services/BLEMidi.cpp

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@
3636

3737
#include "bluefruit.h"
3838

39+
// GCC 5x new feature to detect optional include
40+
#ifdef __has_include
41+
#if __has_include("MIDI.h")
42+
#include <MIDI.h>
43+
#define MIDI_LIB_INCLUDED
44+
#endif
45+
#endif
46+
3947
/* MIDI Service: 03B80E5A-EDE8-4B33-A751-6CE34EC4C700
4048
* MIDI I/O : 7772E5DB-3868-4112-A1A9-F2669D106BF3
4149
*/
@@ -80,18 +88,6 @@ typedef union ATTR_PACKED
8088

8189
VERIFY_STATIC ( sizeof(midi_timestamp_t) == 1 );
8290

83-
typedef union ATTR_PACKED
84-
{
85-
struct {
86-
uint8_t channel : 4;
87-
uint8_t type : 4;
88-
};
89-
90-
uint8_t byte;
91-
} midi_status_t;
92-
93-
VERIFY_STATIC ( sizeof(midi_status_t) == 1 );
94-
9591
typedef struct ATTR_PACKED
9692
{
9793
midi_header_t header;
@@ -101,38 +97,64 @@ typedef struct ATTR_PACKED
10197

10298
VERIFY_STATIC ( sizeof(midi_event_packet_t) == 5 );
10399

104-
typedef struct ATTR_PACKED
105-
{
106-
midi_header_t header;
107-
midi_timestamp_t timestamp;
108-
midi_status_t status;
109-
uint8_t data[16];
110-
} midi_n_event_packet_t;
111-
112-
VERIFY_STATIC ( sizeof(midi_n_event_packet_t) == 19 );
100+
void blemidi_write_cb(BLECharacteristic& chr, uint8_t* data, uint16_t len, uint16_t offset);
113101

114102
/*------------------------------------------------------------------*/
115103
/* IMPLEMENTATION
116104
*------------------------------------------------------------------*/
117105
BLEMidi::BLEMidi(uint16_t fifo_depth)
118106
: BLEService(BLEMIDI_UUID_SERVICE), _io(BLEMIDI_UUID_CHR_IO), _rxd_fifo(fifo_depth, 1)
119107
{
120-
_write_cb = NULL;
108+
_write_cb = NULL;
109+
_midilib_obj = NULL;
121110
}
122111

123112
bool BLEMidi::notifyEnabled(void)
124113
{
125114
return Bluefruit.connBonded() && _io.notifyEnabled();
126115
}
127116

117+
void BLEMidi::setWriteCallback(midi_write_cb_t fp)
118+
{
119+
_write_cb = fp;
120+
}
121+
122+
void BLEMidi::autoMIDIread(void* midi_obj)
123+
{
124+
_midilib_obj = midi_obj;
125+
}
126+
void BLEMidi::begin(int baudrate)
127+
{
128+
(void) baudrate;
129+
begin();
130+
}
131+
132+
err_t BLEMidi::begin(void)
133+
{
134+
VERIFY_STATUS( this->addToGatt() );
135+
136+
// IO characteristic
137+
_io.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE | CHR_PROPS_WRITE_WO_RESP | CHR_PROPS_NOTIFY);
138+
_io.setPermission(SECMODE_ENC_NO_MITM, SECMODE_ENC_NO_MITM);
139+
_io.setWriteCallback(blemidi_write_cb);
140+
141+
VERIFY_STATUS( _io.begin() );
142+
143+
// Attempt to change the connection interval to 11.25-15 ms when starting HID
144+
Bluefruit.setConnInterval(9, 12);
145+
146+
return ERROR_NONE;
147+
}
128148

149+
/*------------------------------------------------------------------*/
150+
/* Callbacks
151+
*------------------------------------------------------------------*/
129152
void blemidi_write_cb(BLECharacteristic& chr, uint8_t* data, uint16_t len, uint16_t offset)
130153
{
131154
(void) offset;
132155
if ( len < 3 ) return;
133156

134157
BLEMidi& midi_svc = (BLEMidi&) chr.parentService();
135-
136158
midi_svc._write_handler(data, len);
137159
}
138160

@@ -193,35 +215,14 @@ void BLEMidi::_write_handler(uint8_t* data, uint16_t len)
193215
data += 2;
194216
}
195217
}
196-
}
197-
198-
void BLEMidi::setWriteCallback(midi_write_cb_t fp)
199-
{
200-
_write_cb = fp;
201-
}
202-
203-
void BLEMidi::begin(int baudrate)
204-
{
205-
(void) baudrate;
206-
begin();
207-
}
208-
209-
err_t BLEMidi::begin(void)
210-
{
211-
_io.setWriteCallback(blemidi_write_cb);
212218

213-
VERIFY_STATUS( this->addToGatt() );
214-
215-
// IO characteristic
216-
_io.setProperties(CHR_PROPS_READ | CHR_PROPS_WRITE | CHR_PROPS_WRITE_WO_RESP | CHR_PROPS_NOTIFY);
217-
_io.setPermission(SECMODE_ENC_NO_MITM, SECMODE_ENC_NO_MITM);
218-
219-
VERIFY_STATUS( _io.begin() );
220-
221-
// Attempt to change the connection interval to 11.25-15 ms when starting HID
222-
Bluefruit.setConnInterval(9, 12);
223-
224-
return ERROR_NONE;
219+
#ifdef MIDI_LIB_INCLUDED
220+
// read while possible if configured
221+
if ( _midilib_obj )
222+
{
223+
while( ((midi::MidiInterface<BLEMidi>*)_midilib_obj)->read() ) { }
224+
}
225+
#endif
225226
}
226227

227228
/*------------------------------------------------------------------*/
@@ -282,7 +283,7 @@ void BLEMidi::flush ( void )
282283
}
283284

284285
/*------------------------------------------------------------------*/
285-
/* Send Event
286+
/* Send Event (notify)
286287
*------------------------------------------------------------------*/
287288
err_t BLEMidi::send(uint8_t data[])
288289
{

libraries/Bluefruit52Lib/src/services/BLEMidi.h

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,6 @@
4949
extern const uint8_t BLEMIDI_UUID_SERVICE[];
5050
extern const uint8_t BLEMIDI_UUID_CHR_IO[];
5151

52-
enum
53-
{
54-
MIDI_TYPE_NOTE_OFF = 0x8,
55-
MIDI_TYPE_NOTE_ON = 0x9,
56-
MIDI_TYPE_AFTER_TOUCH = 0xA,
57-
MIDI_TYPE_CONTROL_CHANGE = 0xB,
58-
MIDI_TYPE_PROGRAM_CHANGE = 0xC,
59-
MIDI_TYPE_CHANNEL_PRESSURE = 0xD,
60-
MIDI_TYPE_PITCH_WHEEL = 0xE,
61-
};
62-
6352
class BLEMidi: public BLEService, public Stream
6453
{
6554
public:
@@ -68,16 +57,14 @@ class BLEMidi: public BLEService, public Stream
6857
BLEMidi(uint16_t fifo_depth = BLE_MIDI_DEFAULT_FIFO_DEPTH);
6958

7059
virtual err_t begin(void);
71-
72-
// MidiInterface
73-
void begin(int baudrate);
74-
60+
void begin(int baudrate); // MidiInterface
7561
bool notifyEnabled(void);
7662

7763
err_t send(uint8_t data[]);
7864
err_t send(uint8_t status, uint8_t byte1, uint8_t byte2);
7965

8066
void setWriteCallback(midi_write_cb_t fp);
67+
void autoMIDIread(void* midi_obj);
8168

8269
// Stream API for MIDI Interface
8370
virtual int read ( void );
@@ -94,6 +81,8 @@ class BLEMidi: public BLEService, public Stream
9481
Adafruit_FIFO _rxd_fifo;
9582
midi_write_cb_t _write_cb;
9683

84+
void* _midilib_obj;
85+
9786
void _write_handler(uint8_t* data, uint16_t len);
9887

9988
friend void blemidi_write_cb(BLECharacteristic& chr, uint8_t* data, uint16_t len, uint16_t offset);

0 commit comments

Comments
 (0)