-
Notifications
You must be signed in to change notification settings - Fork 1
/
MidiBLEDevice.cpp
executable file
·134 lines (103 loc) · 4.63 KB
/
MidiBLEDevice.cpp
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
#include <BLEPeripheral.h>
#include "MidiBLEDevice.h"
// A flag to keep track of whether or not we currently have a MIDI BLE subscriber connected, and waiting...
static bool _hasSubscriberConnected;
void BLESubscribedCallback(BLECentral& central, BLECharacteristic& characteristic) {
// Non class-method callback to track subscriber status...
Serial.println("MIDI BLE subscriber connected");
_hasSubscriberConnected = true;
}
void BLEUnsubscribedCallback(BLECentral& central, BLECharacteristic& characteristic) {
// Non class-method callback to track subscriber status...
Serial.println("MIDI BLE subscriber disconnected");
_hasSubscriberConnected = false;
}
MidiBLEDevice::MidiBLEDevice( char *cDeviceName ) {
deviceName = cDeviceName;
blePeripheral = new BLEPeripheral();
Serial.println("Starting Midi BLE device");
// Setup our bluetooth device according to the MIDI BLE spec...
bleService = new BLEService("03B80E5A-EDE8-4B33-A751-6CE34EC4C700");
bleCharacteristic = new BLECharacteristic("7772E5DB-3868-4112-A1A9-F2669D106BF3", BLERead | BLEWriteWithoutResponse | BLENotify, 20 );
// Set the descriptor/UUID...
// XXX - UUID here instead!?
bleDescriptor = new BLEDescriptor("2902", 0);
//BLEDescriptor descriptor = BLEDescriptor("a3c1291d-0c23-430f-9801-ab46897bddf6", 0);
// Setup the name of the device (for pairing)...
// local name sometimes used by central...
blePeripheral->setLocalName(deviceName);
// device name sometimes used by central...
blePeripheral->setDeviceName(deviceName);
// Advertise the MIDI UUID...
blePeripheral->setAdvertisedServiceUuid(bleService->uuid());
// add attributes (services, characteristics, descriptors) to the peripheral...
blePeripheral->addAttribute(*bleService);
blePeripheral->addAttribute(*bleCharacteristic);
blePeripheral->addAttribute(*bleDescriptor);
// set initial value/data
bleCharacteristic->setValue(0);
// Setup our callbacks (event handlers)...
bleCharacteristic->setEventHandler(BLESubscribed, &BLESubscribedCallback);
bleCharacteristic->setEventHandler(BLEUnsubscribed, &BLEUnsubscribedCallback);
//bleCharacteristic->setEventHandler(BLEWritten, onWrittenCallback);
//bleCharacteristic->setEventHandler(BLEConnected, onConnectedCallback);
//bleCharacteristic->setEventHandler(BLEDisconnected, onDisconnectedCallback);
// Start up our peripheral, as it is ready to go...
blePeripheral->begin();
Serial.println("Started Midi BLE device");
}
bool MidiBLEDevice::hasSubscriberConnected() {
return _hasSubscriberConnected;
}
void MidiBLEDevice::loop() {
// TODO: Add logging that makes sense below...
// Do this so the BLECentral can do its thing, otherwise, the BLE device will not work properly...
BLECentral central = blePeripheral->central();
if (central) {
// Serial.println("central");
if (central.connected() ) {
// Serial.println(" connected");
} else {
// Serial.println(" not connected");
}
} else {
// Serial.println("no central");
}
}
void MidiBLEDevice::sendMidiBlePacket( MidiBlePacket *packet ) {
bleCharacteristic->setValue( (const unsigned char *)packet->data, packet->size );
}
void MidiBLEDevice::sendMIDINoteOn( int midi_channel, int midi_note ) {
Serial.print("Channel: ");
Serial.print(midi_channel);
Serial.print("; Note: ");
Serial.print(midi_note);
Serial.print("; Position: ON");
Serial.println();
MidiBlePacket *packet = MidiBLEProtocol::generateNoteOnOffPacket( midi_channel, midi_note, true );
MidiBLEProtocol::logMidiPacket( packet );
sendMidiBlePacket( packet );
}
void MidiBLEDevice::sendMIDINoteOff( int midi_channel, int midi_note ) {
Serial.print("Channel: ");
Serial.print(midi_channel);
Serial.print("; Note: ");
Serial.print(midi_note);
Serial.print("; Position: OFF");
Serial.println();
MidiBlePacket *packet = MidiBLEProtocol::generateNoteOnOffPacket( midi_channel, midi_note, false );
MidiBLEProtocol::logMidiPacket( packet );
sendMidiBlePacket( packet );
}
void MidiBLEDevice::sendMIDIControlChange( int midi_channel, int midi_control_number, int midi_control_position ) {
Serial.print("Channel: ");
Serial.print(midi_channel);
Serial.print("; Control #: ");
Serial.print(midi_control_number);
Serial.print("; Position: ");
Serial.print(midi_control_position);
Serial.println();
MidiBlePacket *packet = MidiBLEProtocol::generateControlChangePacket( midi_channel, midi_control_number, midi_control_position );
MidiBLEProtocol::logMidiPacket( packet );
sendMidiBlePacket( packet );
}