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 @@ private: class BLEMIDI_ArduinoBLE { private: - static BLEMIDI_Transport* _bleMidiTransport; + static BLEMIDI_Transport* _bleMidiTransport; static BLEDevice* _central; Fifo mRxBuffer; @@ -73,7 +73,7 @@ public: { } - bool begin(const char*, BLEMIDI_Transport*); + bool begin(const char*, BLEMIDI_Transport*); void end() { @@ -183,10 +183,10 @@ protected: } }; -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 @@ private: BLEAdvertising *_advertising = nullptr; BLECharacteristic *_characteristic = nullptr; - BLEMIDI_Transport *_bleMidiTransport = nullptr; + BLEMIDI_Transport *_bleMidiTransport = nullptr; friend class MyServerCallbacks; friend class MyCharacteristicCallbacks; @@ -28,7 +28,7 @@ public: { } - bool begin(const char *, BLEMIDI_Transport *); + bool begin(const char *, BLEMIDI_Transport *); void end() { @@ -121,7 +121,7 @@ protected: } }; -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 @@ private: BLEAdvertising *_advertising = nullptr; BLECharacteristic *_characteristic = nullptr; - BLEMIDI_Transport *_bleMidiTransport = nullptr; + BLEMIDI_Transport *_bleMidiTransport = nullptr; friend class MyServerCallbacks; friend class MyCharacteristicCallbacks; @@ -25,7 +25,7 @@ public: { } - bool begin(const char *, BLEMIDI_Transport *); + bool begin(const char *, BLEMIDI_Transport *); void end() { @@ -114,7 +114,7 @@ protected: } }; -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 @@ private: // BLEDis bledis; // BLEMidi blemidi; - BLEMIDI_NAMESPACE::BLEMIDI_Transport* _bleMidiTransport; + BLEMIDI_NAMESPACE::BLEMIDI_Transport* _bleMidiTransport; friend class MyServerCallbacks; friend class MyCharacteristicCallbacks; @@ -22,7 +22,7 @@ public: { } - bool begin(const char*, BLEMIDI_NAMESPACE::BLEMIDI_Transport*); + bool begin(const char*, BLEMIDI_NAMESPACE::BLEMIDI_Transport*); void end() { @@ -61,7 +61,7 @@ protected: } }; -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) \