dhgdh
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
mbd_os/libraries/USBHost/USBHostMIDI/USBHostMIDI.cpp@1:55a6170b404f, 2016-09-17 (annotated)
- Committer:
- nexpaq
- Date:
- Sat Sep 17 16:32:05 2016 +0000
- Revision:
- 1:55a6170b404f
checking in for sharing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 1:55a6170b404f | 1 | /* Copyright (c) 2014 mbed.org, MIT License |
nexpaq | 1:55a6170b404f | 2 | * |
nexpaq | 1:55a6170b404f | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
nexpaq | 1:55a6170b404f | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
nexpaq | 1:55a6170b404f | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
nexpaq | 1:55a6170b404f | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
nexpaq | 1:55a6170b404f | 7 | * Software is furnished to do so, subject to the following conditions: |
nexpaq | 1:55a6170b404f | 8 | * |
nexpaq | 1:55a6170b404f | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
nexpaq | 1:55a6170b404f | 10 | * substantial portions of the Software. |
nexpaq | 1:55a6170b404f | 11 | * |
nexpaq | 1:55a6170b404f | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
nexpaq | 1:55a6170b404f | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
nexpaq | 1:55a6170b404f | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
nexpaq | 1:55a6170b404f | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
nexpaq | 1:55a6170b404f | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
nexpaq | 1:55a6170b404f | 17 | */ |
nexpaq | 1:55a6170b404f | 18 | |
nexpaq | 1:55a6170b404f | 19 | #include "USBHostMIDI.h" |
nexpaq | 1:55a6170b404f | 20 | |
nexpaq | 1:55a6170b404f | 21 | #if USBHOST_MIDI |
nexpaq | 1:55a6170b404f | 22 | |
nexpaq | 1:55a6170b404f | 23 | #include "dbg.h" |
nexpaq | 1:55a6170b404f | 24 | |
nexpaq | 1:55a6170b404f | 25 | #define SET_LINE_CODING 0x20 |
nexpaq | 1:55a6170b404f | 26 | |
nexpaq | 1:55a6170b404f | 27 | USBHostMIDI::USBHostMIDI() { |
nexpaq | 1:55a6170b404f | 28 | host = USBHost::getHostInst(); |
nexpaq | 1:55a6170b404f | 29 | size_bulk_in = 0; |
nexpaq | 1:55a6170b404f | 30 | size_bulk_out = 0; |
nexpaq | 1:55a6170b404f | 31 | init(); |
nexpaq | 1:55a6170b404f | 32 | } |
nexpaq | 1:55a6170b404f | 33 | |
nexpaq | 1:55a6170b404f | 34 | void USBHostMIDI::init() { |
nexpaq | 1:55a6170b404f | 35 | dev = NULL; |
nexpaq | 1:55a6170b404f | 36 | bulk_in = NULL; |
nexpaq | 1:55a6170b404f | 37 | bulk_out = NULL; |
nexpaq | 1:55a6170b404f | 38 | dev_connected = false; |
nexpaq | 1:55a6170b404f | 39 | midi_intf = -1; |
nexpaq | 1:55a6170b404f | 40 | midi_device_found = false; |
nexpaq | 1:55a6170b404f | 41 | sysExBufferPos = 0; |
nexpaq | 1:55a6170b404f | 42 | } |
nexpaq | 1:55a6170b404f | 43 | |
nexpaq | 1:55a6170b404f | 44 | bool USBHostMIDI::connected() { |
nexpaq | 1:55a6170b404f | 45 | return dev_connected; |
nexpaq | 1:55a6170b404f | 46 | } |
nexpaq | 1:55a6170b404f | 47 | |
nexpaq | 1:55a6170b404f | 48 | bool USBHostMIDI::connect() { |
nexpaq | 1:55a6170b404f | 49 | if (dev_connected) { |
nexpaq | 1:55a6170b404f | 50 | return true; |
nexpaq | 1:55a6170b404f | 51 | } |
nexpaq | 1:55a6170b404f | 52 | |
nexpaq | 1:55a6170b404f | 53 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
nexpaq | 1:55a6170b404f | 54 | if ((dev = host->getDevice(i)) != NULL) { |
nexpaq | 1:55a6170b404f | 55 | |
nexpaq | 1:55a6170b404f | 56 | USB_DBG("Trying to connect MIDI device\r\n"); |
nexpaq | 1:55a6170b404f | 57 | |
nexpaq | 1:55a6170b404f | 58 | if (host->enumerate(dev, this)) { |
nexpaq | 1:55a6170b404f | 59 | break; |
nexpaq | 1:55a6170b404f | 60 | } |
nexpaq | 1:55a6170b404f | 61 | |
nexpaq | 1:55a6170b404f | 62 | if (midi_device_found) { |
nexpaq | 1:55a6170b404f | 63 | bulk_in = dev->getEndpoint(midi_intf, BULK_ENDPOINT, IN); |
nexpaq | 1:55a6170b404f | 64 | bulk_out = dev->getEndpoint(midi_intf, BULK_ENDPOINT, OUT); |
nexpaq | 1:55a6170b404f | 65 | |
nexpaq | 1:55a6170b404f | 66 | if (!bulk_in || !bulk_out) { |
nexpaq | 1:55a6170b404f | 67 | break; |
nexpaq | 1:55a6170b404f | 68 | } |
nexpaq | 1:55a6170b404f | 69 | |
nexpaq | 1:55a6170b404f | 70 | USB_INFO("New MIDI device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, midi_intf); |
nexpaq | 1:55a6170b404f | 71 | dev->setName("MIDI", midi_intf); |
nexpaq | 1:55a6170b404f | 72 | host->registerDriver(dev, midi_intf, this, &USBHostMIDI::init); |
nexpaq | 1:55a6170b404f | 73 | |
nexpaq | 1:55a6170b404f | 74 | size_bulk_in = bulk_in->getSize(); |
nexpaq | 1:55a6170b404f | 75 | size_bulk_out = bulk_out->getSize(); |
nexpaq | 1:55a6170b404f | 76 | |
nexpaq | 1:55a6170b404f | 77 | bulk_in->attach(this, &USBHostMIDI::rxHandler); |
nexpaq | 1:55a6170b404f | 78 | |
nexpaq | 1:55a6170b404f | 79 | host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); |
nexpaq | 1:55a6170b404f | 80 | dev_connected = true; |
nexpaq | 1:55a6170b404f | 81 | return true; |
nexpaq | 1:55a6170b404f | 82 | } |
nexpaq | 1:55a6170b404f | 83 | } |
nexpaq | 1:55a6170b404f | 84 | } |
nexpaq | 1:55a6170b404f | 85 | |
nexpaq | 1:55a6170b404f | 86 | init(); |
nexpaq | 1:55a6170b404f | 87 | return false; |
nexpaq | 1:55a6170b404f | 88 | } |
nexpaq | 1:55a6170b404f | 89 | |
nexpaq | 1:55a6170b404f | 90 | void USBHostMIDI::rxHandler() { |
nexpaq | 1:55a6170b404f | 91 | uint8_t *midi; |
nexpaq | 1:55a6170b404f | 92 | if (bulk_in) { |
nexpaq | 1:55a6170b404f | 93 | int length = bulk_in->getLengthTransferred(); |
nexpaq | 1:55a6170b404f | 94 | if (bulk_in->getState() == USB_TYPE_IDLE || bulk_in->getState() == USB_TYPE_FREE) { |
nexpaq | 1:55a6170b404f | 95 | // MIDI event handling |
nexpaq | 1:55a6170b404f | 96 | for (int i = 0; i < length; i += 4) { |
nexpaq | 1:55a6170b404f | 97 | if (i + 4 > length) { |
nexpaq | 1:55a6170b404f | 98 | // length shortage, ignored. |
nexpaq | 1:55a6170b404f | 99 | break; |
nexpaq | 1:55a6170b404f | 100 | } |
nexpaq | 1:55a6170b404f | 101 | |
nexpaq | 1:55a6170b404f | 102 | // read each four bytes |
nexpaq | 1:55a6170b404f | 103 | midi = &buf[i]; |
nexpaq | 1:55a6170b404f | 104 | // process MIDI message |
nexpaq | 1:55a6170b404f | 105 | // switch by code index number |
nexpaq | 1:55a6170b404f | 106 | switch (midi[0] & 0xf) { |
nexpaq | 1:55a6170b404f | 107 | case 0: // miscellaneous function codes |
nexpaq | 1:55a6170b404f | 108 | miscellaneousFunctionCode(midi[1], midi[2], midi[3]); |
nexpaq | 1:55a6170b404f | 109 | break; |
nexpaq | 1:55a6170b404f | 110 | case 1: // cable events |
nexpaq | 1:55a6170b404f | 111 | cableEvent(midi[1], midi[2], midi[3]); |
nexpaq | 1:55a6170b404f | 112 | break; |
nexpaq | 1:55a6170b404f | 113 | case 2: // two bytes system common messages |
nexpaq | 1:55a6170b404f | 114 | systemCommonTwoBytes(midi[1], midi[2]); |
nexpaq | 1:55a6170b404f | 115 | break; |
nexpaq | 1:55a6170b404f | 116 | case 3: // three bytes system common messages |
nexpaq | 1:55a6170b404f | 117 | systemCommonThreeBytes(midi[1], midi[2], midi[3]); |
nexpaq | 1:55a6170b404f | 118 | break; |
nexpaq | 1:55a6170b404f | 119 | case 4: // SysEx starts or continues |
nexpaq | 1:55a6170b404f | 120 | sysExBuffer[sysExBufferPos++] = midi[1]; |
nexpaq | 1:55a6170b404f | 121 | if (sysExBufferPos >= 64) { |
nexpaq | 1:55a6170b404f | 122 | systemExclusive(sysExBuffer, sysExBufferPos, true); |
nexpaq | 1:55a6170b404f | 123 | sysExBufferPos = 0; |
nexpaq | 1:55a6170b404f | 124 | } |
nexpaq | 1:55a6170b404f | 125 | sysExBuffer[sysExBufferPos++] = midi[2]; |
nexpaq | 1:55a6170b404f | 126 | if (sysExBufferPos >= 64) { |
nexpaq | 1:55a6170b404f | 127 | systemExclusive(sysExBuffer, sysExBufferPos, true); |
nexpaq | 1:55a6170b404f | 128 | sysExBufferPos = 0; |
nexpaq | 1:55a6170b404f | 129 | } |
nexpaq | 1:55a6170b404f | 130 | sysExBuffer[sysExBufferPos++] = midi[3]; |
nexpaq | 1:55a6170b404f | 131 | // SysEx continues. don't send |
nexpaq | 1:55a6170b404f | 132 | break; |
nexpaq | 1:55a6170b404f | 133 | case 5: // SysEx ends with single byte |
nexpaq | 1:55a6170b404f | 134 | sysExBuffer[sysExBufferPos++] = midi[1]; |
nexpaq | 1:55a6170b404f | 135 | systemExclusive(sysExBuffer, sysExBufferPos, false); |
nexpaq | 1:55a6170b404f | 136 | sysExBufferPos = 0; |
nexpaq | 1:55a6170b404f | 137 | break; |
nexpaq | 1:55a6170b404f | 138 | case 6: // SysEx ends with two bytes |
nexpaq | 1:55a6170b404f | 139 | sysExBuffer[sysExBufferPos++] = midi[1]; |
nexpaq | 1:55a6170b404f | 140 | if (sysExBufferPos >= 64) { |
nexpaq | 1:55a6170b404f | 141 | systemExclusive(sysExBuffer, sysExBufferPos, true); |
nexpaq | 1:55a6170b404f | 142 | sysExBufferPos = 0; |
nexpaq | 1:55a6170b404f | 143 | } |
nexpaq | 1:55a6170b404f | 144 | sysExBuffer[sysExBufferPos++] = midi[2]; |
nexpaq | 1:55a6170b404f | 145 | systemExclusive(sysExBuffer, sysExBufferPos, false); |
nexpaq | 1:55a6170b404f | 146 | sysExBufferPos = 0; |
nexpaq | 1:55a6170b404f | 147 | break; |
nexpaq | 1:55a6170b404f | 148 | case 7: // SysEx ends with three bytes |
nexpaq | 1:55a6170b404f | 149 | sysExBuffer[sysExBufferPos++] = midi[1]; |
nexpaq | 1:55a6170b404f | 150 | if (sysExBufferPos >= 64) { |
nexpaq | 1:55a6170b404f | 151 | systemExclusive(sysExBuffer, sysExBufferPos, true); |
nexpaq | 1:55a6170b404f | 152 | sysExBufferPos = 0; |
nexpaq | 1:55a6170b404f | 153 | } |
nexpaq | 1:55a6170b404f | 154 | sysExBuffer[sysExBufferPos++] = midi[2]; |
nexpaq | 1:55a6170b404f | 155 | if (sysExBufferPos >= 64) { |
nexpaq | 1:55a6170b404f | 156 | systemExclusive(sysExBuffer, sysExBufferPos, true); |
nexpaq | 1:55a6170b404f | 157 | sysExBufferPos = 0; |
nexpaq | 1:55a6170b404f | 158 | } |
nexpaq | 1:55a6170b404f | 159 | sysExBuffer[sysExBufferPos++] = midi[3]; |
nexpaq | 1:55a6170b404f | 160 | systemExclusive(sysExBuffer, sysExBufferPos, false); |
nexpaq | 1:55a6170b404f | 161 | sysExBufferPos = 0; |
nexpaq | 1:55a6170b404f | 162 | break; |
nexpaq | 1:55a6170b404f | 163 | case 8: |
nexpaq | 1:55a6170b404f | 164 | noteOff(midi[1] & 0xf, midi[2], midi[3]); |
nexpaq | 1:55a6170b404f | 165 | break; |
nexpaq | 1:55a6170b404f | 166 | case 9: |
nexpaq | 1:55a6170b404f | 167 | if (midi[3]) { |
nexpaq | 1:55a6170b404f | 168 | noteOn(midi[1] & 0xf, midi[2], midi[3]); |
nexpaq | 1:55a6170b404f | 169 | } else { |
nexpaq | 1:55a6170b404f | 170 | noteOff(midi[1] & 0xf, midi[2], midi[3]); |
nexpaq | 1:55a6170b404f | 171 | } |
nexpaq | 1:55a6170b404f | 172 | break; |
nexpaq | 1:55a6170b404f | 173 | case 10: |
nexpaq | 1:55a6170b404f | 174 | polyKeyPress(midi[1] & 0xf, midi[2], midi[3]); |
nexpaq | 1:55a6170b404f | 175 | break; |
nexpaq | 1:55a6170b404f | 176 | case 11: |
nexpaq | 1:55a6170b404f | 177 | controlChange(midi[1] & 0xf, midi[2], midi[3]); |
nexpaq | 1:55a6170b404f | 178 | break; |
nexpaq | 1:55a6170b404f | 179 | case 12: |
nexpaq | 1:55a6170b404f | 180 | programChange(midi[1] & 0xf, midi[2]); |
nexpaq | 1:55a6170b404f | 181 | break; |
nexpaq | 1:55a6170b404f | 182 | case 13: |
nexpaq | 1:55a6170b404f | 183 | channelPressure(midi[1] & 0xf, midi[2]); |
nexpaq | 1:55a6170b404f | 184 | break; |
nexpaq | 1:55a6170b404f | 185 | case 14: |
nexpaq | 1:55a6170b404f | 186 | pitchBend(midi[1] & 0xf, midi[2] | (midi[3] << 7)); |
nexpaq | 1:55a6170b404f | 187 | break; |
nexpaq | 1:55a6170b404f | 188 | case 15: |
nexpaq | 1:55a6170b404f | 189 | singleByte(midi[1]); |
nexpaq | 1:55a6170b404f | 190 | break; |
nexpaq | 1:55a6170b404f | 191 | } |
nexpaq | 1:55a6170b404f | 192 | } |
nexpaq | 1:55a6170b404f | 193 | |
nexpaq | 1:55a6170b404f | 194 | // read another message |
nexpaq | 1:55a6170b404f | 195 | host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); |
nexpaq | 1:55a6170b404f | 196 | } |
nexpaq | 1:55a6170b404f | 197 | } |
nexpaq | 1:55a6170b404f | 198 | } |
nexpaq | 1:55a6170b404f | 199 | |
nexpaq | 1:55a6170b404f | 200 | bool USBHostMIDI::sendMidiBuffer(uint8_t data0, uint8_t data1, uint8_t data2, uint8_t data3) { |
nexpaq | 1:55a6170b404f | 201 | if (bulk_out) { |
nexpaq | 1:55a6170b404f | 202 | uint8_t midi[4]; |
nexpaq | 1:55a6170b404f | 203 | |
nexpaq | 1:55a6170b404f | 204 | midi[0] = data0; |
nexpaq | 1:55a6170b404f | 205 | midi[1] = data1; |
nexpaq | 1:55a6170b404f | 206 | midi[2] = data2; |
nexpaq | 1:55a6170b404f | 207 | midi[3] = data3; |
nexpaq | 1:55a6170b404f | 208 | if (host->bulkWrite(dev, bulk_out, (uint8_t *)midi, 4) == USB_TYPE_OK) { |
nexpaq | 1:55a6170b404f | 209 | return true; |
nexpaq | 1:55a6170b404f | 210 | } |
nexpaq | 1:55a6170b404f | 211 | } |
nexpaq | 1:55a6170b404f | 212 | return false; |
nexpaq | 1:55a6170b404f | 213 | } |
nexpaq | 1:55a6170b404f | 214 | |
nexpaq | 1:55a6170b404f | 215 | bool USBHostMIDI::sendMiscellaneousFunctionCode(uint8_t data1, uint8_t data2, uint8_t data3) { |
nexpaq | 1:55a6170b404f | 216 | return sendMidiBuffer(0, data1, data2, data3); |
nexpaq | 1:55a6170b404f | 217 | } |
nexpaq | 1:55a6170b404f | 218 | |
nexpaq | 1:55a6170b404f | 219 | bool USBHostMIDI::sendCableEvent(uint8_t data1, uint8_t data2, uint8_t data3) { |
nexpaq | 1:55a6170b404f | 220 | return sendMidiBuffer(1, data1, data2, data3); |
nexpaq | 1:55a6170b404f | 221 | } |
nexpaq | 1:55a6170b404f | 222 | |
nexpaq | 1:55a6170b404f | 223 | bool USBHostMIDI::sendSystemCommmonTwoBytes(uint8_t data1, uint8_t data2) { |
nexpaq | 1:55a6170b404f | 224 | return sendMidiBuffer(2, data1, data2, 0); |
nexpaq | 1:55a6170b404f | 225 | } |
nexpaq | 1:55a6170b404f | 226 | |
nexpaq | 1:55a6170b404f | 227 | bool USBHostMIDI::sendSystemCommmonThreeBytes(uint8_t data1, uint8_t data2, uint8_t data3) { |
nexpaq | 1:55a6170b404f | 228 | return sendMidiBuffer(3, data1, data2, 0); |
nexpaq | 1:55a6170b404f | 229 | } |
nexpaq | 1:55a6170b404f | 230 | |
nexpaq | 1:55a6170b404f | 231 | bool USBHostMIDI::sendSystemExclusive(uint8_t *buffer, int length) { |
nexpaq | 1:55a6170b404f | 232 | uint8_t midi[64]; |
nexpaq | 1:55a6170b404f | 233 | int midiLength; |
nexpaq | 1:55a6170b404f | 234 | int midiPos; |
nexpaq | 1:55a6170b404f | 235 | if (bulk_out) { |
nexpaq | 1:55a6170b404f | 236 | for (int i = 0; i < length; i += 48) { |
nexpaq | 1:55a6170b404f | 237 | if (i + 48 >= length) { |
nexpaq | 1:55a6170b404f | 238 | // contains last data |
nexpaq | 1:55a6170b404f | 239 | midiLength = (((length - i) + 2) / 3) * 4; |
nexpaq | 1:55a6170b404f | 240 | for (int pos = i; pos < length; pos += 3) { |
nexpaq | 1:55a6170b404f | 241 | midiPos = (pos + 2) / 3 * 4; |
nexpaq | 1:55a6170b404f | 242 | if (pos + 3 >= length) { |
nexpaq | 1:55a6170b404f | 243 | // last data |
nexpaq | 1:55a6170b404f | 244 | switch (pos % 3) { |
nexpaq | 1:55a6170b404f | 245 | case 0: |
nexpaq | 1:55a6170b404f | 246 | midi[midiPos ] = 7; |
nexpaq | 1:55a6170b404f | 247 | midi[midiPos + 1] = buffer[pos ]; |
nexpaq | 1:55a6170b404f | 248 | midi[midiPos + 2] = buffer[pos + 1]; |
nexpaq | 1:55a6170b404f | 249 | midi[midiPos + 3] = buffer[pos + 2]; |
nexpaq | 1:55a6170b404f | 250 | break; |
nexpaq | 1:55a6170b404f | 251 | case 1: |
nexpaq | 1:55a6170b404f | 252 | midi[midiPos ] = 5; |
nexpaq | 1:55a6170b404f | 253 | midi[midiPos + 1] = buffer[pos ]; |
nexpaq | 1:55a6170b404f | 254 | midi[midiPos + 2] = 0; |
nexpaq | 1:55a6170b404f | 255 | midi[midiPos + 3] = 0; |
nexpaq | 1:55a6170b404f | 256 | break; |
nexpaq | 1:55a6170b404f | 257 | case 2: |
nexpaq | 1:55a6170b404f | 258 | midi[midiPos ] = 6; |
nexpaq | 1:55a6170b404f | 259 | midi[midiPos + 1] = buffer[pos ]; |
nexpaq | 1:55a6170b404f | 260 | midi[midiPos + 2] = buffer[pos + 1]; |
nexpaq | 1:55a6170b404f | 261 | midi[midiPos + 3] = 0; |
nexpaq | 1:55a6170b404f | 262 | break; |
nexpaq | 1:55a6170b404f | 263 | } |
nexpaq | 1:55a6170b404f | 264 | } else { |
nexpaq | 1:55a6170b404f | 265 | // has more data |
nexpaq | 1:55a6170b404f | 266 | midi[midiPos ] = 4; |
nexpaq | 1:55a6170b404f | 267 | midi[midiPos + 1] = buffer[pos ]; |
nexpaq | 1:55a6170b404f | 268 | midi[midiPos + 2] = buffer[pos + 1]; |
nexpaq | 1:55a6170b404f | 269 | midi[midiPos + 3] = buffer[pos + 2]; |
nexpaq | 1:55a6170b404f | 270 | } |
nexpaq | 1:55a6170b404f | 271 | } |
nexpaq | 1:55a6170b404f | 272 | } else { |
nexpaq | 1:55a6170b404f | 273 | // has more data |
nexpaq | 1:55a6170b404f | 274 | midiLength = 64; |
nexpaq | 1:55a6170b404f | 275 | for (int pos = i; pos < length; pos += 3) { |
nexpaq | 1:55a6170b404f | 276 | midiPos = (pos + 2) / 3 * 4; |
nexpaq | 1:55a6170b404f | 277 | midi[midiPos ] = 4; |
nexpaq | 1:55a6170b404f | 278 | midi[midiPos + 1] = buffer[pos ]; |
nexpaq | 1:55a6170b404f | 279 | midi[midiPos + 2] = buffer[pos + 1]; |
nexpaq | 1:55a6170b404f | 280 | midi[midiPos + 3] = buffer[pos + 2]; |
nexpaq | 1:55a6170b404f | 281 | } |
nexpaq | 1:55a6170b404f | 282 | } |
nexpaq | 1:55a6170b404f | 283 | |
nexpaq | 1:55a6170b404f | 284 | if (host->bulkWrite(dev, bulk_out, (uint8_t *)midi, midiLength) != USB_TYPE_OK) { |
nexpaq | 1:55a6170b404f | 285 | return false; |
nexpaq | 1:55a6170b404f | 286 | } |
nexpaq | 1:55a6170b404f | 287 | } |
nexpaq | 1:55a6170b404f | 288 | return true; |
nexpaq | 1:55a6170b404f | 289 | } |
nexpaq | 1:55a6170b404f | 290 | return false; |
nexpaq | 1:55a6170b404f | 291 | } |
nexpaq | 1:55a6170b404f | 292 | |
nexpaq | 1:55a6170b404f | 293 | bool USBHostMIDI::sendNoteOff(uint8_t channel, uint8_t note, uint8_t velocity) { |
nexpaq | 1:55a6170b404f | 294 | return sendMidiBuffer(8, channel & 0xf | 0x80, note & 0x7f, velocity & 0x7f); |
nexpaq | 1:55a6170b404f | 295 | } |
nexpaq | 1:55a6170b404f | 296 | |
nexpaq | 1:55a6170b404f | 297 | bool USBHostMIDI::sendNoteOn(uint8_t channel, uint8_t note, uint8_t velocity) { |
nexpaq | 1:55a6170b404f | 298 | return sendMidiBuffer(9, channel & 0xf | 0x90, note & 0x7f, velocity & 0x7f); |
nexpaq | 1:55a6170b404f | 299 | } |
nexpaq | 1:55a6170b404f | 300 | |
nexpaq | 1:55a6170b404f | 301 | bool USBHostMIDI::sendPolyKeyPress(uint8_t channel, uint8_t note, uint8_t pressure) { |
nexpaq | 1:55a6170b404f | 302 | return sendMidiBuffer(10, channel & 0xf | 0xa0, note & 0x7f, pressure & 0x7f); |
nexpaq | 1:55a6170b404f | 303 | } |
nexpaq | 1:55a6170b404f | 304 | |
nexpaq | 1:55a6170b404f | 305 | bool USBHostMIDI::sendControlChange(uint8_t channel, uint8_t key, uint8_t value) { |
nexpaq | 1:55a6170b404f | 306 | return sendMidiBuffer(11, channel & 0xf | 0xb0, key & 0x7f, value & 0x7f); |
nexpaq | 1:55a6170b404f | 307 | } |
nexpaq | 1:55a6170b404f | 308 | |
nexpaq | 1:55a6170b404f | 309 | bool USBHostMIDI::sendProgramChange(uint8_t channel, uint8_t program) { |
nexpaq | 1:55a6170b404f | 310 | return sendMidiBuffer(12, channel & 0xf | 0xc0, program & 0x7f, 0); |
nexpaq | 1:55a6170b404f | 311 | } |
nexpaq | 1:55a6170b404f | 312 | |
nexpaq | 1:55a6170b404f | 313 | bool USBHostMIDI::sendChannelPressure(uint8_t channel, uint8_t pressure) { |
nexpaq | 1:55a6170b404f | 314 | return sendMidiBuffer(13, channel & 0xf | 0xd0, pressure & 0x7f, 0); |
nexpaq | 1:55a6170b404f | 315 | } |
nexpaq | 1:55a6170b404f | 316 | |
nexpaq | 1:55a6170b404f | 317 | bool USBHostMIDI::sendPitchBend(uint8_t channel, uint16_t value) { |
nexpaq | 1:55a6170b404f | 318 | return sendMidiBuffer(14, channel & 0xf | 0xe0, value & 0x7f, (value >> 7) & 0x7f); |
nexpaq | 1:55a6170b404f | 319 | } |
nexpaq | 1:55a6170b404f | 320 | |
nexpaq | 1:55a6170b404f | 321 | bool USBHostMIDI::sendSingleByte(uint8_t data) { |
nexpaq | 1:55a6170b404f | 322 | return sendMidiBuffer(15, data, 0, 0); |
nexpaq | 1:55a6170b404f | 323 | } |
nexpaq | 1:55a6170b404f | 324 | |
nexpaq | 1:55a6170b404f | 325 | /*virtual*/ void USBHostMIDI::setVidPid(uint16_t vid, uint16_t pid) |
nexpaq | 1:55a6170b404f | 326 | { |
nexpaq | 1:55a6170b404f | 327 | // we don't check VID/PID for this driver |
nexpaq | 1:55a6170b404f | 328 | } |
nexpaq | 1:55a6170b404f | 329 | |
nexpaq | 1:55a6170b404f | 330 | /*virtual*/ bool USBHostMIDI::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed |
nexpaq | 1:55a6170b404f | 331 | { |
nexpaq | 1:55a6170b404f | 332 | // USB MIDI class/subclass |
nexpaq | 1:55a6170b404f | 333 | if ((midi_intf == -1) && |
nexpaq | 1:55a6170b404f | 334 | (intf_class == AUDIO_CLASS) && |
nexpaq | 1:55a6170b404f | 335 | (intf_subclass == 0x03)) { |
nexpaq | 1:55a6170b404f | 336 | midi_intf = intf_nb; |
nexpaq | 1:55a6170b404f | 337 | return true; |
nexpaq | 1:55a6170b404f | 338 | } |
nexpaq | 1:55a6170b404f | 339 | |
nexpaq | 1:55a6170b404f | 340 | // vendor specific device |
nexpaq | 1:55a6170b404f | 341 | if ((midi_intf == -1) && |
nexpaq | 1:55a6170b404f | 342 | (intf_class == 0xff) && |
nexpaq | 1:55a6170b404f | 343 | (intf_subclass == 0x03)) { |
nexpaq | 1:55a6170b404f | 344 | midi_intf = intf_nb; |
nexpaq | 1:55a6170b404f | 345 | return true; |
nexpaq | 1:55a6170b404f | 346 | } |
nexpaq | 1:55a6170b404f | 347 | |
nexpaq | 1:55a6170b404f | 348 | return false; |
nexpaq | 1:55a6170b404f | 349 | } |
nexpaq | 1:55a6170b404f | 350 | |
nexpaq | 1:55a6170b404f | 351 | /*virtual*/ bool USBHostMIDI::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used |
nexpaq | 1:55a6170b404f | 352 | { |
nexpaq | 1:55a6170b404f | 353 | if (intf_nb == midi_intf) { |
nexpaq | 1:55a6170b404f | 354 | if (type == BULK_ENDPOINT) { |
nexpaq | 1:55a6170b404f | 355 | midi_device_found = true; |
nexpaq | 1:55a6170b404f | 356 | return true; |
nexpaq | 1:55a6170b404f | 357 | } |
nexpaq | 1:55a6170b404f | 358 | } |
nexpaq | 1:55a6170b404f | 359 | return false; |
nexpaq | 1:55a6170b404f | 360 | } |
nexpaq | 1:55a6170b404f | 361 | |
nexpaq | 1:55a6170b404f | 362 | #endif |