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_NimBLE.h>
#include <hardware/BLEMIDI_ESP32.h> #include <hardware/BLEMIDI_ESP32.h>
//#include <hardware/BLEMIDI_nRF52.h>
//#include <hardware/BLEMIDI_ArduinoBLE.h> //#include <hardware/BLEMIDI_ArduinoBLE.h>
BLEMIDI_CREATE_DEFAULT_INSTANCE() BLEMIDI_CREATE_DEFAULT_INSTANCE()

View File

@ -28,17 +28,14 @@
#include <Arduino.h> #include <Arduino.h>
#include <BLEMIDI_Transport.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_Client_ESP32.h>
//#include <hardware/BLEMIDI_ESP32_NimBLE.h> //#include <hardware/BLEMIDI_ESP32_NimBLE.h>
//#include <hardware/BLEMIDI_ESP32.h> //#include <hardware/BLEMIDI_ESP32.h>
//#include <hardware/BLEMIDI_nRF52.h>
//#include <hardware/BLEMIDI_ArduinoBLE.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("",MIDI) //Connect to the first server found
//BLEMIDI_CREATE_INSTANCE("f2:c1:d9:36:e7:6b",MIDI) //Connect to a specific BLE address server //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_NimBLE.h>
#include <hardware/BLEMIDI_ESP32.h> #include <hardware/BLEMIDI_ESP32.h>
//#include <hardware/BLEMIDI_nRF52.h>
//#include <hardware/BLEMIDI_ArduinoBLE.h> //#include <hardware/BLEMIDI_ArduinoBLE.h>
BLEMIDI_CREATE_INSTANCE("CustomName", MIDI) BLEMIDI_CREATE_INSTANCE("CustomName", MIDI)

View File

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

View File

