-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathBLEMIDI_ESP32_NimBLE.h
176 lines (139 loc) · 5.14 KB
/
BLEMIDI_ESP32_NimBLE.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#pragma once
// Headers for ESP32 NimBLE
#include <NimBLEDevice.h>
BEGIN_BLEMIDI_NAMESPACE
class BLEMIDI_ESP32_NimBLE
{
private:
BLEServer *_server = nullptr;
BLEAdvertising *_advertising = nullptr;
BLECharacteristic *_characteristic = nullptr;
BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE, DefaultSettings> *_bleMidiTransport = nullptr;
friend class MyServerCallbacks;
friend class MyCharacteristicCallbacks;
protected:
QueueHandle_t mRxQueue;
public:
BLEMIDI_ESP32_NimBLE()
{
}
bool begin(const char *, BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE, DefaultSettings> *);
void end()
{
}
void write(uint8_t *buffer, size_t length)
{
_characteristic->setValue(buffer, length);
_characteristic->notify();
}
bool available(byte *pvBuffer)
{
// return 1 byte from the Queue
return xQueueReceive(mRxQueue, (void *)pvBuffer, 0); // return immediately when the queue is empty
}
void add(byte value)
{
// called from BLE-MIDI, to add it to a buffer here
xQueueSend(mRxQueue, &value, portMAX_DELAY);
}
protected:
void receive(uint8_t *buffer, size_t length)
{
// forward the buffer so it can be parsed
_bleMidiTransport->receive(buffer, length);
}
void connected()
{
if (_bleMidiTransport->_connectedCallback)
_bleMidiTransport->_connectedCallback();
}
void disconnected()
{
if (_bleMidiTransport->_disconnectedCallback)
_bleMidiTransport->_disconnectedCallback();
}
};
class MyServerCallbacks : public BLEServerCallbacks
{
public:
MyServerCallbacks(BLEMIDI_ESP32_NimBLE *bluetoothEsp32)
: _bluetoothEsp32(bluetoothEsp32)
{
}
protected:
BLEMIDI_ESP32_NimBLE *_bluetoothEsp32 = nullptr;
void onConnect(BLEServer *)
{
if (_bluetoothEsp32)
_bluetoothEsp32->connected();
};
void onDisconnect(BLEServer *)
{
if (_bluetoothEsp32)
_bluetoothEsp32->disconnected();
}
};
class MyCharacteristicCallbacks : public BLECharacteristicCallbacks
{
public:
MyCharacteristicCallbacks(BLEMIDI_ESP32_NimBLE *bluetoothEsp32)
: _bluetoothEsp32(bluetoothEsp32)
{
}
protected:
BLEMIDI_ESP32_NimBLE *_bluetoothEsp32 = nullptr;
void onWrite(BLECharacteristic *characteristic)
{
std::string rxValue = characteristic->getValue();
if (rxValue.length() > 0)
{
_bluetoothEsp32->receive((uint8_t *)(rxValue.c_str()), rxValue.length());
}
}
};
bool BLEMIDI_ESP32_NimBLE::begin(const char *deviceName, BLEMIDI_Transport<class BLEMIDI_ESP32_NimBLE, DefaultSettings> *bleMidiTransport)
{
_bleMidiTransport = bleMidiTransport;
BLEDevice::init(deviceName);
// To communicate between the 2 cores.
// Core_0 runs here, core_1 runs the BLE stack
mRxQueue = xQueueCreate(64, sizeof(uint8_t)); // TODO Settings::MaxBufferSize
_server = BLEDevice::createServer();
_server->setCallbacks(new MyServerCallbacks(this));
_server->advertiseOnDisconnect(true);
// Create the BLE Service
auto service = _server->createService(BLEUUID(SERVICE_UUID));
// Create a BLE Characteristic
_characteristic = service->createCharacteristic(
BLEUUID(CHARACTERISTIC_UUID),
NIMBLE_PROPERTY::READ |
NIMBLE_PROPERTY::WRITE |
NIMBLE_PROPERTY::NOTIFY |
NIMBLE_PROPERTY::WRITE_NR);
_characteristic->setCallbacks(new MyCharacteristicCallbacks(this));
auto _security = new NimBLESecurity();
_security->setAuthenticationMode(ESP_LE_AUTH_BOND);
// Start the service
service->start();
// Start advertising
_advertising = _server->getAdvertising();
_advertising->addServiceUUID(service->getUUID());
_advertising->setAppearance(0x00);
_advertising->start();
return true;
}
/*! \brief Create an instance for ESP32 named <DeviceName>
*/
#define BLEMIDI_CREATE_CUSTOM_INSTANCE(DeviceName, Name, CustomSettings) \
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE, CustomSettings> BLE##Name(DeviceName); \
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE, CustomSettings>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE, CustomSettings> &)BLE##Name);
/*! \brief Create an instance for ESP32 named <DeviceName>
*/
#define BLEMIDI_CREATE_INSTANCE(DeviceName, Name) \
BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE> BLE##Name(DeviceName); \
MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE>, BLEMIDI_NAMESPACE::MySettings> Name((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32_NimBLE> &)BLE##Name);
/*! \brief Create a default instance for ESP32 named BLE-MIDI
*/
#define BLEMIDI_CREATE_DEFAULT_INSTANCE() \
BLEMIDI_CREATE_INSTANCE("Esp32-NimBLE-MIDI", MIDI)
END_BLEMIDI_NAMESPACE