Skip to content

Commit 6d0ea97

Browse files
authored
Merge pull request #102 from RobertoHE/CustomSettings
Adaptation to new NimBLE lib methods. thank you!!
2 parents 04babb8 + 96c7914 commit 6d0ea97

File tree

9 files changed

+245
-204
lines changed

9 files changed

+245
-204
lines changed

examples/CustomerBufferSize/CustomerBufferSize.ino renamed to examples/CustomSettings/CustomSettings.ino

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,19 @@
11
#include <BLEMIDI_Transport.h>
22

3-
static uint32_t customPasskeyRequest()
4-
{
5-
// FILL WITH YOUR CUSTOM AUTH METHOD CODE or PASSKEY
6-
// FOR EXAMPLE:
7-
uint32_t passkey = 123456;
8-
9-
// Serial.println("Client Passkey Request");
10-
11-
/** return the passkey to send to the server */
12-
return passkey;
13-
};
3+
//#include <hardware/BLEMIDI_ESP32_NimBLE.h>
4+
#include <hardware/BLEMIDI_ESP32.h>
5+
//#include <hardware/BLEMIDI_ArduinoBLE.h>
6+
//#include <hardware/BLEMIDI_Client_ESP32.h>
147

15-
struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings {
16-
//See all options and them explanation in the library.
17-
18-
/*
19-
##### BLE DEVICE NAME #####
20-
*/
21-
//static constexpr char *name = "BleMidiClient";
22-
/*
23-
###### TX POWER #####
24-
*/
25-
//static const esp_power_level_t clientTXPwr = ESP_PWR_LVL_P9;
26-
/*
27-
###### SECURITY #####
28-
*/
29-
//static const uint8_t clientSecurityCapabilities = BLE_HS_IO_NO_INPUT_OUTPUT;
30-
//static const bool clientBond = true;
31-
//static const bool clientMITM = false;
32-
//static const bool clientPair = true;
33-
//static constexpr PasskeyRequestCallback userOnPassKeyRequest = customPasskeyRequest;
34-
/*
35-
###### BLE COMMUNICATION PARAMS ######
36-
*/
37-
//static const uint16_t commMinInterval = 6; // 7.5ms
38-
//static const uint16_t commMaxInterval = 35; // 40ms
39-
//static const uint16_t commLatency = 0; //
40-
//static const uint16_t commTimeOut = 200; // 2000ms
41-
/*
42-
###### BLE FORCE NEW CONNECTION ######
43-
*/
44-
//static const bool forceNewConnection = false;
45-
/*
46-
###### BLE SUBSCRIPTION: NOTIFICATION & RESPONSE ######
47-
*/
48-
//static const bool notification = true;
49-
//static const bool response = true;
50-
/*
51-
###### AND THE OTHER SETTINGS OF MIDI LIBRARY ######
52-
*/
8+
//User defined settings
9+
struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::BLEDefaultSettings {
10+
//BLE-MIDI settings (see BLEMIDI_Settings.h and Hardware/BLE_class for more details)
5311
static const size_t MaxBufferSize = 16;
54-
55-
};
5612

57-
#include <hardware/BLEMIDI_ESP32_NimBLE.h>
58-
//#include <hardware/BLEMIDI_ESP32.h>
59-
//#include <hardware/BLEMIDI_ArduinoBLE.h>
13+
//MIDI settings (see MIDI_Settings.h)
14+
static const int Use1ByteParsing = true;
15+
static const bool HandleNullVelocityNoteOnAsNoteOff = true;
16+
};
6017

6118
#ifndef LED_BUILTIN
6219
#define LED_BUILTIN 2

examples/MidiBle_Client/MidiBle_Client.ino

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,79 @@
2929
#include <BLEMIDI_Transport.h>
3030

3131
#include <hardware/BLEMIDI_Client_ESP32.h>
32-
//#include <hardware/BLEMIDI_ESP32_NimBLE.h>
33-
//#include <hardware/BLEMIDI_ESP32.h>
34-
//#include <hardware/BLEMIDI_ArduinoBLE.h>
3532

