Compare commits

...

16 Commits

Author SHA1 Message Date
lathoub 6d168da918 Update BLEMIDI_ArduinoBLE.h 2022-11-08 09:03:06 +01:00
lathoub 75e994c95d working on nRF52 2022-06-05 14:11:40 +02:00
lathoub 3c789b0b5f removed all event code
cleanup - removed all polling code
2022-06-05 10:26:47 +02:00
lathoub edd2f21a39 Update BLEMIDI_ArduinoBLE.h
added :     _midiChar.writeValue((uint8_t)0);

otherwise the device will disconnect immediately
2022-06-04 22:34:11 +02:00
lathoub 8c605fea37 Update BLEMIDI_Transport.h
removed logging
2022-06-04 22:33:24 +02:00
lathoub c8334f6b02 rewrote non-customer constructors 2022-06-04 15:09:46 +02:00
lathoub 8536bc4ddf Update BLEMIDI_ESP32.h 2022-06-02 15:59:22 +02:00
lathoub b91186d6a1 added BLE Client template arguments
modig
2022-06-02 15:59:14 +02:00
lathoub 948fc1165f added template params 2022-06-01 23:44:24 +02:00
lathoub 9c0e0e0627 Update CustomerBufferSize.ino 2022-06-01 08:32:02 +02:00
lathoub ab44659ce3 changed template arg to _Setting 2022-06-01 08:30:12 +02:00
lathoub 1ae439d76c tmp simplified template arg to int 2022-06-01 08:14:45 +02:00
lathoub 2f9c684140 Update CustomerBufferSize.ino 2022-05-31 23:08:38 +02:00
lathoub 13b50f32ee Update CustomerBufferSize.ino 2022-05-31 23:04:27 +02:00
lathoub 50aaaf3872 Update CustomerBufferSize.ino 2022-05-31 23:02:44 +02:00
lathoub 12c1e135d4 first commit for customSettings 2022-05-31 23:01:42 +02:00
12 changed files with 356 additions and 238 deletions

View File

@ -0,0 +1,64 @@
#include <BLEMIDI_Transport.h>
struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings {
static const size_t MaxBufferSize = 16;
};
#include <hardware/BLEMIDI_ESP32_NimBLE.h>
//#include <hardware/BLEMIDI_ESP32.h>
//#include <hardware/BLEMIDI_ArduinoBLE.h>
BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-NimBLE-MIDI", MIDI, CustomBufferSizeSettings);
unsigned long t0 = millis();
bool isConnected = false;
// -----------------------------------------------------------------------------
// When BLE connected, LED will turn on (indication that connection was successful)
// When receiving a NoteOn, LED will go out, on NoteOff, light comes back on.
// This is an easy and conveniant way to show that the connection is alive and working.
// -----------------------------------------------------------------------------
void setup()
{
Serial.begin(115200);
while (!Serial) {}
Serial.println("booting");
MIDI.begin();
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
BLEMIDI.setHandleConnected([]() {
isConnected = true;
digitalWrite(LED_BUILTIN, HIGH);
});
BLEMIDI.setHandleDisconnected([]() {
isConnected = false;
digitalWrite(LED_BUILTIN, LOW);
});
MIDI.setHandleNoteOn([](byte channel, byte note, byte velocity) {
digitalWrite(LED_BUILTIN, LOW);
});
MIDI.setHandleNoteOff([](byte channel, byte note, byte velocity) {
digitalWrite(LED_BUILTIN, HIGH);
});
}
// -----------------------------------------------------------------------------
//
// -----------------------------------------------------------------------------
void loop()
{
MIDI.read();
if (isConnected && (millis() - t0) > 1000)
{
t0 = millis();
MIDI.sendNoteOn (60, 100, 1); // note 60, velocity 100 on channel 1
}
}

View File

@ -2,7 +2,6 @@
//#include <hardware/BLEMIDI_ESP32_NimBLE.h>
#include <hardware/BLEMIDI_ESP32.h>
//#include <hardware/BLEMIDI_nRF52.h>
//#include <hardware/BLEMIDI_ArduinoBLE.h>
BLEMIDI_CREATE_DEFAULT_INSTANCE()

View File

