Skip to content

Commit b91186d

Browse files
committed
added BLE Client template arguments
modig
1 parent 948fc11 commit b91186d

File tree

2 files changed

+41
-30
lines changed

2 files changed

+41
-30
lines changed

examples/MidiBle_Client/MidiBle_Client.ino

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@
2828
#include <Arduino.h>
2929
#include <BLEMIDI_Transport.h>
3030

31+
struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings {
32+
static const size_t MaxBufferSize = 16;
33+
};
34+
3135
#include <hardware/BLEMIDI_Client_ESP32.h>
3236

3337
//#include <hardware/BLEMIDI_ESP32_NimBLE.h>
3438
//#include <hardware/BLEMIDI_ESP32.h>
3539
//#include <hardware/BLEMIDI_nRF52.h>
3640
//#include <hardware/BLEMIDI_ArduinoBLE.h>
3741

38-
BLEMIDI_CREATE_DEFAULT_INSTANCE(); //Connect to first server found
42+
BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-BLE-MIDI", MIDI, CustomBufferSizeSettings); // Connect to first server found
3943

4044
//BLEMIDI_CREATE_INSTANCE("",MIDI) //Connect to the first server found
4145
//BLEMIDI_CREATE_INSTANCE("f2:c1:d9:36:e7:6b",MIDI) //Connect to a specific BLE address server

src/hardware/BLEMIDI_Client_ESP32.h

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ BEGIN_BLEMIDI_NAMESPACE
174174
#define BLEMIDI_CLIENT_SECURITY_AUTH (BLEMIDI_CLIENT_BOND_DUMMY | BLEMIDI_CLIENT_MITM_DUMMY | BLEMIDI_CLIENT_PAIR_DUMMY)
175175

176176
/** Define a class to handle the callbacks when advertisments are received */
177+
template <class _Settings>
177178
class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks
178179
{
179180
public:
@@ -220,6 +221,7 @@ class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks
220221
void scanEndedCB(NimBLEScanResults results);
221222

222223
/** Define the class that performs Client Midi (nimBLE) */
224+
template <class _Settings>
223225
class BLEMIDI_Client_ESP32
224226
{
225227
private:
@@ -229,15 +231,14 @@ class BLEMIDI_Client_ESP32
229231
BLERemoteService *pSvc = nullptr;
230232
bool firstTimeSend = true; //First writeValue get sends like Write with reponse for clean security flags. After first time, all messages are send like WriteNoResponse for increase transmision speed.
231233

232-
BLEMIDI_Transport<class BLEMIDI_Client_ESP32> *_bleMidiTransport = nullptr;
234+
BLEMIDI_Transport<class BLEMIDI_Client_ESP32<_Settings>, _Settings> *_bleMidiTransport = nullptr;
233235

234236
bool specificTarget = false;
235237

236-
friend class AdvertisedDeviceCallbacks;
237-
friend class MyClientCallbacks;
238-
friend class MIDI_NAMESPACE::MidiInterface<BLEMIDI_Transport<BLEMIDI_Client_ESP32>, MySettings>; //
238+
// TODO: somehow the forward declaration of the template class is not accepted by the compiler
239+
// template <class> friend MyClientCallbacks;
239240

240-
AdvertisedDeviceCallbacks myAdvCB;
241+
AdvertisedDeviceCallbacks<_Settings> myAdvCB;
241242

242243
protected:
243244
QueueHandle_t mRxQueue;
@@ -247,7 +248,7 @@ class BLEMIDI_Client_ESP32
247248
{
248249
}
249250

250-
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_Client_ESP32> *);
251+
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_Client_ESP32<_Settings>, _Settings> *);
251252

252253
bool end()
253254
{
@@ -294,61 +295,55 @@ class BLEMIDI_Client_ESP32
294295
_bleMidiTransport->receive(buffer, length);
295296
}
296297

297-
void connectCallbacks(MIDI_NAMESPACE::MidiInterface<BLEMIDI_Transport<BLEMIDI_Client_ESP32>, MySettings> *MIDIcallback);
298+
void notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify);
298299

300+
void scan();
301+
bool connect();
302+
303+
public: // TODO: somehow the forward declaration of the template class is not accepted by the compiler
299304
void connected()
300305
{
301306
if (_bleMidiTransport->_connectedCallback)
302-
{
303307
_bleMidiTransport->_connectedCallback();
304-
}
305308
firstTimeSend = true;
306309
}
307310

308311
void disconnected()
309312
{
310313
if (_bleMidiTransport->_disconnectedCallback)
311-
{
312314
_bleMidiTransport->_disconnectedCallback();
313-
}
314315
firstTimeSend = true;
315316
}
316-
317-
void notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify);
318-
319-
void scan();
320-
bool connect();
321317
};
322318