3633
#ifndef LED_BUILTIN
3734
#define LED_BUILTIN 2
3835
#endif
3936

37+
// For customPasskeyRequest callback format type
38+
using PasskeyRequestCallback = uint32_t (*)(void);
39+
40+
// This function is called when the server request a passkey (if you need it)
41+
// If yo need a passkey, you must set the security capabilities and set PasskeyRequestCallback
42+
43+
/*static uint32_t customPasskeyRequest()
44+
{
45+
// FILL WITH YOUR CUSTOM AUTH METHOD CODE or PASSKEY
46+
// FOR EXAMPLE:
47+
uint32_t passkey = 123456;
48+
49+
// Serial.println("Client Passkey Request");
50+
51+
// return the passkey to send to the server
52+
return passkey;
53+
};
54+
*/
55+
4056
//See DefaultSettingsClient in hardware/BLEMIDI_Client_ESP32.h for more configurable settings
4157
// If you do not redefine a parameter, it will use the default value for these parameter
42-
struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettingsClient {
43-
static const size_t MaxBufferSize = 16;
58+
struct CustomSettings : public BLEMIDI_NAMESPACE::BLEDefaultSettings
59+
{
60+
//See all options and them explanation in the library.
61+
62+
/*
63+
##### BLE DEVICE NAME #####
64+
*/
65+
//static constexpr char *name = (char*)"BleMidiClient";
66+
/*
67+
###### TX POWER #####
68+
*/
69+
//static const int8_t clientTXPwr = 9; // in dBm
70+
/*
71+
###### SECURITY #####
72+
*/
73+
//static const uint8_t clientSecurityCapabilities = BLE_HS_IO_NO_INPUT_OUTPUT;
74+
//static const bool clientBond = true;
75+
//static const bool clientMITM = false;
76+
//static const bool clientPair = true;
77+
//static constexpr PasskeyRequestCallback userOnPassKeyRequest = customPasskeyRequest;
78+
/*
79+
###### BLE COMMUNICATION PARAMS ######
80+
*/
81+
//static const uint16_t commMinInterval = 6; // 7.5ms
82+
//static const uint16_t commMaxInterval = 35; // 40ms
83+
//static const uint16_t commLatency = 0; //
84+
//static const uint16_t commTimeOut = 200; // 2000ms
85+
/*
86+
###### BLE FORCE NEW CONNECTION ######
87+
*/
88+
static const bool forceNewConnection = false;
89+
/*
90+
###### BLE SUBSCRIPTION: NOTIFICATION & RESPONSE ######
91+
*/
92+
//static const bool notification = true;
93+
//static const bool response = true;
94+
95+
96+
//BLE-MIDI settings (see BLEMIDI_Settings.h and BLE_class for more details)
97+
static const size_t MaxBufferSize = 16;
98+
99+
// MIDI settings (see MIDI_Settings.h)
100+
static const int Use1ByteParsing = true;
101+
static const bool HandleNullVelocityNoteOnAsNoteOff = true;
44102
};
45103

46-
BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-BLE-MIDI", MIDI, CustomBufferSizeSettings); // Connect to a server named "Esp32-BLE-MIDI" and use CustomBufferSizeSettings as settings of client
104+
BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-BLE-MIDI", MIDI, CustomSettings); // Connect to a server named "Esp32-BLE-MIDI" and use CustomSettings as settings of client
47105

48106
//BLEMIDI_CREATE_INSTANCE("",MIDI) //Connect to the first server found, using default settings
49107
//BLEMIDI_CREATE_INSTANCE("f2:c1:d9:36:e7:6b",MIDI) //Connect to a specific BLE address server, using default settings

src/BLEMIDI_Settings.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
BEGIN_BLEMIDI_NAMESPACE
66

7-
struct DefaultSettings
7+
//Common settings for all BLEMIDI classes
8+
//This is the default settings for all BLEMIDI classes
9+
struct CommonBLEDefaultSettings : MIDI_NAMESPACE::DefaultSettings
810
{
911
static const short MaxBufferSize = 64;
1012
};

src/BLEMIDI_Transport.h

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static const char *const CHARACTERISTIC_UUID = "7772e5db-3868-4112-a1a9-f2669d10
2323

2424
#define MIDI_TYPE 0x80
2525

26-
template <class T, class _Settings = DefaultSettings>
26+
template <class T, class _Settings /*= CommonBLEDefaultSettings*/>
2727
class BLEMIDI_Transport
2828
{
2929
private:
@@ -183,9 +183,7 @@ class BLEMIDI_Transport
183183
public:
184184
// callbacks
185185
void (*_connectedCallback)() = nullptr;
186-
void (*_connectedCallbackDeviceName)(char *) = nullptr;
187186
void (*_disconnectedCallback)() = nullptr;
188-
void (*_connectedCallbackDeviceName)(char *) = nullptr;
189187

190188
BLEMIDI_Transport &setName(const char *deviceName)
191189
{
@@ -199,19 +197,7 @@ class BLEMIDI_Transport
199197
_connectedCallback = fptr;
200198
return *this;
201199
}
202-
203-
BLEMIDI_Transport &setHandleConnected(void (*fptr)(char*))
204-
{
205-
_connectedCallbackDeviceName= fptr;
206-
return *this;
207-
}
208200

209-
BLEMIDI_Transport &setHandleConnected(void (*fptr)(char*))
210-
{
211-
_connectedCallbackDeviceName= fptr;
212-
return *this;
213-
}
214-
215201
BLEMIDI_Transport &setHandleDisconnected(void (*fptr)())
216202
{
217203
_disconnectedCallback = fptr;
@@ -423,9 +409,4 @@ class BLEMIDI_Transport
423409
}
424410
};
425411

426-
struct MySettings : public MIDI_NAMESPACE::DefaultSettings
427-
{
428-
static const bool Use1ByteParsing = false;
429-
};
430-
431412
END_BLEMIDI_NAMESPACE

src/hardware/BLEMIDI_ArduinoBLE.h

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
#pragma once
22

33
#include <ArduinoBLE.h>
4+
#include "BLEMIDI_Namespace.h"
5+
#include "BLEMIDI_Settings.h"
46

57
#define BLE_POLLING
68

79
BEGIN_BLEMIDI_NAMESPACE
810

11+
// Dependanced class settings
12+
struct BLEDefaultSettings : public CommonBLEDefaultSettings
13+
{
14+
//TODO Create parametric configurations
15+
};
16+
917
BLEService midiService(SERVICE_UUID);
1018

1119
BLEStringCharacteristic midiChar(CHARACTERISTIC_UUID, // standard 16-bit characteristic UUID
@@ -73,12 +81,13 @@ class BLEMIDI_ArduinoBLE
7381

7482
Fifo<byte, _Settings::MaxBufferSize> mRxBuffer;
7583

76-
template <class> friend class MyServerCallbacks;
84+
static BLEMIDI_ArduinoBLE<_Settings>* self;
7785

7886
public:
7987
BLEMIDI_ArduinoBLE() : _midiService(SERVICE_UUID),
8088
_midiChar(CHARACTERISTIC_UUID, BLERead | BLEWrite | BLENotify | BLEWriteWithoutResponse, _Settings::MaxBufferSize)
8189
{
90+
self = this;
8291
}
8392

8493
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_ArduinoBLE<_Settings>, _Settings> *);
@@ -139,59 +148,26 @@ class BLEMIDI_ArduinoBLE
139148

140149
end();
141150
}
142-
};
143-
144-
template <class _Settings>
145-
class MyServerCallbacks : public BLEDeviceCallbacks
146-
{
147-
public:
148-
MyServerCallbacks(BLEMIDI_ArduinoBLE<_Settings> *bluetooth)
149-
: _bluetooth(bluetooth)
150-
{
151-
}
152-
153-
protected:
154-
BLEMIDI_ArduinoBLE<_Settings> *_bluetooth = nullptr;
155-
156-
void onConnect(BLEDevice device)
157-
{
158-
if (_bluetooth)
159-
_bluetooth->connected();
160-
};
161-
162-
void onDisconnect(BLEDevice device)
163-
{
164-
if (_bluetooth)
165-
_bluetooth->disconnected();
166-
}
167-
};
168151

