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