@ -28,14 +28,17 @@
#include <Arduino.h>
#include <BLEMIDI_Transport.h>
struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings {
static const size_t MaxBufferSize = 16;
};
#include <hardware/BLEMIDI_Client_ESP32.h>
//#include <hardware/BLEMIDI_ESP32_NimBLE.h>
//#include <hardware/BLEMIDI_ESP32.h>
//#include <hardware/BLEMIDI_nRF52.h>
//#include <hardware/BLEMIDI_ArduinoBLE.h>
BLEMIDI_CREATE_DEFAULT_INSTANCE(); //Connect to first server found
BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-BLE-MIDI", MIDI, CustomBufferSizeSettings); // Connect to first server found
//BLEMIDI_CREATE_INSTANCE("",MIDI) //Connect to the first server found
//BLEMIDI_CREATE_INSTANCE("f2:c1:d9:36:e7:6b",MIDI) //Connect to a specific BLE address server

View File

@ -2,7 +2,6 @@
//#include <hardware/BLEMIDI_ESP32_NimBLE.h>
#include <hardware/BLEMIDI_ESP32.h>
//#include <hardware/BLEMIDI_nRF52.h>
//#include <hardware/BLEMIDI_ArduinoBLE.h>
BLEMIDI_CREATE_INSTANCE("CustomName", MIDI)

View File

@ -2,7 +2,6 @@
#include <hardware/BLEMIDI_ESP32_NimBLE.h>
//#include <hardware/BLEMIDI_ESP32.h>
//#include <hardware/BLEMIDI_nRF52.h>
//#include <hardware/BLEMIDI_ArduinoBLE.h>
byte sysex4[] = { 0xF0, 0x43, 0x20, 0xF7 };

View File

@ -6,7 +6,7 @@ BEGIN_BLEMIDI_NAMESPACE
struct DefaultSettings
{
static const size_t MaxBufferSize = 64;
static const short MaxBufferSize = 64;
};
END_BLEMIDI_NAMESPACE

View File