@ -6,7 +6,7 @@ BEGIN_BLEMIDI_NAMESPACE
struct DefaultSettings struct DefaultSettings
{ {
static const short MaxBufferSize = 64; static const size_t MaxBufferSize = 64;
}; };
END_BLEMIDI_NAMESPACE 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> template <class T, class _Settings = DefaultSettings>
class BLEMIDI_Transport class BLEMIDI_Transport
{ {
typedef _Settings Settings;
private: private:
byte mRxBuffer[_Settings::MaxBufferSize]; byte mRxBuffer[Settings::MaxBufferSize];
unsigned mRxIndex = 0; unsigned mRxIndex = 0;
byte mTxBuffer[_Settings::MaxBufferSize]; // minimum 5 bytes byte mTxBuffer[Settings::MaxBufferSize]; // minimum 5 bytes
unsigned mTxIndex = 0; unsigned mTxIndex = 0;
char mDeviceName[24]; char mDeviceName[24];

View File

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

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

View File

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

View File

@ -5,7 +5,6 @@
BEGIN_BLEMIDI_NAMESPACE BEGIN_BLEMIDI_NAMESPACE
template <class _Settings>
class BLEMIDI_ESP32_NimBLE class BLEMIDI_ESP32_NimBLE
{ {
private: private:
@ -13,10 +12,10 @@ private:
BLEAdvertising *_advertising = nullptr; BLEAdvertising *_advertising = nullptr;
BLECharacteristic *_characteristic = 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; friend class MyServerCallbacks;
template <class> friend class MyCharacteristicCallbacks; friend class MyCharacteristicCallbacks;
protected: protected:
QueueHandle_t mRxQueue; 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() void end()
{ {
@ -70,17 +69,16 @@ protected:
} }
}; };
template <class _Settings>
class MyServerCallbacks : public BLEServerCallbacks class MyServerCallbacks : public BLEServerCallbacks
{ {
public: public:
MyServerCallbacks(BLEMIDI_ESP32_NimBLE<_Settings> *bluetoothEsp32) MyServerCallbacks(BLEMIDI_ESP32_NimBLE *bluetoothEsp32)
: _bluetoothEsp32(bluetoothEsp32) : _bluetoothEsp32(bluetoothEsp32)
{ {
} }
protected: protected:
BLEMIDI_ESP32_NimBLE<_Settings> *_bluetoothEsp32 = nullptr; BLEMIDI_ESP32_NimBLE *_bluetoothEsp32 = nullptr;
void onConnect(BLEServer *) void onConnect(BLEServer *)
{ {
@ -95,17 +93,16 @@ protected:
} }
}; };
template <class _Settings>
class MyCharacteristicCallbacks : public BLECharacteristicCallbacks class MyCharacteristicCallbacks : public BLECharacteristicCallbacks
{ {
public: public:
MyCharacteristicCallbacks(BLEMIDI_ESP32_NimBLE<_Settings> *bluetoothEsp32) MyCharacteristicCallbacks(BLEMIDI_ESP32_NimBLE *bluetoothEsp32)
: _bluetoothEsp32(bluetoothEsp32) : _bluetoothEsp32(bluetoothEsp32)
{ {
} }
protected: protected:
BLEMIDI_ESP32_NimBLE<_Settings> *_bluetoothEsp32 = nullptr; BLEMIDI_ESP32_NimBLE *_bluetoothEsp32 = nullptr;
void onWrite(BLECharacteristic *characteristic) void onWrite(BLECharacteristic *characteristic)
{ {
@ -117,8 +114,7 @@ protected:
} }
}; };
template <class _Settings> bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE> *bleMidiTransport)
bool BLEMIDI_ESP32_NimBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE<_Settings>, _Settings> *bleMidiTransport)
{ {
_bleMidiTransport = bleMidiTransport; _bleMidiTransport = bleMidiTransport;
@ -126,10 +122,10 @@ bool BLEMIDI_ESP32_NimBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Tran
// To communicate between the 2 cores. // To communicate between the 2 cores.
// Core_0 runs here, core_1 runs the BLE stack // 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 = BLEDevice::createServer();
_server->setCallbacks(new MyServerCallbacks<_Settings>(this)); _server->setCallbacks(new MyServerCallbacks(this));
_server->advertiseOnDisconnect(true); _server->advertiseOnDisconnect(true);
// Create the BLE Service // Create the BLE Service
@ -143,7 +139,7 @@ bool BLEMIDI_ESP32_NimBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Tran
NIMBLE_PROPERTY::NOTIFY | NIMBLE_PROPERTY::NOTIFY |
NIMBLE_PROPERTY::WRITE_NR); NIMBLE_PROPERTY::WRITE_NR);
_characteristic->setCallbacks(new MyCharacteristicCallbacks<_Settings>(this)); _characteristic->setCallbacks(new MyCharacteristicCallbacks(this));
auto _security = new NimBLESecurity(); auto _security = new NimBLESecurity();
_security->setAuthenticationMode(ESP_LE_AUTH_BOND); _security->setAuthenticationMode(ESP_LE_AUTH_BOND);
@ -162,14 +158,9 @@ bool BLEMIDI_ESP32_NimBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Tran
/*! \brief Create an instance for ESP32 named <DeviceName> /*! \brief Create an instance for ESP32 named <DeviceName>
*/ */
#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ #define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE<_Settings>, _Settings> BLE##Name(DeviceName); \ BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE> 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); 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 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 /*! \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 // I N D E V E L O P M E N T
#include <bluefruit.h> //#include <bluefruit.h>
BEGIN_BLEMIDI_NAMESPACE BEGIN_BLEMIDI_NAMESPACE
template <class _Settings>
class BLEMIDI_nRF52 class BLEMIDI_nRF52
{ {
private: private:
BLEDis bledis; // BLEDis bledis;
// BLEMidi blemidi; // 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; friend class MyServerCallbacks;
// template <class> friend class MyCharacteristicCallbacks; friend class MyCharacteristicCallbacks;
public: public:
BLEMIDI_nRF52() 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() void end()
{ {
@ -62,64 +61,41 @@ protected:
} }
}; };
void connect_callback(uint16_t conn_handle) bool BLEMIDI_nRF52::begin(const char* deviceName, BLEMIDI_NAMESPACE::BLEMIDI_Transport<class BLEMIDI_nRF52>* bleMidiTransport)
{
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; _bleMidiTransport = bleMidiTransport;
// Config the peripheral connection with maximum bandwidth // Config the peripheral connection with maximum bandwidth
// more SRAM required by SoftDevice // more SRAM required by SoftDevice
// Note: All config***() function must be called before begin() // Note: All config***() function must be called before begin()
Bluefruit.configPrphBandwidth(BANDWIDTH_MAX); // Bluefruit.configPrphBandwidth(BANDWIDTH_MAX);
Bluefruit.begin(); // Bluefruit.begin();
Bluefruit.setName(deviceName); // Bluefruit.setName(deviceName);
Bluefruit.setTxPower(4); // Check bluefruit.h for supported values // Bluefruit.setTxPower(4); // Check bluefruit.h for supported values
// Setup the on board blue LED to be enabled on CONNECT // 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 // Configure and Start Device Information Service
bledis.setManufacturer("Adafruit Industries"); // bledis.setManufacturer("Adafruit Industries");
bledis.setModel("Bluefruit Feather52"); // bledis.setModel("Bluefruit Feather52");
bledis.begin(); // bledis.begin();
// Start advertising ---------------------------- // Start advertising ----------------------------
// Set General Discoverable Mode flag // 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 // Advertise TX Power
Bluefruit.Advertising.addTxPower(); // Bluefruit.Advertising.addTxPower();
// Advertise BLE MIDI Service // Advertise BLE MIDI Service
Bluefruit.Advertising.addService(blemidi); // Bluefruit.Advertising.addService(blemidi);
// blemidi.write((uint8_t)0);
// Secondary Scan Response packet (optional) // Secondary Scan Response packet (optional)
// Since there is no room for 'Name' in Advertising packet // Since there is no room for 'Name' in Advertising packet
Bluefruit.ScanResponse.addName(); // Bluefruit.ScanResponse.addName();
/* Start Advertising /* Start Advertising
* - Enable auto advertising if disconnected * - Enable auto advertising if disconnected
@ -130,28 +106,23 @@ bool BLEMIDI_nRF52<_Settings>::begin(const char* deviceName, BLEMIDI_NAMESPACE::
* For recommended advertising interval * For recommended advertising interval
* https://developer.apple.com/library/content/qa/qa1931/_index.html * https://developer.apple.com/library/content/qa/qa1931/_index.html
*/ */
Bluefruit.Advertising.restartOnDisconnect(true); // Bluefruit.Advertising.restartOnDisconnect(true);
Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms // Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms
Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode // Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode
Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds // Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds
return true; 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> /*! \brief Create an instance for nRF52 named <DeviceName>
*/ */
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \ #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 /*! \brief Create a default instance for nRF52 named BLE-MIDI
*/ */
#define BLEMIDI_CREATE_DEFAULT_INSTANCE() \ #define BLEMIDI_CREATE_DEFAULT_INSTANCE() \
BLEMIDI_CREATE_INSTANCE("nRF85BLE-MIDI", MIDI) BLEMIDI_CREATE_INSTANCE("nRF85BLE-MIDI", MIDI)
END_BLEMIDI_NAMESPACE END_BLEMIDI_NAMESPACE