Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822_BLE_MIDI
Dependents: BLE_MIDI_one_note_TEST
Fork of BLE_MIDI by
Diff: BLEMIDI.cpp
- Revision:
- 1:cba2eba64f5c
- Parent:
- 0:83889dc90473
- Child:
- 2:dbc6f81b9ba0
--- a/BLEMIDI.cpp Thu Apr 02 05:17:14 2015 +0000
+++ b/BLEMIDI.cpp Fri Apr 03 04:24:01 2015 +0000
@@ -246,8 +246,10 @@
if (midiEvent == 0xf7) {
// the end of message
// last written uint8_t is for timestamp
- sysExBuffer[sysExBufferPos - 1] = midiEvent;
- onSystemExclusive(sysExBuffer, sysExBufferPos);
+ if (sysExBufferPos > 0) {
+ sysExBuffer[sysExBufferPos - 1] = midiEvent;
+ onSystemExclusive(sysExBuffer, sysExBufferPos);
+ }
sysExBufferPos = 0;
midiState = MIDI_STATE_TIMESTAMP;
@@ -264,19 +266,22 @@
isConnected = false;
sysExBufferPos = 0;
+ timestamp = 0;
+ midiEventKind = 0;
+ midiEventNote = 0;
+ midiEventVelocity = 0;
+
// Advertise packet length is 31 bytes.
// device name is upto 4 bytes if the service UUID included
const char DEVICE_NAME[] = "MIDI";
// MIDI characteristic
- uint8_t midiPayload[20];
LongUUIDBytes_t characteristicUuid = {
0x77, 0x72, 0xe5, 0xdb, 0x38, 0x68, 0x41, 0x12,
0xa1, 0xa9, 0xf2, 0x66, 0x9d, 0x10, 0x6b, 0xf3
};
- GattCharacteristic midiChar(UUID(characteristicUuid), midiPayload, 0, 20, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
- midiCharacteristic = &midiChar;
+ midiCharacteristic = new GattCharacteristic(UUID(characteristicUuid), midi, 0, 20, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY);
GattCharacteristic *midiChars[] = {midiCharacteristic};
// MIDI service
@@ -317,11 +322,9 @@
void BLEMIDI::sendMidiMessage(uint8_t data0) {
if (isConnected) {
- uint8_t midi[3];
-
unsigned int ticks = tick.read_ms() & 0x1fff;
- midi[0] = 0x80 | (ticks >> 7) & 0x3f;
- midi[1] = ticks & 0x7f;
+ midi[0] = 0x80 | ((ticks >> 7) & 0x3f);
+ midi[1] = 0x80 | (ticks & 0x7f);
midi[2] = data0;
device->updateCharacteristicValue(midiCharacteristic->getValueAttribute().getHandle(), midi, 3);
@@ -330,11 +333,9 @@
void BLEMIDI::sendMidiMessage(uint8_t data0, uint8_t data1) {
if (isConnected) {
- uint8_t midi[4];
-
unsigned int ticks = tick.read_ms() & 0x1fff;
- midi[0] = 0x80 | (ticks >> 7) & 0x3f;
- midi[1] = ticks & 0x7f;
+ midi[0] = 0x80 | ((ticks >> 7) & 0x3f);
+ midi[1] = 0x80 | (ticks & 0x7f);
midi[2] = data0;
midi[3] = data1;
@@ -344,11 +345,9 @@
void BLEMIDI::sendMidiMessage(uint8_t data0, uint8_t data1, uint8_t data2) {
if (isConnected) {
- uint8_t midi[5];
-
unsigned int ticks = tick.read_ms() & 0x1fff;
- midi[0] = 0x80 | (ticks >> 7) & 0x3f;
- midi[1] = ticks & 0x7f;
+ midi[0] = 0x80 | ((ticks >> 7) & 0x3f);
+ midi[1] = 0x80 | (ticks & 0x7f);
midi[2] = data0;
midi[3] = data1;
midi[4] = data2;
@@ -410,19 +409,17 @@
}
void BLEMIDI::sendSystemExclusive(uint8_t * sysex, uint16_t length) {
if (isConnected) {
- uint8_t midi[20];
uint8_t position = 0;
// header
- unsigned int ticks = tick.read_ms() & 0x1fff;
- midi[position++] = 0x80 | (ticks >> 7) & 0x3f;
- midi[position++] = ticks & 0x7f;
+ uint16_t ticks = tick.read_ms() & 0x1fff;
+ midi[position++] = 0x80 | ((ticks >> 7) & 0x3f);
+ midi[position++] = 0x80 | (ticks & 0x7f);
- unsigned int timestamp = tick.read_ms() & 0x1fff;
for (int i = 0; i < length; i++) {
if (i == length - 1) {
- // last byte
- midi[position++] = ticks & 0x7f;
+ // modify last byte
+ midi[position++] = 0x80 | (ticks & 0x7f);
if (position == 20) {
device->updateCharacteristicValue(midiCharacteristic->getValueAttribute().getHandle(), midi, 20);