@ -26,13 +26,11 @@ static const char *const CHARACTERISTIC_UUID = "7772e5db-3868-4112-a1a9-f2669d10
template <class T, class _Settings = DefaultSettings>
class BLEMIDI_Transport
{
typedef _Settings Settings;
private:
byte mRxBuffer[Settings::MaxBufferSize];
byte mRxBuffer[_Settings::MaxBufferSize];
unsigned mRxIndex = 0;
byte mTxBuffer[Settings::MaxBufferSize]; // minimum 5 bytes
byte mTxBuffer[_Settings::MaxBufferSize]; // minimum 5 bytes
unsigned mTxIndex = 0;
char mDeviceName[24];

View File

@ -2,56 +2,51 @@
#include <ArduinoBLE.h>
BLEService midiService(SERVICE_UUID);
BLEStringCharacteristic midiChar(CHARACTERISTIC_UUID, // standard 16-bit characteristic UUID
BLERead | BLEWrite | BLENotify | BLEWriteWithoutResponse, 16); // remote clients will be able to get notifications if this characteristic changes
#define BLE_POLLING
BEGIN_BLEMIDI_NAMESPACE
template<typename T, int rawSize>
class Fifo {
template <typename T, short rawSize>
class Fifo
{
public:
const size_t size; //speculative feature, in case it's needed
const size_t size; // speculative feature, in case it's needed
Fifo(): size(rawSize)
Fifo() : size(rawSize)
{
flush();
flush();
}
T dequeue()
T dequeue()
{
numberOfElements--;
nextOut %= size;
return raw[ nextOut++];
return raw[nextOut++];
};
bool enqueue( T element )
bool enqueue(T element)
{
if ( count() >= rawSize )
if (count() >= rawSize)
return false;
numberOfElements++;
nextIn %= size;
raw[nextIn] = element;
nextIn++; //advance to next index
nextIn++; // advance to next index
return true;
};
T peek() const
{
return raw[ nextOut % size];
return raw[nextOut % size];
}
void flush()
{
nextIn = nextOut = numberOfElements = 0;
nextIn = nextOut = numberOfElements = 0;
}
// how many elements are currently in the FIFO?
size_t count() { return numberOfElements; }
// how many elements are currently in the FIFO?
size_t count() { return numberOfElements; }
private:
size_t numberOfElements;
@ -60,60 +55,56 @@ private:
T raw[rawSize];
};
template <class _Settings>
class BLEMIDI_ArduinoBLE
{
private:
static BLEMIDI_Transport<class BLEMIDI_ArduinoBLE>* _bleMidiTransport;
static BLEDevice* _central;
private:
BLEMIDI_Transport<class BLEMIDI_ArduinoBLE<_Settings>, _Settings> *_bleMidiTransport;
BLEDevice *_central;
Fifo<byte, 64> mRxBuffer;
BLEService _midiService;
BLECharacteristic _midiChar;
Fifo<byte, _Settings::MaxBufferSize> mRxBuffer;
template <class> friend class MyServerCallbacks;
public:
BLEMIDI_ArduinoBLE()
BLEMIDI_ArduinoBLE() : _midiService(SERVICE_UUID),
_midiChar(CHARACTERISTIC_UUID, BLERead | BLEWrite | BLENotify | BLEWriteWithoutResponse, _Settings::MaxBufferSize)
{
}
bool begin(const char*, BLEMIDI_Transport<class BLEMIDI_ArduinoBLE>*);
void end()
{
}
void write(uint8_t* buffer, size_t length)
{
// TODO: test length
((BLECharacteristic)midiChar).writeValue(buffer, length);
}
bool available(byte* pvBuffer)
{
#ifdef BLE_POLLING
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_ArduinoBLE<_Settings>, _Settings> *);
if (mRxBuffer.count() > 0) {
void end()
{
}
void write(uint8_t *buffer, size_t length)
{
if (length > 0)
((BLECharacteristic)_midiChar).writeValue(buffer, length);
}
bool available(byte *pvBuffer)
{
if (mRxBuffer.count() > 0)
{
*pvBuffer = mRxBuffer.dequeue();
return true;
}
poll();
if (midiChar.written()) {
// auto buffer = midiChar.value();
auto length = midiChar.valueLength();
if (length > 0) {
auto buffer = midiChar.value().c_str();
_bleMidiTransport->receive((byte*)buffer, length);
if (_midiChar.written())
{
auto length = _midiChar.valueLength();
if (length > 0)
{
auto buffer = _midiChar.value();
_bleMidiTransport->receive((byte *)buffer, length);
}
}
return false;
#endif
#ifdef BLE_EVENTS
/ BLE.poll();
return ; // ??
#endif
}
void add(byte value)
@ -123,109 +114,123 @@ public:
}
protected:
static void receive(const unsigned char* buffer, size_t length)
{
// forward the buffer so it can be parsed
_bleMidiTransport->receive((uint8_t*)buffer, length);
}
bool poll()
{
BLEDevice central = BLE.central();
if (!central) {
if (_central) {
BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler(*_central);
_central = nullptr;
}
return false;
}
if (!central.connected()) {
return false;
}
if (nullptr == _central) {
BLEMIDI_ArduinoBLE::blePeripheralConnectHandler(central);
_central = &central;
}
else {
if (*_central != central) {
BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler(*_central);
BLEMIDI_ArduinoBLE::blePeripheralConnectHandler(central);
_central = &central;
}
}
return true;
void receive(const unsigned char *buffer, size_t length)
{
if (length > 0)
_bleMidiTransport->receive((uint8_t *)buffer, length);
}
static void blePeripheralConnectHandler(BLEDevice central)
{
_central = &central;
if (_bleMidiTransport->_connectedCallback)
_bleMidiTransport->_connectedCallback();
}
void connected()
{
if (_bleMidiTransport->_connectedCallback)
_bleMidiTransport->_connectedCallback();
}
static void blePeripheralDisconnectHandler(BLEDevice central)
{
if (_bleMidiTransport->_disconnectedCallback)
_bleMidiTransport->_disconnectedCallback();
void disconnected()
{
if (_bleMidiTransport->_disconnectedCallback)
_bleMidiTransport->_disconnectedCallback();
_central = nullptr;
}
static void characteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
auto buffer = characteristic.value();
auto length = characteristic.valueLength();
if (length > 0)
receive(buffer, length);
end();
}
};
BLEMIDI_Transport<class BLEMIDI_ArduinoBLE>* BLEMIDI_ArduinoBLE::_bleMidiTransport = nullptr;
BLEDevice* BLEMIDI_ArduinoBLE::_central = nullptr;
bool BLEMIDI_ArduinoBLE::begin(const char* deviceName, BLEMIDI_Transport<class BLEMIDI_ArduinoBLE>* bleMidiTransport)
template <class _Settings>
class MyServerCallbacks : public BLEDeviceCallbacks
{
_bleMidiTransport = bleMidiTransport;
public:
MyServerCallbacks(BLEMIDI_ArduinoBLE<_Settings> *bluetooth)
: _bluetooth(bluetooth)
{
}
if (!BLE.begin())
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)
{
}
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());
//}
}
void onRead(BLECharacteristic characteristic)
{
}
};
template <class _Settings>
bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_ArduinoBLE<_Settings>, _Settings> *bleMidiTransport)
{
_bleMidiTransport = bleMidiTransport;
// initialize the Bluetooth® Low Energy hardware
if (!BLE.begin())
return false;
BLE.setLocalName(deviceName);
BLE.setAdvertisedService(midiService);
midiService.addCharacteristic(midiChar);
BLE.addService(midiService);
BLE.setAdvertisedService(_midiService);
_midiService.addCharacteristic(_midiChar);
BLE.addService(_midiService);
#ifdef BLE_EVENTS
// assign event handlers for connected, disconnected to peripheral
BLE.setEventHandler(BLEConnected, BLEMIDI_ArduinoBLE::blePeripheralConnectHandler);
BLE.setEventHandler(BLEDisconnected, BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler);
BLE.setCallbacks(new MyServerCallbacks<_Settings>(this));
midiChar.setEventHandler(BLEWritten, characteristicWritten);
#endif
_midiChar.setCallbacks(new MyCharacteristicCallbacks<_Settings>(this));
/* Start advertising BLE. It will start continuously transmitting BLE
advertising packets and will be visible to remote BLE central devices
until it receives a new connection */
// set the initial value for the characeristic:
// (when not set, the device will disconnect after 0.5 seconds)
_midiChar.writeValue((uint8_t)0); // TODO: might not be needed, check!!
// start advertising
/* Start advertising BLE. It will start continuously transmitting BLE
advertising packets and will be visible to remote BLE central devices
until it receives a new connection */
BLE.advertise();
return true;
}
/*! \brief Create an instance for nRF52 named <DeviceName>
/*! \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);
/*! \brief Create an instance for ArduinoBLE <DeviceName>
*/
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ArduinoBLE> BLE##Name(DeviceName); \
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ArduinoBLE>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ArduinoBLE> &)BLE##Name);
BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettings)
/*! \brief Create a default instance for nRF52 (Nano 33 BLE) named BLE-MIDI
/*! \brief Create a default instance for ArduinoBLE named BLE-MIDI
*/
#define BLEMIDI_CREATE_DEFAULT_INSTANCE() \
BLEMIDI_CREATE_INSTANCE("BLE-MIDI", MIDI)
BLEMIDI_CREATE_INSTANCE("BLE-MIDI", MIDI)
END_BLEMIDI_NAMESPACE

