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