Skip to content
Open
Original file line number Diff line number Diff line change
@@ -1,62 +1,19 @@
#include <BLEMIDI_Transport.h>

static uint32_t customPasskeyRequest()
{
// FILL WITH YOUR CUSTOM AUTH METHOD CODE or PASSKEY
// FOR EXAMPLE:
uint32_t passkey = 123456;

// Serial.println("Client Passkey Request");

/** return the passkey to send to the server */
return passkey;
};
//#include <hardware/BLEMIDI_ESP32_NimBLE.h>
#include <hardware/BLEMIDI_ESP32.h>
//#include <hardware/BLEMIDI_ArduinoBLE.h>
//#include <hardware/BLEMIDI_Client_ESP32.h>

struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings {
//See all options and them explanation in the library.

/*
##### BLE DEVICE NAME #####
*/
//static constexpr char *name = "BleMidiClient";
/*
###### TX POWER #####
*/
//static const esp_power_level_t clientTXPwr = ESP_PWR_LVL_P9;
/*
###### SECURITY #####
*/
//static const uint8_t clientSecurityCapabilities = BLE_HS_IO_NO_INPUT_OUTPUT;
//static const bool clientBond = true;
//static const bool clientMITM = false;
//static const bool clientPair = true;
//static constexpr PasskeyRequestCallback userOnPassKeyRequest = customPasskeyRequest;
/*
###### BLE COMMUNICATION PARAMS ######
*/
//static const uint16_t commMinInterval = 6; // 7.5ms
//static const uint16_t commMaxInterval = 35; // 40ms
//static const uint16_t commLatency = 0; //
//static const uint16_t commTimeOut = 200; // 2000ms
/*
###### BLE FORCE NEW CONNECTION ######
*/
//static const bool forceNewConnection = false;
/*
###### BLE SUBSCRIPTION: NOTIFICATION & RESPONSE ######
*/
//static const bool notification = true;
//static const bool response = true;
/*
###### AND THE OTHER SETTINGS OF MIDI LIBRARY ######
*/
//User defined settings
struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::BLEDefaultSettings {
//BLE-MIDI settings (see BLEMIDI_Settings.h and Hardware/BLE_class for more details)
static const size_t MaxBufferSize = 16;

};

#include <hardware/BLEMIDI_ESP32_NimBLE.h>
//#include <hardware/BLEMIDI_ESP32.h>
//#include <hardware/BLEMIDI_ArduinoBLE.h>
//MIDI settings (see MIDI_Settings.h)
static const int Use1ByteParsing = true;
static const bool HandleNullVelocityNoteOnAsNoteOff = true;
};

#ifndef LED_BUILTIN
#define LED_BUILTIN 2
Expand Down
70 changes: 64 additions & 6 deletions examples/MidiBle_Client/MidiBle_Client.ino
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,79 @@
#include <BLEMIDI_Transport.h>

#include <hardware/BLEMIDI_Client_ESP32.h>
//#include <hardware/BLEMIDI_ESP32_NimBLE.h>
//#include <hardware/BLEMIDI_ESP32.h>
//#include <hardware/BLEMIDI_ArduinoBLE.h>

#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif

// For customPasskeyRequest callback format type
using PasskeyRequestCallback = uint32_t (*)(void);

// This function is called when the server request a passkey (if you need it)
// If yo need a passkey, you must set the security capabilities and set PasskeyRequestCallback

/*static uint32_t customPasskeyRequest()
{
// FILL WITH YOUR CUSTOM AUTH METHOD CODE or PASSKEY
// FOR EXAMPLE:
uint32_t passkey = 123456;

// Serial.println("Client Passkey Request");

// return the passkey to send to the server
return passkey;
};
*/