View File

@ -174,6 +174,7 @@ BEGIN_BLEMIDI_NAMESPACE
#define BLEMIDI_CLIENT_SECURITY_AUTH (BLEMIDI_CLIENT_BOND_DUMMY | BLEMIDI_CLIENT_MITM_DUMMY | BLEMIDI_CLIENT_PAIR_DUMMY)
/** Define a class to handle the callbacks when advertisments are received */
template <class _Settings>
class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks
{
public:
@ -220,6 +221,7 @@ protected:
void scanEndedCB(NimBLEScanResults results);
/** Define the class that performs Client Midi (nimBLE) */
template <class _Settings>
class BLEMIDI_Client_ESP32
{
private:
@ -229,15 +231,14 @@ private:
BLERemoteService *pSvc = nullptr;
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.
BLEMIDI_Transport<class BLEMIDI_Client_ESP32> *_bleMidiTransport = nullptr;
BLEMIDI_Transport<class BLEMIDI_Client_ESP32<_Settings>, _Settings> *_bleMidiTransport = nullptr;
bool specificTarget = false;
friend class AdvertisedDeviceCallbacks;
friend class MyClientCallbacks;
friend class MIDI_NAMESPACE::MidiInterface<BLEMIDI_Transport<BLEMIDI_Client_ESP32>, MySettings>; //
// TODO: somehow the forward declaration of the template class is not accepted by the compiler
// template <class> friend MyClientCallbacks;
AdvertisedDeviceCallbacks myAdvCB;
AdvertisedDeviceCallbacks<_Settings> myAdvCB;
protected:
QueueHandle_t mRxQueue;
@ -247,7 +248,7 @@ public:
{
}
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_Client_ESP32> *);
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_Client_ESP32<_Settings>, _Settings> *);
bool end()
{
@ -294,61 +295,55 @@ protected:
_bleMidiTransport->receive(buffer, length);
}
void connectCallbacks(MIDI_NAMESPACE::MidiInterface<BLEMIDI_Transport<BLEMIDI_Client_ESP32>, MySettings> *MIDIcallback);
void notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify);
void scan();
bool connect();
public: // TODO: somehow the forward declaration of the template class is not accepted by the compiler
void connected()
{
if (_bleMidiTransport->_connectedCallback)
{
_bleMidiTransport->_connectedCallback();
}
firstTimeSend = true;
}
void disconnected()
{
if (_bleMidiTransport->_disconnectedCallback)
{
_bleMidiTransport->_disconnectedCallback();
}
firstTimeSend = true;
}
void notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify);
void scan();
bool connect();
};
/** Define the class that performs interruption callbacks */
template <class _Settings>
class MyClientCallbacks : public BLEClientCallbacks
{
public:
MyClientCallbacks(BLEMIDI_Client_ESP32 *bluetoothEsp32)
MyClientCallbacks(BLEMIDI_Client_ESP32<_Settings> *bluetoothEsp32)
: _bluetoothEsp32(bluetoothEsp32)
{
}
protected:
BLEMIDI_Client_ESP32 *_bluetoothEsp32 = nullptr;
BLEMIDI_Client_ESP32<_Settings> *_bluetoothEsp32 = nullptr;
uint32_t onPassKeyRequest()
{
return userOnPassKeyRequest();
};
void onConnect(BLEClient *pClient)
void onConnect(BLEClient*)
{
//Serial.println("##Connected##");
//pClient->updateConnParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT);
vTaskDelay(1);
if (_bluetoothEsp32)
{
_bluetoothEsp32->connected();
}
};
void onDisconnect(BLEClient *pClient)
void onDisconnect(BLEClient*)
{
//Serial.print(pClient->getPeerAddress().toString().c_str());
//Serial.println(" Disconnected - Starting scan");
@ -403,7 +398,8 @@ protected:
##########################################
*/
bool BLEMIDI_Client_ESP32::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_Client_ESP32> *bleMidiTransport)
template <class _Settings>
bool BLEMIDI_Client_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_Client_ESP32<_Settings>, _Settings> *bleMidiTransport)
{
_bleMidiTransport = bleMidiTransport;
@ -450,7 +446,8 @@ bool BLEMIDI_Client_ESP32::begin(const char *deviceName, BLEMIDI_Transport<class
return true;
}
bool BLEMIDI_Client_ESP32::available(byte *pvBuffer)
template <class _Settings>
bool BLEMIDI_Client_ESP32<_Settings>::available(byte *pvBuffer)
{
if (myAdvCB.enableConnection)
{
@ -479,7 +476,8 @@ bool BLEMIDI_Client_ESP32::available(byte *pvBuffer)
}
/** Notification receiving handler callback */
void BLEMIDI_Client_ESP32::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify)
template <class _Settings>
void BLEMIDI_Client_ESP32<_Settings>::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify)
{
if (this->_characteristic == pRemoteCharacteristic) //Redundant protection
{
@ -487,7 +485,8 @@ void BLEMIDI_Client_ESP32::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacter
}
}
void BLEMIDI_Client_ESP32::scan()
template <class _Settings>
void BLEMIDI_Client_ESP32<_Settings>::scan()
{
// Retrieve a Scanner and set the callback you want to use to be informed when a new device is detected.
// Specify that you want active scanning and start the
@ -506,7 +505,8 @@ void BLEMIDI_Client_ESP32::scan()
}
};
bool BLEMIDI_Client_ESP32::connect()
template <class _Settings>
bool BLEMIDI_Client_ESP32<_Settings>::connect()
{
using namespace std::placeholders; //<- for bind funtion in callback notification
@ -553,7 +553,7 @@ bool BLEMIDI_Client_ESP32::connect()
// Create and setup a new client
_client = BLEDevice::createClient();
_client->setClientCallbacks(new MyClientCallbacks(this), false);
_client->setClientCallbacks(new MyClientCallbacks<_Settings>(this), false);
_client->setConnectionParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT);
@ -622,12 +622,18 @@ void scanEndedCB(NimBLEScanResults results)
END_BLEMIDI_NAMESPACE
/*! \brief Create an instance for ESP32 named <DeviceName>, and adviertise it like "Prefix + <DeviceName> + Subfix"
/*! \brief Create a custom instance for ESP32 named <DeviceName>, and advertise it like "Prefix + <DeviceName> + Subfix"
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
*/
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_Client_ESP32> BLE##Name(DeviceName); \
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_Client_ESP32>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_Client_ESP32> &)BLE##Name);
#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_Client_ESP32<_Settings>, _Settings> BLE##Name(DeviceName); \
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);
/*! \brief Create an instance for ESP32 named <DeviceName>, and advertise it like "Prefix + <DeviceName> + Subfix"
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
*/
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
BLEMIDI_CREATE_CUSTOM_INSTANCE (DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettings)
/*! \brief Create a default instance for ESP32 named BLEMIDI-CLIENT.
It will try to connect to first midi ble server found.

View File

@ -8,6 +8,7 @@
BEGIN_BLEMIDI_NAMESPACE
template <class _Settings>
class BLEMIDI_ESP32
{
private:
@ -15,10 +16,10 @@ private:
BLEAdvertising *_advertising = nullptr;
BLECharacteristic *_characteristic = nullptr;
BLEMIDI_Transport<class BLEMIDI_ESP32> *_bleMidiTransport = nullptr;
BLEMIDI_Transport<class BLEMIDI_ESP32<_Settings>, _Settings>* _bleMidiTransport = nullptr;
friend class MyServerCallbacks;
friend class MyCharacteristicCallbacks;
template <class> friend class MyServerCallbacks;
template <class> friend class MyCharacteristicCallbacks;
protected:
QueueHandle_t mRxQueue;
@ -28,7 +29,7 @@ public:
{
}
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_ESP32> *);
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_ESP32<_Settings>, _Settings> *);
void end()
{
@ -74,16 +75,17 @@ protected:
}
};
template <class _Settings>
class MyServerCallbacks : public BLEServerCallbacks
{
public:
MyServerCallbacks(BLEMIDI_ESP32 *bluetoothEsp32)
MyServerCallbacks(BLEMIDI_ESP32<_Settings> *bluetoothEsp32)
: _bluetoothEsp32(bluetoothEsp32)
{
}
protected:
BLEMIDI_ESP32 *_bluetoothEsp32 = nullptr;
BLEMIDI_ESP32<_Settings> *_bluetoothEsp32 = nullptr;
void onConnect(BLEServer *)
{
@ -100,16 +102,17 @@ protected:
}
};
template <class _Settings>
class MyCharacteristicCallbacks : public BLECharacteristicCallbacks
{
public:
MyCharacteristicCallbacks(BLEMIDI_ESP32 *bluetoothEsp32)
MyCharacteristicCallbacks(BLEMIDI_ESP32<_Settings> *bluetoothEsp32)
: _bluetoothEsp32(bluetoothEsp32)
{
}
protected:
BLEMIDI_ESP32 *_bluetoothEsp32 = nullptr;
BLEMIDI_ESP32<_Settings> *_bluetoothEsp32 = nullptr;
void onWrite(BLECharacteristic *characteristic)
{
@ -121,7 +124,8 @@ protected:
}
};
bool BLEMIDI_ESP32::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_ESP32> *bleMidiTransport)
template <class _Settings>
bool BLEMIDI_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_ESP32<_Settings>, _Settings> *bleMidiTransport)
{
_bleMidiTransport = bleMidiTransport;
@ -129,10 +133,10 @@ bool BLEMIDI_ESP32::begin(const char *deviceName, BLEMIDI_Transport<class BLEMID
// To communicate between the 2 cores.
// Core_0 runs here, core_1 runs the BLE stack
mRxQueue = xQueueCreate(64, sizeof(uint8_t)); // TODO Settings::MaxBufferSize
mRxQueue = xQueueCreate(_Settings::MaxBufferSize, sizeof(uint8_t));
_server = BLEDevice::createServer();
_server->setCallbacks(new MyServerCallbacks(this));
_server->setCallbacks(new MyServerCallbacks<_Settings>(this));
// Create the BLE Service
auto service = _server->createService(BLEUUID(SERVICE_UUID));
@ -147,7 +151,7 @@ bool BLEMIDI_ESP32::begin(const char *deviceName, BLEMIDI_Transport<class BLEMID
// Add CCCD 0x2902 to allow notify
_characteristic->addDescriptor(new BLE2902());
_characteristic->setCallbacks(new MyCharacteristicCallbacks(this));
_characteristic->setCallbacks(new MyCharacteristicCallbacks<_Settings>(this));
auto _security = new BLESecurity();
_security->setAuthenticationMode(ESP_LE_AUTH_BOND);
@ -161,16 +165,19 @@ bool BLEMIDI_ESP32::begin(const char *deviceName, BLEMIDI_Transport<class BLEMID
_advertising->setAppearance(0x00);
_advertising->start();
Serial.println("begin");
return true;
}
/*! \brief Create a customer instance for ESP32 named <DeviceName>
*/
#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32<_Settings>, _Settings> BLE##Name(DeviceName); \
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32<_Settings>, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32<_Settings>, _Settings> &)BLE##Name);
/*! \brief Create an instance for ESP32 named <DeviceName>
*/
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32> BLE##Name(DeviceName); \
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32> &)BLE##Name);
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
BLEMIDI_CREATE_CUSTOM_INSTANCE (DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettings)
/*! \brief Create a default instance for ESP32 named BLE-MIDI
*/

