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] 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 @@ public: void end() { - } void write(uint8_t* buffer, size_t length) @@ -112,8 +113,8 @@ public: return false; #endif #ifdef BLE_EVENTS -/ BLE.poll(); - return ; // ?? + BLE.poll(); + return false; #endif } @@ -124,7 +125,7 @@ public: } 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 @@ protected: 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 @@ protected: _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 @@ protected: } }; -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