Skip to content

Instantly share code, notes, and snippets.

@johnty
Created January 16, 2018 22:53
Show Gist options
  • Save johnty/cbfa66c3369a692410f3493c20b2b3e2 to your computer and use it in GitHub Desktop.
Save johnty/cbfa66c3369a692410f3493c20b2b3e2 to your computer and use it in GitHub Desktop.
/*
BLE_MIDI Example by neilbags
https://github.com/neilbags/arduino-esp32-BLE-MIDI
Based on BLE_notify example by Evandro Copercini.
Creates a BLE MIDI service and characteristic.
Once a client subscibes, send a MIDI message every 2 seconds
*/
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
#define MIDI_SERVICE_UUID "03b80e5a-ede8-4b33-a751-6ce34ec4c700"
#define MIDI_CHARACTERISTIC_UUID "7772e5db-3868-4112-a1a9-f2669d106bf3"
uint8_t midiPacket[] = {
0x80, // header
0x80, // timestamp, not implemented
0x00, // status
0x3c, // 0x3c == 60 == middle c
0x00 // velocity
};
uint8_t midiPacketSx[] = {
0x80, // header
0x80, // timestamp, not implemented
0xF0, // SysEx start
0x04, // data
0x05, // data
0x06, // data
0x07, // data
0x08, // data
0x09, // data
0x0A, // data
0x0B, // data
0x0C, // data
0x0D, // data
0x0E, // data
0x0F, // data 15
0x10, //16
0x11, //17
0x12, //18
0x80, // timestamp, not implemented
0xF7 // footer
};
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
digitalWrite(LED_BUILTIN, HIGH);
//esp_ble_gap_update_conn_params(esp_ble_conn_update_params_t *params)
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
digitalWrite(LED_BUILTIN, LOW);
}
};
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
Serial.println("*********");
Serial.print("Received Value: ");
for (int i = 0; i < rxValue.length(); i++)
Serial.print(rxValue[i]);
Serial.println();
Serial.println("*********");
}
}
};
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
Serial.begin(115200);
BLEDevice::init("ESP32 MIDI Example");
// Create the BLE Server
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
// Create the BLE Service
BLEService *pService = pServer->createService(MIDI_SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristic = pService->createCharacteristic(
BLEUUID(MIDI_CHARACTERISTIC_UUID),
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE |
BLECharacteristic::PROPERTY_NOTIFY |
BLECharacteristic::PROPERTY_WRITE_NR
);
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.descriptor.gatt.client_characteristic_configuration.xml
// Create a BLE Descriptor
pCharacteristic->addDescriptor(new BLE2902());
//connect rx callback
pCharacteristic->setCallbacks(new MyCallbacks());
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->start();
for (int i = 0; i < 5; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(100);
}
}
void loop() {
if (deviceConnected) {
// note down
midiPacket[3] = 0x3c; // middle C
midiPacket[2] = 0x90; // note down, channel 0
midiPacket[4] = 127; // velocity
//pCharacteristic->setValue(midiPacket, 5); // packet, length in bytes
pCharacteristic->setValue(midiPacketSx, 20);
pCharacteristic->notify();
// play note for 500ms
delay(500);
// note up
midiPacket[2] = 0x80; // note up, channel 0
midiPacket[4] = 0; // velocity
//pCharacteristic->setValue(midiPacket, 5); // packet, length in bytes)
//pCharacteristic->notify();
delay(1000);
midiPacket[3] = 0x3F; // middle C
// note down
midiPacket[2] = 0x90; // note down, channel 0
midiPacket[4] = 127; // velocity
//pCharacteristic->setValue(midiPacket, 5); // packet, length in bytes
//pCharacteristic->notify();
// play note for 500ms
//delay(500);
// note up
midiPacket[2] = 0x80; // note up, channel 0
midiPacket[4] = 0; // velocity
//pCharacteristic->setValue(midiPacket, 5); // packet, length in bytes)
//pCharacteristic->notify();
//delay(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment