Kaoru Shoji / Mbed 2 deprecated USBHostMIDI_example

Dependencies:   USBHostMIDI mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "USBHostMIDI.h"
00003 
00004 // pots on the mbed application board
00005 AnalogIn pot1(p19);
00006 AnalogIn pot2(p20);
00007 unsigned int lastPot1;
00008 unsigned int lastPot2;
00009 
00010 void noteOn(unsigned char channel, unsigned char note, unsigned char velocity) {
00011     printf("note on channel: %d, note: %d, velocity: %d\r\n", channel, note, velocity);
00012 }
00013 
00014 void noteOff(unsigned char channel, unsigned char note, unsigned char velocity) {
00015     printf("note off channel: %d, note: %d, velocity: %d\r\n", channel, note, velocity);
00016 }
00017 
00018 void controlChange(unsigned char channel, unsigned char key, unsigned char value) {
00019     printf("control change channel: %d, key: %d, value: %d\r\n", channel, key, value);
00020 }
00021 
00022 void programChange(unsigned char channel, unsigned char program) {
00023     printf("progaram change channel: %d, program: %d\r\n", channel, program);
00024 }
00025 
00026 void pitchBend(unsigned char channel, unsigned int value) {
00027     printf("pitch bend channel: %d, value: %d\r\n", channel, value);
00028 }
00029 
00030 void midi_task(void const*) {
00031     USBHostMIDI midi;
00032     
00033     // attach midi event callbacks
00034     midi.attachNoteOn(noteOn);
00035     midi.attachNoteOff(noteOff);
00036     midi.attachControlChange(controlChange);
00037     midi.attachProgramChange(programChange);
00038     midi.attachPitchBend(pitchBend);
00039 
00040     while(1) {
00041         // try to connect a midi device
00042         while(!midi.connect())
00043             Thread::wait(500);
00044         
00045         // if the device is disconnected, we try to connect it again
00046         while (1) {
00047             if (lastPot1 != pot1.read_u16() >> 9) {
00048                 lastPot1 = pot1.read_u16() >> 9;
00049                 // channel volume
00050                 midi.sendControlChange(0, 7, lastPot1);
00051             }
00052             
00053             if (lastPot2 != pot2.read_u16() >> 9) {
00054                 lastPot2 = pot2.read_u16() >> 9;
00055                 // program change
00056                 midi.sendProgramChange(0, lastPot2);
00057             }
00058             
00059             // midi.sendNoteOn(0, 60, 127);
00060             // midi.sendNoteOff(0, 60, 0);
00061             
00062             // if device disconnected, try to connect it again
00063             if (!midi.connected())
00064                 break;
00065              
00066             Thread::wait(50);
00067         }
00068     }
00069 }
00070  
00071 int main() {
00072     lastPot1 = pot1.read_u16() >> 9;
00073     lastPot2 = pot2.read_u16() >> 9;
00074     Thread midiTask(midi_task, NULL, osPriorityNormal, 256 * 4);
00075     while(1) {
00076         Thread::wait(500);
00077     }
00078 }