View File

@ -5,6 +5,7 @@
BEGIN_BLEMIDI_NAMESPACE
template <class _Settings>
class BLEMIDI_ESP32_NimBLE
{
private:
@ -12,10 +13,10 @@ private:
BLEAdvertising *_advertising = nullptr;
BLECharacteristic *_characteristic = nullptr;
BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE> *_bleMidiTransport = nullptr;
BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE<_Settings>, _Settings> *_bleMidiTransport = nullptr;
friend class MyServerCallbacks;
friend class MyCharacteristicCallbacks;
template <class> friend class MyServerCallbacks;
template <class> friend class MyCharacteristicCallbacks;
protected:
QueueHandle_t mRxQueue;
@ -25,7 +26,7 @@ public:
{
}
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE> *);
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE<_Settings>, _Settings> *);
void end()
{
@ -69,16 +70,17 @@ protected:
}
};
template <class _Settings>
class MyServerCallbacks : public BLEServerCallbacks
{
public:
MyServerCallbacks(BLEMIDI_ESP32_NimBLE *bluetoothEsp32)
MyServerCallbacks(BLEMIDI_ESP32_NimBLE<_Settings> *bluetoothEsp32)
: _bluetoothEsp32(bluetoothEsp32)
{
}
protected:
BLEMIDI_ESP32_NimBLE *_bluetoothEsp32 = nullptr;
BLEMIDI_ESP32_NimBLE<_Settings> *_bluetoothEsp32 = nullptr;
void onConnect(BLEServer *)
{
@ -93,16 +95,17 @@ protected:
}
};
template <class _Settings>
class MyCharacteristicCallbacks : public BLECharacteristicCallbacks
{
public:
MyCharacteristicCallbacks(BLEMIDI_ESP32_NimBLE *bluetoothEsp32)
MyCharacteristicCallbacks(BLEMIDI_ESP32_NimBLE<_Settings> *bluetoothEsp32)
: _bluetoothEsp32(bluetoothEsp32)
{
}
protected:
BLEMIDI_ESP32_NimBLE *_bluetoothEsp32 = nullptr;
BLEMIDI_ESP32_NimBLE<_Settings> *_bluetoothEsp32 = nullptr;
void onWrite(BLECharacteristic *characteristic)
{
@ -114,7 +117,8 @@ protected:
}
};
bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE> *bleMidiTransport)
template <class _Settings>
bool BLEMIDI_ESP32_NimBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE<_Settings>, _Settings> *bleMidiTransport)
{
_bleMidiTransport = bleMidiTransport;
@ -122,10 +126,10 @@ bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport<class
// To communicate between the 2 cores.
// Core_0 runs here, core_1 runs the BLE stack
mRxQueue = xQueueCreate(64, sizeof(uint8_t)); // TODO Settings::MaxBufferSize
mRxQueue = xQueueCreate(_Settings::MaxBufferSize, sizeof(uint8_t));
_server = BLEDevice::createServer();
_server->setCallbacks(new MyServerCallbacks(this));
_server->setCallbacks(new MyServerCallbacks<_Settings>(this));
_server->advertiseOnDisconnect(true);
// Create the BLE Service
@ -139,7 +143,7 @@ bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport<class
NIMBLE_PROPERTY::NOTIFY |
NIMBLE_PROPERTY::WRITE_NR);
_characteristic->setCallbacks(new MyCharacteristicCallbacks(this));
_characteristic->setCallbacks(new MyCharacteristicCallbacks<_Settings>(this));
auto _security = new NimBLESecurity();
_security->setAuthenticationMode(ESP_LE_AUTH_BOND);
@ -158,9 +162,14 @@ bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport<class
/*! \brief Create an instance for ESP32 named <DeviceName>
*/
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE> BLE##Name(DeviceName); \
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE> &)BLE##Name);
#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE<_Settings>, _Settings> BLE##Name(DeviceName); \
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE<_Settings>, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE<_Settings>, _Settings> &)BLE##Name);
/*! \brief Create an instance for ESP32 named <DeviceName>
*/
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
BLEMIDI_CREATE_CUSTOM_INSTANCE (DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettings)
/*! \brief Create a default instance for ESP32 named BLE-MIDI
*/

