Compare commits

..

2 Commits

Author SHA1 Message Date
lathoub 929c2fc049
Merge pull request #65 from aselectroworks/ESP32-BLE_Client
fix panic'ed bug when call BLEMIDI_Client_ESP32::end() is called
2022-09-19 12:02:41 +02:00
Atsushi Sasaki 8e740fb499 fix panic'ed bug when call BLEMIDI_Client_ESP32::end() is called 2022-09-19 17:38:11 +09:00
12 changed files with 238 additions and 356 deletions

View File

@ -1,64 +0,0 @@
#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,6 +2,7 @@
//#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,17 +28,14 @@
#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_CUSTOM_INSTANCE("Esp32-BLE-MIDI", MIDI, CustomBufferSizeSettings); // Connect to first server found
BLEMIDI_CREATE_DEFAULT_INSTANCE(); //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,6 +2,7 @@
//#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,6 +2,7 @@
#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 short MaxBufferSize = 64;
static const size_t MaxBufferSize = 64;
};
END_BLEMIDI_NAMESPACE

View File

@ -26,11 +26,13 @@ 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,11 +2,16 @@
#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, short rawSize>
class Fifo
{
template<typename T, int rawSize>
class Fifo {
public:
const size_t size; //speculative feature, in case it's needed
@ -55,56 +60,60 @@ private:
T raw[rawSize];
};
template <class _Settings>
class BLEMIDI_ArduinoBLE
{
private:
BLEMIDI_Transport<class BLEMIDI_ArduinoBLE<_Settings>, _Settings> *_bleMidiTransport;
BLEDevice *_central;
static BLEMIDI_Transport<class BLEMIDI_ArduinoBLE>* _bleMidiTransport;
static BLEDevice* _central;
BLEService _midiService;
BLECharacteristic _midiChar;
Fifo<byte, _Settings::MaxBufferSize> mRxBuffer;
template <class> friend class MyServerCallbacks;
Fifo<byte, 64> mRxBuffer;
public:
BLEMIDI_ArduinoBLE() : _midiService(SERVICE_UUID),
_midiChar(CHARACTERISTIC_UUID, BLERead | BLEWrite | BLENotify | BLEWriteWithoutResponse, _Settings::MaxBufferSize)
BLEMIDI_ArduinoBLE()
{
}
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_ArduinoBLE<_Settings>, _Settings> *);
bool begin(const char*, BLEMIDI_Transport<class BLEMIDI_ArduinoBLE>*);
void end()
{
}
void write(uint8_t* buffer, size_t length)
{
if (length > 0)
((BLECharacteristic)_midiChar).writeValue(buffer, length);
// TODO: test length
((BLECharacteristic)midiChar).writeValue(buffer, length);
}
bool available(byte* pvBuffer)
{
if (mRxBuffer.count() > 0)
{
#ifdef BLE_POLLING
if (mRxBuffer.count() > 0) {
*pvBuffer = mRxBuffer.dequeue();
return true;
}
if (_midiChar.written())
{
auto length = _midiChar.valueLength();
if (length > 0)
{
auto buffer = _midiChar.value();
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);
}
}
return false;
#endif
#ifdef BLE_EVENTS
/ BLE.poll();
return ; // ??
#endif
}
void add(byte value)
@ -114,121 +123,107 @@ public:
}
protected:
void receive(const unsigned char *buffer, size_t length)
static void receive(const unsigned char* buffer, size_t length)
{
if (length > 0)
// forward the buffer so it can be parsed
_bleMidiTransport->receive((uint8_t*)buffer, length);
}
void connected()
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;
}
static void blePeripheralConnectHandler(BLEDevice central)
{
_central = &central;
if (_bleMidiTransport->_connectedCallback)
_bleMidiTransport->_connectedCallback();
}
void disconnected()
static void blePeripheralDisconnectHandler(BLEDevice central)
{
if (_bleMidiTransport->_disconnectedCallback)
_bleMidiTransport->_disconnectedCallback();
end();
_central = nullptr;
}
static void characteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
auto buffer = characteristic.value();
auto length = characteristic.valueLength();
if (length > 0)
receive(buffer, length);
}
};
template <class _Settings>
class MyServerCallbacks : public BLEDeviceCallbacks
{
public:
MyServerCallbacks(BLEMIDI_ArduinoBLE<_Settings> *bluetooth)
: _bluetooth(bluetooth)
{
}
BLEMIDI_Transport<class BLEMIDI_ArduinoBLE>* BLEMIDI_ArduinoBLE::_bleMidiTransport = nullptr;
BLEDevice* BLEMIDI_ArduinoBLE::_central = nullptr;
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)
bool BLEMIDI_ArduinoBLE::begin(const char* deviceName, BLEMIDI_Transport<class BLEMIDI_ArduinoBLE>* 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);
BLE.setCallbacks(new MyServerCallbacks<_Settings>(this));
#ifdef BLE_EVENTS
// assign event handlers for connected, disconnected to peripheral
BLE.setEventHandler(BLEConnected, BLEMIDI_ArduinoBLE::blePeripheralConnectHandler);
BLE.setEventHandler(BLEDisconnected, BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler);
_midiChar.setCallbacks(new MyCharacteristicCallbacks<_Settings>(this));
// 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!!
midiChar.setEventHandler(BLEWritten, characteristicWritten);
#endif
/* 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 */
// start advertising
BLE.advertise();
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);
/*! \brief Create an instance for ArduinoBLE <DeviceName>
/*! \brief Create an instance for nRF52 named <DeviceName>
*/
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettings)
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);
/*! \brief Create a default instance for ArduinoBLE named BLE-MIDI
/*! \brief Create a default instance for nRF52 (Nano 33 BLE) named BLE-MIDI
*/
#define BLEMIDI_CREATE_DEFAULT_INSTANCE() \
BLEMIDI_CREATE_INSTANCE("BLE-MIDI", MIDI)

