Suga koubou
/
usbhost_midi
see https://os.mbed.com/users/okini3939/notebook/USBHostMIDI/
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "USBHostMIDI.h" 00003 00004 Serial pc(USBTX, USBRX); 00005 DigitalOut led1(LED1); 00006 PwmOut led2(LED2), led3(LED3), led4(LED4); 00007 USBHostMIDI midi; 00008 00009 volatile int sendNoteOn = -1, sendNoteOff = -1, sendControlChange = -1; 00010 00011 void noteOn(uint8_t channel, uint8_t note, uint8_t velocity) { 00012 pc.printf("noteOn %02x %02x %02x\r\n", channel, note, velocity); 00013 switch (note) { 00014 case 0: 00015 led2 = (float)velocity / 127.0; 00016 break; 00017 case 1: 00018 led3 = (float)velocity / 127.0; 00019 break; 00020 case 2: 00021 led4 = (float)velocity / 127.0; 00022 break; 00023 } 00024 sendNoteOn = (channel << 16) | (note << 8) | velocity; 00025 } 00026 00027 void noteOff(uint8_t channel, uint8_t note, uint8_t velocity) { 00028 pc.printf("noteOff %02x %02x %02x\r\n", channel, note, velocity); 00029 sendNoteOff = (channel << 16) | (note << 8) | velocity; 00030 } 00031 00032 void controlChange(uint8_t channel, uint8_t key, uint8_t value) { 00033 pc.printf("controlChange %02x %02x %02x\r\n", channel, key, value); 00034 switch (key) { 00035 case 0: 00036 case 0x4d: 00037 led2 = (float)value / 127.0; 00038 break; 00039 case 1: 00040 case 0x4e: 00041 led3 = (float)value / 127.0; 00042 break; 00043 case 2: 00044 case 0x4f: 00045 led4 = (float)value / 127.0; 00046 break; 00047 } 00048 sendControlChange = (channel << 16) | (key << 8) | value; 00049 } 00050 00051 void midi_task(void const*) { 00052 int i; 00053 USBHostMIDI midi; 00054 00055 // attach midi event callbacks 00056 midi.attachNoteOn(noteOn); 00057 midi.attachNoteOff(noteOff); 00058 midi.attachControlChange(controlChange); 00059 pc.printf("begin\r\n"); 00060 00061 for (;;) { 00062 // try to connect a midi device 00063 while(!midi.connect()) { 00064 Thread::wait(500); 00065 led1 = !led1; 00066 } 00067 Thread::wait(1000); 00068 midi.sendControlChange(0, 41, 127); // LED on (nanoKONTROL2) 00069 midi.sendNoteOn(0, 0x29, 127); // LED on (Launch Control XL) 00070 led1 = 1; 00071 00072 for (;;) { 00073 if (!midi.connected()) { 00074 pc.printf("disconnected\r\n"); 00075 break; 00076 } 00077 00078 if (sendNoteOn != -1) { 00079 midi.sendNoteOn(sendNoteOn >> 16, sendNoteOn >> 8, sendNoteOn); 00080 sendNoteOn = -1; 00081 } 00082 if (sendNoteOff != -1) { 00083 midi.sendNoteOff(sendNoteOff >> 16, sendNoteOff >> 8, sendNoteOff); 00084 sendNoteOff = -1; 00085 } 00086 if (sendControlChange != -1) { 00087 midi.sendControlChange(sendControlChange >> 16, sendControlChange >> 8, sendControlChange); 00088 sendControlChange = -1; 00089 } 00090 } 00091 } 00092 } 00093 00094 int main() { 00095 pc.baud(115200); 00096 pc.printf("*** USB Host MIDI\r\n"); 00097 00098 Thread midiTask(midi_task); 00099 for (;;) { 00100 Thread::wait(100); 00101 } 00102 }
Generated on Wed Jul 13 2022 02:24:45 by 1.7.2