View File

@ -2,27 +2,28 @@
// I N D E V E L O P M E N T
//#include <bluefruit.h>
#include <bluefruit.h>
BEGIN_BLEMIDI_NAMESPACE
template <class _Settings>
class BLEMIDI_nRF52
{
private:
// BLEDis bledis;
// BLEMidi blemidi;
BLEDis bledis;
// BLEMidi blemidi;
BLEMIDI_NAMESPACE::BLEMIDI_Transport<class BLEMIDI_nRF52>* _bleMidiTransport;
BLEMIDI_NAMESPACE::BLEMIDI_Transport<class BLEMIDI_nRF52<_Settings>, _Settings>* _bleMidiTransport;
friend class MyServerCallbacks;
friend class MyCharacteristicCallbacks;
// template <class> friend class MyServerCallbacks;
// template <class> friend class MyCharacteristicCallbacks;
public:
BLEMIDI_nRF52()
{
}
bool begin(const char*, BLEMIDI_NAMESPACE::BLEMIDI_Transport<class BLEMIDI_nRF52>*);
bool begin(const char*, BLEMIDI_NAMESPACE::BLEMIDI_Transport<class BLEMIDI_nRF52<_Settings>, _Settings>*);
void end()
{
@ -61,41 +62,64 @@ protected:
}
};
bool BLEMIDI_nRF52::begin(const char* deviceName, BLEMIDI_NAMESPACE::BLEMIDI_Transport<class BLEMIDI_nRF52>* bleMidiTransport)
void connect_callback(uint16_t conn_handle)
{
Serial.println("Connected");
// Get the reference to current connection
BLEConnection* connection = Bluefruit.Connection(conn_handle);
}
void disconnect_callback(uint16_t conn_handle, uint8_t reason)
{
(void) conn_handle;
(void) reason;
Serial.println();
Serial.print("Disconnected, reason = 0x"); Serial.println(reason, HEX);
}
template <class _Settings>
bool BLEMIDI_nRF52<_Settings>::begin(const char* deviceName, BLEMIDI_NAMESPACE::BLEMIDI_Transport<class BLEMIDI_nRF52<_Settings>, _Settings>* bleMidiTransport)
{
_bleMidiTransport = bleMidiTransport;
// Config the peripheral connection with maximum bandwidth
// more SRAM required by SoftDevice
// Note: All config***() function must be called before begin()
// Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
// Bluefruit.begin();
// Bluefruit.setName(deviceName);
// Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
Bluefruit.begin();
Bluefruit.setName(deviceName);
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
// Setup the on board blue LED to be enabled on CONNECT
// Bluefruit.autoConnLed(true);
Bluefruit.autoConnLed(true);
Bluefruit.Periph.setConnectCallback(connect_callback);
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
// Configure and Start Device Information Service
// bledis.setManufacturer("Adafruit Industries");
// bledis.setModel("Bluefruit Feather52");
// bledis.begin();
bledis.setManufacturer("Adafruit Industries");
bledis.setModel("Bluefruit Feather52");
bledis.begin();
// Start advertising ----------------------------
// Set General Discoverable Mode flag
// Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE);
// Advertise TX Power
// Bluefruit.Advertising.addTxPower();
Bluefruit.Advertising.addTxPower();
// Advertise BLE MIDI Service
// Bluefruit.Advertising.addService(blemidi);
Bluefruit.Advertising.addService(blemidi);
// blemidi.write((uint8_t)0);
// Secondary Scan Response packet (optional)
// Since there is no room for 'Name' in Advertising packet
// Bluefruit.ScanResponse.addName();
Bluefruit.ScanResponse.addName();
/* Start Advertising
* - Enable auto advertising if disconnected
@ -106,23 +130,28 @@ bool BLEMIDI_nRF52::begin(const char* deviceName, BLEMIDI_NAMESPACE::BLEMIDI_Tra
* For recommended advertising interval
* https://developer.apple.com/library/content/qa/qa1931/_index.html
*/
// Bluefruit.Advertising.restartOnDisconnect(true);
// Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
// Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
// Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
return true;
}
/*! \brief Create an instance for nRF52 named <DeviceName>
*/
#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_nRF52<_Settings>, _Settings> BLE##Name(DeviceName); \
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_nRF52<_Settings>, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_nRF52<_Settings>, _Settings> &)BLE##Name);
/*! \brief Create an instance for nRF52 named <DeviceName>
*/
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_nRF52> BLE##Name(DeviceName); \
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_nRF52>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_nRF52> &)BLE##Name);
BLEMIDI_CREATE_CUSTOM_INSTANCE (DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettings)
/*! \brief Create a default instance for nRF52 named BLE-MIDI
*/
#define BLEMIDI_CREATE_DEFAULT_INSTANCE() \
BLEMIDI_CREATE_INSTANCE("nRF85BLE-MIDI", MIDI)
BLEMIDI_CREATE_INSTANCE("nRF85BLE-MIDI", MIDI)
END_BLEMIDI_NAMESPACE