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@24:5281ea9f6e68, 2014-09-18 (annotated)
- Committer:
- hsgw
- Date:
- Thu Sep 18 10:50:22 2014 +0000
- Revision:
- 24:5281ea9f6e68
- Parent:
- 23:9c1d09c6a2b6
- Child:
- 26:077ab26227c6
add other midi status. ; but miscellaneous function and cable event has been disabled.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hsgw | 24:5281ea9f6e68 | 1 | /* USBHostMidi library |
hsgw | 24:5281ea9f6e68 | 2 | * Originaled by k.shoji |
hsgw | 24:5281ea9f6e68 | 3 | * porting by Takuya Urakawa |
hsgw | 24:5281ea9f6e68 | 4 | */ |
hsgw | 24:5281ea9f6e68 | 5 | |
hsgw | 24:5281ea9f6e68 | 6 | /* mbed USBHost Library |
hsgw | 24:5281ea9f6e68 | 7 | * Copyright (c) 2006-2013 ARM Limited |
hsgw | 24:5281ea9f6e68 | 8 | * |
hsgw | 24:5281ea9f6e68 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); |
hsgw | 24:5281ea9f6e68 | 10 | * you may not use this file except in compliance with the License. |
hsgw | 24:5281ea9f6e68 | 11 | * You may obtain a copy of the License at |
hsgw | 24:5281ea9f6e68 | 12 | * |
hsgw | 24:5281ea9f6e68 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
hsgw | 24:5281ea9f6e68 | 14 | * |
hsgw | 24:5281ea9f6e68 | 15 | * Unless required by applicable law or agreed to in writing, software |
hsgw | 24:5281ea9f6e68 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, |
hsgw | 24:5281ea9f6e68 | 17 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
hsgw | 24:5281ea9f6e68 | 18 | * See the License for the specific language governing permissions and |
hsgw | 24:5281ea9f6e68 | 19 | * limitations under the License. |
hsgw | 24:5281ea9f6e68 | 20 | */ |
hsgw | 24:5281ea9f6e68 | 21 | |
hsgw | 24:5281ea9f6e68 | 22 | #include "USBHostMIDI.h" |
hsgw | 24:5281ea9f6e68 | 23 | |
hsgw | 24:5281ea9f6e68 | 24 | #if USBHOST_MIDI |
hsgw | 24:5281ea9f6e68 | 25 | |
hsgw | 24:5281ea9f6e68 | 26 | #include "dbg.h" |
hsgw | 24:5281ea9f6e68 | 27 | |
hsgw | 24:5281ea9f6e68 | 28 | #define SET_LINE_CODING 0x20 |
hsgw | 24:5281ea9f6e68 | 29 | |
hsgw | 24:5281ea9f6e68 | 30 | USBHostMIDI::USBHostMIDI() { |
hsgw | 24:5281ea9f6e68 | 31 | host = USBHost::getHostInst(); |
hsgw | 24:5281ea9f6e68 | 32 | size_bulk_in = 0; |
hsgw | 24:5281ea9f6e68 | 33 | size_bulk_out = 0; |
hsgw | 24:5281ea9f6e68 | 34 | init(); |
hsgw | 24:5281ea9f6e68 | 35 | } |
hsgw | 24:5281ea9f6e68 | 36 | |
hsgw | 24:5281ea9f6e68 | 37 | void USBHostMIDI::init() { |
hsgw | 24:5281ea9f6e68 | 38 | dev = NULL; |
hsgw | 24:5281ea9f6e68 | 39 | bulk_in = NULL; |
hsgw | 24:5281ea9f6e68 | 40 | bulk_out = NULL; |
hsgw | 24:5281ea9f6e68 | 41 | dev_connected = false; |
hsgw | 24:5281ea9f6e68 | 42 | midi_intf = -1; |
hsgw | 24:5281ea9f6e68 | 43 | midi_device_found = false; |
hsgw | 24:5281ea9f6e68 | 44 | sysExBufferPos = 0; |
hsgw | 24:5281ea9f6e68 | 45 | } |
hsgw | 24:5281ea9f6e68 | 46 | |
hsgw | 24:5281ea9f6e68 | 47 | bool USBHostMIDI::connected() |
hsgw | 24:5281ea9f6e68 | 48 | { |
hsgw | 24:5281ea9f6e68 | 49 | return dev_connected; |
hsgw | 24:5281ea9f6e68 | 50 | } |
hsgw | 24:5281ea9f6e68 | 51 | |
hsgw | 24:5281ea9f6e68 | 52 | bool USBHostMIDI::connect() { |
hsgw | 24:5281ea9f6e68 | 53 | |
hsgw | 24:5281ea9f6e68 | 54 | if (dev_connected) { |
hsgw | 24:5281ea9f6e68 | 55 | return true; |
hsgw | 24:5281ea9f6e68 | 56 | } |
hsgw | 24:5281ea9f6e68 | 57 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
hsgw | 24:5281ea9f6e68 | 58 | if ((dev = host->getDevice(i)) != NULL) { |
hsgw | 24:5281ea9f6e68 | 59 | |
hsgw | 24:5281ea9f6e68 | 60 | USB_DBG("Trying to connect MIDI device\r\n"); |
hsgw | 24:5281ea9f6e68 | 61 | |
hsgw | 24:5281ea9f6e68 | 62 | if(host->enumerate(dev, this)) |
hsgw | 24:5281ea9f6e68 | 63 | break; |
hsgw | 24:5281ea9f6e68 | 64 | |
hsgw | 24:5281ea9f6e68 | 65 | if (midi_device_found) { |
hsgw | 24:5281ea9f6e68 | 66 | bulk_in = dev->getEndpoint(midi_intf, BULK_ENDPOINT, IN); |
hsgw | 24:5281ea9f6e68 | 67 | bulk_out = dev->getEndpoint(midi_intf, BULK_ENDPOINT, OUT); |
hsgw | 24:5281ea9f6e68 | 68 | |
hsgw | 24:5281ea9f6e68 | 69 | if (!bulk_in || !bulk_out) |
hsgw | 24:5281ea9f6e68 | 70 | break; |
hsgw | 24:5281ea9f6e68 | 71 | |
hsgw | 24:5281ea9f6e68 | 72 | USB_INFO("New MIDI device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, midi_intf); |
hsgw | 24:5281ea9f6e68 | 73 | dev->setName("MIDI", midi_intf); |
hsgw | 24:5281ea9f6e68 | 74 | host->registerDriver(dev, midi_intf, this, &USBHostMIDI::init); |
hsgw | 24:5281ea9f6e68 | 75 | |
hsgw | 24:5281ea9f6e68 | 76 | size_bulk_in = bulk_in->getSize(); |
hsgw | 24:5281ea9f6e68 | 77 | size_bulk_out = bulk_out->getSize(); |
hsgw | 24:5281ea9f6e68 | 78 | |
hsgw | 24:5281ea9f6e68 | 79 | bulk_in->attach(this, &USBHostMIDI::rxHandler); |
hsgw | 24:5281ea9f6e68 | 80 | |
hsgw | 24:5281ea9f6e68 | 81 | host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); |
hsgw | 24:5281ea9f6e68 | 82 | dev_connected = true; |
hsgw | 24:5281ea9f6e68 | 83 | return true; |
hsgw | 24:5281ea9f6e68 | 84 | } |
hsgw | 24:5281ea9f6e68 | 85 | } |
hsgw | 24:5281ea9f6e68 | 86 | } |
hsgw | 24:5281ea9f6e68 | 87 | init(); |
hsgw | 24:5281ea9f6e68 | 88 | return false; |
hsgw | 24:5281ea9f6e68 | 89 | } |
hsgw | 24:5281ea9f6e68 | 90 | |
hsgw | 24:5281ea9f6e68 | 91 | void USBHostMIDI::rxHandler() { |
hsgw | 24:5281ea9f6e68 | 92 | uint8_t *midi; |
hsgw | 24:5281ea9f6e68 | 93 | if (bulk_in) { |
hsgw | 24:5281ea9f6e68 | 94 | int length = bulk_in->getLengthTransferred(); |
hsgw | 24:5281ea9f6e68 | 95 | if (bulk_in->getState() == USB_TYPE_IDLE || bulk_in->getState() == USB_TYPE_FREE) { |
hsgw | 24:5281ea9f6e68 | 96 | // MIDI event handling |
hsgw | 24:5281ea9f6e68 | 97 | for (int i = 0; i < length; i += 4) { |
hsgw | 24:5281ea9f6e68 | 98 | if (i + 4 > length) { |
hsgw | 24:5281ea9f6e68 | 99 | // length shortage, ignored. |
hsgw | 24:5281ea9f6e68 | 100 | break; |
hsgw | 24:5281ea9f6e68 | 101 | } |
hsgw | 24:5281ea9f6e68 | 102 | |
hsgw | 24:5281ea9f6e68 | 103 | // read each four bytes |
hsgw | 24:5281ea9f6e68 | 104 | midi = &buf[i]; |
hsgw | 24:5281ea9f6e68 | 105 | // process MIDI message |
hsgw | 24:5281ea9f6e68 | 106 | // switch by code index number |
hsgw | 24:5281ea9f6e68 | 107 | USB_DBG("midi: %d, %d, %d, %d", midi[0]&0xf, midi[1], midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 108 | switch (midi[0] & 0xf) { |
hsgw | 24:5281ea9f6e68 | 109 | case 0: // miscellaneous function codes |
hsgw | 24:5281ea9f6e68 | 110 | //if(midi[1] == 0) break; |
hsgw | 24:5281ea9f6e68 | 111 | //miscellaneousFunctionCode(midi[1], midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 112 | break; |
hsgw | 24:5281ea9f6e68 | 113 | case 1: // cable events |
hsgw | 24:5281ea9f6e68 | 114 | //cableEvent(midi[1], midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 115 | break; |
hsgw | 24:5281ea9f6e68 | 116 | case 2: // two bytes system common messages |
hsgw | 24:5281ea9f6e68 | 117 | systemCommonTwoBytes(midi[1], midi[2]); |
hsgw | 24:5281ea9f6e68 | 118 | break; |
hsgw | 24:5281ea9f6e68 | 119 | case 3: // three bytes system common messages |
hsgw | 24:5281ea9f6e68 | 120 | systemCommonThreeBytes(midi[1], midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 121 | break; |
hsgw | 24:5281ea9f6e68 | 122 | case 4: // SysEx starts or continues |
hsgw | 24:5281ea9f6e68 | 123 | sysExBuffer[sysExBufferPos++] = midi[1]; |
hsgw | 24:5281ea9f6e68 | 124 | if (sysExBufferPos >= 64) { |
hsgw | 24:5281ea9f6e68 | 125 | systemExclusive(sysExBuffer, sysExBufferPos, true); |
hsgw | 24:5281ea9f6e68 | 126 | sysExBufferPos = 0; |
hsgw | 24:5281ea9f6e68 | 127 | } |
hsgw | 24:5281ea9f6e68 | 128 | sysExBuffer[sysExBufferPos++] = midi[2]; |
hsgw | 24:5281ea9f6e68 | 129 | if (sysExBufferPos >= 64) { |
hsgw | 24:5281ea9f6e68 | 130 | systemExclusive(sysExBuffer, sysExBufferPos, true); |
hsgw | 24:5281ea9f6e68 | 131 | sysExBufferPos = 0; |
hsgw | 24:5281ea9f6e68 | 132 | } |
hsgw | 24:5281ea9f6e68 | 133 | sysExBuffer[sysExBufferPos++] = midi[3]; |
hsgw | 24:5281ea9f6e68 | 134 | // SysEx continues. don't send |
hsgw | 24:5281ea9f6e68 | 135 | break; |
hsgw | 24:5281ea9f6e68 | 136 | case 5: // SysEx ends with single byte |
hsgw | 24:5281ea9f6e68 | 137 | sysExBuffer[sysExBufferPos++] = midi[1]; |
hsgw | 24:5281ea9f6e68 | 138 | systemExclusive(sysExBuffer, sysExBufferPos, false); |
hsgw | 24:5281ea9f6e68 | 139 | sysExBufferPos = 0; |
hsgw | 24:5281ea9f6e68 | 140 | break; |
hsgw | 24:5281ea9f6e68 | 141 | case 6: // SysEx ends with two bytes |
hsgw | 24:5281ea9f6e68 | 142 | sysExBuffer[sysExBufferPos++] = midi[1]; |
hsgw | 24:5281ea9f6e68 | 143 | if (sysExBufferPos >= 64) { |
hsgw | 24:5281ea9f6e68 | 144 | systemExclusive(sysExBuffer, sysExBufferPos, true); |
hsgw | 24:5281ea9f6e68 | 145 | sysExBufferPos = 0; |
hsgw | 24:5281ea9f6e68 | 146 | } |
hsgw | 24:5281ea9f6e68 | 147 | sysExBuffer[sysExBufferPos++] = midi[2]; |
hsgw | 24:5281ea9f6e68 | 148 | systemExclusive(sysExBuffer, sysExBufferPos, false); |
hsgw | 24:5281ea9f6e68 | 149 | sysExBufferPos = 0; |
hsgw | 24:5281ea9f6e68 | 150 | break; |
hsgw | 24:5281ea9f6e68 | 151 | case 7: // SysEx ends with three bytes |
hsgw | 24:5281ea9f6e68 | 152 | sysExBuffer[sysExBufferPos++] = midi[1]; |
hsgw | 24:5281ea9f6e68 | 153 | if (sysExBufferPos >= 64) { |
hsgw | 24:5281ea9f6e68 | 154 | systemExclusive(sysExBuffer, sysExBufferPos, true); |
hsgw | 24:5281ea9f6e68 | 155 | sysExBufferPos = 0; |
hsgw | 24:5281ea9f6e68 | 156 | } |
hsgw | 24:5281ea9f6e68 | 157 | sysExBuffer[sysExBufferPos++] = midi[2]; |
hsgw | 24:5281ea9f6e68 | 158 | if (sysExBufferPos >= 64) { |
hsgw | 24:5281ea9f6e68 | 159 | systemExclusive(sysExBuffer, sysExBufferPos, true); |
hsgw | 24:5281ea9f6e68 | 160 | sysExBufferPos = 0; |
hsgw | 24:5281ea9f6e68 | 161 | } |
hsgw | 24:5281ea9f6e68 | 162 | sysExBuffer[sysExBufferPos++] = midi[3]; |
hsgw | 24:5281ea9f6e68 | 163 | systemExclusive(sysExBuffer, sysExBufferPos, false); |
hsgw | 24:5281ea9f6e68 | 164 | sysExBufferPos = 0; |
hsgw | 24:5281ea9f6e68 | 165 | break; |
hsgw | 24:5281ea9f6e68 | 166 | case 8: |
hsgw | 24:5281ea9f6e68 | 167 | noteOff(midi[1] & 0xf, midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 168 | break; |
hsgw | 24:5281ea9f6e68 | 169 | case 9: |
hsgw | 24:5281ea9f6e68 | 170 | if (midi[3]) { |
hsgw | 24:5281ea9f6e68 | 171 | noteOn(midi[1] & 0xf, midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 172 | } else { |
hsgw | 24:5281ea9f6e68 | 173 | noteOff(midi[1] & 0xf, midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 174 | } |
hsgw | 24:5281ea9f6e68 | 175 | break; |
hsgw | 24:5281ea9f6e68 | 176 | case 10: |
hsgw | 24:5281ea9f6e68 | 177 | polyKeyPress(midi[1] & 0xf, midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 178 | break; |
hsgw | 24:5281ea9f6e68 | 179 | case 11: |
hsgw | 24:5281ea9f6e68 | 180 | controlChange(midi[1] & 0xf, midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 181 | break; |
hsgw | 24:5281ea9f6e68 | 182 | case 12: |
hsgw | 24:5281ea9f6e68 | 183 | programChange(midi[1] & 0xf, midi[2]); |
hsgw | 24:5281ea9f6e68 | 184 | break; |
hsgw | 24:5281ea9f6e68 | 185 | case 13: |
hsgw | 24:5281ea9f6e68 | 186 | channelPressure(midi[1] & 0xf, midi[2]); |
hsgw | 24:5281ea9f6e68 | 187 | break; |
hsgw | 24:5281ea9f6e68 | 188 | case 14: |
hsgw | 24:5281ea9f6e68 | 189 | pitchBend(midi[1] & 0xf, midi[2] | (midi[3] << 7)); |
hsgw | 24:5281ea9f6e68 | 190 | break; |
hsgw | 24:5281ea9f6e68 | 191 | case 15: |
hsgw | 24:5281ea9f6e68 | 192 | singleByte(midi[1]); |
hsgw | 24:5281ea9f6e68 | 193 | break; |
hsgw | 24:5281ea9f6e68 | 194 | default: break; |
hsgw | 24:5281ea9f6e68 | 195 | } |
hsgw | 24:5281ea9f6e68 | 196 | } |
hsgw | 24:5281ea9f6e68 | 197 | |
hsgw | 24:5281ea9f6e68 | 198 | // read another message |
hsgw | 24:5281ea9f6e68 | 199 | host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); |
hsgw | 24:5281ea9f6e68 | 200 | } |
hsgw | 24:5281ea9f6e68 | 201 | } |
hsgw | 24:5281ea9f6e68 | 202 | } |
hsgw | 24:5281ea9f6e68 | 203 | |
hsgw | 24:5281ea9f6e68 | 204 | bool USBHostMIDI::sendMidiBuffer(uint8_t data0, uint8_t data1, uint8_t data2, uint8_t data3) { |
hsgw | 24:5281ea9f6e68 | 205 | if (bulk_out) { |
hsgw | 24:5281ea9f6e68 | 206 | uint8_t midi[4]; |
hsgw | 24:5281ea9f6e68 | 207 | |
hsgw | 24:5281ea9f6e68 | 208 | midi[0] = data0; |
hsgw | 24:5281ea9f6e68 | 209 | midi[1] = data1; |
hsgw | 24:5281ea9f6e68 | 210 | midi[2] = data2; |
hsgw | 24:5281ea9f6e68 | 211 | midi[3] = data3; |
hsgw | 24:5281ea9f6e68 | 212 | if (host->bulkWrite(dev, bulk_out, (uint8_t *)midi, 4) == USB_TYPE_OK) { |
hsgw | 24:5281ea9f6e68 | 213 | return true; |
hsgw | 24:5281ea9f6e68 | 214 | } |
hsgw | 24:5281ea9f6e68 | 215 | } |
hsgw | 24:5281ea9f6e68 | 216 | return false; |
hsgw | 24:5281ea9f6e68 | 217 | } |
hsgw | 24:5281ea9f6e68 | 218 | |
hsgw | 24:5281ea9f6e68 | 219 | bool USBHostMIDI::sendMiscellaneousFunctionCode(uint8_t data1, uint8_t data2, uint8_t data3) { |
hsgw | 24:5281ea9f6e68 | 220 | return sendMidiBuffer(0, data1, data2, data3); |
hsgw | 24:5281ea9f6e68 | 221 | } |
hsgw | 24:5281ea9f6e68 | 222 | |
hsgw | 24:5281ea9f6e68 | 223 | bool USBHostMIDI::sendCableEvent(uint8_t data1, uint8_t data2, uint8_t data3) { |
hsgw | 24:5281ea9f6e68 | 224 | return sendMidiBuffer(1, data1, data2, data3); |
hsgw | 24:5281ea9f6e68 | 225 | } |
hsgw | 24:5281ea9f6e68 | 226 | |
hsgw | 24:5281ea9f6e68 | 227 | bool USBHostMIDI::sendSystemCommmonTwoBytes(uint8_t data1, uint8_t data2) { |
hsgw | 24:5281ea9f6e68 | 228 | return sendMidiBuffer(2, data1, data2, 0); |
hsgw | 24:5281ea9f6e68 | 229 | } |
hsgw | 24:5281ea9f6e68 | 230 | |
hsgw | 24:5281ea9f6e68 | 231 | bool USBHostMIDI::sendSystemCommmonThreeBytes(uint8_t data1, uint8_t data2, uint8_t data3) { |
hsgw | 24:5281ea9f6e68 | 232 | return sendMidiBuffer(3, data1, data2, 0); |
hsgw | 24:5281ea9f6e68 | 233 | } |
hsgw | 24:5281ea9f6e68 | 234 | |
hsgw | 24:5281ea9f6e68 | 235 | bool USBHostMIDI::sendSystemExclusive(uint8_t *buffer, int length) { |
hsgw | 24:5281ea9f6e68 | 236 | uint8_t midi[64]; |
hsgw | 24:5281ea9f6e68 | 237 | int midiLength; |
hsgw | 24:5281ea9f6e68 | 238 | int midiPos; |
hsgw | 24:5281ea9f6e68 | 239 | if (bulk_out) { |
hsgw | 24:5281ea9f6e68 | 240 | for (int i = 0; i < length; i += 48) { |
hsgw | 24:5281ea9f6e68 | 241 | if (i + 48 >= length) { |
hsgw | 24:5281ea9f6e68 | 242 | // contains last data |
hsgw | 24:5281ea9f6e68 | 243 | midiLength = (((length - i) + 2) / 3) * 4; |
hsgw | 24:5281ea9f6e68 | 244 | for (int pos = i; pos < length; pos += 3) { |
hsgw | 24:5281ea9f6e68 | 245 | midiPos = (pos + 2) / 3 * 4; |
hsgw | 24:5281ea9f6e68 | 246 | if (pos + 3 >= length) { |
hsgw | 24:5281ea9f6e68 | 247 | // last data |
hsgw | 24:5281ea9f6e68 | 248 | switch (pos % 3) { |
hsgw | 24:5281ea9f6e68 | 249 | case 0: |
hsgw | 24:5281ea9f6e68 | 250 | midi[midiPos ] = 7; |
hsgw | 24:5281ea9f6e68 | 251 | midi[midiPos + 1] = buffer[pos ]; |
hsgw | 24:5281ea9f6e68 | 252 | midi[midiPos + 2] = buffer[pos + 1]; |
hsgw | 24:5281ea9f6e68 | 253 | midi[midiPos + 3] = buffer[pos + 2]; |
hsgw | 24:5281ea9f6e68 | 254 | break; |
hsgw | 24:5281ea9f6e68 | 255 | case 1: |
hsgw | 24:5281ea9f6e68 | 256 | midi[midiPos ] = 5; |
hsgw | 24:5281ea9f6e68 | 257 | midi[midiPos + 1] = buffer[pos ]; |
hsgw | 24:5281ea9f6e68 | 258 | midi[midiPos + 2] = 0; |
hsgw | 24:5281ea9f6e68 | 259 | midi[midiPos + 3] = 0; |
hsgw | 24:5281ea9f6e68 | 260 | break; |
hsgw | 24:5281ea9f6e68 | 261 | case 2: |
hsgw | 24:5281ea9f6e68 | 262 | midi[midiPos ] = 6; |
hsgw | 24:5281ea9f6e68 | 263 | midi[midiPos + 1] = buffer[pos ]; |
hsgw | 24:5281ea9f6e68 | 264 | midi[midiPos + 2] = buffer[pos + 1]; |
hsgw | 24:5281ea9f6e68 | 265 | midi[midiPos + 3] = 0; |
hsgw | 24:5281ea9f6e68 | 266 | break; |
hsgw | 24:5281ea9f6e68 | 267 | } |
hsgw | 24:5281ea9f6e68 | 268 | } else { |
hsgw | 24:5281ea9f6e68 | 269 | // has more data |
hsgw | 24:5281ea9f6e68 | 270 | midi[midiPos ] = 4; |
hsgw | 24:5281ea9f6e68 | 271 | midi[midiPos + 1] = buffer[pos ]; |
hsgw | 24:5281ea9f6e68 | 272 | midi[midiPos + 2] = buffer[pos + 1]; |
hsgw | 24:5281ea9f6e68 | 273 | midi[midiPos + 3] = buffer[pos + 2]; |
hsgw | 24:5281ea9f6e68 | 274 | } |
hsgw | 24:5281ea9f6e68 | 275 | } |
hsgw | 24:5281ea9f6e68 | 276 | } else { |
hsgw | 24:5281ea9f6e68 | 277 | // has more data |
hsgw | 24:5281ea9f6e68 | 278 | midiLength = 64; |
hsgw | 24:5281ea9f6e68 | 279 | for (int pos = i; pos < length; pos += 3) { |
hsgw | 24:5281ea9f6e68 | 280 | midiPos = (pos + 2) / 3 * 4; |
hsgw | 24:5281ea9f6e68 | 281 | midi[midiPos ] = 4; |
hsgw | 24:5281ea9f6e68 | 282 | midi[midiPos + 1] = buffer[pos ]; |
hsgw | 24:5281ea9f6e68 | 283 | midi[midiPos + 2] = buffer[pos + 1]; |
hsgw | 24:5281ea9f6e68 | 284 | midi[midiPos + 3] = buffer[pos + 2]; |
hsgw | 24:5281ea9f6e68 | 285 | } |
hsgw | 24:5281ea9f6e68 | 286 | } |
hsgw | 24:5281ea9f6e68 | 287 | |
hsgw | 24:5281ea9f6e68 | 288 | if (host->bulkWrite(dev, bulk_out, (uint8_t *)midi, midiLength) != USB_TYPE_OK) { |
hsgw | 24:5281ea9f6e68 | 289 | return false; |
hsgw | 24:5281ea9f6e68 | 290 | } |
hsgw | 24:5281ea9f6e68 | 291 | } |
hsgw | 24:5281ea9f6e68 | 292 | return true; |
hsgw | 24:5281ea9f6e68 | 293 | } |
hsgw | 24:5281ea9f6e68 | 294 | return false; |
hsgw | 24:5281ea9f6e68 | 295 | } |
hsgw | 24:5281ea9f6e68 | 296 | |
hsgw | 24:5281ea9f6e68 | 297 | bool USBHostMIDI::sendNoteOff(uint8_t channel, uint8_t note, uint8_t velocity) { |
hsgw | 24:5281ea9f6e68 | 298 | return sendMidiBuffer(8, channel & 0xf | 0x80, note & 0x7f, velocity & 0x7f); |
hsgw | 24:5281ea9f6e68 | 299 | } |
hsgw | 24:5281ea9f6e68 | 300 | |
hsgw | 24:5281ea9f6e68 | 301 | bool USBHostMIDI::sendNoteOn(uint8_t channel, uint8_t note, uint8_t velocity) { |
hsgw | 24:5281ea9f6e68 | 302 | return sendMidiBuffer(9, channel & 0xf | 0x90, note & 0x7f, velocity & 0x7f); |
hsgw | 24:5281ea9f6e68 | 303 | } |
hsgw | 24:5281ea9f6e68 | 304 | |
hsgw | 24:5281ea9f6e68 | 305 | bool USBHostMIDI::sendPolyKeyPress(uint8_t channel, uint8_t note, uint8_t pressure) { |
hsgw | 24:5281ea9f6e68 | 306 | return sendMidiBuffer(10, channel & 0xf | 0xa0, note & 0x7f, pressure & 0x7f); |
hsgw | 24:5281ea9f6e68 | 307 | } |
hsgw | 24:5281ea9f6e68 | 308 | |
hsgw | 24:5281ea9f6e68 | 309 | bool USBHostMIDI::sendControlChange(uint8_t channel, uint8_t key, uint8_t value) { |
hsgw | 24:5281ea9f6e68 | 310 | return sendMidiBuffer(11, channel & 0xf | 0xb0, key & 0x7f, value & 0x7f); |
hsgw | 24:5281ea9f6e68 | 311 | } |
hsgw | 24:5281ea9f6e68 | 312 | |
hsgw | 24:5281ea9f6e68 | 313 | bool USBHostMIDI::sendProgramChange(uint8_t channel, uint8_t program) { |
hsgw | 24:5281ea9f6e68 | 314 | return sendMidiBuffer(12, channel & 0xf | 0xc0, program & 0x7f, 0); |
hsgw | 24:5281ea9f6e68 | 315 | } |
hsgw | 24:5281ea9f6e68 | 316 | |
hsgw | 24:5281ea9f6e68 | 317 | bool USBHostMIDI::sendChannelPressure(uint8_t channel, uint8_t pressure) { |
hsgw | 24:5281ea9f6e68 | 318 | return sendMidiBuffer(13, channel & 0xf | 0xd0, pressure & 0x7f, 0); |
hsgw | 24:5281ea9f6e68 | 319 | } |
hsgw | 24:5281ea9f6e68 | 320 | |
hsgw | 24:5281ea9f6e68 | 321 | bool USBHostMIDI::sendPitchBend(uint8_t channel, uint16_t value) { |
hsgw | 24:5281ea9f6e68 | 322 | return sendMidiBuffer(14, channel & 0xf | 0xe0, value & 0x7f, (value >> 7) & 0x7f); |
hsgw | 24:5281ea9f6e68 | 323 | } |
hsgw | 24:5281ea9f6e68 | 324 | |
hsgw | 24:5281ea9f6e68 | 325 | bool USBHostMIDI::sendSingleByte(uint8_t data) { |
hsgw | 24:5281ea9f6e68 | 326 | return sendMidiBuffer(15, data, 0, 0); |
hsgw | 24:5281ea9f6e68 | 327 | } |
hsgw | 24:5281ea9f6e68 | 328 | |
hsgw | 24:5281ea9f6e68 | 329 | /*virtual*/ void USBHostMIDI::setVidPid(uint16_t vid, uint16_t pid) |
hsgw | 24:5281ea9f6e68 | 330 | { |
hsgw | 24:5281ea9f6e68 | 331 | // we don't check VID/PID for this driver |
hsgw | 24:5281ea9f6e68 | 332 | } |
hsgw | 24:5281ea9f6e68 | 333 | |
hsgw | 24:5281ea9f6e68 | 334 | /*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 | 24:5281ea9f6e68 | 335 | { |
hsgw | 24:5281ea9f6e68 | 336 | // USB MIDI class/subclass |
hsgw | 24:5281ea9f6e68 | 337 | if ((midi_intf == -1) && |
hsgw | 24:5281ea9f6e68 | 338 | (intf_class == AUDIO_CLASS) && |
hsgw | 24:5281ea9f6e68 | 339 | (intf_subclass == 0x03)) { |
hsgw | 24:5281ea9f6e68 | 340 | midi_intf = intf_nb; |
hsgw | 24:5281ea9f6e68 | 341 | return true; |
hsgw | 24:5281ea9f6e68 | 342 | } |
hsgw | 24:5281ea9f6e68 | 343 | |
hsgw | 24:5281ea9f6e68 | 344 | // vendor specific device |
hsgw | 24:5281ea9f6e68 | 345 | if ((midi_intf == -1) && |
hsgw | 24:5281ea9f6e68 | 346 | (intf_class == 0xff) && |
hsgw | 24:5281ea9f6e68 | 347 | (intf_subclass == 0x03)) { |
hsgw | 24:5281ea9f6e68 | 348 | midi_intf = intf_nb; |
hsgw | 24:5281ea9f6e68 | 349 | return true; |
hsgw | 24:5281ea9f6e68 | 350 | } |
hsgw | 24:5281ea9f6e68 | 351 | |
hsgw | 24:5281ea9f6e68 | 352 | return false; |
hsgw | 24:5281ea9f6e68 | 353 | } |
hsgw | 24:5281ea9f6e68 | 354 | |
hsgw | 24:5281ea9f6e68 | 355 | /*virtual*/ bool USBHostMIDI::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used |
hsgw | 24:5281ea9f6e68 | 356 | { |
hsgw | 24:5281ea9f6e68 | 357 | if (intf_nb == midi_intf) { |
hsgw | 24:5281ea9f6e68 | 358 | if (type == BULK_ENDPOINT) { |
hsgw | 24:5281ea9f6e68 | 359 | midi_device_found = true; |
hsgw | 24:5281ea9f6e68 | 360 | return true; |
hsgw | 24:5281ea9f6e68 | 361 | } |
hsgw | 24:5281ea9f6e68 | 362 | } |
hsgw | 24:5281ea9f6e68 | 363 | return false; |
hsgw | 24:5281ea9f6e68 | 364 | } |
hsgw | 24:5281ea9f6e68 | 365 | |
hsgw | 24:5281ea9f6e68 | 366 | #endif |