USBMIDI

This content relates to a deprecated version of Mbed

Mbed 2 is now deprecated. For the latest version please see the Mbed OS documentation.

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 for the LPC1768 and the LPC11U24
  • The on-board USB connector of the FRDM-KL25Z

Hello World

Import program

00001 // Hello World example for the USBMIDI library
00002 
00003 #include "mbed.h"
00004 #include "USBMIDI.h"
00005 
00006 USBMIDI midi;
00007 
00008 int main() {             
00009     while (1) {    
00010         for(int i=48; i<83; i++) {     // send some messages!
00011             midi.write(MIDIMessage::NoteOn(i));
00012             wait(0.25);
00013             midi.write(MIDIMessage::NoteOff(i));
00014             wait(0.5);
00015         }
00016     }
00017 }

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

Import program

00001 // Hello World example for the USBMIDI library
00002 
00003 #include "mbed.h"
00004 #include "USBMIDI.h"
00005 
00006 //USBMIDI object
00007 USBMIDI midi;
00008 
00009 
00010 // Leds which will be switch on or off according to a MIDImessage
00011 BusOut leds(LED1, LED2, LED3, LED4);
00012 
00013 BusInOut buttons(p22, p23, p24, p25);
00014 
00015 void show_message(MIDIMessage msg) {
00016     switch (msg.type()) {
00017         case MIDIMessage::NoteOnType:
00018             switch (msg.key()) {
00019                 case 48:
00020                     leds = (1 << 0);
00021                     break;
00022                 case 49:
00023                     leds = (1 << 1);
00024                     break;
00025                 case 50:
00026                     leds = (1 << 2);
00027                     break;
00028                 case 51:
00029                     leds = (1 << 3);
00030                     break;
00031             }
00032             break;
00033         case MIDIMessage::NoteOffType:
00034         default:
00035             leds = 0;
00036     }
00037 }
00038 
00039 int main() {
00040     uint8_t bus = 0;
00041     uint8_t p_bus = 0;
00042 
00043     // call back for messages received
00044     midi.attach(show_message);
00045 
00046     while (1) {
00047 
00048         //if buttons state changes, send a MIDI message
00049         bus = buttons.read();
00050         for (int i = 0; i < 4; i++) {
00051             if ( (bus & (1 << i)) != (p_bus & (1 << i))) {
00052                 if (bus & (1 << i)) {
00053                     midi.write(MIDIMessage::NoteOn(48 + i));
00054                 } else if ( !(bus & (1 << i)) ) {
00055                     midi.write(MIDIMessage::NoteOff(48 + i));
00056                 }
00057 
00058             }
00059         }
00060         wait(0.001);
00061         p_bus = bus;
00062     }
00063 }

All wikipages