36
36
37
37
#include " bluefruit.h"
38
38
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
+
39
47
/* MIDI Service: 03B80E5A-EDE8-4B33-A751-6CE34EC4C700
40
48
* MIDI I/O : 7772E5DB-3868-4112-A1A9-F2669D106BF3
41
49
*/
@@ -80,18 +88,6 @@ typedef union ATTR_PACKED
80
88
81
89
VERIFY_STATIC ( sizeof (midi_timestamp_t ) == 1 );
82
90
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
-
95
91
typedef struct ATTR_PACKED
96
92
{
97
93
midi_header_t header;
@@ -101,38 +97,64 @@ typedef struct ATTR_PACKED
101
97
102
98
VERIFY_STATIC ( sizeof (midi_event_packet_t ) == 5 );
103
99
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);
113
101
114
102
/* ------------------------------------------------------------------*/
115
103
/* IMPLEMENTATION
116
104
*------------------------------------------------------------------*/
117
105
BLEMidi::BLEMidi (uint16_t fifo_depth)
118
106
: BLEService(BLEMIDI_UUID_SERVICE), _io(BLEMIDI_UUID_CHR_IO), _rxd_fifo(fifo_depth, 1 )
119
107
{
120
- _write_cb = NULL ;
108
+ _write_cb = NULL ;
109
+ _midilib_obj = NULL ;
121
110
}
122
111
123
112
bool BLEMidi::notifyEnabled (void )
124
113
{
125
114
return Bluefruit.connBonded () && _io.notifyEnabled ();
126
115
}
127
116
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
+ }
128
148
149
+ /* ------------------------------------------------------------------*/
150
+ /* Callbacks
151
+ *------------------------------------------------------------------*/
129
152
void blemidi_write_cb (BLECharacteristic& chr, uint8_t * data, uint16_t len, uint16_t offset)
130
153
{
131
154
(void ) offset;
132
155
if ( len < 3 ) return ;
133
156
134
157
BLEMidi& midi_svc = (BLEMidi&) chr.parentService ();
135
-
136
158
midi_svc._write_handler (data, len);
137
159
}
138
160
@@ -193,35 +215,14 @@ void BLEMidi::_write_handler(uint8_t* data, uint16_t len)
193
215
data += 2 ;
194
216
}
195
217
}
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);
212
218
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
225
226
}
226
227
227
228
/* ------------------------------------------------------------------*/
@@ -282,7 +283,7 @@ void BLEMidi::flush ( void )
282
283
}
283
284
284
285
/* ------------------------------------------------------------------*/
285
- /* Send Event
286
+ /* Send Event (notify)
286
287
*------------------------------------------------------------------*/
287
288
err_t BLEMidi::send (uint8_t data[])
288
289
{
0 commit comments