@@ -11,37 +11,37 @@ BEGIN_BLEMIDI_NAMESPACE
11
11
class BLEMIDI_ESP32
12
12
{
13
13
private:
14
- BLEServer* _server = nullptr ;
15
- BLEAdvertising* _advertising = nullptr ;
16
- BLECharacteristic* _characteristic = nullptr ;
17
-
18
- BLEMIDI_Transport<class BLEMIDI_ESP32 >* _bleMidiTransport = nullptr ;
14
+ BLEServer *_server = nullptr ;
15
+ BLEAdvertising *_advertising = nullptr ;
16
+ BLECharacteristic *_characteristic = nullptr ;
19
17
20
- friend class MyServerCallbacks ;
21
- friend class MyCharacteristicCallbacks ;
18
+ BLEMIDI_Transport<class BLEMIDI_ESP32 > *_bleMidiTransport = nullptr ;
19
+
20
+ friend class MyServerCallbacks ;
21
+ friend class MyCharacteristicCallbacks ;
22
22
23
23
protected:
24
24
QueueHandle_t mRxQueue ;
25
25
26
26
public:
27
- BLEMIDI_ESP32 ()
27
+ BLEMIDI_ESP32 ()
28
28
{
29
29
}
30
-
31
- bool begin (const char *, BLEMIDI_Transport<class BLEMIDI_ESP32 >*);
32
-
33
- void end ()
30
+
31
+ bool begin (const char *, BLEMIDI_Transport<class BLEMIDI_ESP32 > *);
32
+
33
+ void end ()
34
34
{
35
-
35
+ Serial. println ( " end " );
36
36
}
37
37
38
- void write (uint8_t * buffer, size_t length)
38
+ void write (uint8_t * buffer, size_t length)
39
39
{
40
40
_characteristic->setValue (buffer, length);
41
41
_characteristic->notify ();
42
42
}
43
-
44
- bool available (byte* pvBuffer)
43
+
44
+ bool available (byte * pvBuffer)
45
45
{
46
46
return xQueueReceive (mRxQueue , pvBuffer, 0 ); // return immediately when the queue is empty
47
47
}
@@ -53,86 +53,97 @@ class BLEMIDI_ESP32
53
53
}
54
54
55
55
protected:
56
- void receive (uint8_t * buffer, size_t length)
57
- {
56
+ void receive (uint8_t * buffer, size_t length)
57
+ {
58
58
// parse the incoming buffer
59
59
_bleMidiTransport->receive (buffer, length);
60
- }
61
-
62
- void connected ()
63
- {
64
- if (_bleMidiTransport->_connectedCallback )
65
- _bleMidiTransport->_connectedCallback ();
66
- }
67
-
68
- void disconnected ()
69
- {
70
- if (_bleMidiTransport->_disconnectedCallback )
71
- _bleMidiTransport->_disconnectedCallback ();
72
- }
60
+ }
61
+
62
+ void connected ()
63
+ {
64
+ if (_bleMidiTransport->_connectedCallback )
65
+ _bleMidiTransport->_connectedCallback ();
66
+ }
67
+
68
+ void disconnected ()
69
+ {
70
+ if (_bleMidiTransport->_disconnectedCallback )
71
+ _bleMidiTransport->_disconnectedCallback ();
72
+
73
+ end ();
74
+ }
73
75
};
74
76
75
- class MyServerCallbacks : public BLEServerCallbacks {
77
+ class MyServerCallbacks : public BLEServerCallbacks
78
+ {
76
79
public:
77
- MyServerCallbacks (BLEMIDI_ESP32* bluetoothEsp32)
78
- : _bluetoothEsp32(bluetoothEsp32) {
80
+ MyServerCallbacks (BLEMIDI_ESP32 *bluetoothEsp32)
81
+ : _bluetoothEsp32(bluetoothEsp32)
82
+ {
79
83
}
80
84
81
85
protected:
82
- BLEMIDI_ESP32* _bluetoothEsp32 = nullptr ;
86
+ BLEMIDI_ESP32 * _bluetoothEsp32 = nullptr ;
83
87
84
- void onConnect (BLEServer*) {
88
+ void onConnect (BLEServer *)
89
+ {
85
90
if (_bluetoothEsp32)
86
91
_bluetoothEsp32->connected ();
87
92
};
88
-
89
- void onDisconnect (BLEServer*) {
93
+
94
+ void onDisconnect (BLEServer *server)
95
+ {
90
96
if (_bluetoothEsp32)
91
97
_bluetoothEsp32->disconnected ();
92
- }
98
+
99
+ server->getAdvertising ()->start ();
100
+ }
93
101
};
94
102
95
- class MyCharacteristicCallbacks : public BLECharacteristicCallbacks {
103
+ class MyCharacteristicCallbacks : public BLECharacteristicCallbacks
104
+ {
96
105
public:
97
- MyCharacteristicCallbacks (BLEMIDI_ESP32* bluetoothEsp32)
98
- : _bluetoothEsp32(bluetoothEsp32 ) {
106
+ MyCharacteristicCallbacks (BLEMIDI_ESP32 *bluetoothEsp32)
107
+ : _bluetoothEsp32(bluetoothEsp32)
108
+ {
99
109
}
100
-
110
+
101
111
protected:
102
- BLEMIDI_ESP32* _bluetoothEsp32 = nullptr ;
112
+ BLEMIDI_ESP32 * _bluetoothEsp32 = nullptr ;
103
113
104
- void onWrite (BLECharacteristic * characteristic) {
114
+ void onWrite (BLECharacteristic *characteristic)
115
+ {
105
116
std::string rxValue = characteristic->getValue ();
106
- if (rxValue.length () > 0 ) {
107
- _bluetoothEsp32->receive ((uint8_t *)(rxValue.c_str ()), rxValue.length ());
117
+ if (rxValue.length () > 0 )
118
+ {
119
+ _bluetoothEsp32->receive ((uint8_t *)(rxValue.c_str ()), rxValue.length ());
108
120
}
109
121
}
110
122
};
111
123
112
- bool BLEMIDI_ESP32::begin (const char * deviceName, BLEMIDI_Transport<class BLEMIDI_ESP32 >* bleMidiTransport)
124
+ bool BLEMIDI_ESP32::begin (const char * deviceName, BLEMIDI_Transport<class BLEMIDI_ESP32 > * bleMidiTransport)
113
125
{
114
- _bleMidiTransport = bleMidiTransport;
126
+ _bleMidiTransport = bleMidiTransport;
115
127
116
128
BLEDevice::init (deviceName);
117
-
129
+
118
130
// To communicate between the 2 cores.
119
131
// Core_0 runs here, core_1 runs the BLE stack
120
132
mRxQueue = xQueueCreate (64 , sizeof (uint8_t )); // TODO Settings::MaxBufferSize
121
133
122
134
_server = BLEDevice::createServer ();
123
135
_server->setCallbacks (new MyServerCallbacks (this ));
124
-
136
+
125
137
// Create the BLE Service
126
138
auto service = _server->createService (BLEUUID (SERVICE_UUID));
127
-
139
+
128
140
// Create a BLE Characteristic
129
141
_characteristic = service->createCharacteristic (
130
- BLEUUID (CHARACTERISTIC_UUID),
131
- BLECharacteristic::PROPERTY_READ |
132
- BLECharacteristic::PROPERTY_WRITE |
133
- BLECharacteristic::PROPERTY_NOTIFY |
134
- BLECharacteristic::PROPERTY_WRITE_NR
135
- );
142
+ BLEUUID (CHARACTERISTIC_UUID),
143
+ BLECharacteristic::PROPERTY_READ |
144
+ BLECharacteristic::PROPERTY_WRITE |
145
+ BLECharacteristic::PROPERTY_NOTIFY |
146
+ BLECharacteristic::PROPERTY_WRITE_NR);
136
147
// Add CCCD 0x2902 to allow notify
137
148
_characteristic->addDescriptor (new BLE2902 ());
138
149
@@ -143,25 +154,27 @@ bool BLEMIDI_ESP32::begin(const char* deviceName, BLEMIDI_Transport<class BLEMID
143
154
144
155
// Start the service
145
156
service->start ();
146
-
157
+
147
158
// Start advertising
148
159
_advertising = _server->getAdvertising ();
149
160
_advertising->addServiceUUID (service->getUUID ());
150
161
_advertising->setAppearance (0x00 );
151
162
_advertising->start ();
152
-
163
+
164
+ Serial.println (" begin" );
165
+
153
166
return true ;
154
167
}
155
168
156
- /* ! \brief Create an instance for ESP32 named <DeviceName>
169
+ /* ! \brief Create an instance for ESP32 named <DeviceName>
157
170
*/
158
- #define BLEMIDI_CREATE_INSTANCE (DeviceName, Name ) \
159
- BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32> BLE##Name(DeviceName); \
160
- MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32>, BLEMIDI_NAMESPACE::MySettings> Name ((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32> &)BLE##Name);
171
+ #define BLEMIDI_CREATE_INSTANCE (DeviceName, Name ) \
172
+ BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32> BLE##Name(DeviceName); \
173
+ MIDI_NAMESPACE::MidiInterface<BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32>, BLEMIDI_NAMESPACE::MySettings> Name ((BLEMIDI_NAMESPACE::BLEMIDI_Transport<BLEMIDI_NAMESPACE::BLEMIDI_ESP32> &)BLE##Name);
161
174
162
- /* ! \brief Create a default instance for ESP32 named BLE-MIDI
175
+ /* ! \brief Create a default instance for ESP32 named BLE-MIDI
163
176
*/
164
177
#define BLEMIDI_CREATE_DEFAULT_INSTANCE () \
165
- BLEMIDI_CREATE_INSTANCE (" Esp32-BLE-MIDI" , MIDI)
178
+ BLEMIDI_CREATE_INSTANCE (" Esp32-BLE-MIDI" , MIDI)
166
179
167
180
END_BLEMIDI_NAMESPACE
0 commit comments