//See DefaultSettingsClient in hardware/BLEMIDI_Client_ESP32.h for more configurable settings
// If you do not redefine a parameter, it will use the default value for these parameter
struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettingsClient {
static const size_t MaxBufferSize = 16;
struct CustomSettings : public BLEMIDI_NAMESPACE::BLEDefaultSettings
{
//See all options and them explanation in the library.

/*
##### BLE DEVICE NAME #####
*/
//static constexpr char *name = (char*)"BleMidiClient";
/*
###### TX POWER #####
*/
//static const int8_t clientTXPwr = 9; // in dBm
/*
###### SECURITY #####
*/
//static const uint8_t clientSecurityCapabilities = BLE_HS_IO_NO_INPUT_OUTPUT;
//static const bool clientBond = true;
//static const bool clientMITM = false;
//static const bool clientPair = true;
//static constexpr PasskeyRequestCallback userOnPassKeyRequest = customPasskeyRequest;
/*
###### BLE COMMUNICATION PARAMS ######
*/
//static const uint16_t commMinInterval = 6; // 7.5ms
//static const uint16_t commMaxInterval = 35; // 40ms
//static const uint16_t commLatency = 0; //
//static const uint16_t commTimeOut = 200; // 2000ms
/*
###### BLE FORCE NEW CONNECTION ######
*/
static const bool forceNewConnection = false;
/*
###### BLE SUBSCRIPTION: NOTIFICATION & RESPONSE ######
*/
//static const bool notification = true;
//static const bool response = true;


//BLE-MIDI settings (see BLEMIDI_Settings.h and BLE_class for more details)
static const size_t MaxBufferSize = 16;

// MIDI settings (see MIDI_Settings.h)
static const int Use1ByteParsing = true;
static const bool HandleNullVelocityNoteOnAsNoteOff = true;
};

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

//BLEMIDI_CREATE_INSTANCE("",MIDI) //Connect to the first server found, using default settings
//BLEMIDI_CREATE_INSTANCE("f2:c1:d9:36:e7:6b",MIDI) //Connect to a specific BLE address server, using default settings
Expand Down
4 changes: 3 additions & 1 deletion src/BLEMIDI_Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

BEGIN_BLEMIDI_NAMESPACE

struct DefaultSettings
//Common settings for all BLEMIDI classes
//This is the default settings for all BLEMIDI classes
struct CommonBLEDefaultSettings : MIDI_NAMESPACE::DefaultSettings
{
static const short MaxBufferSize = 64;
};
Expand Down
21 changes: 1 addition & 20 deletions src/BLEMIDI_Transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static const char *const CHARACTERISTIC_UUID = "7772e5db-3868-4112-a1a9-f2669d10

#define MIDI_TYPE 0x80

template <class T, class _Settings = DefaultSettings>
template <class T, class _Settings /*= CommonBLEDefaultSettings*/>
class BLEMIDI_Transport
{
private:
Expand Down Expand Up @@ -183,9 +183,7 @@ class BLEMIDI_Transport
public:
// callbacks
void (*_connectedCallback)() = nullptr;
void (*_connectedCallbackDeviceName)(char *) = nullptr;
void (*_disconnectedCallback)() = nullptr;
void (*_connectedCallbackDeviceName)(char *) = nullptr;

BLEMIDI_Transport &setName(const char *deviceName)
{
Expand All @@ -199,19 +197,7 @@ class BLEMIDI_Transport
_connectedCallback = fptr;
return *this;
}

BLEMIDI_Transport &setHandleConnected(void (*fptr)(char*))
{
_connectedCallbackDeviceName= fptr;
return *this;
}

BLEMIDI_Transport &setHandleConnected(void (*fptr)(char*))
{
_connectedCallbackDeviceName= fptr;
return *this;
}

BLEMIDI_Transport &setHandleDisconnected(void (*fptr)())
{
_disconnectedCallback = fptr;
Expand Down Expand Up @@ -423,9 +409,4 @@ class BLEMIDI_Transport
}
};

struct MySettings : public MIDI_NAMESPACE::DefaultSettings
{
static const bool Use1ByteParsing = false;
};

END_BLEMIDI_NAMESPACE
84 changes: 31 additions & 53 deletions src/hardware/BLEMIDI_ArduinoBLE.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#pragma once

#include <ArduinoBLE.h>
#include "BLEMIDI_Namespace.h"
#include "BLEMIDI_Settings.h"

#define BLE_POLLING

BEGIN_BLEMIDI_NAMESPACE

// Dependanced class settings
struct BLEDefaultSettings : public CommonBLEDefaultSettings
{
//TODO Create parametric configurations
};

BLEService midiService(SERVICE_UUID);

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

Fifo<byte, _Settings::MaxBufferSize> mRxBuffer;

template <class> friend class MyServerCallbacks;
static BLEMIDI_ArduinoBLE<_Settings>* self;

public:
BLEMIDI_ArduinoBLE() : _midiService(SERVICE_UUID),
_midiChar(CHARACTERISTIC_UUID, BLERead | BLEWrite | BLENotify | BLEWriteWithoutResponse, _Settings::MaxBufferSize)
{
self = this;
}

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

end();
}
};

