Skip to content

Commit edd2f21

Browse files
committed
Update BLEMIDI_ArduinoBLE.h
added : _midiChar.writeValue((uint8_t)0); otherwise the device will disconnect immediately
1 parent 8c605fe commit edd2f21

File tree

1 file changed

+98
-93
lines changed

1 file changed

+98
-93
lines changed

src/hardware/BLEMIDI_ArduinoBLE.h

Lines changed: 98 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,49 @@
77

88
BEGIN_BLEMIDI_NAMESPACE
99

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+
{
1713
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
1915

20-
Fifo(): size(rawSize)
16+
Fifo() : size(rawSize)
2117
{
22-
flush();
18+
flush();
2319
}
2420

25-
T dequeue()
21+
T dequeue()
2622
{
2723
numberOfElements--;
2824
nextOut %= size;
29-
return raw[ nextOut++];
25+
return raw[nextOut++];
3026
};
31-
32-
bool enqueue( T element )
27+
28+
bool enqueue(T element)
3329
{
34-
if ( count() >= rawSize )
30+
if (count() >= rawSize)
3531
return false;
3632

3733
numberOfElements++;
3834
nextIn %= size;
3935
raw[nextIn] = element;
40-
nextIn++; //advance to next index
36+
nextIn++; // advance to next index
4137

4238
return true;
4339
};
4440

4541
T peek() const
4642
{
47-
return raw[ nextOut % size];
43+
return raw[nextOut % size];
4844
}
4945

5046
void flush()
5147
{
52-
nextIn = nextOut = numberOfElements = 0;
48+
nextIn = nextOut = numberOfElements = 0;
5349
}
5450

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; }
5753

5854
private:
5955
size_t numberOfElements;
@@ -65,56 +61,58 @@ class Fifo {
6561
template <class _Settings>
6662
class BLEMIDI_ArduinoBLE
6763
{
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;
7170

7271
Fifo<byte, _Settings::MaxBufferSize> mRxBuffer;
7372

7473
public:
75-
BLEMIDI_ArduinoBLE()
74+
BLEMIDI_ArduinoBLE() : _midiService(SERVICE_UUID),
75+
_midiChar(CHARACTERISTIC_UUID, BLERead | BLEWrite | BLENotify | BLEWriteWithoutResponse, _Settings::MaxBufferSize)
7676
{
7777
}
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()
8282
{
8383
}
8484

85-
void write(uint8_t* buffer, size_t length)
85+
void write(uint8_t *buffer, size_t length)
8686
{
87-
// TODO: test length
88-
((BLECharacteristic)midiChar).writeValue(buffer, length);
87+
if (length > 0)
88+
((BLECharacteristic)_midiChar).writeValue(buffer, length);
8989
}
90-
91-
bool available(byte* pvBuffer)
92-
{
93-
#ifdef BLE_POLLING
9490

95-
if (mRxBuffer.count() > 0) {
91+
bool available(byte *pvBuffer)
92+
{
93+
#ifdef BLE_POLLING
94+
if (mRxBuffer.count() > 0)
95+
{
9696
*pvBuffer = mRxBuffer.dequeue();
97-
9897
return true;
9998
}
10099

101100
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);
111109
}
112110
}
113111
return false;
114112
#endif
115113
#ifdef BLE_EVENTS
116114
BLE.poll();
117-
return false;
115+
return false;
118116
#endif
119117
}
120118

@@ -125,59 +123,64 @@ class BLEMIDI_ArduinoBLE
125123
}
126124

127125
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+
{
136134
BLEDevice central = BLE.central();
137-
if (!central) {
138-
if (_central) {
135+
if (!central)
136+
{
137+
if (_central)
138+
{
139139
BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler(*_central);
140140
_central = nullptr;
141141
}
142142
return false;
143143
}
144144

145-
if (!central.connected()) {
145+
if (!central.connected())
146146
return false;
147-
}
148147

149-
if (nullptr == _central) {
148+
if (nullptr == _central)
149+
{
150150
BLEMIDI_ArduinoBLE::blePeripheralConnectHandler(central);
151151
_central = &central;
152152
}
153-
else {
154-
if (*_central != central) {
153+
else
154+
{
155+
if (*_central != central)
156+
{
155157
BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler(*_central);
156158
BLEMIDI_ArduinoBLE::blePeripheralConnectHandler(central);
157159
_central = &central;
158-
}
160+
}
159161
}
160162

161163
return true;
162164
}
163165

164-
void blePeripheralConnectHandler(BLEDevice central)
165-
{
166+
void blePeripheralConnectHandler(BLEDevice central)
167+
{
166168
_central = &central;
167169

168-
if (_bleMidiTransport->_connectedCallback)
169-
_bleMidiTransport->_connectedCallback();
170-
}
170+
if (_bleMidiTransport->_connectedCallback)
171+
_bleMidiTransport->_connectedCallback();
172+
}
171173

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();
176178

177179
_central = nullptr;
178-
}
180+
}
179181

180-
void characteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
182+
void characteristicWritten(BLEDevice central, BLECharacteristic characteristic)
183+
{
181184
auto buffer = characteristic.value();
182185
auto length = characteristic.valueLength();
183186

@@ -187,50 +190,52 @@ class BLEMIDI_ArduinoBLE
187190
};
188191

189192
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)
191194
{
192-
_bleMidiTransport = bleMidiTransport;
195+
_bleMidiTransport = bleMidiTransport;
193196

194197
// initialize the Bluetooth® Low Energy hardware
195-
if (!BLE.begin())
198+
if (!BLE.begin())
196199
return false;
197200

198201
BLE.setLocalName(deviceName);
199202

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);
203210

204211
#ifdef BLE_EVENTS
205212
// assign event handlers for connected, disconnected to peripheral
206-
BLE.setEventHandler(BLEConnected, BLEMIDI_ArduinoBLE::blePeripheralConnectHandler);
213+
BLE.setEventHandler(BLEConnected, BLEMIDI_ArduinoBLE::blePeripheralConnectHandler);
207214
BLE.setEventHandler(BLEDisconnected, BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler);
208215

209-
midiChar.setEventHandler(BLEWritten, characteristicWritten);
216+
_midiChar.setEventHandler(BLEWritten, characteristicWritten);
210217
#endif
211218

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 */
217222
BLE.advertise();
218-
223+
219224
return true;
220225
}
221226

222-
/*! \brief Create an instance for ArduinoBLE <DeviceName>
227+
/*! \brief Create an instance for ArduinoBLE <DeviceName>
223228
*/
224-
#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \
229+
#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \
225230
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ArduinoBLE<_Settings>, _Settings> BLE##Name(DeviceName); \
226231
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);
227232

228-
/*! \brief Create an instance for ArduinoBLE <DeviceName>
233+
/*! \brief Create an instance for ArduinoBLE <DeviceName>
229234
*/
230235
#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)
232237

233-
/*! \brief Create a default instance for ArduinoBLE named BLE-MIDI
238+
/*! \brief Create a default instance for ArduinoBLE named BLE-MIDI
234239
*/
235240
#define BLEMIDI_CREATE_DEFAULT_INSTANCE() \
236241
BLEMIDI_CREATE_INSTANCE("BLE-MIDI", MIDI)

0 commit comments

Comments
 (0)