Skip to content

Commit ab1dd15

Browse files
authored
Create BLE-MIDI_Client_ESP32.h
1 parent ad26e7b commit ab1dd15

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 write(uint8_t* data, uint8_t length)
26+
{
27+
_characteristic->setValue(data, length);
28+
_characteristic->notify();
29+
}
30+
31+
void receive(uint8_t* buffer, size_t length)
32+
{
33+
// Post the items to the back of the queue
34+
// (drop the first 2 items)
35+
for (size_t i = 2; i < length; i++)
36+
xQueueSend(_bleMidiTransport->mRxQueue, &buffer[i], portMAX_DELAY);
37+
}
38+
39+
void connected()
40+
{
41+
if (_bleMidiTransport->_connectedCallback)
42+
_bleMidiTransport->_connectedCallback();
43+
}
44+
45+
void disconnected()
46+
{
47+
if (_bleMidiTransport->_disconnectedCallback)
48+
_bleMidiTransport->_disconnectedCallback();
49+
}
50+
};
51+
52+
class MyClientCallbacks: public BLEClientCallbacks {
53+
public:
54+
MyClientCallbacks(BLEMIDI_Client_ESP32* bluetoothEsp32)
55+
: _bluetoothEsp32(bluetoothEsp32) {
56+
}
57+
58+
protected:
59+
BLEMIDI_Client_ESP32* _bluetoothEsp32 = nullptr;
60+
61+
void onConnect(BLEClient*) {
62+
if (_bluetoothEsp32)
63+
_bluetoothEsp32->connected();
64+
};
65+
66+
void onDisconnect(BLEClient*) {
67+
if (_bluetoothEsp32)
68+
_bluetoothEsp32->disconnected();
69+
}
70+
};
71+
72+
bool BLEMIDI_Client_ESP32::begin(const char* deviceName, BLEMIDI<class BLEMIDI_ESP32>* bleMidiTransport)
73+
{
74+
_bleMidiTransport = bleMidiTransport;
75+
76+
BLEDevice::init(deviceName);
77+
78+
_client = BLEDevice::createClient();
79+
_client->setCallbacks(new MyClientCallbacks(this));
80+
81+
// Retrieve a Scanner and set the callback we want to use to be informed when we
82+
// have detected a new device. Specify that we want active scanning and start the
83+
// scan to run for 5 seconds.
84+
pBLEScan = BLEDevice::getScan();
85+
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks(this));
86+
pBLEScan->setInterval(1349);
87+
pBLEScan->setWindow(449);
88+
pBLEScan->setActiveScan(true);
89+
doScan = true;
90+
pBLEScan->start(10, scanCompleteCB);
91+
92+
return true;
93+
}
94+
95+
END_BLEMIDI_NAMESPACE

0 commit comments

Comments
 (0)