see https://os.mbed.com/users/okini3939/notebook/USBHostMIDI/

Dependencies:   USBHost mbed

Committer:
okini3939
Date:
Wed Jan 03 08:07:31 2018 +0000
Revision:
0:0fd787d62a99
1st build

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:0fd787d62a99 1 #include "mbed.h"
okini3939 0:0fd787d62a99 2 #include "USBHostMIDI.h"
okini3939 0:0fd787d62a99 3
okini3939 0:0fd787d62a99 4 Serial pc(USBTX, USBRX);
okini3939 0:0fd787d62a99 5 DigitalOut led1(LED1);
okini3939 0:0fd787d62a99 6 PwmOut led2(LED2), led3(LED3), led4(LED4);
okini3939 0:0fd787d62a99 7 USBHostMIDI midi;
okini3939 0:0fd787d62a99 8
okini3939 0:0fd787d62a99 9 volatile int sendNoteOn = -1, sendNoteOff = -1, sendControlChange = -1;
okini3939 0:0fd787d62a99 10
okini3939 0:0fd787d62a99 11 void noteOn(uint8_t channel, uint8_t note, uint8_t velocity) {
okini3939 0:0fd787d62a99 12 pc.printf("noteOn %02x %02x %02x\r\n", channel, note, velocity);
okini3939 0:0fd787d62a99 13 switch (note) {
okini3939 0:0fd787d62a99 14 case 0:
okini3939 0:0fd787d62a99 15 led2 = (float)velocity / 127.0;
okini3939 0:0fd787d62a99 16 break;
okini3939 0:0fd787d62a99 17 case 1:
okini3939 0:0fd787d62a99 18 led3 = (float)velocity / 127.0;
okini3939 0:0fd787d62a99 19 break;
okini3939 0:0fd787d62a99 20 case 2:
okini3939 0:0fd787d62a99 21 led4 = (float)velocity / 127.0;
okini3939 0:0fd787d62a99 22 break;
okini3939 0:0fd787d62a99 23 }
okini3939 0:0fd787d62a99 24 sendNoteOn = (channel << 16) | (note << 8) | velocity;
okini3939 0:0fd787d62a99 25 }
okini3939 0:0fd787d62a99 26
okini3939 0:0fd787d62a99 27 void noteOff(uint8_t channel, uint8_t note, uint8_t velocity) {
okini3939 0:0fd787d62a99 28 pc.printf("noteOff %02x %02x %02x\r\n", channel, note, velocity);
okini3939 0:0fd787d62a99 29 sendNoteOff = (channel << 16) | (note << 8) | velocity;
okini3939 0:0fd787d62a99 30 }
okini3939 0:0fd787d62a99 31
okini3939 0:0fd787d62a99 32 void controlChange(uint8_t channel, uint8_t key, uint8_t value) {
okini3939 0:0fd787d62a99 33 pc.printf("controlChange %02x %02x %02x\r\n", channel, key, value);
okini3939 0:0fd787d62a99 34 switch (key) {
okini3939 0:0fd787d62a99 35 case 0:
okini3939 0:0fd787d62a99 36 case 0x4d:
okini3939 0:0fd787d62a99 37 led2 = (float)value / 127.0;
okini3939 0:0fd787d62a99 38 break;
okini3939 0:0fd787d62a99 39 case 1:
okini3939 0:0fd787d62a99 40 case 0x4e:
okini3939 0:0fd787d62a99 41 led3 = (float)value / 127.0;
okini3939 0:0fd787d62a99 42 break;
okini3939 0:0fd787d62a99 43 case 2:
okini3939 0:0fd787d62a99 44 case 0x4f:
okini3939 0:0fd787d62a99 45 led4 = (float)value / 127.0;
okini3939 0:0fd787d62a99 46 break;
okini3939 0:0fd787d62a99 47 }
okini3939 0:0fd787d62a99 48 sendControlChange = (channel << 16) | (key << 8) | value;
okini3939 0:0fd787d62a99 49 }
okini3939 0:0fd787d62a99 50
okini3939 0:0fd787d62a99 51 void midi_task(void const*) {
okini3939 0:0fd787d62a99 52 int i;
okini3939 0:0fd787d62a99 53 USBHostMIDI midi;
okini3939 0:0fd787d62a99 54
okini3939 0:0fd787d62a99 55 // attach midi event callbacks
okini3939 0:0fd787d62a99 56 midi.attachNoteOn(noteOn);
okini3939 0:0fd787d62a99 57 midi.attachNoteOff(noteOff);
okini3939 0:0fd787d62a99 58 midi.attachControlChange(controlChange);
okini3939 0:0fd787d62a99 59 pc.printf("begin\r\n");
okini3939 0:0fd787d62a99 60
okini3939 0:0fd787d62a99 61 for (;;) {
okini3939 0:0fd787d62a99 62 // try to connect a midi device
okini3939 0:0fd787d62a99 63 while(!midi.connect()) {
okini3939 0:0fd787d62a99 64 Thread::wait(500);
okini3939 0:0fd787d62a99 65 led1 = !led1;
okini3939 0:0fd787d62a99 66 }
okini3939 0:0fd787d62a99 67 Thread::wait(1000);
okini3939 0:0fd787d62a99 68 midi.sendControlChange(0, 41, 127); // LED on (nanoKONTROL2)
okini3939 0:0fd787d62a99 69 midi.sendNoteOn(0, 0x29, 127); // LED on (Launch Control XL)
okini3939 0:0fd787d62a99 70 led1 = 1;
okini3939 0:0fd787d62a99 71
okini3939 0:0fd787d62a99 72 for (;;) {
okini3939 0:0fd787d62a99 73 if (!midi.connected()) {
okini3939 0:0fd787d62a99 74 pc.printf("disconnected\r\n");
okini3939 0:0fd787d62a99 75 break;
okini3939 0:0fd787d62a99 76 }
okini3939 0:0fd787d62a99 77
okini3939 0:0fd787d62a99 78 if (sendNoteOn != -1) {
okini3939 0:0fd787d62a99 79 midi.sendNoteOn(sendNoteOn >> 16, sendNoteOn >> 8, sendNoteOn);
okini3939 0:0fd787d62a99 80 sendNoteOn = -1;
okini3939 0:0fd787d62a99 81 }
okini3939 0:0fd787d62a99 82 if (sendNoteOff != -1) {
okini3939 0:0fd787d62a99 83 midi.sendNoteOff(sendNoteOff >> 16, sendNoteOff >> 8, sendNoteOff);
okini3939 0:0fd787d62a99 84 sendNoteOff = -1;
okini3939 0:0fd787d62a99 85 }
okini3939 0:0fd787d62a99 86 if (sendControlChange != -1) {
okini3939 0:0fd787d62a99 87 midi.sendControlChange(sendControlChange >> 16, sendControlChange >> 8, sendControlChange);
okini3939 0:0fd787d62a99 88 sendControlChange = -1;
okini3939 0:0fd787d62a99 89 }
okini3939 0:0fd787d62a99 90 }
okini3939 0:0fd787d62a99 91 }
okini3939 0:0fd787d62a99 92 }
okini3939 0:0fd787d62a99 93
okini3939 0:0fd787d62a99 94 int main() {
okini3939 0:0fd787d62a99 95 pc.baud(115200);
okini3939 0:0fd787d62a99 96 pc.printf("*** USB Host MIDI\r\n");
okini3939 0:0fd787d62a99 97
okini3939 0:0fd787d62a99 98 Thread midiTask(midi_task);
okini3939 0:0fd787d62a99 99 for (;;) {
okini3939 0:0fd787d62a99 100 Thread::wait(100);
okini3939 0:0fd787d62a99 101 }
okini3939 0:0fd787d62a99 102 }