Example program for USBHostMIDI http://mbed.org/users/kshoji/code/USBHostMIDI/

Dependencies:   USBHostMIDI mbed

USB MIDI event handling examples for this library.

http://mbed.org/users/kshoji/code/USBHostMIDI/

Committer:
kshoji
Date:
Fri Dec 06 03:28:49 2013 +0000
Revision:
1:01305cc0e2a2
Parent:
0:78ea62ee7eab
example for sending MIDI messages

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kshoji 0:78ea62ee7eab 1 #include "mbed.h"
kshoji 0:78ea62ee7eab 2 #include "USBHostMIDI.h"
kshoji 1:01305cc0e2a2 3
kshoji 1:01305cc0e2a2 4 // pots on the mbed application board
kshoji 1:01305cc0e2a2 5 AnalogIn pot1(p19);
kshoji 1:01305cc0e2a2 6 AnalogIn pot2(p20);
kshoji 1:01305cc0e2a2 7 unsigned int lastPot1;
kshoji 1:01305cc0e2a2 8 unsigned int lastPot2;
kshoji 1:01305cc0e2a2 9
kshoji 0:78ea62ee7eab 10 void noteOn(unsigned char channel, unsigned char note, unsigned char velocity) {
kshoji 0:78ea62ee7eab 11 printf("note on channel: %d, note: %d, velocity: %d\r\n", channel, note, velocity);
kshoji 0:78ea62ee7eab 12 }
kshoji 0:78ea62ee7eab 13
kshoji 0:78ea62ee7eab 14 void noteOff(unsigned char channel, unsigned char note, unsigned char velocity) {
kshoji 0:78ea62ee7eab 15 printf("note off channel: %d, note: %d, velocity: %d\r\n", channel, note, velocity);
kshoji 0:78ea62ee7eab 16 }
kshoji 0:78ea62ee7eab 17
kshoji 0:78ea62ee7eab 18 void controlChange(unsigned char channel, unsigned char key, unsigned char value) {
kshoji 0:78ea62ee7eab 19 printf("control change channel: %d, key: %d, value: %d\r\n", channel, key, value);
kshoji 0:78ea62ee7eab 20 }
kshoji 0:78ea62ee7eab 21
kshoji 0:78ea62ee7eab 22 void programChange(unsigned char channel, unsigned char program) {
kshoji 0:78ea62ee7eab 23 printf("progaram change channel: %d, program: %d\r\n", channel, program);
kshoji 0:78ea62ee7eab 24 }
kshoji 0:78ea62ee7eab 25
kshoji 0:78ea62ee7eab 26 void pitchBend(unsigned char channel, unsigned int value) {
kshoji 0:78ea62ee7eab 27 printf("pitch bend channel: %d, value: %d\r\n", channel, value);
kshoji 0:78ea62ee7eab 28 }
kshoji 0:78ea62ee7eab 29
kshoji 0:78ea62ee7eab 30 void midi_task(void const*) {
kshoji 0:78ea62ee7eab 31 USBHostMIDI midi;
kshoji 1:01305cc0e2a2 32
kshoji 0:78ea62ee7eab 33 // attach midi event callbacks
kshoji 0:78ea62ee7eab 34 midi.attachNoteOn(noteOn);
kshoji 0:78ea62ee7eab 35 midi.attachNoteOff(noteOff);
kshoji 0:78ea62ee7eab 36 midi.attachControlChange(controlChange);
kshoji 0:78ea62ee7eab 37 midi.attachProgramChange(programChange);
kshoji 0:78ea62ee7eab 38 midi.attachPitchBend(pitchBend);
kshoji 0:78ea62ee7eab 39
kshoji 0:78ea62ee7eab 40 while(1) {
kshoji 0:78ea62ee7eab 41 // try to connect a midi device
kshoji 0:78ea62ee7eab 42 while(!midi.connect())
kshoji 0:78ea62ee7eab 43 Thread::wait(500);
kshoji 0:78ea62ee7eab 44
kshoji 0:78ea62ee7eab 45 // if the device is disconnected, we try to connect it again
kshoji 0:78ea62ee7eab 46 while (1) {
kshoji 1:01305cc0e2a2 47 if (lastPot1 != pot1.read_u16() >> 9) {
kshoji 1:01305cc0e2a2 48 lastPot1 = pot1.read_u16() >> 9;
kshoji 1:01305cc0e2a2 49 // channel volume
kshoji 1:01305cc0e2a2 50 midi.sendControlChange(0, 7, lastPot1);
kshoji 1:01305cc0e2a2 51 }
kshoji 1:01305cc0e2a2 52
kshoji 1:01305cc0e2a2 53 if (lastPot2 != pot2.read_u16() >> 9) {
kshoji 1:01305cc0e2a2 54 lastPot2 = pot2.read_u16() >> 9;
kshoji 1:01305cc0e2a2 55 // program change
kshoji 1:01305cc0e2a2 56 midi.sendProgramChange(0, lastPot2);
kshoji 1:01305cc0e2a2 57 }
kshoji 1:01305cc0e2a2 58
kshoji 1:01305cc0e2a2 59 // midi.sendNoteOn(0, 60, 127);
kshoji 1:01305cc0e2a2 60 // midi.sendNoteOff(0, 60, 0);
kshoji 1:01305cc0e2a2 61
kshoji 0:78ea62ee7eab 62 // if device disconnected, try to connect it again
kshoji 0:78ea62ee7eab 63 if (!midi.connected())
kshoji 0:78ea62ee7eab 64 break;
kshoji 0:78ea62ee7eab 65
kshoji 0:78ea62ee7eab 66 Thread::wait(50);
kshoji 0:78ea62ee7eab 67 }
kshoji 0:78ea62ee7eab 68 }
kshoji 0:78ea62ee7eab 69 }
kshoji 0:78ea62ee7eab 70
kshoji 0:78ea62ee7eab 71 int main() {
kshoji 1:01305cc0e2a2 72 lastPot1 = pot1.read_u16() >> 9;
kshoji 1:01305cc0e2a2 73 lastPot2 = pot2.read_u16() >> 9;
kshoji 0:78ea62ee7eab 74 Thread midiTask(midi_task, NULL, osPriorityNormal, 256 * 4);
kshoji 0:78ea62ee7eab 75 while(1) {
kshoji 0:78ea62ee7eab 76 Thread::wait(500);
kshoji 0:78ea62ee7eab 77 }
kshoji 0:78ea62ee7eab 78 }