template <class _Settings>
class MyServerCallbacks : public BLEDeviceCallbacks
{
public:
MyServerCallbacks(BLEMIDI_ArduinoBLE<_Settings> *bluetooth)
: _bluetooth(bluetooth)
{
}

protected:
BLEMIDI_ArduinoBLE<_Settings> *_bluetooth = nullptr;

void onConnect(BLEDevice device)
{
if (_bluetooth)
_bluetooth->connected();
};

void onDisconnect(BLEDevice device)
{
if (_bluetooth)
_bluetooth->disconnected();
}
};

template <class _Settings>
class MyCharacteristicCallbacks : public BLECharacteristicCallbacks
{
public:
MyCharacteristicCallbacks(BLEMIDI_ArduinoBLE<_Settings> *bluetooth)
: _bluetooth(bluetooth)
{
static void blePeripheralConnectedHandler(BLEDevice central) {
self->connected();
}

protected:
BLEMIDI_ArduinoBLE<_Settings> *_bluetooth = nullptr;

void onWrite(BLECharacteristic characteristic)
{
// std::string rxValue = characteristic->getValue();
// if (rxValue.length() > 0)
// {
// _bluetooth->receive((uint8_t *)(rxValue.c_str()), rxValue.length());
//}

static void blePeripheralDisconnectedHandler(BLEDevice central) {
self->disconnected();
}

void onRead(BLECharacteristic characteristic)
{

static void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
// std::string rxValue = characteristic->value();
// if (rxValue.length() > 0)
// receive((uint8_t *)(rxValue.c_str()), rxValue.length());
}
};

// this pattern of static to instance variable is limited to 1 instance of ArduinoBLE
// for multiple instances, turn this into a vector and lookup
template <class _Settings> BLEMIDI_ArduinoBLE<_Settings>* BLEMIDI_ArduinoBLE<_Settings>::self;

template <class _Settings>
bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_ArduinoBLE<_Settings>, _Settings> *bleMidiTransport)
{
Expand All @@ -207,9 +183,10 @@ bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transp
_midiService.addCharacteristic(_midiChar);
BLE.addService(_midiService);

BLE.setCallbacks(new MyServerCallbacks<_Settings>(this));
BLE.setEventHandler(BLEConnected, blePeripheralConnectedHandler);
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectedHandler);

_midiChar.setCallbacks(new MyCharacteristicCallbacks<_Settings>(this));
_midiChar.setEventHandler(BLEWritten, switchCharacteristicWritten);

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


/*! \brief Create an instance for ArduinoBLE <DeviceName>
*/
#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ArduinoBLE<_Settings>, _Settings> BLE##Name(DeviceName); \
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);
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);

/*! \brief Create an instance for ArduinoBLE <DeviceName>
*/
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettings);
BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, BLEMIDI_NAMESPACE::BLEDefaultSettings);

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

END_BLEMIDI_NAMESPACE
END_BLEMIDI_NAMESPACE
Loading