|
| 1 | +#pragma once |
| 2 | + |
| 3 | +// Headers for ESP32 BLE |
| 4 | +#include <BLEDevice.h> |
| 5 | +#include <BLEUtils.h> |
| 6 | +#include <BLEServer.h> |
| 7 | +#include <BLE2902.h> |
| 8 | + |
| 9 | +BEGIN_BLEMIDI_NAMESPACE |
| 10 | + |
| 11 | +class BLEMIDI_Client_ESP32 |
| 12 | +{ |
| 13 | +private: |
| 14 | + BLEClient* _client = nullptr; |
| 15 | + |
| 16 | + BLEMIDI<class BLEMIDI_Client_ESP32>* _bleMidiTransport = nullptr; |
| 17 | + |
| 18 | +public: |
| 19 | + BLEMIDI_Client_ESP32() |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + bool begin(const char*, BLEMIDI<class BLEMIDI_Client_ESP32>*); |
| 24 | + |
| 25 | + void end() |
| 26 | + { |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | + void write(uint8_t* data, uint8_t length) |
| 31 | + { |
| 32 | + _characteristic->setValue(data, length); |
| 33 | + _characteristic->notify(); |
| 34 | + } |
| 35 | + |
| 36 | + void receive(uint8_t* buffer, size_t length) |
| 37 | + { |
| 38 | + // Post the items to the back of the queue |
| 39 | + // (drop the first 2 items) |
| 40 | + for (size_t i = 2; i < length; i++) |
| 41 | + xQueueSend(_bleMidiTransport->mRxQueue, &buffer[i], portMAX_DELAY); |
| 42 | + } |
| 43 | + |
| 44 | + void connected() |
| 45 | + { |
| 46 | + if (_bleMidiTransport->_connectedCallback) |
| 47 | + _bleMidiTransport->_connectedCallback(); |
| 48 | + } |
| 49 | + |
| 50 | + void disconnected() |
| 51 | + { |
| 52 | + if (_bleMidiTransport->_disconnectedCallback) |
| 53 | + _bleMidiTransport->_disconnectedCallback(); |
| 54 | + } |
| 55 | +}; |
| 56 | + |
| 57 | +class MyClientCallbacks: public BLEClientCallbacks { |
| 58 | +public: |
| 59 | + MyClientCallbacks(BLEMIDI_Client_ESP32* bluetoothEsp32) |
| 60 | + : _bluetoothEsp32(bluetoothEsp32) { |
| 61 | + } |
| 62 | + |
| 63 | +protected: |
| 64 | + BLEMIDI_Client_ESP32* _bluetoothEsp32 = nullptr; |
| 65 | + |
| 66 | + void onConnect(BLEClient*) { |
| 67 | + if (_bluetoothEsp32) |
| 68 | + _bluetoothEsp32->connected(); |
| 69 | + }; |
| 70 | + |
| 71 | + void onDisconnect(BLEClient*) { |
| 72 | + if (_bluetoothEsp32) |
| 73 | + _bluetoothEsp32->disconnected(); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +bool BLEMIDI_Client_ESP32::begin(const char* deviceName, BLEMIDI<class BLEMIDI_ESP32>* bleMidiTransport) |
| 78 | +{ |
| 79 | + _bleMidiTransport = bleMidiTransport; |
| 80 | + |
| 81 | + BLEDevice::init(deviceName); |
| 82 | + |
| 83 | + _client = BLEDevice::createClient(); |
| 84 | + _client->setCallbacks(new MyClientCallbacks(this)); |
| 85 | + |
| 86 | + // Retrieve a Scanner and set the callback we want to use to be informed when we |
| 87 | + // have detected a new device. Specify that we want active scanning and start the |
| 88 | + // scan to run for 5 seconds. |
| 89 | + pBLEScan = BLEDevice::getScan(); |
| 90 | + pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(this)); |
| 91 | + pBLEScan->setInterval(1349); |
| 92 | + pBLEScan->setWindow(449); |
| 93 | + pBLEScan->setActiveScan(true); |
| 94 | + doScan = true; |
| 95 | + pBLEScan->start(10, scanCompleteCB); |
| 96 | + |
| 97 | + return true; |
| 98 | +} |
| 99 | + |
| 100 | +END_BLEMIDI_NAMESPACE |
0 commit comments