You are viewing an older revision! See the latest version
USBMIDI
The USBMIDI interface can be used to send and receive MIDI messages over USB using the standard USB-MIDI protocol.
Using this library, you can do things like send MIDI messages to a computer (such as to record in a sequencer, or trigger a software synthesiser) and receive messages from a computer (such as actuate things based on MIDI events)
The USB connector should be attached to p31 (D+), p32 (D-) and GND. You can also connect the USB power to VIN to power the mbed when connected.
Hello World¶
USBMIDI Hello World
// Hello World example for the USBMIDI library
#include "mbed.h"
#include "USBMIDI.h"
USBMIDI midi;
int main() {
while (1) {
for(int i=48; i<83; i++) { // send some messages!
midi.write(MIDIMessage::NoteOn(i));
wait(0.25);
midi.write(MIDIMessage::NoteOff(i));
wait(0.5);
}
}
}
Import programUSBMIDI_HelloWorld
USBMIDI Hello World
API¶
Import library
Public Member Functions |
|
| USBMIDI (uint16_t vendor_id=0x0700, uint16_t product_id=0x0101, uint16_t product_release=0x0001) | |
|
Constructor.
|
|
| void | write ( MIDIMessage m) |
|
Send a
MIDIMessage
.
|
|
| void | attach (void(*fptr)( MIDIMessage )) |
|
Attach a callback for when a MIDIEvent is received.
|
|
More example¶
In this example, you can control the MIDI message sent with buttons
USBMIDI: leds controlled by MIDI messages received
#include "mbed.h"
#include "USBMIDI.h"
//USBMIDI object
USBMIDI midi;
// Leds which will be switch on or off according to a MIDImessage
BusOut leds(LED1, LED2, LED3, LED4);
BusInOut buttons(p22, p23, p24, p25);
void show_message(MIDIMessage msg) {
switch (msg.type()) {
case MIDIMessage::NoteOnType:
switch (msg.key()) {
case 48:
leds = (1 << 0);
break;
case 49:
leds = (1 << 1);
break;
case 50:
leds = (1 << 2);
break;
case 51:
leds = (1 << 3);
break;
}
break;
case MIDIMessage::NoteOffType:
default:
leds = 0;
}
}
int main() {
uint8_t bus = 0;
uint8_t p_bus = 0;
// call back for messages received
midi.attach(show_message);
while (1) {
//if buttons state changes, send a MIDI message
bus = buttons.read();
for (int i = 0; i < 4; i++) {
if ( (bus & (1 << i)) != (p_bus & (1 << i))) {
if (bus & (1 << i)) {
midi.write(MIDIMessage::NoteOn(48 + i));
} else if ( !(bus & (1 << i)) ) {
midi.write(MIDIMessage::NoteOff(48 + i));
}
}
}
wait(0.001);
p_bus = bus;
}
}
Import programUSBMIDI_Receive
USBMIDI example
