From 6d6fa61f832c1c766bf3ab8b86d18bb049e2d996 Mon Sep 17 00:00:00 2001 From: lathoub Date: Sat, 18 Apr 2020 11:40:57 +0200 Subject: [PATCH] renamed BLEMIDI to BLEMIDITransport --- examples/MidiBle/MidiBle.ino | 50 ++++---- keywords.txt | 8 +- src/BLE-MIDI.h | 22 +++- src/BLE-MIDI_Defs.h | 21 ---- src/hardware/BLE-MIDI_ESP32.h | 6 +- src/utility/Deque.h | 217 ---------------------------------- 6 files changed, 49 insertions(+), 275 deletions(-) delete mode 100644 src/utility/Deque.h diff --git a/examples/MidiBle/MidiBle.ino b/examples/MidiBle/MidiBle.ino index 7d098f3..50cd632 100644 --- a/examples/MidiBle/MidiBle.ino +++ b/examples/MidiBle/MidiBle.ino @@ -1,33 +1,29 @@ -#define DEBUG 4 #include #include BLEMIDI_CREATE_DEFAULT_ESP32_INSTANCE() -USING_NAMESPACE_BLEMIDI - unsigned long t0 = millis(); -#ifdef ESP32 bool isConnected = false; -#endif // ----------------------------------------------------------------------------- // // ----------------------------------------------------------------------------- void setup() { - // Serial communications and wait for port to open: - DEBUG_BEGIN(115200); - + Serial.begin(115200); + while (!Serial); + Serial.println("Booting"); + MIDI.begin(1); - bm.onConnected(OnBleMidiConnected); - bm.onDisconnected(OnBleMidiDisconnected); + BLEMIDI.onConnected(OnBleMidiConnected); + BLEMIDI.onDisconnected(OnBleMidiDisconnected); MIDI.setHandleNoteOn(OnBleMidiNoteOn); MIDI.setHandleNoteOff(OnBleMidiNoteOff); - N_DEBUG_PRINTLN(F("Ready")); + Serial.println(F("Ready")); } // ----------------------------------------------------------------------------- @@ -44,7 +40,6 @@ void loop() MIDI.sendNoteOn(60, 127, 1); // note 60, velocity 127 on channel 1 MIDI.sendNoteOff(60, 0, 1); } - } // ==================================================================================== @@ -55,7 +50,7 @@ void loop() // rtpMIDI session. Device connected // ----------------------------------------------------------------------------- void OnBleMidiConnected() { - N_DEBUG_PRINTLN(F("Connected")); + Serial.println(F("Connected")); isConnected = true; } @@ -71,25 +66,24 @@ void OnBleMidiDisconnected() { // received note on // ----------------------------------------------------------------------------- void OnBleMidiNoteOn(byte channel, byte note, byte velocity) { - N_DEBUG_PRINT(F("Incoming NoteOn from channel:")); - N_DEBUG_PRINT(channel); - N_DEBUG_PRINT(F(" note:")); - N_DEBUG_PRINT(note); - N_DEBUG_PRINT(F(" velocity:")); - N_DEBUG_PRINT(velocity); - N_DEBUG_PRINTLN(); + Serial.print(F("Incoming NoteOn from channel:")); + Serial.print(channel); + Serial.print(F(" note:")); + Serial.print(note); + Serial.print(F(" velocity:")); + Serial.print(velocity); + Serial.println(); } - // ----------------------------------------------------------------------------- // received note off // ----------------------------------------------------------------------------- void OnBleMidiNoteOff(byte channel, byte note, byte velocity) { - N_DEBUG_PRINT(F("Incoming NoteOff from channel:")); - N_DEBUG_PRINT(channel); - N_DEBUG_PRINT(F(" note:")); - N_DEBUG_PRINT(note); - N_DEBUG_PRINT(F(" velocity:")); - N_DEBUG_PRINT(velocity); - N_DEBUG_PRINTLN(); + Serial.print(F("Incoming NoteOff from channel:")); + Serial.print(channel); + Serial.print(F(" note:")); + Serial.print(note); + Serial.print(F(" velocity:")); + Serial.print(velocity); + Serial.println(); } diff --git a/keywords.txt b/keywords.txt index b63d67f..ed9ca64 100644 --- a/keywords.txt +++ b/keywords.txt @@ -5,9 +5,8 @@ ####################################### # Datatypes (KEYWORD1) ####################################### -midi_bleTransport KEYWORD1 -midi_bleTransport.h KEYWORD1 -Ble_esp32.h KEYWORD1 +BLE-MIDI.h KEYWORD1 +BLEMIDI KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) @@ -41,10 +40,11 @@ tick KEYWORD2 ####################################### # Instances (KEYWORD3) ####################################### +BLEMIDI KEYWORD3 ####################################### # Constants (LITERAL1) ####################################### # Namespace, considering it as a literal -blemidi LITERAL1 +BLEMIDI LITERAL1 diff --git a/src/BLE-MIDI.h b/src/BLE-MIDI.h index 35d79b0..4e365d4 100755 --- a/src/BLE-MIDI.h +++ b/src/BLE-MIDI.h @@ -14,10 +14,12 @@ using namespace MIDI_NAMESPACE; BEGIN_BLEMIDI_NAMESPACE template -class BLEMIDI +class BLEMIDITransport { typedef _Settings Settings; + friend class MIDI_NAMESPACE::MidiInterface>; + private: byte mRxBuffer[Settings::MaxBufferSize]; unsigned mRxIndex = 0; @@ -31,7 +33,7 @@ private: T mBleClass; public: - BLEMIDI(const char* deviceName) + BLEMIDITransport(const char* deviceName) { strncpy(mDeviceName, deviceName, 24); @@ -169,4 +171,20 @@ public: }; +/*! \brief Create an instance of the library + */ +#define BLEMIDI_CREATE_INSTANCE(Type, DeviceName, Name) \ +BLEMIDI_NAMESPACE::BLEMIDITransport BLE##Name(DeviceName); \ +MIDI_NAMESPACE::MidiInterface> Name((BLEMIDI_NAMESPACE::BLEMIDITransport &)BLE##Name); + + /*! \brief Create an instance for ESP32 named + */ +#define BLEMIDI_CREATE_ESP32_INSTANCE(DeviceName) \ +BLEMIDI_CREATE_INSTANCE(BLEMIDI_NAMESPACE::BLEMIDI_ESP32, DeviceName, MIDI); + + /*! \brief Create a default instance for ESP32 named BLE-MIDI + */ +#define BLEMIDI_CREATE_DEFAULT_ESP32_INSTANCE() \ +BLEMIDI_CREATE_ESP32_INSTANCE("BLE-MIDI") + END_BLEMIDI_NAMESPACE diff --git a/src/BLE-MIDI_Defs.h b/src/BLE-MIDI_Defs.h index 7b9672b..b1feb49 100755 --- a/src/BLE-MIDI_Defs.h +++ b/src/BLE-MIDI_Defs.h @@ -15,24 +15,3 @@ #include typedef uint8_t byte; #endif - -BEGIN_BLEMIDI_NAMESPACE - -/*! \brief Create an instance of the library - */ -#define BLEMIDI_CREATE_INSTANCE(Type, DeviceName, Name) \ -typedef BLEMIDI_NAMESPACE::BLEMIDI BLEMIDI_t; \ -BLEMIDI_t Name(DeviceName); \ -MIDI_NAMESPACE::MidiInterface MIDI((BLEMIDI_t &)Name); - -/*! \brief Create an instance for ESP32 named -*/ -#define BLEMIDI_CREATE_ESP32_INSTANCE(DeviceName) \ -BLEMIDI_CREATE_INSTANCE(BLEMIDI_NAMESPACE::BLEMIDI_ESP32, DeviceName, bm); - -/*! \brief Create a default instance for ESP32 named BLE-MIDI -*/ -#define BLEMIDI_CREATE_DEFAULT_ESP32_INSTANCE() \ -BLEMIDI_CREATE_ESP32_INSTANCE("BLE-MIDI") - -END_BLEMIDI_NAMESPACE diff --git a/src/hardware/BLE-MIDI_ESP32.h b/src/hardware/BLE-MIDI_ESP32.h index ac32155..98ed95b 100755 --- a/src/hardware/BLE-MIDI_ESP32.h +++ b/src/hardware/BLE-MIDI_ESP32.h @@ -15,14 +15,14 @@ private: BLEAdvertising* _advertising = nullptr; BLECharacteristic* _characteristic = nullptr; - BLEMIDI* _bleMidiTransport = nullptr; + BLEMIDITransport* _bleMidiTransport = nullptr; public: BLEMIDI_ESP32() { } - bool begin(const char*, BLEMIDI*); + bool begin(const char*, BLEMIDITransport*); void write(uint8_t* buffer, size_t length) { @@ -187,7 +187,7 @@ protected: } }; -bool BLEMIDI_ESP32::begin(const char* deviceName, BLEMIDI* bleMidiTransport) +bool BLEMIDI_ESP32::begin(const char* deviceName, BLEMIDITransport* bleMidiTransport) { _bleMidiTransport = bleMidiTransport; diff --git a/src/utility/Deque.h b/src/utility/Deque.h deleted file mode 100644 index bbd8f1a..0000000 --- a/src/utility/Deque.h +++ /dev/null @@ -1,217 +0,0 @@ -#pragma once - -#include - -template -class Deque { -// class iterator; - -private: - int _head, _tail; - T _data[Size]; - -public: - Deque() - { - clear(); - }; - - size_t free(); - const size_t size() const; - const size_t max_size() const; - T & front(); - const T & front() const; - T & back(); - const T & back() const; - void push_front(const T &); - void push_back(const T &); - T pop_front(); - T pop_back(); - - T& operator[](size_t); - const T& operator[](size_t) const; - T& at(size_t); - const T& at(size_t) const; - - void clear(); - -// iterator begin(); -// iterator end(); - - void erase(size_t); - void erase(size_t, size_t); - - bool empty() const { - return size() == 0; - } - bool full() const { - return (size() == Size); - } -}; - -template -size_t Deque::free() -{ - return Size - size(); -} - -template -const size_t Deque::size() const -{ - if (_tail < 0) - return 0; // empty - else if (_head > _tail) - return _head - _tail; - else - return Size - _tail + _head; -} - -template -const size_t Deque::max_size() const -{ - return Size; -} - -template -T & Deque::front() -{ - return _data[_tail]; -} - -template -const T & Deque::front() const -{ - return _data[_tail]; -} - -template -T & Deque::back() -{ - int idx = _head - 1; - if (idx < 0) idx = Size - 1; - return _data[idx]; -} - -template -const T & Deque::back() const -{ - int idx = _head - 1; - if (idx < 0) idx = Size - 1; - return _data[idx]; -} - -template -void Deque::push_front(const T &value) -{ - //if container is full, do nothing. - if (free()){ - if (--_tail < 0) - _tail = Size - 1; - _data[_tail] = value; - } -} - -template -void Deque::push_back(const T &value) -{ - //if container is full, do nothing. - if (free()){ - _data[_head] = value; - if (empty()) - _tail = _head; - if (++_head >= Size) - _head %= Size; - } -} - -template -T Deque::pop_front() { - if (empty()) // if empty, do nothing. - return T(); - auto item = front(); - if (++_tail >= Size) - _tail %= Size; - if (_tail == _head) - clear(); - return item; -} - -template -T Deque::pop_back() { - if (empty()) // if empty, do nothing. - return T(); - auto item = front(); - if (--_head < 0) - _head = Size - 1; - if (_head == _tail) //now buffer is empty - clear(); - return item; -} - -template -void Deque::erase(size_t position) { - if (position >= size()) // out-of-range! - return; // do nothing. - for (size_t i = position; i < size() - 1; i++){ - at(i) = at(i + 1); - } - pop_back(); -} - -template -void Deque::erase(size_t first, size_t last) { - if (first > last // invalid arguments - || first >= size()) // out-of-range - return; //do nothing. - - size_t tgt = first; - for (size_t i = last + 1; i < size(); i++){ - at(tgt++) = at(i); - } - for (size_t i = first; i <= last; i++){ - pop_back(); - } -} - -template -T& Deque::operator[](size_t index) -{ - auto i = _tail + index; - if (i >= Size) - i %= Size; - return _data[i]; -} - -template -const T& Deque::operator[](size_t index) const -{ - auto i = _tail + index; - if (i >= Size) - i %= Size; - return _data[i]; -} - -template -T& Deque::at(size_t index) -{ - auto i = _tail + index; - if (i >= Size) - i %= Size; - return _data[i]; -} - -template -const T& Deque::at(size_t index) const -{ - auto i = _tail + index; - if (i >= Size) - i %= Size; - return _data[i]; -} - -template -void Deque::clear() -{ - _tail = -1; - _head = 0; -}