323319
/** Define the class that performs interruption callbacks */
320+
template <class _Settings>
324321
class MyClientCallbacks : public BLEClientCallbacks
325322
{
326323
public:
327-
MyClientCallbacks(BLEMIDI_Client_ESP32 *bluetoothEsp32)
324+
MyClientCallbacks(BLEMIDI_Client_ESP32<_Settings> *bluetoothEsp32)
328325
: _bluetoothEsp32(bluetoothEsp32)
329326
{
330327
}
331328

332329
protected:
333-
BLEMIDI_Client_ESP32 *_bluetoothEsp32 = nullptr;
330+
BLEMIDI_Client_ESP32<_Settings> *_bluetoothEsp32 = nullptr;
334331

335332
uint32_t onPassKeyRequest()
336333
{
337334
return userOnPassKeyRequest();
338335
};
339336

340-
void onConnect(BLEClient *pClient)
337+
void onConnect(BLEClient*)
341338
{
342339
//Serial.println("##Connected##");
343340
//pClient->updateConnParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT);
344341
vTaskDelay(1);
345342
if (_bluetoothEsp32)
346-
{
347343
_bluetoothEsp32->connected();
348-
}
349344
};
350345

351-
void onDisconnect(BLEClient *pClient)
346+
void onDisconnect(BLEClient*)
352347
{
353348
//Serial.print(pClient->getPeerAddress().toString().c_str());
354349
//Serial.println(" Disconnected - Starting scan");
@@ -403,7 +398,8 @@ class MyClientCallbacks : public BLEClientCallbacks
403398
##########################################
404399
*/
405400

406-
bool BLEMIDI_Client_ESP32::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_Client_ESP32> *bleMidiTransport)
401+
template <class _Settings>
402+
bool BLEMIDI_Client_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_Client_ESP32<_Settings>, _Settings> *bleMidiTransport)
407403
{
408404
_bleMidiTransport = bleMidiTransport;
409405

@@ -450,7 +446,8 @@ bool BLEMIDI_Client_ESP32::begin(const char *deviceName, BLEMIDI_Transport<class
450446
return true;
451447
}
452448

453-
bool BLEMIDI_Client_ESP32::available(byte *pvBuffer)
449+
template <class _Settings>
450+
bool BLEMIDI_Client_ESP32<_Settings>::available(byte *pvBuffer)
454451
{
455452
if (myAdvCB.enableConnection)
456453
{
@@ -479,15 +476,17 @@ bool BLEMIDI_Client_ESP32::available(byte *pvBuffer)
479476
}
480477

481478
/** Notification receiving handler callback */
482-
void BLEMIDI_Client_ESP32::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify)
479+
template <class _Settings>
480+
void BLEMIDI_Client_ESP32<_Settings>::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify)
483481
{
484482
if (this->_characteristic == pRemoteCharacteristic) //Redundant protection
485483
{
486484
receive(pData, length);
487485
}
488486
}
489487

490-
void BLEMIDI_Client_ESP32::scan()
488+
template <class _Settings>
489+
void BLEMIDI_Client_ESP32<_Settings>::scan()
491490
{
492491
// Retrieve a Scanner and set the callback you want to use to be informed when a new device is detected.
493492
// Specify that you want active scanning and start the
@@ -506,7 +505,8 @@ void BLEMIDI_Client_ESP32::scan()
506505
}
507506
};
508507

509-
bool BLEMIDI_Client_ESP32::connect()
508+
template <class _Settings>
509+
bool BLEMIDI_Client_ESP32<_Settings>::connect()
510510
{
511511
using namespace std::placeholders; //<- for bind funtion in callback notification
512512

@@ -553,7 +553,7 @@ bool BLEMIDI_Client_ESP32::connect()
553553
// Create and setup a new client
554554
_client = BLEDevice::createClient();
555555

556-
_client->setClientCallbacks(new MyClientCallbacks(this), false);
556+
_client->setClientCallbacks(new MyClientCallbacks<_Settings>(this), false);
557557

558558
_client->setConnectionParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT);
559559

@@ -622,7 +622,14 @@ void scanEndedCB(NimBLEScanResults results)
622622

623623
END_BLEMIDI_NAMESPACE
624624

625-
/*! \brief Create an instance for ESP32 named <DeviceName>, and adviertise it like "Prefix + <DeviceName> + Subfix"
625+
/*! \brief Create a custom instance for ESP32 named <DeviceName>, and advertise it like "Prefix + <DeviceName> + Subfix"
626+
It will try to connect to a specific server with equal name or addr than <DeviceName>. If <DeviceName> is "", it will connect to first midi server
627+
*/
628+
#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \
629+
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_Client_ESP32<_Settings>, _Settings> BLE##Name(DeviceName); \
630+
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_Client_ESP32<_Settings>, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_Client_ESP32<_Settings>, _Settings> &)BLE##Name);
631+
632+
/*! \brief Create an instance for ESP32 named <DeviceName>, and advertise it like "Prefix + <DeviceName> + Subfix"
626633
It will try to connect to a specific server with equal name or addr than <DeviceName>. If <DeviceName> is "", it will connect to first midi server
627634
*/
628635
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \

0 commit comments

Comments
 (0)