7
7
8
8
BEGIN_BLEMIDI_NAMESPACE
9
9
10
- BLEService midiService (SERVICE_UUID);
11
-
12
- BLEStringCharacteristic midiChar (CHARACTERISTIC_UUID, // standard 16-bit characteristic UUID
13
- BLERead | BLEWrite | BLENotify | BLEWriteWithoutResponse, 16 ); // remote clients will be able to get notifications if this characteristic changes
14
-
15
- template <typename T, short rawSize>
16
- class Fifo {
10
+ template <typename T, short rawSize>
11
+ class Fifo
12
+ {
17
13
public:
18
- const size_t size; // speculative feature, in case it's needed
14
+ const size_t size; // speculative feature, in case it's needed
19
15
20
- Fifo (): size(rawSize)
16
+ Fifo () : size(rawSize)
21
17
{
22
- flush ();
18
+ flush ();
23
19
}
24
20
25
- T dequeue ()
21
+ T dequeue ()
26
22
{
27
23
numberOfElements--;
28
24
nextOut %= size;
29
- return raw[ nextOut++];
25
+ return raw[nextOut++];
30
26
};
31
-
32
- bool enqueue ( T element )
27
+
28
+ bool enqueue (T element)
33
29
{
34
- if ( count () >= rawSize )
30
+ if (count () >= rawSize)
35
31
return false ;
36
32
37
33
numberOfElements++;
38
34
nextIn %= size;
39
35
raw[nextIn] = element;
40
- nextIn++; // advance to next index
36
+ nextIn++; // advance to next index
41
37
42
38
return true ;
43
39
};
44
40
45
41
T peek () const
46
42
{
47
- return raw[ nextOut % size];
43
+ return raw[nextOut % size];
48
44
}
49
45
50
46
void flush ()
51
47
{
52
- nextIn = nextOut = numberOfElements = 0 ;
48
+ nextIn = nextOut = numberOfElements = 0 ;
53
49
}
54
50
55
- // how many elements are currently in the FIFO?
56
- size_t count () { return numberOfElements; }
51
+ // how many elements are currently in the FIFO?
52
+ size_t count () { return numberOfElements; }
57
53
58
54
private:
59
55
size_t numberOfElements;
@@ -65,56 +61,58 @@ class Fifo {
65
61
template <class _Settings >
66
62
class BLEMIDI_ArduinoBLE
67
63
{
68
- private:
69
- BLEMIDI_Transport<class BLEMIDI_ArduinoBLE <_Settings>, _Settings>* _bleMidiTransport;
70
- BLEDevice* _central;
64
+ private:
65
+ BLEMIDI_Transport<class BLEMIDI_ArduinoBLE <_Settings>, _Settings> *_bleMidiTransport;
66
+ BLEDevice *_central;
67
+
68
+ BLEService _midiService;
69
+ BLECharacteristic _midiChar;
71
70
72
71
Fifo<byte, _Settings::MaxBufferSize> mRxBuffer ;
73
72
74
73
public:
75
- BLEMIDI_ArduinoBLE ()
74
+ BLEMIDI_ArduinoBLE () : _midiService(SERVICE_UUID),
75
+ _midiChar (CHARACTERISTIC_UUID, BLERead | BLEWrite | BLENotify | BLEWriteWithoutResponse, _Settings::MaxBufferSize)
76
76
{
77
77
}
78
-
79
- bool begin (const char *, BLEMIDI_Transport<class BLEMIDI_ArduinoBLE <_Settings>, _Settings>*);
80
-
81
- void end ()
78
+
79
+ bool begin (const char *, BLEMIDI_Transport<class BLEMIDI_ArduinoBLE <_Settings>, _Settings> *);
80
+
81
+ void end ()
82
82
{
83
83
}
84
84
85
- void write (uint8_t * buffer, size_t length)
85
+ void write (uint8_t * buffer, size_t length)
86
86
{
87
- // TODO: test length
88
- ((BLECharacteristic)midiChar ).writeValue (buffer, length);
87
+ if (length > 0 )
88
+ ((BLECharacteristic)_midiChar ).writeValue (buffer, length);
89
89
}
90
-
91
- bool available (byte* pvBuffer)
92
- {
93
- #ifdef BLE_POLLING
94
90
95
- if (mRxBuffer .count () > 0 ) {
91
+ bool available (byte *pvBuffer)
92
+ {
93
+ #ifdef BLE_POLLING
94
+ if (mRxBuffer .count () > 0 )
95
+ {
96
96
*pvBuffer = mRxBuffer .dequeue ();
97
-
98
97
return true ;
99
98
}
100
99
101
100
poll ();
102
-
103
- if (midiChar.written ()) {
104
- // auto buffer = midiChar.value();
105
- auto length = midiChar.valueLength ();
106
-
107
- if (length > 0 ) {
108
- auto buffer = midiChar.value ().c_str ();
109
- _bleMidiTransport->receive ((byte*)buffer, length);
110
-
101
+
102
+ if (_midiChar.written ())
103
+ {
104
+ auto length = _midiChar.valueLength ();
105
+ if (length > 0 )
106
+ {
107
+ auto buffer = _midiChar.value ();
108
+ _bleMidiTransport->receive ((byte *)buffer, length);
111
109
}
112
110
}
113
111
return false ;
114
112
#endif
115
113
#ifdef BLE_EVENTS
116
114
BLE.poll ();
117
- return false ;
115
+ return false ;
118
116
#endif
119
117
}
120
118
@@ -125,59 +123,64 @@ class BLEMIDI_ArduinoBLE
125
123
}
126
124
127
125
protected:
128
- void receive (const unsigned char * buffer, size_t length)
129
- {
130
- // forward the buffer so it can be parsed
131
- _bleMidiTransport->receive ((uint8_t *)buffer, length);
132
- }
133
-
134
- bool poll ()
135
- {
126
+ void receive (const unsigned char * buffer, size_t length)
127
+ {
128
+ if (length > 0 )
129
+ _bleMidiTransport->receive ((uint8_t *)buffer, length);
130
+ }
131
+
132
+ bool poll ()
133
+ {
136
134
BLEDevice central = BLE.central ();
137
- if (!central) {
138
- if (_central) {
135
+ if (!central)
136
+ {
137
+ if (_central)
138
+ {
139
139
BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler (*_central);
140
140
_central = nullptr ;
141
141
}
142
142
return false ;
143
143
}
144
144
145
- if (!central.connected ()) {
145
+ if (!central.connected ())
146
146
return false ;
147
- }
148
147
149
- if (nullptr == _central) {
148
+ if (nullptr == _central)
149
+ {
150
150
BLEMIDI_ArduinoBLE::blePeripheralConnectHandler (central);
151
151
_central = ¢ral;
152
152
}
153
- else {
154
- if (*_central != central) {
153
+ else
154
+ {
155
+ if (*_central != central)
156
+ {
155
157
BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler (*_central);
156
158
BLEMIDI_ArduinoBLE::blePeripheralConnectHandler (central);
157
159
_central = ¢ral;
158
- }
160
+ }
159
161
}
160
162
161
163
return true ;
162
164
}
163
165
164
- void blePeripheralConnectHandler (BLEDevice central)
165
- {
166
+ void blePeripheralConnectHandler (BLEDevice central)
167
+ {
166
168
_central = ¢ral;
167
169
168
- if (_bleMidiTransport->_connectedCallback )
169
- _bleMidiTransport->_connectedCallback ();
170
- }
170
+ if (_bleMidiTransport->_connectedCallback )
171
+ _bleMidiTransport->_connectedCallback ();
172
+ }
171
173
172
- void blePeripheralDisconnectHandler (BLEDevice central)
173
- {
174
- if (_bleMidiTransport->_disconnectedCallback )
175
- _bleMidiTransport->_disconnectedCallback ();
174
+ void blePeripheralDisconnectHandler (BLEDevice central)
175
+ {
176
+ if (_bleMidiTransport->_disconnectedCallback )
177
+ _bleMidiTransport->_disconnectedCallback ();
176
178
177
179
_central = nullptr ;
178
- }
180
+ }
179
181
180
- void characteristicWritten (BLEDevice central, BLECharacteristic characteristic) {
182
+ void characteristicWritten (BLEDevice central, BLECharacteristic characteristic)
183
+ {
181
184
auto buffer = characteristic.value ();
182
185
auto length = characteristic.valueLength ();
183
186
@@ -187,50 +190,52 @@ class BLEMIDI_ArduinoBLE
187
190
};
188
191
189
192
template <class _Settings >
190
- bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char * deviceName, BLEMIDI_Transport<class BLEMIDI_ArduinoBLE <_Settings>, _Settings>* bleMidiTransport)
193
+ bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char * deviceName, BLEMIDI_Transport<class BLEMIDI_ArduinoBLE <_Settings>, _Settings> * bleMidiTransport)
191
194
{
192
- _bleMidiTransport = bleMidiTransport;
195
+ _bleMidiTransport = bleMidiTransport;
193
196
194
197
// initialize the Bluetooth® Low Energy hardware
195
- if (!BLE.begin ())
198
+ if (!BLE.begin ())
196
199
return false ;
197
200
198
201
BLE.setLocalName (deviceName);
199
202
200
- BLE.setAdvertisedService (midiService);
201
- midiService.addCharacteristic (midiChar);
202
- BLE.addService (midiService);
203
+ BLE.setAdvertisedService (_midiService);
204
+ _midiService.addCharacteristic (_midiChar);
205
+ BLE.addService (_midiService);
206
+
207
+ // set the initial value for the characeristic:
208
+ // (when not set, the device will disconnect after 0.5 seconds)
209
+ _midiChar.writeValue ((uint8_t )0 );
203
210
204
211
#ifdef BLE_EVENTS
205
212
// assign event handlers for connected, disconnected to peripheral
206
- BLE.setEventHandler (BLEConnected, BLEMIDI_ArduinoBLE::blePeripheralConnectHandler);
213
+ BLE.setEventHandler (BLEConnected, BLEMIDI_ArduinoBLE::blePeripheralConnectHandler);
207
214
BLE.setEventHandler (BLEDisconnected, BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler);
208
215
209
- midiChar .setEventHandler (BLEWritten, characteristicWritten);
216
+ _midiChar .setEventHandler (BLEWritten, characteristicWritten);
210
217
#endif
211
218
212
- /* Start advertising BLE. It will start continuously transmitting BLE
213
- advertising packets and will be visible to remote BLE central devices
214
- until it receives a new connection */
215
-
216
- // start advertising
219
+ /* Start advertising BLE. It will start continuously transmitting BLE
220
+ advertising packets and will be visible to remote BLE central devices
221
+ until it receives a new connection */
217
222
BLE.advertise ();
218
-
223
+
219
224
return true ;
220
225
}
221
226
222
- /* ! \brief Create an instance for ArduinoBLE <DeviceName>
227
+ /* ! \brief Create an instance for ArduinoBLE <DeviceName>
223
228
*/
224
- #define BLEMIDI_CREATE_CUSTOM_INSTANCE (DeviceName, Name, _Settings ) \
229
+ #define BLEMIDI_CREATE_CUSTOM_INSTANCE (DeviceName, Name, _Settings ) \
225
230
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ArduinoBLE<_Settings>, _Settings> BLE##Name(DeviceName); \
226
231
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ArduinoBLE<_Settings>, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name ((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ArduinoBLE<_Settings>, _Settings> &)BLE##Name);
227
232
228
- /* ! \brief Create an instance for ArduinoBLE <DeviceName>
233
+ /* ! \brief Create an instance for ArduinoBLE <DeviceName>
229
234
*/
230
235
#define BLEMIDI_CREATE_INSTANCE (DeviceName, Name ) \
231
- BLEMIDI_CREATE_CUSTOM_INSTANCE (DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettings)
236
+ BLEMIDI_CREATE_CUSTOM_INSTANCE (DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettings)
232
237
233
- /* ! \brief Create a default instance for ArduinoBLE named BLE-MIDI
238
+ /* ! \brief Create a default instance for ArduinoBLE named BLE-MIDI
234
239
*/
235
240
#define BLEMIDI_CREATE_DEFAULT_INSTANCE () \
236
241
BLEMIDI_CREATE_INSTANCE (" BLE-MIDI" , MIDI)
0 commit comments