From 12c1e135d431a5bbf8dd600d244448be27deadbd Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Tue, 31 May 2022 23:01:42 +0200 Subject: [PATCH 01/48] first commit for customSettings --- .../CustomerBufferSize/CustomerBufferSize.ino | 61 +++++++++++++++++++ src/hardware/BLEMIDI_ArduinoBLE.h | 14 +++-- src/hardware/BLEMIDI_ESP32.h | 14 +++-- src/hardware/BLEMIDI_ESP32_NimBLE.h | 12 +++- src/hardware/BLEMIDI_nRF52.h | 12 +++- 5 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 examples/CustomerBufferSize/CustomerBufferSize.ino diff --git a/examples/CustomerBufferSize/CustomerBufferSize.ino b/examples/CustomerBufferSize/CustomerBufferSize.ino new file mode 100644 index 0000000..6883a7a --- /dev/null +++ b/examples/CustomerBufferSize/CustomerBufferSize.ino @@ -0,0 +1,61 @@ +#include + +struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings { + static const size_t MaxBufferSize = 16; // was 64 +}; + +#include +//#include +//#include +//#include + +BLEMIDI_NAMESPACE::BLEMIDI_Transport BLEMIDI("Esp32-NimBLE-MIDI"); \ +MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::MySettings> MIDI((BLEMIDI_NAMESPACE::BLEMIDI_Transport &)BLEMIDI); + +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() +{ + 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 + } +} diff --git a/src/hardware/BLEMIDI_ArduinoBLE.h b/src/hardware/BLEMIDI_ArduinoBLE.h index 82f3a04..4044c60 100644 --- a/src/hardware/BLEMIDI_ArduinoBLE.h +++ b/src/hardware/BLEMIDI_ArduinoBLE.h @@ -63,7 +63,7 @@ class Fifo { class BLEMIDI_ArduinoBLE { private: - static BLEMIDI_Transport* _bleMidiTransport; + static BLEMIDI_Transport* _bleMidiTransport; static BLEDevice* _central; Fifo mRxBuffer; @@ -73,7 +73,7 @@ class BLEMIDI_ArduinoBLE { } - bool begin(const char*, BLEMIDI_Transport*); + bool begin(const char*, BLEMIDI_Transport*); void end() { @@ -183,10 +183,10 @@ class BLEMIDI_ArduinoBLE } }; -BLEMIDI_Transport* BLEMIDI_ArduinoBLE::_bleMidiTransport = nullptr; +BLEMIDI_Transport* BLEMIDI_ArduinoBLE::_bleMidiTransport = nullptr; BLEDevice* BLEMIDI_ArduinoBLE::_central = nullptr; -bool BLEMIDI_ArduinoBLE::begin(const char* deviceName, BLEMIDI_Transport* bleMidiTransport) +bool BLEMIDI_ArduinoBLE::begin(const char* deviceName, BLEMIDI_Transport* bleMidiTransport) { _bleMidiTransport = bleMidiTransport; @@ -217,6 +217,12 @@ bool BLEMIDI_ArduinoBLE::begin(const char* deviceName, BLEMIDI_Transport + */ +#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, CustomSettings) \ +BLEMIDI_NAMESPACE::BLEMIDI_Transport BLE##Name(DeviceName); \ +MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport &)BLE##Name); + /*! \brief Create an instance for nRF52 named */ #define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \ diff --git a/src/hardware/BLEMIDI_ESP32.h b/src/hardware/BLEMIDI_ESP32.h index f67a419..6aba70d 100644 --- a/src/hardware/BLEMIDI_ESP32.h +++ b/src/hardware/BLEMIDI_ESP32.h @@ -15,7 +15,7 @@ class BLEMIDI_ESP32 BLEAdvertising *_advertising = nullptr; BLECharacteristic *_characteristic = nullptr; - BLEMIDI_Transport *_bleMidiTransport = nullptr; + BLEMIDI_Transport *_bleMidiTransport = nullptr; friend class MyServerCallbacks; friend class MyCharacteristicCallbacks; @@ -28,7 +28,7 @@ class BLEMIDI_ESP32 { } - bool begin(const char *, BLEMIDI_Transport *); + bool begin(const char *, BLEMIDI_Transport *); void end() { @@ -121,7 +121,7 @@ class MyCharacteristicCallbacks : public BLECharacteristicCallbacks } }; -bool BLEMIDI_ESP32::begin(const char *deviceName, BLEMIDI_Transport *bleMidiTransport) +bool BLEMIDI_ESP32::begin(const char *deviceName, BLEMIDI_Transport *bleMidiTransport) { _bleMidiTransport = bleMidiTransport; @@ -161,11 +161,15 @@ bool BLEMIDI_ESP32::begin(const char *deviceName, BLEMIDI_TransportsetAppearance(0x00); _advertising->start(); - Serial.println("begin"); - return true; } +/*! \brief Create an instance for ESP32 named + */ +#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, CustomSettings) \ + BLEMIDI_NAMESPACE::BLEMIDI_Transport BLE##Name(DeviceName); \ + MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport &)BLE##Name); + /*! \brief Create an instance for ESP32 named */ #define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \ diff --git a/src/hardware/BLEMIDI_ESP32_NimBLE.h b/src/hardware/BLEMIDI_ESP32_NimBLE.h index 75f894d..4684250 100644 --- a/src/hardware/BLEMIDI_ESP32_NimBLE.h +++ b/src/hardware/BLEMIDI_ESP32_NimBLE.h @@ -12,7 +12,7 @@ class BLEMIDI_ESP32_NimBLE BLEAdvertising *_advertising = nullptr; BLECharacteristic *_characteristic = nullptr; - BLEMIDI_Transport *_bleMidiTransport = nullptr; + BLEMIDI_Transport *_bleMidiTransport = nullptr; friend class MyServerCallbacks; friend class MyCharacteristicCallbacks; @@ -25,7 +25,7 @@ class BLEMIDI_ESP32_NimBLE { } - bool begin(const char *, BLEMIDI_Transport *); + bool begin(const char *, BLEMIDI_Transport *); void end() { @@ -114,7 +114,7 @@ class MyCharacteristicCallbacks : public BLECharacteristicCallbacks } }; -bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport *bleMidiTransport) +bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport *bleMidiTransport) { _bleMidiTransport = bleMidiTransport; @@ -156,6 +156,12 @@ bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport + */ +#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, CustomSettings) \ + BLEMIDI_NAMESPACE::BLEMIDI_Transport BLE##Name(DeviceName); \ + MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport &)BLE##Name); + /*! \brief Create an instance for ESP32 named */ #define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \ diff --git a/src/hardware/BLEMIDI_nRF52.h b/src/hardware/BLEMIDI_nRF52.h index 2210897..4be4a31 100644 --- a/src/hardware/BLEMIDI_nRF52.h +++ b/src/hardware/BLEMIDI_nRF52.h @@ -12,7 +12,7 @@ class BLEMIDI_nRF52 // BLEDis bledis; // BLEMidi blemidi; - BLEMIDI_NAMESPACE::BLEMIDI_Transport* _bleMidiTransport; + BLEMIDI_NAMESPACE::BLEMIDI_Transport* _bleMidiTransport; friend class MyServerCallbacks; friend class MyCharacteristicCallbacks; @@ -22,7 +22,7 @@ class BLEMIDI_nRF52 { } - bool begin(const char*, BLEMIDI_NAMESPACE::BLEMIDI_Transport*); + bool begin(const char*, BLEMIDI_NAMESPACE::BLEMIDI_Transport*); void end() { @@ -61,7 +61,7 @@ class BLEMIDI_nRF52 } }; -bool BLEMIDI_nRF52::begin(const char* deviceName, BLEMIDI_NAMESPACE::BLEMIDI_Transport* bleMidiTransport) +bool BLEMIDI_nRF52::begin(const char* deviceName, BLEMIDI_NAMESPACE::BLEMIDI_Transport* bleMidiTransport) { _bleMidiTransport = bleMidiTransport; @@ -114,6 +114,12 @@ bool BLEMIDI_nRF52::begin(const char* deviceName, BLEMIDI_NAMESPACE::BLEMIDI_Tra return true; } + /*! \brief Create an instance for nRF52 named + */ +#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, CustomSettings) \ +BLEMIDI_NAMESPACE::BLEMIDI_Transport BLE##Name(DeviceName); \ +MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport &)BLE##Name); + /*! \brief Create an instance for nRF52 named */ #define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \ From 50aaaf387291d73f6266133f5307bc5423d4da93 Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Tue, 31 May 2022 23:02:44 +0200 Subject: [PATCH 02/48] Update CustomerBufferSize.ino --- examples/CustomerBufferSize/CustomerBufferSize.ino | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/examples/CustomerBufferSize/CustomerBufferSize.ino b/examples/CustomerBufferSize/CustomerBufferSize.ino index 6883a7a..5d86f67 100644 --- a/examples/CustomerBufferSize/CustomerBufferSize.ino +++ b/examples/CustomerBufferSize/CustomerBufferSize.ino @@ -9,8 +9,7 @@ struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings { //#include //#include -BLEMIDI_NAMESPACE::BLEMIDI_Transport BLEMIDI("Esp32-NimBLE-MIDI"); \ -MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::MySettings> MIDI((BLEMIDI_NAMESPACE::BLEMIDI_Transport &)BLEMIDI); +BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-NimBLE-MIDI", MIDI, BLEMIDI_NAMESPACE::DefaultSettings); unsigned long t0 = millis(); bool isConnected = false; @@ -18,10 +17,14 @@ 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. +// 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); @@ -58,4 +61,4 @@ void loop() MIDI.sendNoteOn (60, 100, 1); // note 60, velocity 100 on channel 1 } -} +} \ No newline at end of file From 13b50f32ee07c6047d3c4a908591134e619be123 Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Tue, 31 May 2022 23:04:27 +0200 Subject: [PATCH 03/48] Update CustomerBufferSize.ino --- examples/CustomerBufferSize/CustomerBufferSize.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/CustomerBufferSize/CustomerBufferSize.ino b/examples/CustomerBufferSize/CustomerBufferSize.ino index 5d86f67..fce9117 100644 --- a/examples/CustomerBufferSize/CustomerBufferSize.ino +++ b/examples/CustomerBufferSize/CustomerBufferSize.ino @@ -9,7 +9,7 @@ struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings { //#include //#include -BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-NimBLE-MIDI", MIDI, BLEMIDI_NAMESPACE::DefaultSettings); +BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-NimBLE-MIDI", MIDI, CustomBufferSizeSettings); unsigned long t0 = millis(); bool isConnected = false; From 2f9c684140affec506e759b9448918e32cc58461 Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Tue, 31 May 2022 23:08:38 +0200 Subject: [PATCH 04/48] Update CustomerBufferSize.ino --- examples/CustomerBufferSize/CustomerBufferSize.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/CustomerBufferSize/CustomerBufferSize.ino b/examples/CustomerBufferSize/CustomerBufferSize.ino index fce9117..5d86f67 100644 --- a/examples/CustomerBufferSize/CustomerBufferSize.ino +++ b/examples/CustomerBufferSize/CustomerBufferSize.ino @@ -9,7 +9,7 @@ struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings { //#include //#include -BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-NimBLE-MIDI", MIDI, CustomBufferSizeSettings); +BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-NimBLE-MIDI", MIDI, BLEMIDI_NAMESPACE::DefaultSettings); unsigned long t0 = millis(); bool isConnected = false; From 1ae439d76cb0b4d9627989f6eb7edc615deee85d Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Wed, 1 Jun 2022 08:14:45 +0200 Subject: [PATCH 05/48] tmp simplified template arg to int --- .../CustomerBufferSize/CustomerBufferSize.ino | 8 ++--- src/BLEMIDI_Settings.h | 8 ++--- src/BLEMIDI_Transport.h | 11 +++--- src/hardware/BLEMIDI_ArduinoBLE.h | 2 +- src/hardware/BLEMIDI_ESP32_NimBLE.h | 34 +++++++++++-------- 5 files changed, 34 insertions(+), 29 deletions(-) diff --git a/examples/CustomerBufferSize/CustomerBufferSize.ino b/examples/CustomerBufferSize/CustomerBufferSize.ino index 5d86f67..6f25bb9 100644 --- a/examples/CustomerBufferSize/CustomerBufferSize.ino +++ b/examples/CustomerBufferSize/CustomerBufferSize.ino @@ -1,15 +1,15 @@ #include -struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings { - static const size_t MaxBufferSize = 16; // was 64 -}; +// struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings { +// static const size_t MaxBufferSize = 16; // was 64 +//}; #include //#include //#include //#include -BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-NimBLE-MIDI", MIDI, BLEMIDI_NAMESPACE::DefaultSettings); +BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-NimBLE-MIDI", MIDI, 16); unsigned long t0 = millis(); bool isConnected = false; diff --git a/src/BLEMIDI_Settings.h b/src/BLEMIDI_Settings.h index f4c8b5a..871d683 100644 --- a/src/BLEMIDI_Settings.h +++ b/src/BLEMIDI_Settings.h @@ -4,9 +4,9 @@ BEGIN_BLEMIDI_NAMESPACE -struct DefaultSettings -{ - static const size_t MaxBufferSize = 64; -}; +//struct DefaultSettings +//{ +// static const short MaxBufferSize = 64; +//}; END_BLEMIDI_NAMESPACE diff --git a/src/BLEMIDI_Transport.h b/src/BLEMIDI_Transport.h index 45b028a..db75080 100644 --- a/src/BLEMIDI_Transport.h +++ b/src/BLEMIDI_Transport.h @@ -23,16 +23,14 @@ static const char *const CHARACTERISTIC_UUID = "7772e5db-3868-4112-a1a9-f2669d10 #define MIDI_TYPE 0x80 -template +template class BLEMIDI_Transport { - typedef _Settings Settings; - private: - byte mRxBuffer[Settings::MaxBufferSize]; + byte mRxBuffer[Size]; unsigned mRxIndex = 0; - byte mTxBuffer[Settings::MaxBufferSize]; // minimum 5 bytes + byte mTxBuffer[Size]; // minimum 5 bytes unsigned mTxIndex = 0; char mDeviceName[24]; @@ -57,6 +55,9 @@ class BLEMIDI_Transport void begin() { mBleClass.begin(mDeviceName, this); + + Serial.print("size : "); + Serial.println(Size); } void end() diff --git a/src/hardware/BLEMIDI_ArduinoBLE.h b/src/hardware/BLEMIDI_ArduinoBLE.h index 4044c60..8a6cabc 100644 --- a/src/hardware/BLEMIDI_ArduinoBLE.h +++ b/src/hardware/BLEMIDI_ArduinoBLE.h @@ -66,7 +66,7 @@ class BLEMIDI_ArduinoBLE static BLEMIDI_Transport* _bleMidiTransport; static BLEDevice* _central; - Fifo mRxBuffer; + Fifo mRxBuffer; public: BLEMIDI_ArduinoBLE() diff --git a/src/hardware/BLEMIDI_ESP32_NimBLE.h b/src/hardware/BLEMIDI_ESP32_NimBLE.h index 4684250..cedb88f 100644 --- a/src/hardware/BLEMIDI_ESP32_NimBLE.h +++ b/src/hardware/BLEMIDI_ESP32_NimBLE.h @@ -5,6 +5,7 @@ BEGIN_BLEMIDI_NAMESPACE +template class BLEMIDI_ESP32_NimBLE { private: @@ -12,10 +13,10 @@ class BLEMIDI_ESP32_NimBLE BLEAdvertising *_advertising = nullptr; BLECharacteristic *_characteristic = nullptr; - BLEMIDI_Transport *_bleMidiTransport = nullptr; + BLEMIDI_Transport, Size> *_bleMidiTransport = nullptr; - friend class MyServerCallbacks; - friend class MyCharacteristicCallbacks; + template friend class MyServerCallbacks; + template friend class MyCharacteristicCallbacks; protected: QueueHandle_t mRxQueue; @@ -25,7 +26,7 @@ class BLEMIDI_ESP32_NimBLE { } - bool begin(const char *, BLEMIDI_Transport *); + bool begin(const char *, BLEMIDI_Transport, Size> *); void end() { @@ -69,16 +70,17 @@ class BLEMIDI_ESP32_NimBLE } }; +template class MyServerCallbacks : public BLEServerCallbacks { public: - MyServerCallbacks(BLEMIDI_ESP32_NimBLE *bluetoothEsp32) + MyServerCallbacks(BLEMIDI_ESP32_NimBLE *bluetoothEsp32) : _bluetoothEsp32(bluetoothEsp32) { } protected: - BLEMIDI_ESP32_NimBLE *_bluetoothEsp32 = nullptr; + BLEMIDI_ESP32_NimBLE *_bluetoothEsp32 = nullptr; void onConnect(BLEServer *) { @@ -93,16 +95,17 @@ class MyServerCallbacks : public BLEServerCallbacks } }; +template class MyCharacteristicCallbacks : public BLECharacteristicCallbacks { public: - MyCharacteristicCallbacks(BLEMIDI_ESP32_NimBLE *bluetoothEsp32) + MyCharacteristicCallbacks(BLEMIDI_ESP32_NimBLE *bluetoothEsp32) : _bluetoothEsp32(bluetoothEsp32) { } protected: - BLEMIDI_ESP32_NimBLE *_bluetoothEsp32 = nullptr; + BLEMIDI_ESP32_NimBLE *_bluetoothEsp32 = nullptr; void onWrite(BLECharacteristic *characteristic) { @@ -114,7 +117,8 @@ class MyCharacteristicCallbacks : public BLECharacteristicCallbacks } }; -bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport *bleMidiTransport) +template +bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport, Size> *bleMidiTransport) { _bleMidiTransport = bleMidiTransport; @@ -122,10 +126,10 @@ bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_TransportsetCallbacks(new MyServerCallbacks(this)); + _server->setCallbacks(new MyServerCallbacks(this)); _server->advertiseOnDisconnect(true); // Create the BLE Service @@ -139,7 +143,7 @@ bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_TransportsetCallbacks(new MyCharacteristicCallbacks(this)); + _characteristic->setCallbacks(new MyCharacteristicCallbacks(this)); auto _security = new NimBLESecurity(); _security->setAuthenticationMode(ESP_LE_AUTH_BOND); @@ -158,9 +162,9 @@ bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport */ -#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, CustomSettings) \ - BLEMIDI_NAMESPACE::BLEMIDI_Transport BLE##Name(DeviceName); \ - MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport &)BLE##Name); +#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, Size) \ + BLEMIDI_NAMESPACE::BLEMIDI_Transport, Size> BLE##Name(DeviceName); \ + MIDI_NAMESPACE::MidiInterface, Size>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, Size> &)BLE##Name); /*! \brief Create an instance for ESP32 named */ From ab44659ce3dba2d6342494915855224619ea8dd8 Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Wed, 1 Jun 2022 08:30:12 +0200 Subject: [PATCH 06/48] changed template arg to _Setting --- .../CustomerBufferSize/CustomerBufferSize.ino | 9 +++-- src/BLEMIDI_Settings.h | 8 ++-- src/BLEMIDI_Transport.h | 8 ++-- src/hardware/BLEMIDI_ESP32.h | 2 +- src/hardware/BLEMIDI_ESP32_NimBLE.h | 38 +++++++++---------- 5 files changed, 33 insertions(+), 32 deletions(-) diff --git a/examples/CustomerBufferSize/CustomerBufferSize.ino b/examples/CustomerBufferSize/CustomerBufferSize.ino index 6f25bb9..dc55071 100644 --- a/examples/CustomerBufferSize/CustomerBufferSize.ino +++ b/examples/CustomerBufferSize/CustomerBufferSize.ino @@ -1,19 +1,20 @@ #include -// struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings { -// static const size_t MaxBufferSize = 16; // was 64 -//}; + struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings { + static const size_t MaxBufferSize = 16; +}; #include //#include //#include //#include -BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-NimBLE-MIDI", MIDI, 16); +BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-NimBLE-MIDI", MIDI, BLEMIDI_NAMESPACE::DefaultSettings); 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. diff --git a/src/BLEMIDI_Settings.h b/src/BLEMIDI_Settings.h index 871d683..5463737 100644 --- a/src/BLEMIDI_Settings.h +++ b/src/BLEMIDI_Settings.h @@ -4,9 +4,9 @@ BEGIN_BLEMIDI_NAMESPACE -//struct DefaultSettings -//{ -// static const short MaxBufferSize = 64; -//}; +struct DefaultSettings +{ + static const short MaxBufferSize = 64; +}; END_BLEMIDI_NAMESPACE diff --git a/src/BLEMIDI_Transport.h b/src/BLEMIDI_Transport.h index db75080..bf5b830 100644 --- a/src/BLEMIDI_Transport.h +++ b/src/BLEMIDI_Transport.h @@ -23,14 +23,14 @@ static const char *const CHARACTERISTIC_UUID = "7772e5db-3868-4112-a1a9-f2669d10 #define MIDI_TYPE 0x80 -template +template class BLEMIDI_Transport { private: - byte mRxBuffer[Size]; + byte mRxBuffer[_Settings::MaxBufferSize]; unsigned mRxIndex = 0; - byte mTxBuffer[Size]; // minimum 5 bytes + byte mTxBuffer[_Settings::MaxBufferSize]; // minimum 5 bytes unsigned mTxIndex = 0; char mDeviceName[24]; @@ -57,7 +57,7 @@ class BLEMIDI_Transport mBleClass.begin(mDeviceName, this); Serial.print("size : "); - Serial.println(Size); + Serial.println(_Settings::MaxBufferSize); } void end() diff --git a/src/hardware/BLEMIDI_ESP32.h b/src/hardware/BLEMIDI_ESP32.h index 6aba70d..038f4f4 100644 --- a/src/hardware/BLEMIDI_ESP32.h +++ b/src/hardware/BLEMIDI_ESP32.h @@ -129,7 +129,7 @@ bool BLEMIDI_ESP32::begin(const char *deviceName, BLEMIDI_TransportsetCallbacks(new MyServerCallbacks(this)); diff --git a/src/hardware/BLEMIDI_ESP32_NimBLE.h b/src/hardware/BLEMIDI_ESP32_NimBLE.h index cedb88f..702114a 100644 --- a/src/hardware/BLEMIDI_ESP32_NimBLE.h +++ b/src/hardware/BLEMIDI_ESP32_NimBLE.h @@ -5,7 +5,7 @@ BEGIN_BLEMIDI_NAMESPACE -template +template class BLEMIDI_ESP32_NimBLE { private: @@ -13,10 +13,10 @@ class BLEMIDI_ESP32_NimBLE BLEAdvertising *_advertising = nullptr; BLECharacteristic *_characteristic = nullptr; - BLEMIDI_Transport, Size> *_bleMidiTransport = nullptr; + BLEMIDI_Transport, _Settings> *_bleMidiTransport = nullptr; - template friend class MyServerCallbacks; - template friend class MyCharacteristicCallbacks; + template friend class MyServerCallbacks; + template friend class MyCharacteristicCallbacks; protected: QueueHandle_t mRxQueue; @@ -26,7 +26,7 @@ class BLEMIDI_ESP32_NimBLE { } - bool begin(const char *, BLEMIDI_Transport, Size> *); + bool begin(const char *, BLEMIDI_Transport, _Settings> *); void end() { @@ -70,17 +70,17 @@ class BLEMIDI_ESP32_NimBLE } }; -template +template 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 *) { @@ -95,17 +95,17 @@ class MyServerCallbacks : public BLEServerCallbacks } }; -template +template 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) { @@ -117,8 +117,8 @@ class MyCharacteristicCallbacks : public BLECharacteristicCallbacks } }; -template -bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport, Size> *bleMidiTransport) +template +bool BLEMIDI_ESP32_NimBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transport, _Settings> *bleMidiTransport) { _bleMidiTransport = bleMidiTransport; @@ -126,10 +126,10 @@ bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport // To communicate between the 2 cores. // Core_0 runs here, core_1 runs the BLE stack - mRxQueue = xQueueCreate(Size, sizeof(uint8_t)); // TODO DefaultSettings::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 @@ -143,7 +143,7 @@ bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport 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); @@ -162,9 +162,9 @@ bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport /*! \brief Create an instance for ESP32 named */ -#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, Size) \ - BLEMIDI_NAMESPACE::BLEMIDI_Transport, Size> BLE##Name(DeviceName); \ - MIDI_NAMESPACE::MidiInterface, Size>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, Size> &)BLE##Name); +#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ + BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> BLE##Name(DeviceName); \ + MIDI_NAMESPACE::MidiInterface, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> &)BLE##Name); /*! \brief Create an instance for ESP32 named */ From 9c0e0e0627d5334f52021d90d592420bb9a9bf83 Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Wed, 1 Jun 2022 08:32:02 +0200 Subject: [PATCH 07/48] Update CustomerBufferSize.ino --- examples/CustomerBufferSize/CustomerBufferSize.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/CustomerBufferSize/CustomerBufferSize.ino b/examples/CustomerBufferSize/CustomerBufferSize.ino index dc55071..3303207 100644 --- a/examples/CustomerBufferSize/CustomerBufferSize.ino +++ b/examples/CustomerBufferSize/CustomerBufferSize.ino @@ -9,7 +9,7 @@ //#include //#include -BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-NimBLE-MIDI", MIDI, BLEMIDI_NAMESPACE::DefaultSettings); +BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-NimBLE-MIDI", MIDI, CustomBufferSizeSettings); unsigned long t0 = millis(); bool isConnected = false; From 948fc1165f57efb73daf2d32d4f3eed2154a34d1 Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Wed, 1 Jun 2022 23:44:24 +0200 Subject: [PATCH 08/48] added template params --- src/hardware/BLEMIDI_ArduinoBLE.h | 16 +++++++++------- src/hardware/BLEMIDI_ESP32.h | 32 +++++++++++++++++-------------- src/hardware/BLEMIDI_nRF52.h | 18 +++++++++-------- 3 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/hardware/BLEMIDI_ArduinoBLE.h b/src/hardware/BLEMIDI_ArduinoBLE.h index 8a6cabc..fd5007b 100644 --- a/src/hardware/BLEMIDI_ArduinoBLE.h +++ b/src/hardware/BLEMIDI_ArduinoBLE.h @@ -60,10 +60,11 @@ class Fifo { T raw[rawSize]; }; +template class BLEMIDI_ArduinoBLE { private: - static BLEMIDI_Transport* _bleMidiTransport; + static BLEMIDI_Transport, _Settings>* _bleMidiTransport; static BLEDevice* _central; Fifo mRxBuffer; @@ -73,7 +74,7 @@ class BLEMIDI_ArduinoBLE { } - bool begin(const char*, BLEMIDI_Transport*); + bool begin(const char*, BLEMIDI_Transport, _Settings>*); void end() { @@ -183,10 +184,11 @@ class BLEMIDI_ArduinoBLE } }; -BLEMIDI_Transport* BLEMIDI_ArduinoBLE::_bleMidiTransport = nullptr; +BLEMIDI_Transport, _Settings>* BLEMIDI_ArduinoBLE::_bleMidiTransport = nullptr; BLEDevice* BLEMIDI_ArduinoBLE::_central = nullptr; -bool BLEMIDI_ArduinoBLE::begin(const char* deviceName, BLEMIDI_Transport* bleMidiTransport) +template +bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char* deviceName, BLEMIDI_Transport, _Settings>* bleMidiTransport) { _bleMidiTransport = bleMidiTransport; @@ -219,9 +221,9 @@ bool BLEMIDI_ArduinoBLE::begin(const char* deviceName, BLEMIDI_Transport */ -#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, CustomSettings) \ -BLEMIDI_NAMESPACE::BLEMIDI_Transport BLE##Name(DeviceName); \ -MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport &)BLE##Name); +#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ +BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> BLE##Name(DeviceName); \ +MIDI_NAMESPACE::MidiInterface, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> &)BLE##Name); /*! \brief Create an instance for nRF52 named */ diff --git a/src/hardware/BLEMIDI_ESP32.h b/src/hardware/BLEMIDI_ESP32.h index 038f4f4..e1898c7 100644 --- a/src/hardware/BLEMIDI_ESP32.h +++ b/src/hardware/BLEMIDI_ESP32.h @@ -8,6 +8,7 @@ BEGIN_BLEMIDI_NAMESPACE +template class BLEMIDI_ESP32 { private: @@ -15,10 +16,10 @@ class BLEMIDI_ESP32 BLEAdvertising *_advertising = nullptr; BLECharacteristic *_characteristic = nullptr; - BLEMIDI_Transport *_bleMidiTransport = nullptr; + BLEMIDI_Transport, _Settings> *_bleMidiTransport = nullptr; - friend class MyServerCallbacks; - friend class MyCharacteristicCallbacks; + template friend class MyServerCallbacks; + template friend class MyCharacteristicCallbacks; protected: QueueHandle_t mRxQueue; @@ -28,7 +29,7 @@ class BLEMIDI_ESP32 { } - bool begin(const char *, BLEMIDI_Transport *); + bool begin(const char *, BLEMIDI_Transport, _Settings> *); void end() { @@ -74,16 +75,17 @@ class BLEMIDI_ESP32 } }; +template 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 @@ class MyServerCallbacks : public BLEServerCallbacks } }; +template 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 @@ class MyCharacteristicCallbacks : public BLECharacteristicCallbacks } }; -bool BLEMIDI_ESP32::begin(const char *deviceName, BLEMIDI_Transport *bleMidiTransport) +template +bool BLEMIDI_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Transport, _Settings> *bleMidiTransport) { _bleMidiTransport = bleMidiTransport; @@ -132,7 +136,7 @@ bool BLEMIDI_ESP32::begin(const char *deviceName, BLEMIDI_TransportsetCallbacks(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_TransportaddDescriptor(new BLE2902()); - _characteristic->setCallbacks(new MyCharacteristicCallbacks(this)); + _characteristic->setCallbacks(new MyCharacteristicCallbacks<_Settings>(this)); auto _security = new BLESecurity(); _security->setAuthenticationMode(ESP_LE_AUTH_BOND); @@ -166,9 +170,9 @@ bool BLEMIDI_ESP32::begin(const char *deviceName, BLEMIDI_Transport */ -#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, CustomSettings) \ - BLEMIDI_NAMESPACE::BLEMIDI_Transport BLE##Name(DeviceName); \ - MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport &)BLE##Name); +#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ + BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> BLE##Name(DeviceName); \ + MIDI_NAMESPACE::MidiInterface, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> &)BLE##Name); /*! \brief Create an instance for ESP32 named */ diff --git a/src/hardware/BLEMIDI_nRF52.h b/src/hardware/BLEMIDI_nRF52.h index 4be4a31..429c7c4 100644 --- a/src/hardware/BLEMIDI_nRF52.h +++ b/src/hardware/BLEMIDI_nRF52.h @@ -6,23 +6,24 @@ BEGIN_BLEMIDI_NAMESPACE +template class BLEMIDI_nRF52 { private: // BLEDis bledis; // BLEMidi blemidi; - BLEMIDI_NAMESPACE::BLEMIDI_Transport* _bleMidiTransport; + BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings>* _bleMidiTransport; - friend class MyServerCallbacks; - friend class MyCharacteristicCallbacks; + template friend class MyServerCallbacks; + template friend class MyCharacteristicCallbacks; public: BLEMIDI_nRF52() { } - bool begin(const char*, BLEMIDI_NAMESPACE::BLEMIDI_Transport*); + bool begin(const char*, BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings>*); void end() { @@ -61,7 +62,8 @@ class BLEMIDI_nRF52 } }; -bool BLEMIDI_nRF52::begin(const char* deviceName, BLEMIDI_NAMESPACE::BLEMIDI_Transport* bleMidiTransport) +template +bool BLEMIDI_nRF52<_Settings>::begin(const char* deviceName, BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings>* bleMidiTransport) { _bleMidiTransport = bleMidiTransport; @@ -116,9 +118,9 @@ bool BLEMIDI_nRF52::begin(const char* deviceName, BLEMIDI_NAMESPACE::BLEMIDI_Tra /*! \brief Create an instance for nRF52 named */ -#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, CustomSettings) \ -BLEMIDI_NAMESPACE::BLEMIDI_Transport BLE##Name(DeviceName); \ -MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport &)BLE##Name); +#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ +BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> BLE##Name(DeviceName); \ +MIDI_NAMESPACE::MidiInterface, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> &)BLE##Name); /*! \brief Create an instance for nRF52 named */ From b91186d6a146fab487d56fdb8bd5a4bbcbb378d6 Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Thu, 2 Jun 2022 15:59:14 +0200 Subject: [PATCH 09/48] added BLE Client template arguments modig --- examples/MidiBle_Client/MidiBle_Client.ino | 6 +- src/hardware/BLEMIDI_Client_ESP32.h | 65 ++++++++++++---------- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/examples/MidiBle_Client/MidiBle_Client.ino b/examples/MidiBle_Client/MidiBle_Client.ino index 9a44bdf..f11f9dd 100644 --- a/examples/MidiBle_Client/MidiBle_Client.ino +++ b/examples/MidiBle_Client/MidiBle_Client.ino @@ -28,6 +28,10 @@ #include #include + struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings { + static const size_t MaxBufferSize = 16; +}; + #include //#include @@ -35,7 +39,7 @@ //#include //#include -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 diff --git a/src/hardware/BLEMIDI_Client_ESP32.h b/src/hardware/BLEMIDI_Client_ESP32.h index d1642fc..94196ae 100644 --- a/src/hardware/BLEMIDI_Client_ESP32.h +++ b/src/hardware/BLEMIDI_Client_ESP32.h @@ -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 AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks { public: @@ -220,6 +221,7 @@ class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks void scanEndedCB(NimBLEScanResults results); /** Define the class that performs Client Midi (nimBLE) */ +template class BLEMIDI_Client_ESP32 { private: @@ -229,15 +231,14 @@ class BLEMIDI_Client_ESP32 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 *_bleMidiTransport = nullptr; + BLEMIDI_Transport, _Settings> *_bleMidiTransport = nullptr; bool specificTarget = false; - friend class AdvertisedDeviceCallbacks; - friend class MyClientCallbacks; - friend class MIDI_NAMESPACE::MidiInterface, MySettings>; // + // TODO: somehow the forward declaration of the template class is not accepted by the compiler +// template friend MyClientCallbacks; - AdvertisedDeviceCallbacks myAdvCB; + AdvertisedDeviceCallbacks<_Settings> myAdvCB; protected: QueueHandle_t mRxQueue; @@ -247,7 +248,7 @@ class BLEMIDI_Client_ESP32 { } - bool begin(const char *, BLEMIDI_Transport *); + bool begin(const char *, BLEMIDI_Transport, _Settings> *); bool end() { @@ -294,61 +295,55 @@ class BLEMIDI_Client_ESP32 _bleMidiTransport->receive(buffer, length); } - void connectCallbacks(MIDI_NAMESPACE::MidiInterface, 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 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 @@ class MyClientCallbacks : public BLEClientCallbacks ########################################## */ -bool BLEMIDI_Client_ESP32::begin(const char *deviceName, BLEMIDI_Transport *bleMidiTransport) +template +bool BLEMIDI_Client_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Transport, _Settings> *bleMidiTransport) { _bleMidiTransport = bleMidiTransport; @@ -450,7 +446,8 @@ bool BLEMIDI_Client_ESP32::begin(const char *deviceName, BLEMIDI_Transport +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 +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 +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 +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,7 +622,14 @@ void scanEndedCB(NimBLEScanResults results) END_BLEMIDI_NAMESPACE -/*! \brief Create an instance for ESP32 named , and adviertise it like "Prefix + + Subfix" +/*! \brief Create a custom instance for ESP32 named , and advertise it like "Prefix + + Subfix" + It will try to connect to a specific server with equal name or addr than . If is "", it will connect to first midi server + */ +#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ + BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> BLE##Name(DeviceName); \ + MIDI_NAMESPACE::MidiInterface, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> &)BLE##Name); + +/*! \brief Create an instance for ESP32 named , and advertise it like "Prefix + + Subfix" It will try to connect to a specific server with equal name or addr than . If is "", it will connect to first midi server */ #define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \ From 8536bc4ddf474f592609ef1dd59a5a4c046c2b28 Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Thu, 2 Jun 2022 15:59:22 +0200 Subject: [PATCH 10/48] Update BLEMIDI_ESP32.h --- src/hardware/BLEMIDI_ESP32.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hardware/BLEMIDI_ESP32.h b/src/hardware/BLEMIDI_ESP32.h index e1898c7..dc8dc88 100644 --- a/src/hardware/BLEMIDI_ESP32.h +++ b/src/hardware/BLEMIDI_ESP32.h @@ -16,7 +16,7 @@ class BLEMIDI_ESP32 BLEAdvertising *_advertising = nullptr; BLECharacteristic *_characteristic = nullptr; - BLEMIDI_Transport, _Settings> *_bleMidiTransport = nullptr; + BLEMIDI_Transport, _Settings>* _bleMidiTransport = nullptr; template friend class MyServerCallbacks; template friend class MyCharacteristicCallbacks; @@ -168,7 +168,7 @@ bool BLEMIDI_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Transport +/*! \brief Create a customer instance for ESP32 named */ #define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> BLE##Name(DeviceName); \ From 26eeb9261ac6e94b495d20186e9416bf41b4e1a3 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Thu, 2 Jun 2022 18:42:59 +0200 Subject: [PATCH 11/48] Update BLEMIDI_Client_ESP32.h Some bug fixed related with AdvertisedDeviceCallbacks and templates. #define BLEMIDI_CREATE_INTANCE fixed --- src/hardware/BLEMIDI_Client_ESP32.h | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/hardware/BLEMIDI_Client_ESP32.h b/src/hardware/BLEMIDI_Client_ESP32.h index 94196ae..52a4b80 100644 --- a/src/hardware/BLEMIDI_Client_ESP32.h +++ b/src/hardware/BLEMIDI_Client_ESP32.h @@ -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 AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks { public: @@ -235,10 +234,7 @@ class BLEMIDI_Client_ESP32 bool specificTarget = false; - // TODO: somehow the forward declaration of the template class is not accepted by the compiler -// template friend MyClientCallbacks; - - AdvertisedDeviceCallbacks<_Settings> myAdvCB; + AdvertisedDeviceCallbacks myAdvCB; protected: QueueHandle_t mRxQueue; @@ -300,7 +296,7 @@ class BLEMIDI_Client_ESP32 void scan(); bool connect(); -public: // TODO: somehow the forward declaration of the template class is not accepted by the compiler +public: void connected() { if (_bleMidiTransport->_connectedCallback) @@ -432,7 +428,7 @@ bool BLEMIDI_Client_ESP32<_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(256, sizeof(uint8_t)); // TODO Settings::MaxBufferSize + mRxQueue = xQueueCreate(_Settings::MaxBufferSize, sizeof(uint8_t)); NimBLEDevice::setSecurityIOCap(BLEMIDI_CLIENT_SECURITY_CAP); // Attention, it may need a passkey NimBLEDevice::setSecurityAuth(BLEMIDI_CLIENT_SECURITY_AUTH); @@ -633,8 +629,8 @@ END_BLEMIDI_NAMESPACE It will try to connect to a specific server with equal name or addr than . If is "", it will connect to first midi server */ #define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \ - BLEMIDI_NAMESPACE::BLEMIDI_Transport BLE##Name(DeviceName); \ - MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport &)BLE##Name); + BLEMIDI_NAMESPACE::BLEMIDI_Transport, BLEMIDI_NAMESPACE::DefaultSettings> BLE##Name(DeviceName); \ + MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::DefaultSettings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, BLEMIDI_NAMESPACE::DefaultSettings> &)BLE##Name); /*! \brief Create a default instance for ESP32 named BLEMIDI-CLIENT. It will try to connect to first midi ble server found. From c8334f6b02a6c75df441d3dd24ee0603aa8321f4 Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Sat, 4 Jun 2022 15:09:46 +0200 Subject: [PATCH 12/48] rewrote non-customer constructors --- src/hardware/BLEMIDI_ArduinoBLE.h | 51 ++++++++++++++--------------- src/hardware/BLEMIDI_Client_ESP32.h | 5 ++- src/hardware/BLEMIDI_ESP32.h | 7 ++-- src/hardware/BLEMIDI_ESP32_NimBLE.h | 7 ++-- src/hardware/BLEMIDI_nRF52.h | 9 +++-- 5 files changed, 37 insertions(+), 42 deletions(-) diff --git a/src/hardware/BLEMIDI_ArduinoBLE.h b/src/hardware/BLEMIDI_ArduinoBLE.h index fd5007b..8a232bb 100644 --- a/src/hardware/BLEMIDI_ArduinoBLE.h +++ b/src/hardware/BLEMIDI_ArduinoBLE.h @@ -2,15 +2,17 @@ #include -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 +//#define BLE_EVENTS // TODO: requires static function (don't like) BEGIN_BLEMIDI_NAMESPACE -template +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 + +template class Fifo { public: const size_t size; //speculative feature, in case it's needed @@ -64,10 +66,10 @@ template class BLEMIDI_ArduinoBLE { private: - static BLEMIDI_Transport, _Settings>* _bleMidiTransport; - static BLEDevice* _central; + BLEMIDI_Transport, _Settings>* _bleMidiTransport; + BLEDevice* _central; - Fifo mRxBuffer; + Fifo mRxBuffer; public: BLEMIDI_ArduinoBLE() @@ -78,7 +80,6 @@ class BLEMIDI_ArduinoBLE void end() { - } void write(uint8_t* buffer, size_t length) @@ -112,8 +113,8 @@ class BLEMIDI_ArduinoBLE return false; #endif #ifdef BLE_EVENTS -/ BLE.poll(); - return ; // ?? + BLE.poll(); + return false; #endif } @@ -124,7 +125,7 @@ class BLEMIDI_ArduinoBLE } protected: - static void receive(const unsigned char* buffer, size_t length) + void receive(const unsigned char* buffer, size_t length) { // forward the buffer so it can be parsed _bleMidiTransport->receive((uint8_t*)buffer, length); @@ -160,14 +161,15 @@ class BLEMIDI_ArduinoBLE return true; } - static void blePeripheralConnectHandler(BLEDevice central) + void blePeripheralConnectHandler(BLEDevice central) { _central = ¢ral; + if (_bleMidiTransport->_connectedCallback) _bleMidiTransport->_connectedCallback(); } - static void blePeripheralDisconnectHandler(BLEDevice central) + void blePeripheralDisconnectHandler(BLEDevice central) { if (_bleMidiTransport->_disconnectedCallback) _bleMidiTransport->_disconnectedCallback(); @@ -175,7 +177,7 @@ class BLEMIDI_ArduinoBLE _central = nullptr; } - static void characteristicWritten(BLEDevice central, BLECharacteristic characteristic) { + void characteristicWritten(BLEDevice central, BLECharacteristic characteristic) { auto buffer = characteristic.value(); auto length = characteristic.valueLength(); @@ -184,14 +186,12 @@ class BLEMIDI_ArduinoBLE } }; -BLEMIDI_Transport, _Settings>* BLEMIDI_ArduinoBLE::_bleMidiTransport = nullptr; -BLEDevice* BLEMIDI_ArduinoBLE::_central = nullptr; - template bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char* deviceName, BLEMIDI_Transport, _Settings>* bleMidiTransport) { _bleMidiTransport = bleMidiTransport; + // initialize the Bluetooth® Low Energy hardware if (!BLE.begin()) return false; @@ -219,21 +219,20 @@ bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char* deviceName, BLEMIDI_Transp return true; } - /*! \brief Create an instance for nRF52 named + /*! \brief Create an instance for ArduinoBLE */ #define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ -BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> BLE##Name(DeviceName); \ -MIDI_NAMESPACE::MidiInterface, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> &)BLE##Name); + BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> BLE##Name(DeviceName); \ + MIDI_NAMESPACE::MidiInterface, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> &)BLE##Name); - /*! \brief Create an instance for nRF52 named + /*! \brief Create an instance for ArduinoBLE */ #define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \ -BLEMIDI_NAMESPACE::BLEMIDI_Transport BLE##Name(DeviceName); \ -MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport &)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 diff --git a/src/hardware/BLEMIDI_Client_ESP32.h b/src/hardware/BLEMIDI_Client_ESP32.h index 94196ae..0c1fcd0 100644 --- a/src/hardware/BLEMIDI_Client_ESP32.h +++ b/src/hardware/BLEMIDI_Client_ESP32.h @@ -632,9 +632,8 @@ END_BLEMIDI_NAMESPACE /*! \brief Create an instance for ESP32 named , and advertise it like "Prefix + + Subfix" It will try to connect to a specific server with equal name or addr than . If is "", it will connect to first midi server */ -#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \ - BLEMIDI_NAMESPACE::BLEMIDI_Transport BLE##Name(DeviceName); \ - MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport &)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 BLEMIDI-CLIENT. It will try to connect to first midi ble server found. diff --git a/src/hardware/BLEMIDI_ESP32.h b/src/hardware/BLEMIDI_ESP32.h index dc8dc88..955f62c 100644 --- a/src/hardware/BLEMIDI_ESP32.h +++ b/src/hardware/BLEMIDI_ESP32.h @@ -170,15 +170,14 @@ bool BLEMIDI_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Transport */ -#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ +#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> BLE##Name(DeviceName); \ MIDI_NAMESPACE::MidiInterface, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> &)BLE##Name); /*! \brief Create an instance for ESP32 named */ -#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \ - BLEMIDI_NAMESPACE::BLEMIDI_Transport BLE##Name(DeviceName); \ - MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport &)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 */ diff --git a/src/hardware/BLEMIDI_ESP32_NimBLE.h b/src/hardware/BLEMIDI_ESP32_NimBLE.h index 702114a..8f9a237 100644 --- a/src/hardware/BLEMIDI_ESP32_NimBLE.h +++ b/src/hardware/BLEMIDI_ESP32_NimBLE.h @@ -162,15 +162,14 @@ bool BLEMIDI_ESP32_NimBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Tran /*! \brief Create an instance for ESP32 named */ -#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ +#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> BLE##Name(DeviceName); \ MIDI_NAMESPACE::MidiInterface, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> &)BLE##Name); /*! \brief Create an instance for ESP32 named */ -#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \ - BLEMIDI_NAMESPACE::BLEMIDI_Transport BLE##Name(DeviceName); \ - MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport &)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 */ diff --git a/src/hardware/BLEMIDI_nRF52.h b/src/hardware/BLEMIDI_nRF52.h index 429c7c4..9e2da5e 100644 --- a/src/hardware/BLEMIDI_nRF52.h +++ b/src/hardware/BLEMIDI_nRF52.h @@ -119,18 +119,17 @@ bool BLEMIDI_nRF52<_Settings>::begin(const char* deviceName, BLEMIDI_NAMESPACE:: /*! \brief Create an instance for nRF52 named */ #define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ -BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> BLE##Name(DeviceName); \ -MIDI_NAMESPACE::MidiInterface, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> &)BLE##Name); + BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> BLE##Name(DeviceName); \ + MIDI_NAMESPACE::MidiInterface, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> &)BLE##Name); /*! \brief Create an instance for nRF52 named */ #define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \ -BLEMIDI_NAMESPACE::BLEMIDI_Transport BLE##Name(DeviceName); \ -MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport &)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 From 8c605fea375a744c47c87e06f958734c98dc3cc4 Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Sat, 4 Jun 2022 22:33:24 +0200 Subject: [PATCH 13/48] Update BLEMIDI_Transport.h removed logging --- src/BLEMIDI_Transport.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/BLEMIDI_Transport.h b/src/BLEMIDI_Transport.h index bf5b830..d37d943 100644 --- a/src/BLEMIDI_Transport.h +++ b/src/BLEMIDI_Transport.h @@ -55,9 +55,6 @@ class BLEMIDI_Transport void begin() { mBleClass.begin(mDeviceName, this); - - Serial.print("size : "); - Serial.println(_Settings::MaxBufferSize); } void end() From edd2f21a399c09a4daaf095ea07c8e8d9669e473 Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Sat, 4 Jun 2022 22:34:11 +0200 Subject: [PATCH 14/48] Update BLEMIDI_ArduinoBLE.h added : _midiChar.writeValue((uint8_t)0); otherwise the device will disconnect immediately --- src/hardware/BLEMIDI_ArduinoBLE.h | 191 +++++++++++++++--------------- 1 file changed, 98 insertions(+), 93 deletions(-) diff --git a/src/hardware/BLEMIDI_ArduinoBLE.h b/src/hardware/BLEMIDI_ArduinoBLE.h index 8a232bb..5209dfa 100644 --- a/src/hardware/BLEMIDI_ArduinoBLE.h +++ b/src/hardware/BLEMIDI_ArduinoBLE.h @@ -7,53 +7,49 @@ BEGIN_BLEMIDI_NAMESPACE -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 - -template -class Fifo { +template +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; @@ -65,56 +61,58 @@ class Fifo { template class BLEMIDI_ArduinoBLE { -private: - BLEMIDI_Transport, _Settings>* _bleMidiTransport; - BLEDevice* _central; +private: + BLEMIDI_Transport, _Settings> *_bleMidiTransport; + BLEDevice *_central; + + BLEService _midiService; + BLECharacteristic _midiChar; Fifo mRxBuffer; public: - BLEMIDI_ArduinoBLE() + BLEMIDI_ArduinoBLE() : _midiService(SERVICE_UUID), + _midiChar(CHARACTERISTIC_UUID, BLERead | BLEWrite | BLENotify | BLEWriteWithoutResponse, _Settings::MaxBufferSize) { } - - bool begin(const char*, BLEMIDI_Transport, _Settings>*); - - void end() + + bool begin(const char *, BLEMIDI_Transport, _Settings> *); + + void end() { } - void write(uint8_t* buffer, size_t length) + void write(uint8_t *buffer, size_t length) { - // TODO: test length - ((BLECharacteristic)midiChar).writeValue(buffer, length); + if (length > 0) + ((BLECharacteristic)_midiChar).writeValue(buffer, length); } - - bool available(byte* pvBuffer) - { - #ifdef BLE_POLLING - if (mRxBuffer.count() > 0) { + bool available(byte *pvBuffer) + { +#ifdef BLE_POLLING + 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 false; + return false; #endif } @@ -125,59 +123,64 @@ class BLEMIDI_ArduinoBLE } protected: - 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() - { + void receive(const unsigned char *buffer, size_t length) + { + if (length > 0) + _bleMidiTransport->receive((uint8_t *)buffer, length); + } + + bool poll() + { BLEDevice central = BLE.central(); - if (!central) { - if (_central) { + if (!central) + { + if (_central) + { BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler(*_central); _central = nullptr; } return false; } - if (!central.connected()) { + if (!central.connected()) return false; - } - if (nullptr == _central) { + if (nullptr == _central) + { BLEMIDI_ArduinoBLE::blePeripheralConnectHandler(central); _central = ¢ral; } - else { - if (*_central != central) { + else + { + if (*_central != central) + { BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler(*_central); BLEMIDI_ArduinoBLE::blePeripheralConnectHandler(central); _central = ¢ral; - } + } } return true; } - void blePeripheralConnectHandler(BLEDevice central) - { + void blePeripheralConnectHandler(BLEDevice central) + { _central = ¢ral; - if (_bleMidiTransport->_connectedCallback) - _bleMidiTransport->_connectedCallback(); - } + if (_bleMidiTransport->_connectedCallback) + _bleMidiTransport->_connectedCallback(); + } - void blePeripheralDisconnectHandler(BLEDevice central) - { - if (_bleMidiTransport->_disconnectedCallback) - _bleMidiTransport->_disconnectedCallback(); + void blePeripheralDisconnectHandler(BLEDevice central) + { + if (_bleMidiTransport->_disconnectedCallback) + _bleMidiTransport->_disconnectedCallback(); _central = nullptr; - } + } - void characteristicWritten(BLEDevice central, BLECharacteristic characteristic) { + void characteristicWritten(BLEDevice central, BLECharacteristic characteristic) + { auto buffer = characteristic.value(); auto length = characteristic.valueLength(); @@ -187,50 +190,52 @@ class BLEMIDI_ArduinoBLE }; template -bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char* deviceName, BLEMIDI_Transport, _Settings>* bleMidiTransport) +bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transport, _Settings> *bleMidiTransport) { - _bleMidiTransport = bleMidiTransport; + _bleMidiTransport = bleMidiTransport; // initialize the Bluetooth® Low Energy hardware - if (!BLE.begin()) + 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); + + // set the initial value for the characeristic: + // (when not set, the device will disconnect after 0.5 seconds) + _midiChar.writeValue((uint8_t)0); #ifdef BLE_EVENTS // assign event handlers for connected, disconnected to peripheral - BLE.setEventHandler(BLEConnected, BLEMIDI_ArduinoBLE::blePeripheralConnectHandler); + BLE.setEventHandler(BLEConnected, BLEMIDI_ArduinoBLE::blePeripheralConnectHandler); BLE.setEventHandler(BLEDisconnected, BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler); - midiChar.setEventHandler(BLEWritten, characteristicWritten); + _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 + /* 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 ArduinoBLE +/*! \brief Create an instance for ArduinoBLE */ -#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ +#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> BLE##Name(DeviceName); \ MIDI_NAMESPACE::MidiInterface, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> &)BLE##Name); - /*! \brief Create an instance for ArduinoBLE +/*! \brief Create an instance for ArduinoBLE */ #define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \ - BLEMIDI_CREATE_CUSTOM_INSTANCE (DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettings) + BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettings) - /*! \brief Create a default instance for ArduinoBLE named BLE-MIDI +/*! \brief Create a default instance for ArduinoBLE named BLE-MIDI */ #define BLEMIDI_CREATE_DEFAULT_INSTANCE() \ BLEMIDI_CREATE_INSTANCE("BLE-MIDI", MIDI) From 3c789b0b5f3ed24c1562275ea0bfbb718692e39b Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Sun, 5 Jun 2022 10:26:47 +0200 Subject: [PATCH 15/48] removed all event code cleanup - removed all polling code --- src/hardware/BLEMIDI_ArduinoBLE.h | 38 +++++-------------------------- 1 file changed, 6 insertions(+), 32 deletions(-) diff --git a/src/hardware/BLEMIDI_ArduinoBLE.h b/src/hardware/BLEMIDI_ArduinoBLE.h index 5209dfa..59397c9 100644 --- a/src/hardware/BLEMIDI_ArduinoBLE.h +++ b/src/hardware/BLEMIDI_ArduinoBLE.h @@ -2,9 +2,6 @@ #include -#define BLE_POLLING -//#define BLE_EVENTS // TODO: requires static function (don't like) - BEGIN_BLEMIDI_NAMESPACE template @@ -90,7 +87,6 @@ class BLEMIDI_ArduinoBLE bool available(byte *pvBuffer) { -#ifdef BLE_POLLING if (mRxBuffer.count() > 0) { *pvBuffer = mRxBuffer.dequeue(); @@ -109,11 +105,6 @@ class BLEMIDI_ArduinoBLE } } return false; -#endif -#ifdef BLE_EVENTS - BLE.poll(); - return false; -#endif } void add(byte value) @@ -136,7 +127,7 @@ class BLEMIDI_ArduinoBLE { if (_central) { - BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler(*_central); + onDisconnected(*_central); _central = nullptr; } return false; @@ -147,15 +138,15 @@ class BLEMIDI_ArduinoBLE if (nullptr == _central) { - BLEMIDI_ArduinoBLE::blePeripheralConnectHandler(central); + onConnected(central); _central = ¢ral; } else { if (*_central != central) { - BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler(*_central); - BLEMIDI_ArduinoBLE::blePeripheralConnectHandler(central); + onDisconnected(*_central); + onConnected(central); _central = ¢ral; } } @@ -163,7 +154,7 @@ class BLEMIDI_ArduinoBLE return true; } - void blePeripheralConnectHandler(BLEDevice central) + void onConnected(BLEDevice central) { _central = ¢ral; @@ -171,22 +162,13 @@ class BLEMIDI_ArduinoBLE _bleMidiTransport->_connectedCallback(); } - void blePeripheralDisconnectHandler(BLEDevice central) + void onDisconnected(BLEDevice central) { if (_bleMidiTransport->_disconnectedCallback) _bleMidiTransport->_disconnectedCallback(); _central = nullptr; } - - void characteristicWritten(BLEDevice central, BLECharacteristic characteristic) - { - auto buffer = characteristic.value(); - auto length = characteristic.valueLength(); - - if (length > 0) - receive(buffer, length); - } }; template @@ -208,14 +190,6 @@ bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transp // (when not set, the device will disconnect after 0.5 seconds) _midiChar.writeValue((uint8_t)0); -#ifdef BLE_EVENTS - // assign event handlers for connected, disconnected to peripheral - BLE.setEventHandler(BLEConnected, BLEMIDI_ArduinoBLE::blePeripheralConnectHandler); - BLE.setEventHandler(BLEDisconnected, BLEMIDI_ArduinoBLE::blePeripheralDisconnectHandler); - - _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 */ From 75e994c95d8ce3319b71934a47df9afc8b6b6050 Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Sun, 5 Jun 2022 14:11:40 +0200 Subject: [PATCH 16/48] working on nRF52 --- .../CustomerBufferSize/CustomerBufferSize.ino | 1 - examples/MidiBle/MidiBle.ino | 1 - examples/MidiBle_Client/MidiBle_Client.ino | 1 - examples/SysEx_Receive/SysEx_Receive.ino | 1 - examples/SysEx_Send/SysEx_Send.ino | 1 - src/hardware/BLEMIDI_nRF52.h | 64 +++++++++++++------ 6 files changed, 43 insertions(+), 26 deletions(-) diff --git a/examples/CustomerBufferSize/CustomerBufferSize.ino b/examples/CustomerBufferSize/CustomerBufferSize.ino index 3303207..3dc8515 100644 --- a/examples/CustomerBufferSize/CustomerBufferSize.ino +++ b/examples/CustomerBufferSize/CustomerBufferSize.ino @@ -6,7 +6,6 @@ #include //#include -//#include //#include BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-NimBLE-MIDI", MIDI, CustomBufferSizeSettings); diff --git a/examples/MidiBle/MidiBle.ino b/examples/MidiBle/MidiBle.ino index b1cfa58..69968b1 100644 --- a/examples/MidiBle/MidiBle.ino +++ b/examples/MidiBle/MidiBle.ino @@ -2,7 +2,6 @@ //#include #include -//#include //#include BLEMIDI_CREATE_DEFAULT_INSTANCE() diff --git a/examples/MidiBle_Client/MidiBle_Client.ino b/examples/MidiBle_Client/MidiBle_Client.ino index f11f9dd..5109890 100644 --- a/examples/MidiBle_Client/MidiBle_Client.ino +++ b/examples/MidiBle_Client/MidiBle_Client.ino @@ -36,7 +36,6 @@ //#include //#include -//#include //#include BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-BLE-MIDI", MIDI, CustomBufferSizeSettings); // Connect to first server found diff --git a/examples/SysEx_Receive/SysEx_Receive.ino b/examples/SysEx_Receive/SysEx_Receive.ino index 98cc209..3b3adb1 100644 --- a/examples/SysEx_Receive/SysEx_Receive.ino +++ b/examples/SysEx_Receive/SysEx_Receive.ino @@ -2,7 +2,6 @@ //#include #include -//#include //#include BLEMIDI_CREATE_INSTANCE("CustomName", MIDI) diff --git a/examples/SysEx_Send/SysEx_Send.ino b/examples/SysEx_Send/SysEx_Send.ino index b1c28b8..9e83e60 100644 --- a/examples/SysEx_Send/SysEx_Send.ino +++ b/examples/SysEx_Send/SysEx_Send.ino @@ -2,7 +2,6 @@ #include //#include -//#include //#include byte sysex4[] = { 0xF0, 0x43, 0x20, 0xF7 }; diff --git a/src/hardware/BLEMIDI_nRF52.h b/src/hardware/BLEMIDI_nRF52.h index 9e2da5e..06544c2 100644 --- a/src/hardware/BLEMIDI_nRF52.h +++ b/src/hardware/BLEMIDI_nRF52.h @@ -2,7 +2,7 @@ // I N D E V E L O P M E N T -//#include +#include BEGIN_BLEMIDI_NAMESPACE @@ -10,13 +10,13 @@ template class BLEMIDI_nRF52 { private: -// BLEDis bledis; -// BLEMidi blemidi; + BLEDis bledis; + // BLEMidi blemidi; BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings>* _bleMidiTransport; - template friend class MyServerCallbacks; - template friend class MyCharacteristicCallbacks; + // template friend class MyServerCallbacks; + // template friend class MyCharacteristicCallbacks; public: BLEMIDI_nRF52() @@ -62,6 +62,23 @@ class BLEMIDI_nRF52 } }; +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 bool BLEMIDI_nRF52<_Settings>::begin(const char* deviceName, BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings>* bleMidiTransport) { @@ -70,34 +87,39 @@ bool BLEMIDI_nRF52<_Settings>::begin(const char* deviceName, BLEMIDI_NAMESPACE:: // 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 @@ -108,10 +130,10 @@ 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; } From c69bb30ff1366e19280e7cc20f60948c19e60a0d Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 6 Jun 2022 16:32:10 +0200 Subject: [PATCH 17/48] Change Define Template --- src/hardware/BLEMIDI_Client_ESP32.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/hardware/BLEMIDI_Client_ESP32.h b/src/hardware/BLEMIDI_Client_ESP32.h index 52a4b80..780855e 100644 --- a/src/hardware/BLEMIDI_Client_ESP32.h +++ b/src/hardware/BLEMIDI_Client_ESP32.h @@ -628,9 +628,8 @@ END_BLEMIDI_NAMESPACE /*! \brief Create an instance for ESP32 named , and advertise it like "Prefix + + Subfix" It will try to connect to a specific server with equal name or addr than . If is "", it will connect to first midi server */ -#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \ - BLEMIDI_NAMESPACE::BLEMIDI_Transport, BLEMIDI_NAMESPACE::DefaultSettings> BLE##Name(DeviceName); \ - MIDI_NAMESPACE::MidiInterface, BLEMIDI_NAMESPACE::DefaultSettings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, BLEMIDI_NAMESPACE::DefaultSettings> &)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 BLEMIDI-CLIENT. It will try to connect to first midi ble server found. From 53e01392be704168ed30c1bda3319a2b9239ab78 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Tue, 21 Jun 2022 18:46:23 +0200 Subject: [PATCH 18/48] Debug Verbose Serial.print changed by debugging verbose. It may be abled using #define MIDIBLECLIENTVERBOSE --- src/hardware/BLEMIDI_Client_ESP32.h | 48 ++++++++++++++++------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/hardware/BLEMIDI_Client_ESP32.h b/src/hardware/BLEMIDI_Client_ESP32.h index 780855e..33e7fdb 100644 --- a/src/hardware/BLEMIDI_Client_ESP32.h +++ b/src/hardware/BLEMIDI_Client_ESP32.h @@ -1,5 +1,14 @@ #pragma once +//#define MIDIBLECLIENTVERBOSE + +#ifdef MIDIBLECLIENTVERBOSE +#define DEBUGCLIENT(_text_) Serial.println("DbgBC: " + (String)_text_); +#else +#define DEBUGCLIENT(_text_) ; +#endif + + /* ############################################# ########## USER DEFINES BEGINNING ########### @@ -189,11 +198,11 @@ class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks { if (enableConnection) //not begin() or end() { - Serial.print("Advertised Device found: "); - Serial.println(advertisedDevice->toString().c_str()); + DEBUGCLIENT("Advertised Device found: "); + DEBUGCLIENT(advertisedDevice->toString().c_str()); if (advertisedDevice->isAdvertisingService(NimBLEUUID(SERVICE_UUID))) { - Serial.println("Found MIDI Service"); + DEBUGCLIENT("Found MIDI Service"); if (!specificTarget || (advertisedDevice->getName() == nameTarget.c_str() || advertisedDevice->getAddress() == nameTarget)) { /** Ready to connect now */ @@ -205,7 +214,7 @@ class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks } else { - Serial.println("Name error"); + DEBUGCLIENT("Name error"); } } else @@ -332,7 +341,7 @@ class MyClientCallbacks : public BLEClientCallbacks void onConnect(BLEClient*) { - //Serial.println("##Connected##"); + DEBUGCLIENT("##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) @@ -341,8 +350,8 @@ class MyClientCallbacks : public BLEClientCallbacks void onDisconnect(BLEClient*) { - //Serial.print(pClient->getPeerAddress().toString().c_str()); - //Serial.println(" Disconnected - Starting scan"); + DEBUGCLIENT(pClient->getPeerAddress().toString().c_str()); + DEBUGCLIENT(" Disconnected - Starting scan"); if (_bluetoothEsp32) { @@ -422,7 +431,7 @@ bool BLEMIDI_Client_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Tran strDeviceName = BLEMIDI_CLIENT_NAME_PREFIX + strDeviceName + BLEMIDI_CLIENT_NAME_SUBFIX; #endif } - Serial.println(strDeviceName.c_str()); + DEBUGCLIENT(strDeviceName.c_str()); NimBLEDevice::init(strDeviceName); @@ -496,7 +505,7 @@ void BLEMIDI_Client_ESP32<_Settings>::scan() pBLEScan->setWindow(500); pBLEScan->setActiveScan(true); - Serial.println("Scanning..."); + DEBUGCLIENT("Scanning..."); pBLEScan->start(1, scanEndedCB); } }; @@ -542,7 +551,7 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect() if (NimBLEDevice::getClientListSize() >= NIMBLE_MAX_CONNECTIONS) { - Serial.println("Max clients reached - no more connections available"); + DEBUGCLIENT("Max clients reached - no more connections available"); return false; } @@ -561,29 +570,24 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect() /** Created a client but failed to connect, don't need to keep it as it has no data */ NimBLEDevice::deleteClient(_client); _client = nullptr; - //Serial.println("Failed to connect, deleted client"); + DEBUGCLIENT("Failed to connect, deleted client"); return false; } if (!_client->isConnected()) { - //Serial.println("Failed to connect"); + DEBUGCLIENT("Failed to connect"); _client->disconnect(); NimBLEDevice::deleteClient(_client); _client = nullptr; return false; } - Serial.print("Connected to: "); - Serial.print(myAdvCB.advDevice.getName().c_str()); - Serial.print(" / "); - Serial.println(_client->getPeerAddress().toString().c_str()); - - /* - Serial.print("RSSI: "); - Serial.println(_client->getRssi()); - */ + DEBUGCLIENT("Connected to: " + myAdvCB.advDevice.getName().c_str() + " / " + _client->getPeerAddress().toString().c_str()); + DEBUGCLIENT("RSSI: "); + DEBUGCLIENT(_client->getRssi()); + /** Now we can read/write/subscribe the charateristics of the services we are interested in */ pSvc = _client->getService(SERVICE_UUID); if (pSvc) /** make sure it's not null */ @@ -613,7 +617,7 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect() /** Callback to process the results of the last scan or restart it */ void scanEndedCB(NimBLEScanResults results) { - // Serial.println("Scan Ended"); + DEBUGCLIENT("Scan Ended"); } END_BLEMIDI_NAMESPACE From 39c16474b30b6dd88d106ee326be0d0fe2867f73 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Tue, 21 Jun 2022 18:53:46 +0200 Subject: [PATCH 19/48] Increased readability in onResult Improved IF-syntaxis --- src/hardware/BLEMIDI_Client_ESP32.h | 50 +++++++++++++++-------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/src/hardware/BLEMIDI_Client_ESP32.h b/src/hardware/BLEMIDI_Client_ESP32.h index 33e7fdb..a92d9f8 100644 --- a/src/hardware/BLEMIDI_Client_ESP32.h +++ b/src/hardware/BLEMIDI_Client_ESP32.h @@ -196,32 +196,34 @@ class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks protected: void onResult(NimBLEAdvertisedDevice *advertisedDevice) { - if (enableConnection) //not begin() or end() + if (!enableConnection) // not begin() or end() { - DEBUGCLIENT("Advertised Device found: "); - DEBUGCLIENT(advertisedDevice->toString().c_str()); - if (advertisedDevice->isAdvertisingService(NimBLEUUID(SERVICE_UUID))) - { - DEBUGCLIENT("Found MIDI Service"); - if (!specificTarget || (advertisedDevice->getName() == nameTarget.c_str() || advertisedDevice->getAddress() == nameTarget)) - { - /** Ready to connect now */ - doConnect = true; - /** Save the device reference in a public variable that the client can use*/ - advDevice = *advertisedDevice; - /** stop scan before connecting */ - NimBLEDevice::getScan()->stop(); - } - else - { - DEBUGCLIENT("Name error"); - } - } - else - { - doConnect = false; - } + return; + } + + DEBUGCLIENT("Advertised Device found: "); + DEBUGCLIENT(advertisedDevice->toString().c_str()); + if (!advertisedDevice->isAdvertisingService(NimBLEUUID(SERVICE_UUID))) + { + doConnect = false; + return; + } + + DEBUGCLIENT("Found MIDI Service"); + if (!(!specificTarget || (advertisedDevice->getName() == nameTarget.c_str() || advertisedDevice->getAddress() == nameTarget))) + { + DEBUGCLIENT("Name error"); + return; } + + /** Ready to connect now */ + doConnect = true; + /** Save the device reference in a public variable that the client can use*/ + advDevice = *advertisedDevice; + /** stop scan before connecting */ + NimBLEDevice::getScan()->stop(); + + return; }; }; From 24dce9ac6a2fc19ef74d70a7919c38536ec6bde3 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Tue, 21 Jun 2022 19:05:44 +0200 Subject: [PATCH 20/48] Changed #define for Struct in Config Mayor changes in all configuration setup. Create a custom Config Struct for the client, it heritages from regular repo Struct and adds specific settings of the class. All configurations, including onPassRequest function, may be configured in the upper layers of the code, like in main.c --- src/hardware/BLEMIDI_Client_ESP32.h | 359 +++++++++++----------------- 1 file changed, 145 insertions(+), 214 deletions(-) diff --git a/src/hardware/BLEMIDI_Client_ESP32.h b/src/hardware/BLEMIDI_Client_ESP32.h index a92d9f8..badbdce 100644 --- a/src/hardware/BLEMIDI_Client_ESP32.h +++ b/src/hardware/BLEMIDI_Client_ESP32.h @@ -8,179 +8,116 @@ #define DEBUGCLIENT(_text_) ; #endif +// Headers for ESP32 nimBLE +#include -/* -############################################# -########## USER DEFINES BEGINNING ########### -####### Modify only these parameters ######## -############################################# -*/ +BEGIN_BLEMIDI_NAMESPACE -/* -##### BLE DEVICE NAME ##### -*/ +using PasskeyRequestCallback = uint32_t (*)(void); -/** - * Set always the same name independently of name server - */ -//#define BLEMIDI_CLIENT_FIXED_NAME "BleMidiClient" - -#ifndef BLEMIDI_CLIENT_FIXED_NAME //Not modify -/** - * When client tries to connect to specific server, BLE name is composed as follows: - * BLEMIDI_CLIENT_NAME_PREFIX + + BLEMIDI_CLIENT_NAME_SUBFIX - * - * example: - * BLEMIDI_CLIENT_NAME_PREFIX "Client-" - * "AX-Edge" - * BLEMIDI_CLIENT_NAME_SUBFIX "-Midi1" - * - * Result: "Client-AX-Edge-Midi1" - */ -#define BLEMIDI_CLIENT_NAME_PREFIX "C-" -#define BLEMIDI_CLIENT_NAME_SUBFIX "" +static uint32_t defautlPasskeyRequest() +{ + // FILL WITH YOUR CUSTOM AUTH METHOD CODE or PASSKEY + // FOR EXAMPLE: + uint32_t passkey = 123456; -/** - * When client tries to connect to the first midi server found: - */ -#define BLEMIDI_CLIENT_DEFAULT_NAME "BLEMIDI-CLIENT" -#endif //Not modify + // Serial.println("Client Passkey Request"); -/* -###### TX POWER ##### -*/ -/** - * Set power transmision - * - * ESP_PWR_LVL_N12 // Corresponding to -12dbm Minimum - * ESP_PWR_LVL_N9 // Corresponding to -9dbm - * ESP_PWR_LVL_N6 // Corresponding to -6dbm - * ESP_PWR_LVL_N3 // Corresponding to -3dbm - * ESP_PWR_LVL_N0 // Corresponding to 0dbm - * ESP_PWR_LVL_P3 // Corresponding to +3dbm - * ESP_PWR_LVL_P6 // Corresponding to +6dbm - * ESP_PWR_LVL_P9 // Corresponding to +9dbm Maximum -*/ + /** return the passkey to send to the server */ + return passkey; +}; -#define BLEMIDI_TX_PWR ESP_PWR_LVL_P9 +struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings +{ -/* -###### SECURITY ##### -*/ + /* + ##### BLE DEVICE NAME ##### + */ + /** + * Set name of ble device (not affect to connection with server) + * max 16 characters + */ + static constexpr char *name = "BleMidiClient"; + + /* + ###### TX POWER ##### + */ + /** + * Set power transmision + * + * ESP_PWR_LVL_N12 // Corresponding to -12dbm Minimum + * ESP_PWR_LVL_N9 // Corresponding to -9dbm + * ESP_PWR_LVL_N6 // Corresponding to -6dbm + * ESP_PWR_LVL_N3 // Corresponding to -3dbm + * ESP_PWR_LVL_N0 // Corresponding to 0dbm + * ESP_PWR_LVL_P3 // Corresponding to +3dbm + * ESP_PWR_LVL_P6 // Corresponding to +6dbm + * ESP_PWR_LVL_P9 // Corresponding to +9dbm Maximum + */ + static const esp_power_level_t clientTXPwr = ESP_PWR_LVL_P9; -/** Set the IO capabilities of the device, each option will trigger a different pairing method. + /* + ###### SECURITY ##### + */ + /** Set the IO capabilities of the device, each option will trigger a different pairing method. * BLE_HS_IO_KEYBOARD_ONLY - Passkey pairing * BLE_HS_IO_DISPLAY_YESNO - Numeric comparison pairing * BLE_HS_IO_NO_INPUT_OUTPUT - DEFAULT setting - just works pairing */ -#define BLEMIDI_CLIENT_SECURITY_CAP BLE_HS_IO_NO_INPUT_OUTPUT + static const uint8_t clientSecurityCapabilities = BLE_HS_IO_NO_INPUT_OUTPUT; -/** Set the security method. + /** Set the security method. * bonding * man in the middle protection * pair. secure connections - * + * * More info in nimBLE lib - * - * Uncomment what you need - * These are the default values. - * You can select some simultaneously. */ -#define BLEMIDI_CLIENT_BOND -//#define BLEMIDI_CLIENT_MITM -#define BLEMIDI_CLIENT_PAIR - -/** - * This callback function defines what will be done when server requieres PassKey. - * Add your custom code here. - */ -static uint32_t userOnPassKeyRequest() -{ - //FILL WITH YOUR CUSTOM AUTH METHOD CODE or PASSKEY - //FOR EXAMPLE: - uint32_t passkey = 123456; - - //Serial.println("Client Passkey Request"); + static const bool clientBond = true; + static const bool clientMITM = false; + static const bool clientPair = true; - /** return the passkey to send to the server */ - return passkey; -}; + /** + * This callback function defines what will be done when server requieres PassKey. + * Add your custom code here. + */ + static constexpr PasskeyRequestCallback userOnPassKeyRequest = defautlPasskeyRequest; /* -###### BLE COMMUNICATION PARAMS ###### -*/ - /** Set connection parameters: - * If you only use one connection, put recomended BLE server param communication - * (you may scan it ussing "nRF Connect" app or other similar apps). - * - * If you use more than one connection adjust, for example, settings like 15ms interval, 0 latency, 120ms timout. - * These settings may be safe for 3 clients to connect reliably, set faster values if you have less - * connections. - * - * Min interval (unit: 1.25ms): 12 * 1.25ms = 15 ms, - * Max interval (unit: 1.25ms): 12 * 1.25ms = 15, - * 0 latency (Number of intervals allowed to skip), - * TimeOut (unit: 10ms) 51 * 10ms = 510ms. Timeout should be minimum 100ms. - */ -#define BLEMIDI_CLIENT_COMM_MIN_INTERVAL 6 // 7.5ms -#define BLEMIDI_CLIENT_COMM_MAX_INTERVAL 35 // 40ms -#define BLEMIDI_CLIENT_COMM_LATENCY 0 // -#define BLEMIDI_CLIENT_COMM_TIMEOUT 200 //2000ms - -/* -###### BLE FORCE NEW CONNECTION ###### -*/ - -/** - * This parameter force to skip the "soft-reconnection" and force to create a new connection after a disconnect event. - * "Soft-reconnection" save some time and energy in comparation with performming a new connection, but some BLE devices - * don't support or don't perform correctly a "soft-reconnection" after a disconnection event. - * Uncomment this define if your device doesn't work propertily after a reconnection. - * -*/ -//#define BLEMIDI_FORCE_NEW_CONNECTION - -/** - * -*/ - -/* -############################################# -############ USER DEFINES END ############### -############################################# -*/ - -// Headers for ESP32 nimBLE -#include - -BEGIN_BLEMIDI_NAMESPACE - -#ifdef BLEMIDI_CLIENT_BOND -#define BLEMIDI_CLIENT_BOND_DUMMY BLE_SM_PAIR_AUTHREQ_BOND -#else -#define BLEMIDI_CLIENT_BOND_DUMMY 0x00 -#endif - -#ifdef BLEMIDI_CLIENT_MITM -#define BLEMIDI_CLIENT_MITM_DUMMY BLE_SM_PAIR_AUTHREQ_MITM -#else -#define BLEMIDI_CLIENT_MITM_DUMMY 0x00 -#endif - -#ifdef BLEMIDI_CLIENT_PAIR -#define BLEMIDI_CLIENT_PAIR_DUMMY BLE_SM_PAIR_AUTHREQ_SC -#else -#define BLEMIDI_CLIENT_PAIR_DUMMY 0x00 -#endif + ###### BLE COMMUNICATION PARAMS ###### + */ + /** Set connection parameters: + * If you only use one connection, put recomended BLE server param communication + * (you may scan it ussing "nRF Connect" app or other similar apps). + * + * If you use more than one connection adjust, for example, settings like 15ms interval, 0 latency, 120ms timout. + * These settings may be safe for 3 clients to connect reliably, set faster values if you have less + * connections. + * + * Min interval (unit: 1.25ms): 12 * 1.25ms = 15 ms, + * Max interval (unit: 1.25ms): 12 * 1.25ms = 15, + * 0 latency (Number of intervals allowed to skip), + * TimeOut (unit: 10ms) 51 * 10ms = 510ms. Timeout should be minimum 100ms. + */ + static const uint16_t commMinInterval = 6; // 7.5ms + static const uint16_t commMaxInterval = 35; // 40ms + static const uint16_t commLatency = 0; // + static const uint16_t commTimeOut = 200; // 2000ms -/** Set the security method. - * bonding - * man in the middle protection - * pair. secure connections - * - * More info in nimBLE lib + /* + ###### BLE FORCE NEW CONNECTION ###### + */ + + /** + * This parameter force to skip the "soft-reconnection" and force to create a new connection after a disconnect event. + * "Soft-reconnection" save some time and energy in comparation with performming a new connection, but some BLE devices + * don't support or don't perform correctly a "soft-reconnection" after a disconnection event. + * Set to "true" if your device doesn't work propertily after a reconnection. + * */ -#define BLEMIDI_CLIENT_SECURITY_AUTH (BLEMIDI_CLIENT_BOND_DUMMY | BLEMIDI_CLIENT_MITM_DUMMY | BLEMIDI_CLIENT_PAIR_DUMMY) + static const bool forceNewConnection = false; +}; + /** Define a class to handle the callbacks when advertisments are received */ class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks @@ -239,7 +176,7 @@ class BLEMIDI_Client_ESP32 BLEAdvertising *_advertising = nullptr; BLERemoteCharacteristic *_characteristic = 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, _Settings> *_bleMidiTransport = nullptr; @@ -307,7 +244,7 @@ class BLEMIDI_Client_ESP32 void scan(); bool connect(); -public: +public: void connected() { if (_bleMidiTransport->_connectedCallback) @@ -338,19 +275,21 @@ class MyClientCallbacks : public BLEClientCallbacks uint32_t onPassKeyRequest() { - return userOnPassKeyRequest(); + //if (nullptr != _Settings::userOnPassKeyRequest) + return _Settings::userOnPassKeyRequest(); + //return 0; }; - void onConnect(BLEClient*) + void onConnect(BLEClient *pClient) { DEBUGCLIENT("##Connected##"); - //pClient->updateConnParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT); + //pClient->updateConnParams(_Settings::commMinInterval, _Settings::commMaxInterval, _Settings::commLatency, _Settings::commTimeOut); vTaskDelay(1); if (_bluetoothEsp32) _bluetoothEsp32->connected(); }; - void onDisconnect(BLEClient*) + void onDisconnect(BLEClient *pClient) { DEBUGCLIENT(pClient->getPeerAddress().toString().c_str()); DEBUGCLIENT(" Disconnected - Starting scan"); @@ -358,38 +297,34 @@ class MyClientCallbacks : public BLEClientCallbacks if (_bluetoothEsp32) { _bluetoothEsp32->disconnected(); -#ifdef BLEMIDI_FORCE_NEW_CONNECTION - // Try reconnection or search a new one - _bluetoothEsp32->scan(); -#endif // BLEMIDI_FORCE_NEW_CONNECTION } -#ifdef BLEMIDI_FORCE_NEW_CONNECTION +if (_Settings::forceNewConnection) +{ // Renew Client NimBLEDevice::deleteClient(pClient); - NimBLEDevice::createClient(); pClient = nullptr; -#endif // BLEMIDI_FORCE_NEW_CONNECTION +} - //Try reconnection or search a new one + // Try reconnection or search a new one NimBLEDevice::getScan()->start(1, scanEndedCB); } - bool onConnParamsUpdateRequest(NimBLEClient *pClient, const ble_gap_upd_params *params) + bool onConnParamsUpdateRequest(NimBLEClient *pClient, const ble_gap_upd_params *params) { - if (params->itvl_min < BLEMIDI_CLIENT_COMM_MIN_INTERVAL) + if (params->itvl_min < _Settings::commMinInterval) { /** 1.25ms units */ return false; } - else if (params->itvl_max > BLEMIDI_CLIENT_COMM_MAX_INTERVAL) + else if (params->itvl_max > _Settings::commMaxInterval) { /** 1.25ms units */ return false; } - else if (params->latency > BLEMIDI_CLIENT_COMM_LATENCY) + else if (params->latency > _Settings::commLatency) { /** Number of intervals allowed to skip */ return false; } - else if (params->supervision_timeout > BLEMIDI_CLIENT_COMM_TIMEOUT) + else if (params->supervision_timeout > _Settings::commMinInterval) { /** 10ms units */ return false; } @@ -415,24 +350,18 @@ bool BLEMIDI_Client_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Tran { myAdvCB.specificTarget = false; myAdvCB.nameTarget = ""; - -#ifdef BLEMIDI_CLIENT_FIXED_NAME - strDeviceName = BLEMIDI_CLIENT_FIXED_NAME; -#else - strDeviceName = BLEMIDI_CLIENT_DEFAULT_NAME; -#endif } else // Connect to a specific name or address { myAdvCB.specificTarget = true; myAdvCB.nameTarget = strDeviceName; - -#ifdef BLEMIDI_CLIENT_FIXED_NAME - strDeviceName = BLEMIDI_CLIENT_FIXED_NAME; -#else - strDeviceName = BLEMIDI_CLIENT_NAME_PREFIX + strDeviceName + BLEMIDI_CLIENT_NAME_SUBFIX; -#endif } + + static char array[16] = "patata"; + + memcpy(array, _Settings::name, 16); + strDeviceName = array; + DEBUGCLIENT(strDeviceName.c_str()); NimBLEDevice::init(strDeviceName); @@ -441,11 +370,11 @@ bool BLEMIDI_Client_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Tran // Core_0 runs here, core_1 runs the BLE stack mRxQueue = xQueueCreate(_Settings::MaxBufferSize, sizeof(uint8_t)); - NimBLEDevice::setSecurityIOCap(BLEMIDI_CLIENT_SECURITY_CAP); // Attention, it may need a passkey - NimBLEDevice::setSecurityAuth(BLEMIDI_CLIENT_SECURITY_AUTH); + NimBLEDevice::setSecurityIOCap(_Settings::clientSecurityCapabilities); // Attention, it may need a passkey + NimBLEDevice::setSecurityAuth(_Settings::clientBond, _Settings::clientMITM, _Settings::clientPair); /** Optional: set the transmit power, default is 3db */ - NimBLEDevice::setPower(BLEMIDI_TX_PWR); /** +9db */ + NimBLEDevice::setPower(_Settings::clientTXPwr); /** +9db */ myAdvCB.enableConnection = true; scan(); @@ -456,37 +385,36 @@ bool BLEMIDI_Client_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Tran template bool BLEMIDI_Client_ESP32<_Settings>::available(byte *pvBuffer) { - if (myAdvCB.enableConnection) + if (!myAdvCB.enableConnection) + { + return false; + } + + if (_client == nullptr || !_client->isConnected()) // Try to connect/reconnect { - if (_client == nullptr || !_client->isConnected()) //Try to connect/reconnect + if (myAdvCB.doConnect) { - if (myAdvCB.doConnect) - { - myAdvCB.doConnect = false; - if (!connect()) - { - scan(); - } - } - else if (myAdvCB.scanDone) + myAdvCB.doConnect = false; + if (!connect()) { scan(); } } - // return 1 byte from the Queue - return xQueueReceive(mRxQueue, (void *)pvBuffer, 0); // return immediately when the queue is empty - } - else - { - return false; + else if (myAdvCB.scanDone) + { + scan(); + } } + + // return 1 byte from the Queue + return xQueueReceive(mRxQueue, (void *)pvBuffer, 0); // return immediately when the queue is empty } /** Notification receiving handler callback */ template 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 { receive(pData, length); } @@ -517,7 +445,9 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect() { using namespace std::placeholders; //<- for bind funtion in callback notification -#ifndef BLEMIDI_FORCE_NEW_CONNECTION +//Retry to connecto to last one +if (!_Settings::forceNewConnection) +{ /** Check if we have a client we should reuse first * Special case when we already know this device * This saves considerable time and power. @@ -533,7 +463,7 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect() { if (_characteristic->subscribe(true, std::bind(&BLEMIDI_Client_ESP32::notifyCB, this, _1, _2, _3, _4))) { - //Re-connection SUCCESS + // Re-connection SUCCESS return true; } } @@ -549,7 +479,8 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect() NimBLEDevice::deleteClient(_client); _client = nullptr; } -#endif //BLEMIDI_FORCE_NEW_CONNECTION +} + if (NimBLEDevice::getClientListSize() >= NIMBLE_MAX_CONNECTIONS) { @@ -562,7 +493,7 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect() _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); + _client->setConnectionParams(_Settings::commMinInterval,_Settings::commMaxInterval, _Settings::commLatency, _Settings::commTimeOut); /** Set how long we are willing to wait for the connection to complete (seconds), default is 30. */ _client->setConnectTimeout(15); @@ -586,10 +517,10 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect() } DEBUGCLIENT("Connected to: " + myAdvCB.advDevice.getName().c_str() + " / " + _client->getPeerAddress().toString().c_str()); - + DEBUGCLIENT("RSSI: "); DEBUGCLIENT(_client->getRssi()); - + /** Now we can read/write/subscribe the charateristics of the services we are interested in */ pSvc = _client->getService(SERVICE_UUID); if (pSvc) /** make sure it's not null */ @@ -602,14 +533,14 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect() { if (_characteristic->subscribe(true, std::bind(&BLEMIDI_Client_ESP32::notifyCB, this, _1, _2, _3, _4))) { - //Connection SUCCESS + // Connection SUCCESS return true; } } } } - //If anything fails, disconnect and delete client + // If anything fails, disconnect and delete client _client->disconnect(); NimBLEDevice::deleteClient(_client); _client = nullptr; @@ -619,7 +550,7 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect() /** Callback to process the results of the last scan or restart it */ void scanEndedCB(NimBLEScanResults results) { - DEBUGCLIENT("Scan Ended"); + // DEBUGCLIENT("Scan Ended"); } END_BLEMIDI_NAMESPACE @@ -627,7 +558,7 @@ END_BLEMIDI_NAMESPACE /*! \brief Create a custom instance for ESP32 named , and advertise it like "Prefix + + Subfix" It will try to connect to a specific server with equal name or addr than . If is "", it will connect to first midi server */ -#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ +#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, _Settings) \ BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> BLE##Name(DeviceName); \ MIDI_NAMESPACE::MidiInterface, _Settings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport, _Settings> &)BLE##Name); @@ -635,9 +566,9 @@ END_BLEMIDI_NAMESPACE It will try to connect to a specific server with equal name or addr than . If is "", it will connect to first midi server */ #define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \ - BLEMIDI_CREATE_CUSTOM_INSTANCE (DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettings) + BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, BLEMIDI_NAMESPACE::DefaultSettingsClient) -/*! \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. */ #define BLEMIDI_CREATE_DEFAULT_INSTANCE() \ From fe3412b289ac60e09593304a9b73a62e5a03be81 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Tue, 21 Jun 2022 19:19:32 +0200 Subject: [PATCH 21/48] Minor Syntax correction Tabulation indentation fixed after last commit changes and other minor changes in comments --- src/hardware/BLEMIDI_Client_ESP32.h | 100 +++++++++++++++------------- 1 file changed, 53 insertions(+), 47 deletions(-) diff --git a/src/hardware/BLEMIDI_Client_ESP32.h b/src/hardware/BLEMIDI_Client_ESP32.h index badbdce..2f7fc70 100644 --- a/src/hardware/BLEMIDI_Client_ESP32.h +++ b/src/hardware/BLEMIDI_Client_ESP32.h @@ -33,15 +33,18 @@ struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings /* ##### BLE DEVICE NAME ##### */ + /** * Set name of ble device (not affect to connection with server) * max 16 characters */ - static constexpr char *name = "BleMidiClient"; - + static constexpr char *name = "BleMidiClient"; + + /* ###### TX POWER ##### */ + /** * Set power transmision * @@ -56,9 +59,11 @@ struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings */ static const esp_power_level_t clientTXPwr = ESP_PWR_LVL_P9; + /* ###### SECURITY ##### */ + /** Set the IO capabilities of the device, each option will trigger a different pairing method. * BLE_HS_IO_KEYBOARD_ONLY - Passkey pairing * BLE_HS_IO_DISPLAY_YESNO - Numeric comparison pairing @@ -83,9 +88,11 @@ struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings */ static constexpr PasskeyRequestCallback userOnPassKeyRequest = defautlPasskeyRequest; + /* ###### BLE COMMUNICATION PARAMS ###### */ + /** Set connection parameters: * If you only use one connection, put recomended BLE server param communication * (you may scan it ussing "nRF Connect" app or other similar apps). @@ -104,6 +111,7 @@ struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings static const uint16_t commLatency = 0; // static const uint16_t commTimeOut = 200; // 2000ms + /* ###### BLE FORCE NEW CONNECTION ###### */ @@ -118,7 +126,6 @@ struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings static const bool forceNewConnection = false; }; - /** Define a class to handle the callbacks when advertisments are received */ class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks { @@ -275,15 +282,15 @@ class MyClientCallbacks : public BLEClientCallbacks uint32_t onPassKeyRequest() { - //if (nullptr != _Settings::userOnPassKeyRequest) - return _Settings::userOnPassKeyRequest(); - //return 0; + // if (nullptr != _Settings::userOnPassKeyRequest) + return _Settings::userOnPassKeyRequest(); + // return 0; }; void onConnect(BLEClient *pClient) { DEBUGCLIENT("##Connected##"); - //pClient->updateConnParams(_Settings::commMinInterval, _Settings::commMaxInterval, _Settings::commLatency, _Settings::commTimeOut); + // pClient->updateConnParams(_Settings::commMinInterval, _Settings::commMaxInterval, _Settings::commLatency, _Settings::commTimeOut); vTaskDelay(1); if (_bluetoothEsp32) _bluetoothEsp32->connected(); @@ -299,18 +306,18 @@ class MyClientCallbacks : public BLEClientCallbacks _bluetoothEsp32->disconnected(); } -if (_Settings::forceNewConnection) -{ - // Renew Client - NimBLEDevice::deleteClient(pClient); - pClient = nullptr; -} + if (_Settings::forceNewConnection) + { + // Renew Client + NimBLEDevice::deleteClient(pClient); + pClient = nullptr; + } // Try reconnection or search a new one NimBLEDevice::getScan()->start(1, scanEndedCB); } - bool onConnParamsUpdateRequest(NimBLEClient *pClient, const ble_gap_upd_params *params) + bool onConnParamsUpdateRequest(NimBLEClient *pClient, const ble_gap_upd_params *params) { if (params->itvl_min < _Settings::commMinInterval) { /** 1.25ms units */ @@ -346,24 +353,23 @@ bool BLEMIDI_Client_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Tran _bleMidiTransport = bleMidiTransport; std::string strDeviceName(deviceName); - if (strDeviceName == "") // Connect to the first midi server found + // Connect to the first midi server found + if (strDeviceName == "") { myAdvCB.specificTarget = false; myAdvCB.nameTarget = ""; } - else // Connect to a specific name or address + // Connect to a specific name or address + else { myAdvCB.specificTarget = true; myAdvCB.nameTarget = strDeviceName; } - static char array[16] = "patata"; - + static char array[16]; memcpy(array, _Settings::name, 16); strDeviceName = array; - DEBUGCLIENT(strDeviceName.c_str()); - NimBLEDevice::init(strDeviceName); // To communicate between the 2 cores. @@ -390,7 +396,8 @@ bool BLEMIDI_Client_ESP32<_Settings>::available(byte *pvBuffer) return false; } - if (_client == nullptr || !_client->isConnected()) // Try to connect/reconnect + // Try to connect/reconnect + if (_client == nullptr || !_client->isConnected()) { if (myAdvCB.doConnect) { @@ -445,42 +452,41 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect() { using namespace std::placeholders; //<- for bind funtion in callback notification -//Retry to connecto to last one -if (!_Settings::forceNewConnection) -{ - /** Check if we have a client we should reuse first - * Special case when we already know this device - * This saves considerable time and power. - */ - - if (_client) + // Retry to connecto to last one + if (!_Settings::forceNewConnection) { - if (_client == NimBLEDevice::getClientByPeerAddress(myAdvCB.advDevice.getAddress())) + /** Check if we have a client we should reuse first + * Special case when we already know this device + * This saves considerable time and power. + */ + + if (_client) { - if (_client->connect(&myAdvCB.advDevice, false)) + if (_client == NimBLEDevice::getClientByPeerAddress(myAdvCB.advDevice.getAddress())) { - if (_characteristic->canNotify()) + if (_client->connect(&myAdvCB.advDevice, false)) { - if (_characteristic->subscribe(true, std::bind(&BLEMIDI_Client_ESP32::notifyCB, this, _1, _2, _3, _4))) + if (_characteristic->canNotify()) { - // Re-connection SUCCESS - return true; + if (_characteristic->subscribe(true, std::bind(&BLEMIDI_Client_ESP32::notifyCB, this, _1, _2, _3, _4))) + { + // Re-connection SUCCESS + return true; + } } + /** Disconnect if subscribe failed */ + _client->disconnect(); } - /** Disconnect if subscribe failed */ - _client->disconnect(); + /* If any connection problem exits, delete previous client and try again in the next attemp as new client*/ + NimBLEDevice::deleteClient(_client); + _client = nullptr; + return false; } - /* If any connection problem exits, delete previous client and try again in the next attemp as new client*/ + /*If client does not match, delete previous client and create a new one*/ NimBLEDevice::deleteClient(_client); _client = nullptr; - return false; } - /*If client does not match, delete previous client and create a new one*/ - NimBLEDevice::deleteClient(_client); - _client = nullptr; } -} - if (NimBLEDevice::getClientListSize() >= NIMBLE_MAX_CONNECTIONS) { @@ -493,7 +499,7 @@ if (!_Settings::forceNewConnection) _client->setClientCallbacks(new MyClientCallbacks<_Settings>(this), false); - _client->setConnectionParams(_Settings::commMinInterval,_Settings::commMaxInterval, _Settings::commLatency, _Settings::commTimeOut); + _client->setConnectionParams(_Settings::commMinInterval, _Settings::commMaxInterval, _Settings::commLatency, _Settings::commTimeOut); /** Set how long we are willing to wait for the connection to complete (seconds), default is 30. */ _client->setConnectTimeout(15); @@ -517,7 +523,7 @@ if (!_Settings::forceNewConnection) } DEBUGCLIENT("Connected to: " + myAdvCB.advDevice.getName().c_str() + " / " + _client->getPeerAddress().toString().c_str()); - + DEBUGCLIENT("RSSI: "); DEBUGCLIENT(_client->getRssi()); From f31f8db2542715c410366e7de9614c68d6963f92 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 27 Jun 2022 13:43:20 +0200 Subject: [PATCH 22/48] Code Example changed for new settings struct --- examples/MidiBle_Client/MidiBle_Client.ino | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/examples/MidiBle_Client/MidiBle_Client.ino b/examples/MidiBle_Client/MidiBle_Client.ino index 5109890..120adf9 100644 --- a/examples/MidiBle_Client/MidiBle_Client.ino +++ b/examples/MidiBle_Client/MidiBle_Client.ino @@ -28,21 +28,22 @@ #include #include - struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings { - static const size_t MaxBufferSize = 16; -}; - #include - //#include //#include //#include -BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-BLE-MIDI", MIDI, CustomBufferSizeSettings); // Connect to first server found +//See DefaultSettingsClient in hardware/BLEMIDI_Client_ESP32.h for more configurable settings +// If you do not redefine a parameter, it will use the default value for these parameter +struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettingsClient { + static const size_t MaxBufferSize = 16; +}; + +BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-BLE-MIDI", MIDI, CustomBufferSizeSettings); // Connect to a server named "Esp32-BLE-MIDI" and use CustomBufferSizeSettings as settings of client -//BLEMIDI_CREATE_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("MyBLEserver",MIDI) //Connect to a specific name server +//BLEMIDI_CREATE_INSTANCE("",MIDI) //Connect to the first server found, using default settings +//BLEMIDI_CREATE_INSTANCE("f2:c1:d9:36:e7:6b",MIDI) //Connect to a specific BLE address server, using default settings +//BLEMIDI_CREATE_INSTANCE("MyBLEserver",MIDI) //Connect to a specific name server, using default settings #ifndef LED_BUILTIN #define LED_BUILTIN 2 //modify for match with yout board From 24f4addb36a33bae24b22c6ba274a88e225551e6 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 27 Jun 2022 16:12:10 +0200 Subject: [PATCH 23/48] Intro explanation modified --- examples/MidiBle_Client/MidiBle_Client.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/MidiBle_Client/MidiBle_Client.ino b/examples/MidiBle_Client/MidiBle_Client.ino index 120adf9..c565af6 100644 --- a/examples/MidiBle_Client/MidiBle_Client.ino +++ b/examples/MidiBle_Client/MidiBle_Client.ino @@ -14,10 +14,10 @@ * the name of the server or the BLE address of the server. If you want to connect * to the first MIDI server BLE found by the device, you just have to set the name field empty (""). * - * FOR ADVANCED USERS: Other advanced BLE configurations can be changed in hardware/BLEMIDI_Client_ESP32.h - * #defines in the head of the file (IMPORTANT: Only the first user defines must be modified). These configurations - * are related to security (password, pairing and securityCallback()), communication params, the device name - * and other stuffs. Modify defines at your own risk. + * FOR ADVANCED USERS: Other advanced BLE configurations can be changed with a struct that heritate + * from BLEMIDI_NAMESPACE::DefaultSettingsClient. These configurations are related to + * security (password, pairing and securityCallback()), communication params, the device name + * and other stuffs. Modify those settings at your own risk. * * * From 6d168da918e167b2c711f44522d0fed1ef07321a Mon Sep 17 00:00:00 2001 From: lathoub <4082369+lathoub@users.noreply.github.com> Date: Tue, 8 Nov 2022 09:03:06 +0100 Subject: [PATCH 24/48] Update BLEMIDI_ArduinoBLE.h --- src/hardware/BLEMIDI_ArduinoBLE.h | 97 ++++++++++++++++++------------- 1 file changed, 58 insertions(+), 39 deletions(-) diff --git a/src/hardware/BLEMIDI_ArduinoBLE.h b/src/hardware/BLEMIDI_ArduinoBLE.h index 59397c9..2bdf9d5 100644 --- a/src/hardware/BLEMIDI_ArduinoBLE.h +++ b/src/hardware/BLEMIDI_ArduinoBLE.h @@ -67,6 +67,8 @@ class BLEMIDI_ArduinoBLE Fifo mRxBuffer; + template friend class MyServerCallbacks; + public: BLEMIDI_ArduinoBLE() : _midiService(SERVICE_UUID), _midiChar(CHARACTERISTIC_UUID, BLERead | BLEWrite | BLENotify | BLEWriteWithoutResponse, _Settings::MaxBufferSize) @@ -93,8 +95,6 @@ class BLEMIDI_ArduinoBLE return true; } - poll(); - if (_midiChar.written()) { auto length = _midiChar.valueLength(); @@ -120,54 +120,69 @@ class BLEMIDI_ArduinoBLE _bleMidiTransport->receive((uint8_t *)buffer, length); } - bool poll() + void connected() { - BLEDevice central = BLE.central(); - if (!central) - { - if (_central) - { - onDisconnected(*_central); - _central = nullptr; - } - return false; - } + if (_bleMidiTransport->_connectedCallback) + _bleMidiTransport->_connectedCallback(); + } - if (!central.connected()) - return false; + void disconnected() + { + if (_bleMidiTransport->_disconnectedCallback) + _bleMidiTransport->_disconnectedCallback(); - if (nullptr == _central) - { - onConnected(central); - _central = ¢ral; - } - else - { - if (*_central != central) - { - onDisconnected(*_central); - onConnected(central); - _central = ¢ral; - } - } + end(); + } +}; - return true; +template +class MyServerCallbacks : public BLEDeviceCallbacks +{ +public: + MyServerCallbacks(BLEMIDI_ArduinoBLE<_Settings> *bluetooth) + : _bluetooth(bluetooth) + { } - void onConnected(BLEDevice central) +protected: + BLEMIDI_ArduinoBLE<_Settings> *_bluetooth = nullptr; + + void onConnect(BLEDevice device) + { + if (_bluetooth) + _bluetooth->connected(); + }; + + void onDisconnect(BLEDevice device) { - _central = ¢ral; + if (_bluetooth) + _bluetooth->disconnected(); + } +}; - if (_bleMidiTransport->_connectedCallback) - _bleMidiTransport->_connectedCallback(); +template +class MyCharacteristicCallbacks : public BLECharacteristicCallbacks +{ +public: + MyCharacteristicCallbacks(BLEMIDI_ArduinoBLE<_Settings> *bluetooth) + : _bluetooth(bluetooth) + { } - void onDisconnected(BLEDevice central) +protected: + BLEMIDI_ArduinoBLE<_Settings> *_bluetooth = nullptr; + + void onWrite(BLECharacteristic characteristic) { - if (_bleMidiTransport->_disconnectedCallback) - _bleMidiTransport->_disconnectedCallback(); + // std::string rxValue = characteristic->getValue(); + // if (rxValue.length() > 0) + // { + // _bluetooth->receive((uint8_t *)(rxValue.c_str()), rxValue.length()); + //} + } - _central = nullptr; + void onRead(BLECharacteristic characteristic) + { } }; @@ -186,9 +201,13 @@ bool BLEMIDI_ArduinoBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Transp _midiService.addCharacteristic(_midiChar); BLE.addService(_midiService); + BLE.setCallbacks(new MyServerCallbacks<_Settings>(this)); + + _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); + _midiChar.writeValue((uint8_t)0); // TODO: might not be needed, check!! /* Start advertising BLE. It will start continuously transmitting BLE advertising packets and will be visible to remote BLE central devices From 81462befb2c6c005c072e56690036a943bbbca44 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 21 Nov 2022 17:45:01 +0100 Subject: [PATCH 25/48] Add Notification and Response parameters to Config Added after Nimble Lib update. end() updated. --- src/hardware/BLEMIDI_Client_ESP32.h | 31 ++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/hardware/BLEMIDI_Client_ESP32.h b/src/hardware/BLEMIDI_Client_ESP32.h index 2f7fc70..92b3318 100644 --- a/src/hardware/BLEMIDI_Client_ESP32.h +++ b/src/hardware/BLEMIDI_Client_ESP32.h @@ -40,7 +40,6 @@ struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings */ static constexpr char *name = "BleMidiClient"; - /* ###### TX POWER ##### */ @@ -59,7 +58,6 @@ struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings */ static const esp_power_level_t clientTXPwr = ESP_PWR_LVL_P9; - /* ###### SECURITY ##### */ @@ -88,7 +86,6 @@ struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings */ static constexpr PasskeyRequestCallback userOnPassKeyRequest = defautlPasskeyRequest; - /* ###### BLE COMMUNICATION PARAMS ###### */ @@ -111,7 +108,6 @@ struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings static const uint16_t commLatency = 0; // static const uint16_t commTimeOut = 200; // 2000ms - /* ###### BLE FORCE NEW CONNECTION ###### */ @@ -124,6 +120,21 @@ struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings * */ static const bool forceNewConnection = false; + + /* + ###### BLE SUBSCRIPTION: NOTIFICATION & RESPONSE ###### + */ + + /** + * Subscribe in Notification Mode [true] or Indication Mode [false] + * Don't modify this parameter except is completely necessary. + */ + static const bool notification = true; + /** + * Respond to after a notification message. + * Don't modify this parameter except is completely necessary. + */ + static const bool response = true; }; /** Define a class to handle the callbacks when advertisments are received */ @@ -206,9 +217,11 @@ class BLEMIDI_Client_ESP32 myAdvCB.enableConnection = false; xQueueReset(mRxQueue); _client->disconnect(); - _client = nullptr; + bool success = !_client->isConnected(); + if (success) + _client = nullptr; - return !_client->isConnected(); + return success; } void write(uint8_t *data, uint8_t length) @@ -397,7 +410,7 @@ bool BLEMIDI_Client_ESP32<_Settings>::available(byte *pvBuffer) } // Try to connect/reconnect - if (_client == nullptr || !_client->isConnected()) + if (_client == nullptr || !_client->isConnected()) { if (myAdvCB.doConnect) { @@ -468,7 +481,7 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect() { if (_characteristic->canNotify()) { - if (_characteristic->subscribe(true, std::bind(&BLEMIDI_Client_ESP32::notifyCB, this, _1, _2, _3, _4))) + if (_characteristic->subscribe(_Settings::notification, std::bind(&BLEMIDI_Client_ESP32::notifyCB, this, _1, _2, _3, _4), _Settings::response)) { // Re-connection SUCCESS return true; @@ -537,7 +550,7 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect() { if (_characteristic->canNotify()) { - if (_characteristic->subscribe(true, std::bind(&BLEMIDI_Client_ESP32::notifyCB, this, _1, _2, _3, _4))) + if (_characteristic->subscribe(_Settings::notification, std::bind(&BLEMIDI_Client_ESP32::notifyCB, this, _1, _2, _3, _4), _Settings::response)) { // Connection SUCCESS return true; From 3e83235a8b71b12403434d99347f21b713c4445b Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Tue, 22 Nov 2022 17:54:56 +0100 Subject: [PATCH 26/48] Update build-arduino.sh Write directly to .yaml file --- ci/build-arduino.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ci/build-arduino.sh b/ci/build-arduino.sh index 55a9814..3299012 100644 --- a/ci/build-arduino.sh +++ b/ci/build-arduino.sh @@ -6,15 +6,16 @@ shopt -s globstar # Make sure we are inside the github workspace cd $GITHUB_WORKSPACE # Create directories -mkdir $HOME/Arduino -mkdir $HOME/Arduino/libraries +mkdir $HOME/Arduino -p +mkdir $HOME/Arduino/libraries -p # Install Arduino IDE export PATH=$PATH:$GITHUB_WORKSPACE/bin curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | sh arduino-cli config init arduino-cli config set library.enable_unsafe_install true # arduino-cli core update-index --additional-urls https://arduino.esp8266.com/stable/package_esp8266com_index.json -arduino-cli core update-index --additional-urls https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json +#arduino-cli core update-index --additional-urls https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json +sed -i 's+[]+[https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json]+g' ./.arduino15/arduino-cli.yaml arduino-cli core update-index # Install Arduino AVR core @@ -24,7 +25,7 @@ arduino-cli core install arduino:samd arduino-cli core install esp32:esp32 # List the boards -arduino-cli board list +arduino-cli board listall # Link Arduino library ln -s $GITHUB_WORKSPACE $HOME/Arduino/libraries/CI_Test_Library From 27ee609c0507cc301854c8c3eb8d09c2df7a3f68 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Tue, 22 Nov 2022 18:18:17 +0100 Subject: [PATCH 27/48] Update build-arduino.sh --- ci/build-arduino.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/build-arduino.sh b/ci/build-arduino.sh index 3299012..9dd5328 100644 --- a/ci/build-arduino.sh +++ b/ci/build-arduino.sh @@ -15,7 +15,7 @@ arduino-cli config init arduino-cli config set library.enable_unsafe_install true # arduino-cli core update-index --additional-urls https://arduino.esp8266.com/stable/package_esp8266com_index.json #arduino-cli core update-index --additional-urls https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json -sed -i 's+[]+[https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json]+g' ./.arduino15/arduino-cli.yaml +sed -i 's+\[\]+\[https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json\]+g' /home/runner/.arduino15/arduino-cli.yaml arduino-cli core update-index # Install Arduino AVR core From 03523e562c57125a0df9709e50ea81c5f9c097f8 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Tue, 22 Nov 2022 18:25:06 +0100 Subject: [PATCH 28/48] Update build-arduino.sh --- ci/build-arduino.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/build-arduino.sh b/ci/build-arduino.sh index 9dd5328..eb1f5b1 100644 --- a/ci/build-arduino.sh +++ b/ci/build-arduino.sh @@ -51,5 +51,5 @@ arduino-cli lib install NimBLE-Arduino # Compile all *.ino files for the Arduino Uno for f in **/*.ino ; do - arduino-cli compile -b arduino:esp32:??? $f + arduino-cli compile -b esp32:esp32:??? $f done From f9b792277192e72af81420e4e92ec79cb6599df7 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Wed, 23 Nov 2022 09:12:40 +0100 Subject: [PATCH 29/48] Update build-arduino.sh --- ci/build-arduino.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/build-arduino.sh b/ci/build-arduino.sh index eb1f5b1..fdb9457 100644 --- a/ci/build-arduino.sh +++ b/ci/build-arduino.sh @@ -51,5 +51,5 @@ arduino-cli lib install NimBLE-Arduino # Compile all *.ino files for the Arduino Uno for f in **/*.ino ; do - arduino-cli compile -b esp32:esp32:??? $f + arduino-cli compile -b esp32:esp32:esp32 $f done From 03365fc2255d8a5a8074fbfd1c39674d4e3b2de2 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Wed, 23 Nov 2022 10:35:55 +0100 Subject: [PATCH 30/48] Update CustomerBufferSize.ino --- examples/CustomerBufferSize/CustomerBufferSize.ino | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/CustomerBufferSize/CustomerBufferSize.ino b/examples/CustomerBufferSize/CustomerBufferSize.ino index 3dc8515..864e456 100644 --- a/examples/CustomerBufferSize/CustomerBufferSize.ino +++ b/examples/CustomerBufferSize/CustomerBufferSize.ino @@ -8,6 +8,10 @@ //#include //#include +#ifndef LED_BUILTIN +#define LED_BUILTIN 2 +#endif + BLEMIDI_CREATE_CUSTOM_INSTANCE("Esp32-NimBLE-MIDI", MIDI, CustomBufferSizeSettings); unsigned long t0 = millis(); @@ -61,4 +65,4 @@ void loop() MIDI.sendNoteOn (60, 100, 1); // note 60, velocity 100 on channel 1 } -} \ No newline at end of file +} From a294fc61e05e28a192a5c5090f905b5931f107a5 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 28 Nov 2022 08:42:35 +0100 Subject: [PATCH 31/48] Update build-arduino.sh --- ci/build-arduino.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ci/build-arduino.sh b/ci/build-arduino.sh index fdb9457..603e190 100644 --- a/ci/build-arduino.sh +++ b/ci/build-arduino.sh @@ -51,5 +51,7 @@ arduino-cli lib install NimBLE-Arduino # Compile all *.ino files for the Arduino Uno for f in **/*.ino ; do - arduino-cli compile -b esp32:esp32:esp32 $f + echo "P^roject $f" + arduino-cli compile -b esp32:esp32:esp32 $f + arduino-cli complile --clean done From 2e2cb865c3e80c59691eb809bc9718ddf49cade0 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 28 Nov 2022 08:42:57 +0100 Subject: [PATCH 32/48] Update build-arduino.sh --- ci/build-arduino.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/build-arduino.sh b/ci/build-arduino.sh index 603e190..ea36f8d 100644 --- a/ci/build-arduino.sh +++ b/ci/build-arduino.sh @@ -51,7 +51,7 @@ arduino-cli lib install NimBLE-Arduino # Compile all *.ino files for the Arduino Uno for f in **/*.ino ; do - echo "P^roject $f" + echo "Project $f" arduino-cli compile -b esp32:esp32:esp32 $f arduino-cli complile --clean done From 37eb60b3deaa1945297dd36c5c89019227e5438c Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 28 Nov 2022 08:45:44 +0100 Subject: [PATCH 33/48] Update build-arduino.sh --- ci/build-arduino.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/build-arduino.sh b/ci/build-arduino.sh index ea36f8d..507c86b 100644 --- a/ci/build-arduino.sh +++ b/ci/build-arduino.sh @@ -53,5 +53,5 @@ arduino-cli lib install NimBLE-Arduino for f in **/*.ino ; do echo "Project $f" arduino-cli compile -b esp32:esp32:esp32 $f - arduino-cli complile --clean + arduino-cli compile --clean done From 0afc91c98fb2e7f12765f9d13e7904e268a8cc2f Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 28 Nov 2022 16:58:54 +0100 Subject: [PATCH 34/48] Update build-arduino.sh --- ci/build-arduino.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ci/build-arduino.sh b/ci/build-arduino.sh index 507c86b..ba4ea20 100644 --- a/ci/build-arduino.sh +++ b/ci/build-arduino.sh @@ -49,9 +49,13 @@ arduino-cli lib install NimBLE-Arduino # arduino-cli compile -b arduino:esp8266:??? $f # done +dR = $(pwd) + # Compile all *.ino files for the Arduino Uno for f in **/*.ino ; do - echo "Project $f" + echo "Project: $f" + d =$(dirname $f) + cd $d arduino-cli compile -b esp32:esp32:esp32 $f - arduino-cli compile --clean + cd $dR done From 06dc8d04b396f1c86f2c7393c7c2e8849c6dd15c Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 28 Nov 2022 17:02:17 +0100 Subject: [PATCH 35/48] Update build-arduino.sh --- ci/build-arduino.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/build-arduino.sh b/ci/build-arduino.sh index ba4ea20..aaca623 100644 --- a/ci/build-arduino.sh +++ b/ci/build-arduino.sh @@ -49,12 +49,12 @@ arduino-cli lib install NimBLE-Arduino # arduino-cli compile -b arduino:esp8266:??? $f # done -dR = $(pwd) +dR=$(pwd) # Compile all *.ino files for the Arduino Uno for f in **/*.ino ; do echo "Project: $f" - d =$(dirname $f) + d=$(dirname $f) cd $d arduino-cli compile -b esp32:esp32:esp32 $f cd $dR From 186cc21192c242fdd6725b9ec1b832bfb37fda5b Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 28 Nov 2022 17:06:58 +0100 Subject: [PATCH 36/48] Update build-arduino.sh --- ci/build-arduino.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ci/build-arduino.sh b/ci/build-arduino.sh index aaca623..5c67bfd 100644 --- a/ci/build-arduino.sh +++ b/ci/build-arduino.sh @@ -54,7 +54,8 @@ dR=$(pwd) # Compile all *.ino files for the Arduino Uno for f in **/*.ino ; do echo "Project: $f" - d=$(dirname $f) + d=$(dirname $(readlink -f $f)) + echo $d cd $d arduino-cli compile -b esp32:esp32:esp32 $f cd $dR From c3ecb0711c9b277f77acee7452d0cf41ad946f5e Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 28 Nov 2022 17:13:23 +0100 Subject: [PATCH 37/48] Update build-arduino.sh --- ci/build-arduino.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/build-arduino.sh b/ci/build-arduino.sh index 5c67bfd..7f84e16 100644 --- a/ci/build-arduino.sh +++ b/ci/build-arduino.sh @@ -57,6 +57,6 @@ for f in **/*.ino ; do d=$(dirname $(readlink -f $f)) echo $d cd $d - arduino-cli compile -b esp32:esp32:esp32 $f + arduino-cli compile -b esp32:esp32:esp32 *.ino cd $dR done From 99c2151c8bee6b9f4f88b6f82a78b862c0723309 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 28 Nov 2022 17:27:58 +0100 Subject: [PATCH 38/48] Update build-arduino.sh --- ci/build-arduino.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/build-arduino.sh b/ci/build-arduino.sh index 7f84e16..f8f2665 100644 --- a/ci/build-arduino.sh +++ b/ci/build-arduino.sh @@ -58,5 +58,6 @@ for f in **/*.ino ; do echo $d cd $d arduino-cli compile -b esp32:esp32:esp32 *.ino + arduino-cli compile --clean cd $dR done From fe4a021b5f49f877a2f3577dc9f5ddf06fef8ba6 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 28 Nov 2022 17:31:44 +0100 Subject: [PATCH 39/48] Update build-arduino.sh --- ci/build-arduino.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ci/build-arduino.sh b/ci/build-arduino.sh index f8f2665..a6cb533 100644 --- a/ci/build-arduino.sh +++ b/ci/build-arduino.sh @@ -57,7 +57,6 @@ for f in **/*.ino ; do d=$(dirname $(readlink -f $f)) echo $d cd $d - arduino-cli compile -b esp32:esp32:esp32 *.ino - arduino-cli compile --clean + arduino-cli compile -b esp32:esp32:esp32 *.ino --clean cd $dR done From f871a35c7a1f0256882dffa55fca4bb28bd1d8c5 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 28 Nov 2022 17:46:42 +0100 Subject: [PATCH 40/48] Update build-arduino.sh --- ci/build-arduino.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/build-arduino.sh b/ci/build-arduino.sh index a6cb533..fd30016 100644 --- a/ci/build-arduino.sh +++ b/ci/build-arduino.sh @@ -31,7 +31,7 @@ arduino-cli board listall ln -s $GITHUB_WORKSPACE $HOME/Arduino/libraries/CI_Test_Library arduino-cli lib install "MIDI library" -arduino-cli lib install ArduinoBLE +#arduino-cli lib install ArduinoBLE arduino-cli lib install NimBLE-Arduino # Compile all *.ino files for the Arduino Uno From c7121d01d1fc4625713f15b9e06d1e414d2e9305 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 28 Nov 2022 17:50:38 +0100 Subject: [PATCH 41/48] Update MidiBle.ino --- examples/MidiBle/MidiBle.ino | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/MidiBle/MidiBle.ino b/examples/MidiBle/MidiBle.ino index 69968b1..9022671 100644 --- a/examples/MidiBle/MidiBle.ino +++ b/examples/MidiBle/MidiBle.ino @@ -4,6 +4,10 @@ #include //#include +#ifndef LED_BUILTIN +#define LED_BUILTIN 2 +#endif + BLEMIDI_CREATE_DEFAULT_INSTANCE() unsigned long t0 = millis(); From 2aa2183f92b12cc8b08d7e4edb9ddfc90e24f2e6 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 28 Nov 2022 17:51:12 +0100 Subject: [PATCH 42/48] Update MidiBle_Client.ino --- examples/MidiBle_Client/MidiBle_Client.ino | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/MidiBle_Client/MidiBle_Client.ino b/examples/MidiBle_Client/MidiBle_Client.ino index c565af6..08efbeb 100644 --- a/examples/MidiBle_Client/MidiBle_Client.ino +++ b/examples/MidiBle_Client/MidiBle_Client.ino @@ -33,6 +33,10 @@ //#include //#include +#ifndef LED_BUILTIN +#define LED_BUILTIN 2 +#endif + //See DefaultSettingsClient in hardware/BLEMIDI_Client_ESP32.h for more configurable settings // If you do not redefine a parameter, it will use the default value for these parameter struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettingsClient { From 7fdaffcbfb3e796f2f598875243958654141fbee Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 28 Nov 2022 17:51:30 +0100 Subject: [PATCH 43/48] Update SysEx_Receive.ino --- examples/SysEx_Receive/SysEx_Receive.ino | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/SysEx_Receive/SysEx_Receive.ino b/examples/SysEx_Receive/SysEx_Receive.ino index 3b3adb1..79b4684 100644 --- a/examples/SysEx_Receive/SysEx_Receive.ino +++ b/examples/SysEx_Receive/SysEx_Receive.ino @@ -4,6 +4,10 @@ #include //#include +#ifndef LED_BUILTIN +#define LED_BUILTIN 2 +#endif + BLEMIDI_CREATE_INSTANCE("CustomName", MIDI) bool isConnected = false; From 06a1b8f9268560e5d0f2857a064d4e4cfa50bade Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Mon, 28 Nov 2022 17:51:52 +0100 Subject: [PATCH 44/48] Update SysEx_Send.ino --- examples/SysEx_Send/SysEx_Send.ino | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/examples/SysEx_Send/SysEx_Send.ino b/examples/SysEx_Send/SysEx_Send.ino index 9e83e60..4c5b855 100644 --- a/examples/SysEx_Send/SysEx_Send.ino +++ b/examples/SysEx_Send/SysEx_Send.ino @@ -4,6 +4,10 @@ //#include //#include +#ifndef LED_BUILTIN +#define LED_BUILTIN 2 +#endif + byte sysex4[] = { 0xF0, 0x43, 0x20, 0xF7 }; byte sysex5[] = { 0xF0, 0x43, 0x20, 0x7E, 0xF7 }; byte sysex6[] = { 0xF0, 0x43, 0x20, 0x7E, 0x4C, 0xF7 }; From 25a78fa7f1fba6eb3e307dd5457af6202f26e2c7 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Tue, 29 Nov 2022 09:05:26 +0100 Subject: [PATCH 45/48] Added all configurations options to example Added all configurations options --- .../CustomerBufferSize/CustomerBufferSize.ino | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/examples/CustomerBufferSize/CustomerBufferSize.ino b/examples/CustomerBufferSize/CustomerBufferSize.ino index 864e456..33c41c1 100644 --- a/examples/CustomerBufferSize/CustomerBufferSize.ino +++ b/examples/CustomerBufferSize/CustomerBufferSize.ino @@ -1,7 +1,56 @@ #include +static uint32_t customPasskeyRequest() +{ + // FILL WITH YOUR CUSTOM AUTH METHOD CODE or PASSKEY + // FOR EXAMPLE: + uint32_t passkey = 123456; + + // Serial.println("Client Passkey Request"); + + /** return the passkey to send to the server */ + return passkey; +}; + struct CustomBufferSizeSettings : public BLEMIDI_NAMESPACE::DefaultSettings { + //See all options and them explanation in the library. + + /* + ##### BLE DEVICE NAME ##### + */ + //static constexpr char *name = "BleMidiClient"; + /* + ###### TX POWER ##### + */ + //static const esp_power_level_t clientTXPwr = ESP_PWR_LVL_P9; + /* + ###### SECURITY ##### + */ + //static const uint8_t clientSecurityCapabilities = BLE_HS_IO_NO_INPUT_OUTPUT; + //static const bool clientBond = true; + //static const bool clientMITM = false; + //static const bool clientPair = true; + //static constexpr PasskeyRequestCallback userOnPassKeyRequest = customPasskeyRequest; + /* + ###### BLE COMMUNICATION PARAMS ###### + */ + //static const uint16_t commMinInterval = 6; // 7.5ms + //static const uint16_t commMaxInterval = 35; // 40ms + //static const uint16_t commLatency = 0; // + //static const uint16_t commTimeOut = 200; // 2000ms + /* + ###### BLE FORCE NEW CONNECTION ###### + */ + //static const bool forceNewConnection = false; + /* + ###### BLE SUBSCRIPTION: NOTIFICATION & RESPONSE ###### + */ + //static const bool notification = true; + //static const bool response = true; + /* + ###### AND THE OTHER SETTINGS OF MIDI LIBRARY ###### static const size_t MaxBufferSize = 16; + }; #include From a1ec13758b2db514cb7290816b536eafa9291606 Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Tue, 29 Nov 2022 09:09:21 +0100 Subject: [PATCH 46/48] */ syntax correction in example --- examples/CustomerBufferSize/CustomerBufferSize.ino | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/CustomerBufferSize/CustomerBufferSize.ino b/examples/CustomerBufferSize/CustomerBufferSize.ino index 33c41c1..8aa7e94 100644 --- a/examples/CustomerBufferSize/CustomerBufferSize.ino +++ b/examples/CustomerBufferSize/CustomerBufferSize.ino @@ -49,6 +49,7 @@ static uint32_t customPasskeyRequest() //static const bool response = true; /* ###### AND THE OTHER SETTINGS OF MIDI LIBRARY ###### + */ static const size_t MaxBufferSize = 16; }; From 38fea5dfe32a93ce09cd271df03bfe8fb2407c9e Mon Sep 17 00:00:00 2001 From: RobertoHE Date: Wed, 28 Dec 2022 18:16:21 +0100 Subject: [PATCH 47/48] Add lines to avoid warning about unused var When using a template instance in two different ways (for example, different settings struct for differents objects), a lot of lines of warning appear due to some variables being unused. Using these lines, those variables are used for doing nothing and the warnings go away. --- src/BLEMIDI_Transport.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BLEMIDI_Transport.h b/src/BLEMIDI_Transport.h index d37d943..e84e7ad 100644 --- a/src/BLEMIDI_Transport.h +++ b/src/BLEMIDI_Transport.h @@ -256,10 +256,10 @@ class BLEMIDI_Transport byte headerByte = buffer[lPtr++]; auto timestampHigh = 0x3f & headerByte; - + timestampHigh = timestampHigh; // <-- This line is for avoid Warning message due it is unused byte timestampByte = buffer[lPtr++]; uint16_t timestamp = 0; - + timestamp = timestamp; // <-- This line is for avoid Warning message due it is unused bool sysExContinuation = false; bool runningStatusContinuation = false; From e09fa432987dfeb4ab7804d212d22e59ce756f73 Mon Sep 17 00:00:00 2001 From: Bart De Lathouwer <4082369+lathoub@users.noreply.github.com> Date: Sun, 2 Feb 2025 11:42:50 +0100 Subject: [PATCH 48/48] bug fix from master fix bug when end() called (@aselectroworks) callback with deviceName (@MicroMidi) --- src/BLEMIDI_Transport.h | 7 +++++++ src/hardware/BLEMIDI_Client_ESP32.h | 15 +++++++++++---- src/hardware/BLEMIDI_ESP32_NimBLE.h | 25 +++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/BLEMIDI_Transport.h b/src/BLEMIDI_Transport.h index e84e7ad..671f828 100644 --- a/src/BLEMIDI_Transport.h +++ b/src/BLEMIDI_Transport.h @@ -184,6 +184,7 @@ class BLEMIDI_Transport // callbacks void (*_connectedCallback)() = nullptr; void (*_disconnectedCallback)() = nullptr; + void (*_connectedCallbackDeviceName)(char *) = nullptr; BLEMIDI_Transport &setName(const char *deviceName) { @@ -198,6 +199,12 @@ class BLEMIDI_Transport return *this; } + BLEMIDI_Transport &setHandleConnected(void (*fptr)(char*)) + { + _connectedCallbackDeviceName= fptr; + return *this; + } + BLEMIDI_Transport &setHandleDisconnected(void (*fptr)()) { _disconnectedCallback = fptr; diff --git a/src/hardware/BLEMIDI_Client_ESP32.h b/src/hardware/BLEMIDI_Client_ESP32.h index 92b3318..605b6ca 100644 --- a/src/hardware/BLEMIDI_Client_ESP32.h +++ b/src/hardware/BLEMIDI_Client_ESP32.h @@ -196,6 +196,8 @@ class BLEMIDI_Client_ESP32 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. + char connectedDeviceName[24]; + BLEMIDI_Transport, _Settings> *_bleMidiTransport = nullptr; bool specificTarget = false; @@ -217,11 +219,9 @@ class BLEMIDI_Client_ESP32 myAdvCB.enableConnection = false; xQueueReset(mRxQueue); _client->disconnect(); - bool success = !_client->isConnected(); - if (success) - _client = nullptr; + _client = nullptr; - return success; + return true; } void write(uint8_t *data, uint8_t length) @@ -270,6 +270,13 @@ class BLEMIDI_Client_ESP32 if (_bleMidiTransport->_connectedCallback) _bleMidiTransport->_connectedCallback(); firstTimeSend = true; + + if (_bleMidiTransport->_connectedCallbackDeviceName) + { + sprintf(connectedDeviceName, "%s", myAdvCB.advDevice.getName().c_str()); + _bleMidiTransport->_connectedCallbackDeviceName(connectedDeviceName); + } + } void disconnected() diff --git a/src/hardware/BLEMIDI_ESP32_NimBLE.h b/src/hardware/BLEMIDI_ESP32_NimBLE.h index 8f9a237..db76ca1 100644 --- a/src/hardware/BLEMIDI_ESP32_NimBLE.h +++ b/src/hardware/BLEMIDI_ESP32_NimBLE.h @@ -124,6 +124,27 @@ bool BLEMIDI_ESP32_NimBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Tran BLEDevice::init(deviceName); + /** + * Set the IO capabilities of the device, each option will trigger a different pairing method. + * BLE_HS_IO_DISPLAY_ONLY - Passkey pairing + * BLE_HS_IO_DISPLAY_YESNO - Numeric comparison pairing + * BLE_HS_IO_NO_INPUT_OUTPUT - DEFAULT setting - just works pairing + */ + // NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_ONLY); // use passkey + // NimBLEDevice::setSecurityIOCap(BLE_HS_IO_DISPLAY_YESNO); //use numeric comparison + + /** + * 2 different ways to set security - both calls achieve the same result. + * no bonding, no man in the middle protection, BLE secure connections. + * + * These are the default values, only shown here for demonstration. + */ + // NimBLEDevice::setSecurityAuth(false, false, true); + +// NimBLEDevice::setSecurityAuth(/*BLE_SM_PAIR_AUTHREQ_BOND | BLE_SM_PAIR_AUTHREQ_MITM |*/ BLE_SM_PAIR_AUTHREQ_SC); + + NimBLEDevice::setSecurityAuth(true, false, false); + // To communicate between the 2 cores. // Core_0 runs here, core_1 runs the BLE stack mRxQueue = xQueueCreate(_Settings::MaxBufferSize, sizeof(uint8_t)); @@ -145,8 +166,8 @@ bool BLEMIDI_ESP32_NimBLE<_Settings>::begin(const char *deviceName, BLEMIDI_Tran _characteristic->setCallbacks(new MyCharacteristicCallbacks<_Settings>(this)); - auto _security = new NimBLESecurity(); - _security->setAuthenticationMode(ESP_LE_AUTH_BOND); +// auto _security = new NimBLESecurity(); +// _security->setAuthenticationMode(ESP_LE_AUTH_BOND); // Start the service service->start();