View File

@ -174,7 +174,6 @@ 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:
@ -221,7 +220,6 @@ protected:
void scanEndedCB(NimBLEScanResults results);
/** Define the class that performs Client Midi (nimBLE) */
template <class _Settings>
class BLEMIDI_Client_ESP32
{
private:
@ -231,14 +229,15 @@ 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<_Settings>, _Settings> *_bleMidiTransport = nullptr;
BLEMIDI_Transport<class BLEMIDI_Client_ESP32> *_bleMidiTransport = nullptr;
bool specificTarget = false;
// TODO: somehow the forward declaration of the template class is not accepted by the compiler
// template <class> friend MyClientCallbacks;
friend class AdvertisedDeviceCallbacks;
friend class MyClientCallbacks;
friend class MIDI_NAMESPACE::MidiInterface<BLEMIDI_Transport<BLEMIDI_Client_ESP32>, MySettings>; //
AdvertisedDeviceCallbacks<_Settings> myAdvCB;
AdvertisedDeviceCallbacks myAdvCB;
protected:
QueueHandle_t mRxQueue;
@ -248,7 +247,7 @@ public:
{
}
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_Client_ESP32<_Settings>, _Settings> *);
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_Client_ESP32> *);
bool end()
{
@ -257,7 +256,7 @@ public:
_client->disconnect();
_client = nullptr;
return !_client->isConnected();
return true;
}
void write(uint8_t *data, uint8_t length)
@ -295,55 +294,61 @@ protected:
_bleMidiTransport->receive(buffer, length);
}
void notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify);
void connectCallbacks(MIDI_NAMESPACE::MidiInterface<BLEMIDI_Transport<BLEMIDI_Client_ESP32>, MySettings> *MIDIcallback);
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<_Settings> *bluetoothEsp32)
MyClientCallbacks(BLEMIDI_Client_ESP32 *bluetoothEsp32)
: _bluetoothEsp32(bluetoothEsp32)
{
}
protected:
BLEMIDI_Client_ESP32<_Settings> *_bluetoothEsp32 = nullptr;
BLEMIDI_Client_ESP32 *_bluetoothEsp32 = nullptr;
uint32_t onPassKeyRequest()
{
return userOnPassKeyRequest();
};
void onConnect(BLEClient*)
void onConnect(BLEClient *pClient)
{
//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*)
void onDisconnect(BLEClient *pClient)
{
//Serial.print(pClient->getPeerAddress().toString().c_str());
//Serial.println(" Disconnected - Starting scan");
@ -398,8 +403,7 @@ protected:
##########################################
*/
template <class _Settings>
bool BLEMIDI_Client_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_Client_ESP32<_Settings>, _Settings> *bleMidiTransport)
bool BLEMIDI_Client_ESP32::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_Client_ESP32> *bleMidiTransport)
{
_bleMidiTransport = bleMidiTransport;
@ -446,8 +450,7 @@ bool BLEMIDI_Client_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Tran
return true;
}
template <class _Settings>
bool BLEMIDI_Client_ESP32<_Settings>::available(byte *pvBuffer)
bool BLEMIDI_Client_ESP32::available(byte *pvBuffer)
{
if (myAdvCB.enableConnection)
{
@ -476,8 +479,7 @@ bool BLEMIDI_Client_ESP32<_Settings>::available(byte *pvBuffer)
}
/** Notification receiving handler callback */
template <class _Settings>
void BLEMIDI_Client_ESP32<_Settings>::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify)
void BLEMIDI_Client_ESP32::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify)
{
if (this->_characteristic == pRemoteCharacteristic) //Redundant protection
{
@ -485,8 +487,7 @@ void BLEMIDI_Client_ESP32<_Settings>::notifyCB(NimBLERemoteCharacteristic *pRemo
}
}
template <class _Settings>
void BLEMIDI_Client_ESP32<_Settings>::scan()
void BLEMIDI_Client_ESP32::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
@ -505,8 +506,7 @@ void BLEMIDI_Client_ESP32<_Settings>::scan()
}
};
template <class _Settings>
bool BLEMIDI_Client_ESP32<_Settings>::connect()
bool BLEMIDI_Client_ESP32::connect()
{
using namespace std::placeholders; //<- for bind funtion in callback notification
@ -553,7 +553,7 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect()
// Create and setup a new client
_client = BLEDevice::createClient();
_client->setClientCallbacks(new MyClientCallbacks<_Settings>(this), false);
_client->setClientCallbacks(new MyClientCallbacks(this), false);
_client->setConnectionParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT);
@ -622,18 +622,12 @@ void scanEndedCB(NimBLEScanResults results)
END_BLEMIDI_NAMESPACE
/*! \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_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"
/*! \brief Create an instance for ESP32 named <DeviceName>, and adviertise 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)
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);
/*! \brief Create a default instance for ESP32 named BLEMIDI-CLIENT.
It will try to connect to first midi ble server found.

View File

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

View File

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

View File

@ -2,28 +2,27 @@
// 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;
// BLEDis bledis;
// BLEMidi blemidi;
BLEMIDI_NAMESPACE::BLEMIDI_Transport<class BLEMIDI_nRF52<_Settings>, _Settings>* _bleMidiTransport;
BLEMIDI_NAMESPACE::BLEMIDI_Transport<class BLEMIDI_nRF52>* _bleMidiTransport;
// template <class> friend class MyServerCallbacks;
// template <class> friend class MyCharacteristicCallbacks;
friend class MyServerCallbacks;
friend class MyCharacteristicCallbacks;
public:
BLEMIDI_nRF52()
{
}
bool begin(const char*, BLEMIDI_NAMESPACE::BLEMIDI_Transport<class BLEMIDI_nRF52<_Settings>, _Settings>*);
bool begin(const char*, BLEMIDI_NAMESPACE::BLEMIDI_Transport<class BLEMIDI_nRF52>*);
void end()
{
@ -62,64 +61,41 @@ protected:
}
};
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)
bool BLEMIDI_nRF52::begin(const char* deviceName, BLEMIDI_NAMESPACE::BLEMIDI_Transport<class BLEMIDI_nRF52>* 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.Periph.setConnectCallback(connect_callback);
Bluefruit.Periph.setDisconnectCallback(disconnect_callback);
// Bluefruit.autoConnLed(true);
// 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);
// blemidi.write((uint8_t)0);
// Bluefruit.Advertising.addService(blemidi);
// 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
@ -130,24 +106,19 @@ bool BLEMIDI_nRF52<_Settings>::begin(const char* deviceName, BLEMIDI_NAMESPACE::
* 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_CREATE_CUSTOM_INSTANCE (DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettings)
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);
/*! \brief Create a default instance for nRF52 named BLE-MIDI
*/