169-
template <class _Settings>
170-
class MyCharacteristicCallbacks : public BLECharacteristicCallbacks
171-
{
172-
public:
173-
MyCharacteristicCallbacks(BLEMIDI_ArduinoBLE<_Settings> *bluetooth)
174-
: _bluetooth(bluetooth)
175-
{
152+
static void blePeripheralConnectedHandler(BLEDevice central) {
153+
self->connected();
176154
}
177-
178-
protected:
179-
BLEMIDI_ArduinoBLE<_Settings> *_bluetooth = nullptr;
180-
181-
void onWrite(BLECharacteristic characteristic)
182-
{
183-
// std::string rxValue = characteristic->getValue();
184-
// if (rxValue.length() > 0)
185-
// {
186-
// _bluetooth->receive((uint8_t *)(rxValue.c_str()), rxValue.length());
187-
//}
155+
156+
static void blePeripheralDisconnectedHandler(BLEDevice central) {
157+
self->disconnected();
188158
}
189-
190-
void onRead(BLECharacteristic characteristic)
191-
{
159+
160+
static void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
161+
// std::string rxValue = characteristic->value();
162+
// if (rxValue.length() > 0)
163+
// receive((uint8_t *)(rxValue.c_str()), rxValue.length());
192164
}
193165
};
194166

167+
// this pattern of static to instance variable is limited to 1 instance of ArduinoBLE
168+
// for multiple instances, turn this into a vector and lookup
169+
template <class _Settings> BLEMIDI_ArduinoBLE<_Settings>* BLEMIDI_ArduinoBLE<_Settings>::self;
170+
195171
template <class _Settings>
196172
bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_ArduinoBLE<_Settings>, _Settings> *bleMidiTransport)
197173
{
@@ -207,9 +183,10 @@ bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transp
207183
_midiService.addCharacteristic(_midiChar);
208184
BLE.addService(_midiService);
209185

210-
BLE.setCallbacks(new MyServerCallbacks<_Settings>(this));
186+
BLE.setEventHandler(BLEConnected, blePeripheralConnectedHandler);
187+
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectedHandler);
211188

212-
_midiChar.setCallbacks(new MyCharacteristicCallbacks<_Settings>(this));
189+
_midiChar.setEventHandler(BLEWritten, switchCharacteristicWritten);
213190

214191
// set the initial value for the characeristic:
215192
// (when not set, the device will disconnect after 0.5 seconds)
@@ -223,20 +200,21 @@ bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transp
223200
return true;
224201
}
225202

203+
226204
/*! \brief Create an instance for ArduinoBLE <DeviceName>
227205
*/
228206
#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \
229207
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ArduinoBLE<_Settings>, _Settings> BLE##Name(DeviceName); \
230-
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);
208+
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ArduinoBLE<_Settings>, _Settings>, BLEMIDI_NAMESPACE::BLEDefaultSettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ArduinoBLE<_Settings>, _Settings> &)BLE##Name);
231209

232210
/*! \brief Create an instance for ArduinoBLE <DeviceName>
233211
*/
234212
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
235-
BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettings);
213+
BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, BLEMIDI_NAMESPACE::BLEDefaultSettings);
236214

237215
/*! \brief Create a default instance for ArduinoBLE named BLE-MIDI
238216
*/
239217
#define BLEMIDI_CREATE_DEFAULT_INSTANCE() \
240218
BLEMIDI_CREATE_INSTANCE("BLE-MIDI", MIDI)
241219

242-
END_BLEMIDI_NAMESPACE
220+
END_BLEMIDI_NAMESPACE

0 commit comments

Comments
 (0)