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:
Mon Apr 06 09:10:07 2015 +0000
Parent:
1:cba2eba64f5c
Child:
3:2b71bfbaa458
Commit message:
Add documents

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	Fri Apr 03 04:24:01 2015 +0000
+++ b/BLEMIDI.cpp	Mon Apr 06 09:10:07 2015 +0000
@@ -29,7 +29,6 @@
 }
 
 void BLEMIDI::dataWrittenCallback(const GattCharacteristicWriteCBParams *params) {
-    // read characteristic and write serial
     uint16_t length;
     uint8_t rxBuffer[20];
 
@@ -248,12 +247,16 @@
                     // last written uint8_t is for timestamp
                     if (sysExBufferPos > 0) {
                         sysExBuffer[sysExBufferPos - 1] = midiEvent;
-                        onSystemExclusive(sysExBuffer, sysExBufferPos);
+                        onSystemExclusive(sysExBuffer, sysExBufferPos, false);
                     }
 
                     sysExBufferPos = 0;
                     midiState = MIDI_STATE_TIMESTAMP;
                 } else {
+                    if (sysExBufferPos == 128) {
+                        onSystemExclusive(sysExBuffer, sysExBufferPos, true);
+                        sysExBufferPos = 0;
+                    }
                     sysExBuffer[sysExBufferPos++] = midiEvent;
                 }
             }
@@ -262,6 +265,10 @@
 }
 
 BLEMIDI::BLEMIDI(BLEDevice *dev) {
+    BLEMIDI(dev, "MIDI");
+}
+
+BLEMIDI::BLEMIDI(BLEDevice *dev, char *deviceName) {
     device = dev;
     isConnected = false;
     sysExBufferPos = 0;
@@ -271,10 +278,6 @@
     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
     LongUUIDBytes_t characteristicUuid = {
         0x77, 0x72, 0xe5, 0xdb, 0x38, 0x68, 0x41, 0x12, 
@@ -307,7 +310,7 @@
     /* setup advertising */
     device->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (uint8_t*)uuid128_list, sizeof(uuid128_list));
     device->accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
-    device->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
+    device->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)deviceName, sizeof(deviceName));
 
     device->setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
     device->setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
--- a/BLEMIDI.h	Fri Apr 03 04:24:01 2015 +0000
+++ b/BLEMIDI.h	Mon Apr 06 09:10:07 2015 +0000
@@ -30,6 +30,11 @@
      * Constructor
      */
     BLEMIDI(BLEDevice *device);
