Goran Mahovlic / Mbed 2 deprecated BLE_MIDI

Dependencies:   BLE_API mbed nRF51822_BLE_MIDI

Dependents:   BLE_MIDI_one_note_TEST

Fork of BLE_MIDI by Kaoru Shoji

Files at this revision

API Documentation at this revision

Comitter:
kshoji
Date:
Fri Apr 03 04:24:01 2015 +0000
Parent:
0:83889dc90473
Child:
2:dbc6f81b9ba0
Commit message:
Tested MIDI sending

Changed in this revision

BLEMIDI.cpp Show annotated file Show diff for this revision Revisions of this file
BLEMIDI.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- 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);
--- a/BLEMIDI.h	Thu Apr 02 05:17:14 2015 +0000
+++ b/BLEMIDI.h	Fri Apr 03 04:24:01 2015 +0000
@@ -246,7 +246,7 @@
 
     uint16_t sysExBufferPos;
     uint8_t sysExBuffer[256];
-
+    
     uint16_t timestamp;
 
     uint8_t midiEventKind;
@@ -289,6 +289,8 @@
 
     void dataWrittenCallback(const GattCharacteristicWriteCBParams *params);
 
+    uint8_t midi[20];
+
     BLEDevice *device;
     GattCharacteristic *midiCharacteristic;    
     Timer tick;
--- a/main.cpp	Thu Apr 02 05:17:14 2015 +0000
+++ b/main.cpp	Fri Apr 03 04:24:01 2015 +0000
@@ -6,6 +6,7 @@
 BLEMIDI bleMidi(&device);
 
 DigitalOut led1(P0_0);
+InterruptIn button(P0_1);
 
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) {
     bleMidi.onBleDisconnection(handle, reason);
@@ -17,16 +18,16 @@
 
 void onNoteOn(uint8_t channel, uint8_t note, uint8_t velocity) {
     led1 = 1;
-//    if (bleMidi.connected()) {
-//        bleMidi.sendNoteOn(channel, note, velocity);
-//    }
+    if (bleMidi.connected()) {
+        bleMidi.sendNoteOn(channel, note, velocity);
+    }
 }
 
 void onNoteOff(uint8_t channel, uint8_t note, uint8_t velocity) {
     led1 = 0;
-//    if (bleMidi.connected()) {
-//        bleMidi.sendNoteOff(channel, note, velocity);
-//    }
+    if (bleMidi.connected()) {
+        bleMidi.sendNoteOff(channel, note, velocity);
+    }
 }
 
 int main(void) {