Utility class for MIDI over Bluetooth LE

Dependencies:   BLE_API mbed nRF51822

Using MIDI over Bluetooth LE

This class enables sending and receiving MIDI packets.

Set up an "BLEMIDI" class instance

Create an instance, and set up to start using it.

Initialization

#include "mbed.h"
#include "BLEDevice.h"
#include "BLEMIDI.h"

BLEDevice device;
BLEMIDI bleMidi(&device);

void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) {
    bleMidi.onBleDisconnection(handle, reason);
}

void connectionCallback(Gap::Handle_t handle, Gap::addr_type_t type, const Gap::address_t addr, const Gap::ConnectionParams_t *params) {
    bleMidi.onBleConnection(handle, type, addr, params);
}

int main(void) {
    device.onConnection(connectionCallback);
    device.onDisconnection(disconnectionCallback);
    
    for (;;) {
        device.waitForEvent();
    }
}

Changing the device name

The default device name is 'MIDI'.
If the another name is preferred, use this constructor. The maximum length of device name is 4 bytes.

Constructor with device name

BLEDevice device;
// BLEMIDI bleMidi(&device);
BLEMIDI bleMidi(&device, "NAME");

Sending MIDI events

For example, send a note on event:

send a note on event

bleMidi.sendNoteOn(0, 63, 127);

About other events, see the BLEMIDI class reference.

Receiving MIDI events

For example, receive note on events:

receive note on events

void onNoteOn(uint8_t channel, uint8_t note, uint8_t velocity) {
    // do something with Note On event
}

bleMidi.attachNoteOn(onNoteOn);

About other events, see the BLEMIDI class reference.

Committer:
kshoji
Date:
Thu Apr 02 05:17:14 2015 +0000
Revision:
0:83889dc90473
Child:
1:cba2eba64f5c
Tested MIDI receiving

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kshoji 0:83889dc90473 1 #include "mbed.h"
kshoji 0:83889dc90473 2 #include "BLEDevice.h"
kshoji 0:83889dc90473 3 #include "BLEMIDI.h"
kshoji 0:83889dc90473 4
kshoji 0:83889dc90473 5 BLEDevice device;
kshoji 0:83889dc90473 6 BLEMIDI bleMidi(&device);
kshoji 0:83889dc90473 7
kshoji 0:83889dc90473 8 DigitalOut led1(P0_0);
kshoji 0:83889dc90473 9
kshoji 0:83889dc90473 10 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) {
kshoji 0:83889dc90473 11 bleMidi.onBleDisconnection(handle, reason);
kshoji 0:83889dc90473 12 }
kshoji 0:83889dc90473 13
kshoji 0:83889dc90473 14 void connectionCallback(Gap::Handle_t handle, Gap::addr_type_t type, const Gap::address_t addr, const Gap::ConnectionParams_t *params) {
kshoji 0:83889dc90473 15 bleMidi.onBleConnection(handle, type, addr, params);
kshoji 0:83889dc90473 16 }
kshoji 0:83889dc90473 17
kshoji 0:83889dc90473 18 void onNoteOn(uint8_t channel, uint8_t note, uint8_t velocity) {
kshoji 0:83889dc90473 19 led1 = 1;
kshoji 0:83889dc90473 20 // if (bleMidi.connected()) {
kshoji 0:83889dc90473 21 // bleMidi.sendNoteOn(channel, note, velocity);
kshoji 0:83889dc90473 22 // }
kshoji 0:83889dc90473 23 }
kshoji 0:83889dc90473 24
kshoji 0:83889dc90473 25 void onNoteOff(uint8_t channel, uint8_t note, uint8_t velocity) {
kshoji 0:83889dc90473 26 led1 = 0;
kshoji 0:83889dc90473 27 // if (bleMidi.connected()) {
kshoji 0:83889dc90473 28 // bleMidi.sendNoteOff(channel, note, velocity);
kshoji 0:83889dc90473 29 // }
kshoji 0:83889dc90473 30 }
kshoji 0:83889dc90473 31
kshoji 0:83889dc90473 32 int main(void) {
kshoji 0:83889dc90473 33 device.onConnection(connectionCallback);
kshoji 0:83889dc90473 34 device.onDisconnection(disconnectionCallback);
kshoji 0:83889dc90473 35
kshoji 0:83889dc90473 36 bleMidi.attachNoteOn(onNoteOn);
kshoji 0:83889dc90473 37 bleMidi.attachNoteOff(onNoteOff);
kshoji 0:83889dc90473 38
kshoji 0:83889dc90473 39 for (;;) {
kshoji 0:83889dc90473 40 device.waitForEvent();
kshoji 0:83889dc90473 41 }
kshoji 0:83889dc90473 42 }