A library to send and receive MIDI messages over USB using the default USB-MIDI drivers on Win/Mac

Dependents:   USBMIDI_HelloWorld USBMIDI_DrumExample USBMIDI_MonoSynth MIDI_Interface_ver_1 ... more

Committer:
simon
Date:
Sun Feb 20 13:10:05 2011 +0000
Revision:
1:ff74eabe02cd
Child:
2:10d694d6ccdc
First version, supporting most USB MIDI functionality

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 1:ff74eabe02cd 1 /** @license The MIT License
simon 1:ff74eabe02cd 2 * Copyright (c) 2011 mux, simon
simon 1:ff74eabe02cd 3 *
simon 1:ff74eabe02cd 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
simon 1:ff74eabe02cd 5 * of this software and associated documentation files (the "Software"), to deal
simon 1:ff74eabe02cd 6 * in the Software without restriction, including without limitation the rights
simon 1:ff74eabe02cd 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
simon 1:ff74eabe02cd 8 * copies of the Software, and to permit persons to whom the Software is
simon 1:ff74eabe02cd 9 * furnished to do so, subject to the following conditions:
simon 1:ff74eabe02cd 10 *
simon 1:ff74eabe02cd 11 * The above copyright notice and this permission notice shall be included in
simon 1:ff74eabe02cd 12 * all copies or substantial portions of the Software.
simon 1:ff74eabe02cd 13 *
simon 1:ff74eabe02cd 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
simon 1:ff74eabe02cd 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
simon 1:ff74eabe02cd 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
simon 1:ff74eabe02cd 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
simon 1:ff74eabe02cd 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
simon 1:ff74eabe02cd 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
simon 1:ff74eabe02cd 20 * THE SOFTWARE.
simon 1:ff74eabe02cd 21 */
simon 1:ff74eabe02cd 22
simon 1:ff74eabe02cd 23 #include "USBMIDI.h"
simon 1:ff74eabe02cd 24
simon 1:ff74eabe02cd 25 #include "mbed.h"
simon 1:ff74eabe02cd 26 #include "usbcore.h"
simon 1:ff74eabe02cd 27
simon 1:ff74eabe02cd 28 static void (*midi_evt)(MIDIMessage) = NULL;
simon 1:ff74eabe02cd 29
simon 1:ff74eabe02cd 30 USBMIDI::USBMIDI() {
simon 1:ff74eabe02cd 31 usb_init();
simon 1:ff74eabe02cd 32 usb_connect();
simon 1:ff74eabe02cd 33 while (!usb_configured());
simon 1:ff74eabe02cd 34 }
simon 1:ff74eabe02cd 35
simon 1:ff74eabe02cd 36 void USBMIDI::write(MIDIMessage m) {
simon 1:ff74eabe02cd 37 while (!writeable());
simon 1:ff74eabe02cd 38 ep_write(EP5, m.data, 4);
simon 1:ff74eabe02cd 39 }
simon 1:ff74eabe02cd 40
simon 1:ff74eabe02cd 41 bool USBMIDI::writeable() {
simon 1:ff74eabe02cd 42 return (bool)ep_writable(EP5);
simon 1:ff74eabe02cd 43 }
simon 1:ff74eabe02cd 44
simon 1:ff74eabe02cd 45 void USBMIDI::attach(void (*fptr)(MIDIMessage)) {
simon 1:ff74eabe02cd 46 midi_evt = fptr;
simon 1:ff74eabe02cd 47 }
simon 1:ff74eabe02cd 48
simon 1:ff74eabe02cd 49 void ep2_in() {}
simon 1:ff74eabe02cd 50
simon 1:ff74eabe02cd 51 void ep2_out() {
simon 1:ff74eabe02cd 52 uint8_t buf[MAX_EPn_PSIZE];
simon 1:ff74eabe02cd 53 int len = ep_read(EP4, buf);
simon 1:ff74eabe02cd 54
simon 1:ff74eabe02cd 55 if (midi_evt != NULL) {
simon 1:ff74eabe02cd 56 for(int i=0; i<len; i+=4){
simon 1:ff74eabe02cd 57 midi_evt(MIDIMessage(buf+i));
simon 1:ff74eabe02cd 58 }
simon 1:ff74eabe02cd 59 }
simon 1:ff74eabe02cd 60 }