+    
+    /**
+     * Constructor with device name
+     */
+    BLEMIDI(BLEDevice *dev, char *deviceName);
 
     /**
      * Check if a BLE MIDI device is connected
@@ -39,7 +44,7 @@
     bool connected();
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Tune Request` event is received
      *
      * @param ptr function pointer
      *   prototype: void onTuneRequest();
@@ -49,7 +54,7 @@
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Timing Clock` event is received
      *
      * @param ptr function pointer
      *   prototype: void onTimingClock();
@@ -59,7 +64,7 @@
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Start` event is received
      *
      * @param ptr function pointer
      *   prototype: void onStart();
@@ -69,7 +74,7 @@
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Continue` event is received
      *
      * @param ptr function pointer
      *   prototype: void onContinue();
@@ -79,7 +84,7 @@
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Stop` event is received
      *
      * @param ptr function pointer
      *   prototype: void onStop();
@@ -89,7 +94,7 @@
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Active Sensing` event is received
      *
      * @param ptr function pointer
      *   prototype: void onActiveSensing();
@@ -99,7 +104,7 @@
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Reset` event is received
      *
      * @param ptr function pointer
      *   prototype: void onReset();
@@ -109,143 +114,254 @@
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Program Change` event is received
      *
      * @param ptr function pointer
-     *   prototype: void onProgramChange(uint8_t, uint8_t);
+     *   prototype: void onProgramChange(uint8_t channel, uint8_t program);
      */
     inline void attachnProgramChange(void (*fn)(uint8_t, uint8_t)) {
         onProgramChange = fn;
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Channel Aftertouch` event is received
      *
      * @param ptr function pointer
-     *   prototype: void onChannelAftertouch(uint8_t, uint8_t);
+     *   prototype: void onChannelAftertouch(uint8_t channel, uint8_t pressure);
      */
     inline void attachChannelAftertouch(void (*fn)(uint8_t, uint8_t)) {
         onChannelAftertouch = fn;
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Time Code Quarter Frame` event is received
      *
      * @param ptr function pointer
-     *   prototype: void onTimeCodeQuarterFrame(uint8_t);
+     *   prototype: void onTimeCodeQuarterFrame(uint8_t timing);
      */
     inline void attachTimeCodeQuarterFrame(void (*fn)(uint8_t)) {
         onTimeCodeQuarterFrame = fn;
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Song Select` event is received
      *
      * @param ptr function pointer
-     *   prototype: void onSongSelect(uint8_t);
+     *   prototype: void onSongSelect(uint8_t song);
      */
     inline void attachSongSelect(void (*fn)(uint8_t)) {
         onSongSelect = fn;
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Note Off` event is received
      *
      * @param ptr function pointer
-     *   prototype: void onNoteOff(uint8_t, uint8_t, uint8_t);
+     *   prototype: void onNoteOff(uint8_t channel, uint8_t note, uint8_t velocity);
      */
     inline void attachNoteOff(void (*fn)(uint8_t, uint8_t, uint8_t)) {
         onNoteOff = fn;
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Note On` event is received
      *
      * @param ptr function pointer
-     *   prototype: void onNoteOn(uint8_t, uint8_t, uint8_t);
+     *   prototype: void onNoteOn(uint8_t channel, uint8_t note, uint8_t velocity);
      */
     inline void attachNoteOn(void (*fn)(uint8_t, uint8_t, uint8_t)) {
         onNoteOn = fn;
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Polyphonic Aftertouch` event is received
      *
      * @param ptr function pointer
-     *   prototype: void onPolyphonicAftertouch(uint8_t, uint8_t, uint8_t);
+     *   prototype: void onPolyphonicAftertouch(uint8_t channel, uint8_t note, uint8_t pressure);
      */
     inline void attachPolyphonicAftertouch(void (*fn)(uint8_t, uint8_t, uint8_t)) {
         onPolyphonicAftertouch = fn;
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Control Change` event is received
      *
      * @param ptr function pointer
-     *   prototype: void onControlChange(uint8_t, uint8_t, uint8_t);
+     *   prototype: void onControlChange(uint8_t channel, uint8_t function, uint8_t value);
      */
     inline void attachControlChange(void (*fn)(uint8_t, uint8_t, uint8_t)) {
         onControlChange = fn;
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Pitch Wheel` event is received
      *
      * @param ptr function pointer
-     *   prototype: void onPitchWheel(uint8_t, uint16_t);
+     *   prototype: void onPitchWheel(uint8_t channel, uint16_t amount);
      */
     inline void attachPitchWheel(void (*fn)(uint8_t, uint16_t)) {
         onPitchWheel = fn;
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `Song Position Pointer` event is received
      *
      * @param ptr function pointer
-     *   prototype: void onSongPositionPointer(uint16_t);
+     *   prototype: void onSongPositionPointer(uint16_t position);
      */
     inline void attachSongPositionPointer(void (*fn)(uint16_t)) {
         onSongPositionPointer = fn;
     }
 
     /**
-     * Attach a callback called when the MIDI event is received
+     * Attach a callback called when the `System Exclusive` event is received
      *
      * @param ptr function pointer
-     *   prototype: void onSystemExclusive(uint8_t *, uint16_t);
+     *   prototype: void onSystemExclusive(uint8_t *sysex, uint16_t length, bool hasNextData);
      */
-    inline void attachSystemExclusive(void (*fn)(uint8_t *, uint16_t)) {
+    inline void attachSystemExclusive(void (*fn)(uint8_t *, uint16_t, bool)) {
         onSystemExclusive = fn;
     }
 
+    /**
+     * Send a `Tune Request` event
+     */
     void sendTuneRequest();
+
+    /**
+     * Send a `Timing Clock` event
+     */
     void sendTimingClock();
+
+    /**
+     * Send a `Start` event
+     */
     void sendStart();
+
+    /**
+     * Send a `Continue` event
+     */
     void sendContinue();
+
+    /**
+     * Send a `Stop` event
+     */
     void sendStop();
+
+    /**
+     * Send a `Active Sensing` event
+     */
     void sendActiveSensing();
+
+    /**
+     * Send a `Reset` event
+     */
     void sendReset();
+
+    /**
+     * Send a `Program Change` event
+     *
+     * @param channel 0-15
+     * @param program 0-127
+     */
     void sendProgramChange(uint8_t channel, uint8_t program);
+
+    /**
+     * Send a `Channel Aftertouch` event
+     *
+     * @param channel 0-15
+     * @param pressure 0-127
+     */
     void sendChannelAftertouch(uint8_t channel, uint8_t pressure);
+
+    /**
+     * Send a `Time Code Quarter Frame` event
+     *
+     * @param timing 0-127
+     */
     void sendTimeCodeQuarterFrame(uint8_t timing);
+
+    /**
+     * Send a `Song Select` event
+     *
+     * @param song 0-127
+     */
     void sendSongSelect(uint8_t song);
+
+    /**
+     * Send a `Note Off` event
+     *
+     * @param channel 0-15
+     * @param note 0-127
+     * @param velocity 0-127
+     */
     void sendNoteOff(uint8_t channel, uint8_t note, uint8_t velocity);
+
+    /**
+     * Send a `Note On` event
+     *
+     * @param channel 0-15
+     * @param note 0-127
+     * @param velocity 0-127
+     */
     void sendNoteOn(uint8_t channel, uint8_t note, uint8_t velocity);
+
+    /**
+     * Send a `Polyphonic Aftertouch` event
+     *
+     * @param channel 0-15
+     * @param note 0-127
+     * @param pressure 0-127
+     */
     void sendPolyphonicAftertouch(uint8_t channel, uint8_t note, uint8_t pressure);
+
+    /**
+     * Send a `Control Change` event
+     *
+     * @param channel 0-15
+     * @param function 0-127
+     * @param value 0-127
+     */
     void sendControlChange(uint8_t channel, uint8_t function, uint8_t value);
+
+    /**
+     * Send a `Pitch Wheel` event
+     *
+     * @param channel 0-15
+     * @param amount 0-8192(center)-16383
+     */
     void sendPitchWheel(uint8_t channel, uint16_t amount);
+
+    /**
+     * Send a `Song Position Pointer` event
+     *
+     * @param position 0-16383
+     */
     void sendSongPositionPointer(uint16_t position);
+
+    /**
+     * Send a `System Exclusive` event
+     *
+     * @param sysex the data starts with `0xf0` and ends with `0xf7`
+     * @param length
+     */
     void sendSystemExclusive(uint8_t * sysex, uint16_t length);
 
+    /**
+     * Notifies BLE disconnection to this BLE MIDI instance
+     */
     void onBleDisconnection(Gap::Handle_t handle, Gap::DisconnectionReason_t reason);
+    
+    /**
+     * Notifies BLE connection to this BLE MIDI instance
+     */
     void onBleConnection(Gap::Handle_t handle, Gap::addr_type_t type, const Gap::address_t addr, const Gap::ConnectionParams_t *params);
 
-
 private:
     bool isConnected;
 
     uint16_t sysExBufferPos;
-    uint8_t sysExBuffer[256];
+    uint8_t sysExBuffer[128];
     
     uint16_t timestamp;
 
@@ -281,7 +397,7 @@
     void (*onControlChange)(uint8_t, uint8_t, uint8_t);
     void (*onPitchWheel)(uint8_t, uint16_t);
     void (*onSongPositionPointer)(uint16_t);
-    void (*onSystemExclusive)(uint8_t *, uint16_t);
+    void (*onSystemExclusive)(uint8_t *, uint16_t, bool);
 
     void sendMidiMessage(uint8_t data0);
     void sendMidiMessage(uint8_t data0, uint8_t data1);
--- a/main.cpp	Fri Apr 03 04:24:01 2015 +0000
+++ b/main.cpp	Mon Apr 06 09:10:07 2015 +0000
@@ -1,12 +1,12 @@
 #include "mbed.h"
 #include "BLEDevice.h"
 #include "BLEMIDI.h"
+#include "math.h"
 
 BLEDevice device;
 BLEMIDI bleMidi(&device);
 
-DigitalOut led1(P0_0);
-InterruptIn button(P0_1);
+PwmOut sound(P0_0);
 
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) {
     bleMidi.onBleDisconnection(handle, reason);
@@ -17,17 +17,12 @@
 }
 
 void onNoteOn(uint8_t channel, uint8_t note, uint8_t velocity) {
-    led1 = 1;
-    if (bleMidi.connected()) {
-        bleMidi.sendNoteOn(channel, note, velocity);
-    }
+    sound.write(0.5f);
+    sound.period(1.0f / (440.0f * (float)pow(2.0, (note - 69.0) / 12.0)));
 }
 
 void onNoteOff(uint8_t channel, uint8_t note, uint8_t velocity) {
-    led1 = 0;
-    if (bleMidi.connected()) {
-        bleMidi.sendNoteOff(channel, note, velocity);
-    }
+    sound.pulsewidth(0);
 }
 
 int main(void) {