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@27:23fa4e04b1db, 2014-10-13 (annotated)
- Committer:
- hsgw
- Date:
- Mon Oct 13 19:33:40 2014 +0000
- Revision:
- 27:23fa4e04b1db
- Parent:
- 26:077ab26227c6
Fix freeze bug on F401RE.; callback functions are initialized by dummy functions.
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 | 27:23fa4e04b1db | 45 | |
hsgw | 27:23fa4e04b1db | 46 | // init callback functions |
hsgw | 27:23fa4e04b1db | 47 | |
hsgw | 27:23fa4e04b1db | 48 | miscellaneousFunctionCode = &callbackDummy3Bytes; |
hsgw | 27:23fa4e04b1db | 49 | cableEvent = callbackDummy3Bytes; |
hsgw | 27:23fa4e04b1db | 50 | systemCommonTwoBytes = &callbackDummy2Bytes; |
hsgw | 27:23fa4e04b1db | 51 | systemCommonThreeBytes = &callbackDummy3Bytes; |
hsgw | 27:23fa4e04b1db | 52 | systemExclusive = &callbackDummysystemExclusive; |
hsgw | 27:23fa4e04b1db | 53 | noteOff = &callbackDummy3Bytes; |
hsgw | 27:23fa4e04b1db | 54 | noteOn = &callbackDummy3Bytes; |
hsgw | 27:23fa4e04b1db | 55 | polyKeyPress = &callbackDummy3Bytes; |
hsgw | 27:23fa4e04b1db | 56 | controlChange = &callbackDummy3Bytes; |
hsgw | 27:23fa4e04b1db | 57 | programChange = &callbackDummy2Bytes; |
hsgw | 27:23fa4e04b1db | 58 | channelPressure = &callbackDummy2Bytes; |
hsgw | 27:23fa4e04b1db | 59 | pitchBend = &callbackDummypitchBend; |
hsgw | 27:23fa4e04b1db | 60 | singleByte = &callbackDummysingleByte; |
hsgw | 27:23fa4e04b1db | 61 | |
hsgw | 24:5281ea9f6e68 | 62 | } |
hsgw | 24:5281ea9f6e68 | 63 | |
hsgw | 24:5281ea9f6e68 | 64 | bool USBHostMIDI::connected() |
hsgw | 24:5281ea9f6e68 | 65 | { |
hsgw | 24:5281ea9f6e68 | 66 | return dev_connected; |
hsgw | 24:5281ea9f6e68 | 67 | } |
hsgw | 24:5281ea9f6e68 | 68 | |
hsgw | 24:5281ea9f6e68 | 69 | bool USBHostMIDI::connect() { |
hsgw | 24:5281ea9f6e68 | 70 | |
hsgw | 24:5281ea9f6e68 | 71 | if (dev_connected) { |
hsgw | 24:5281ea9f6e68 | 72 | return true; |
hsgw | 24:5281ea9f6e68 | 73 | } |
hsgw | 24:5281ea9f6e68 | 74 | for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) { |
hsgw | 24:5281ea9f6e68 | 75 | if ((dev = host->getDevice(i)) != NULL) { |
hsgw | 24:5281ea9f6e68 | 76 | |
hsgw | 24:5281ea9f6e68 | 77 | USB_DBG("Trying to connect MIDI device\r\n"); |
hsgw | 24:5281ea9f6e68 | 78 | |
hsgw | 24:5281ea9f6e68 | 79 | if(host->enumerate(dev, this)) |
hsgw | 24:5281ea9f6e68 | 80 | break; |
hsgw | 24:5281ea9f6e68 | 81 | |
hsgw | 24:5281ea9f6e68 | 82 | if (midi_device_found) { |
hsgw | 24:5281ea9f6e68 | 83 | bulk_in = dev->getEndpoint(midi_intf, BULK_ENDPOINT, IN); |
hsgw | 24:5281ea9f6e68 | 84 | bulk_out = dev->getEndpoint(midi_intf, BULK_ENDPOINT, OUT); |
hsgw | 24:5281ea9f6e68 | 85 | |
hsgw | 24:5281ea9f6e68 | 86 | if (!bulk_in || !bulk_out) |
hsgw | 24:5281ea9f6e68 | 87 | break; |
hsgw | 24:5281ea9f6e68 | 88 | |
hsgw | 24:5281ea9f6e68 | 89 | USB_INFO("New MIDI device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, midi_intf); |
hsgw | 24:5281ea9f6e68 | 90 | dev->setName("MIDI", midi_intf); |
hsgw | 24:5281ea9f6e68 | 91 | host->registerDriver(dev, midi_intf, this, &USBHostMIDI::init); |
hsgw | 24:5281ea9f6e68 | 92 | |
hsgw | 24:5281ea9f6e68 | 93 | size_bulk_in = bulk_in->getSize(); |
hsgw | 24:5281ea9f6e68 | 94 | size_bulk_out = bulk_out->getSize(); |
hsgw | 24:5281ea9f6e68 | 95 | |
hsgw | 24:5281ea9f6e68 | 96 | bulk_in->attach(this, &USBHostMIDI::rxHandler); |
hsgw | 24:5281ea9f6e68 | 97 | |
hsgw | 24:5281ea9f6e68 | 98 | host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); |
hsgw | 24:5281ea9f6e68 | 99 | dev_connected = true; |
hsgw | 24:5281ea9f6e68 | 100 | return true; |
hsgw | 24:5281ea9f6e68 | 101 | } |
hsgw | 24:5281ea9f6e68 | 102 | } |
hsgw | 24:5281ea9f6e68 | 103 | } |
hsgw | 24:5281ea9f6e68 | 104 | init(); |
hsgw | 24:5281ea9f6e68 | 105 | return false; |
hsgw | 24:5281ea9f6e68 | 106 | } |
hsgw | 24:5281ea9f6e68 | 107 | |
hsgw | 24:5281ea9f6e68 | 108 | void USBHostMIDI::rxHandler() { |
hsgw | 24:5281ea9f6e68 | 109 | uint8_t *midi; |
hsgw | 24:5281ea9f6e68 | 110 | if (bulk_in) { |
hsgw | 26:077ab26227c6 | 111 | |
hsgw | 26:077ab26227c6 | 112 | int length = bulk_in->getLengthTransferred(); // why does this method always return 64? |
hsgw | 24:5281ea9f6e68 | 113 | if (bulk_in->getState() == USB_TYPE_IDLE || bulk_in->getState() == USB_TYPE_FREE) { |
hsgw | 24:5281ea9f6e68 | 114 | // MIDI event handling |
hsgw | 24:5281ea9f6e68 | 115 | for (int i = 0; i < length; i += 4) { |
hsgw | 24:5281ea9f6e68 | 116 | if (i + 4 > length) { |
hsgw | 24:5281ea9f6e68 | 117 | // length shortage, ignored. |
hsgw | 24:5281ea9f6e68 | 118 | break; |
hsgw | 24:5281ea9f6e68 | 119 | } |
hsgw | 24:5281ea9f6e68 | 120 | |
hsgw | 24:5281ea9f6e68 | 121 | // read each four bytes |
hsgw | 24:5281ea9f6e68 | 122 | midi = &buf[i]; |
hsgw | 26:077ab26227c6 | 123 | |
hsgw | 24:5281ea9f6e68 | 124 | // process MIDI message |
hsgw | 26:077ab26227c6 | 125 | if (midi[0] == 0 && midi[1] == 0) { |
hsgw | 26:077ab26227c6 | 126 | // {0,0,0,0} may be not collect data |
hsgw | 26:077ab26227c6 | 127 | continue; |
hsgw | 26:077ab26227c6 | 128 | } |
hsgw | 26:077ab26227c6 | 129 | |
hsgw | 26:077ab26227c6 | 130 | USB_DBG("raw: %d, %d, %d, %d", midi[0]&0xf, midi[1], midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 131 | // switch by code index number |
hsgw | 24:5281ea9f6e68 | 132 | switch (midi[0] & 0xf) { |
hsgw | 24:5281ea9f6e68 | 133 | case 0: // miscellaneous function codes |
hsgw | 26:077ab26227c6 | 134 | if(midi[1] == 0) break; |
hsgw | 26:077ab26227c6 | 135 | miscellaneousFunctionCode(midi[1], midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 136 | break; |
hsgw | 24:5281ea9f6e68 | 137 | case 1: // cable events |
hsgw | 26:077ab26227c6 | 138 | cableEvent(midi[1], midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 139 | break; |
hsgw | 24:5281ea9f6e68 | 140 | case 2: // two bytes system common messages |
hsgw | 24:5281ea9f6e68 | 141 | systemCommonTwoBytes(midi[1], midi[2]); |
hsgw | 24:5281ea9f6e68 | 142 | break; |
hsgw | 24:5281ea9f6e68 | 143 | case 3: // three bytes system common messages |
hsgw | 24:5281ea9f6e68 | 144 | systemCommonThreeBytes(midi[1], midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 145 | break; |
hsgw | 24:5281ea9f6e68 | 146 | case 4: // SysEx starts or continues |
hsgw | 24:5281ea9f6e68 | 147 | sysExBuffer[sysExBufferPos++] = midi[1]; |
hsgw | 24:5281ea9f6e68 | 148 | if (sysExBufferPos >= 64) { |
hsgw | 24:5281ea9f6e68 | 149 | systemExclusive(sysExBuffer, sysExBufferPos, true); |
hsgw | 24:5281ea9f6e68 | 150 | sysExBufferPos = 0; |
hsgw | 24:5281ea9f6e68 | 151 | } |
hsgw | 24:5281ea9f6e68 | 152 | sysExBuffer[sysExBufferPos++] = midi[2]; |
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[3]; |
hsgw | 24:5281ea9f6e68 | 158 | // SysEx continues. don't send |
hsgw | 24:5281ea9f6e68 | 159 | break; |
hsgw | 24:5281ea9f6e68 | 160 | case 5: // SysEx ends with single byte |
hsgw | 24:5281ea9f6e68 | 161 | sysExBuffer[sysExBufferPos++] = midi[1]; |
hsgw | 24:5281ea9f6e68 | 162 | systemExclusive(sysExBuffer, sysExBufferPos, false); |
hsgw | 24:5281ea9f6e68 | 163 | sysExBufferPos = 0; |
hsgw | 24:5281ea9f6e68 | 164 | break; |
hsgw | 24:5281ea9f6e68 | 165 | case 6: // SysEx ends with two bytes |
hsgw | 24:5281ea9f6e68 | 166 | sysExBuffer[sysExBufferPos++] = midi[1]; |
hsgw | 24:5281ea9f6e68 | 167 | if (sysExBufferPos >= 64) { |
hsgw | 24:5281ea9f6e68 | 168 | systemExclusive(sysExBuffer, sysExBufferPos, true); |
hsgw | 24:5281ea9f6e68 | 169 | sysExBufferPos = 0; |
hsgw | 24:5281ea9f6e68 | 170 | } |
hsgw | 24:5281ea9f6e68 | 171 | sysExBuffer[sysExBufferPos++] = midi[2]; |
hsgw | 24:5281ea9f6e68 | 172 | systemExclusive(sysExBuffer, sysExBufferPos, false); |
hsgw | 24:5281ea9f6e68 | 173 | sysExBufferPos = 0; |
hsgw | 24:5281ea9f6e68 | 174 | break; |
hsgw | 24:5281ea9f6e68 | 175 | case 7: // SysEx ends with three bytes |
hsgw | 24:5281ea9f6e68 | 176 | sysExBuffer[sysExBufferPos++] = midi[1]; |
hsgw | 24:5281ea9f6e68 | 177 | if (sysExBufferPos >= 64) { |
hsgw | 24:5281ea9f6e68 | 178 | systemExclusive(sysExBuffer, sysExBufferPos, true); |
hsgw | 24:5281ea9f6e68 | 179 | sysExBufferPos = 0; |
hsgw | 24:5281ea9f6e68 | 180 | } |
hsgw | 24:5281ea9f6e68 | 181 | sysExBuffer[sysExBufferPos++] = midi[2]; |
hsgw | 24:5281ea9f6e68 | 182 | if (sysExBufferPos >= 64) { |
hsgw | 24:5281ea9f6e68 | 183 | systemExclusive(sysExBuffer, sysExBufferPos, true); |
hsgw | 24:5281ea9f6e68 | 184 | sysExBufferPos = 0; |
hsgw | 24:5281ea9f6e68 | 185 | } |
hsgw | 24:5281ea9f6e68 | 186 | sysExBuffer[sysExBufferPos++] = midi[3]; |
hsgw | 24:5281ea9f6e68 | 187 | systemExclusive(sysExBuffer, sysExBufferPos, false); |
hsgw | 24:5281ea9f6e68 | 188 | sysExBufferPos = 0; |
hsgw | 24:5281ea9f6e68 | 189 | break; |
hsgw | 24:5281ea9f6e68 | 190 | case 8: |
hsgw | 24:5281ea9f6e68 | 191 | noteOff(midi[1] & 0xf, midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 192 | break; |
hsgw | 24:5281ea9f6e68 | 193 | case 9: |
hsgw | 24:5281ea9f6e68 | 194 | if (midi[3]) { |
hsgw | 24:5281ea9f6e68 | 195 | noteOn(midi[1] & 0xf, midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 196 | } else { |
hsgw | 24:5281ea9f6e68 | 197 | noteOff(midi[1] & 0xf, midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 198 | } |
hsgw | 24:5281ea9f6e68 | 199 | break; |
hsgw | 24:5281ea9f6e68 | 200 | case 10: |
hsgw | 24:5281ea9f6e68 | 201 | polyKeyPress(midi[1] & 0xf, midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 202 | break; |
hsgw | 24:5281ea9f6e68 | 203 | case 11: |
hsgw | 24:5281ea9f6e68 | 204 | controlChange(midi[1] & 0xf, midi[2], midi[3]); |
hsgw | 24:5281ea9f6e68 | 205 | break; |
hsgw | 24:5281ea9f6e68 | 206 | case 12: |
hsgw | 24:5281ea9f6e68 | 207 | programChange(midi[1] & 0xf, midi[2]); |
hsgw | 24:5281ea9f6e68 | 208 | break; |
hsgw | 24:5281ea9f6e68 | 209 | case 13: |
hsgw | 24:5281ea9f6e68 | 210 | channelPressure(midi[1] & 0xf, midi[2]); |
hsgw | 24:5281ea9f6e68 | 211 | break; |
hsgw | 24:5281ea9f6e68 | 212 | case 14: |
hsgw | 24:5281ea9f6e68 | 213 | pitchBend(midi[1] & 0xf, midi[2] | (midi[3] << 7)); |
hsgw | 24:5281ea9f6e68 | 214 | break; |
hsgw | 24:5281ea9f6e68 | 215 | case 15: |
hsgw | 24:5281ea9f6e68 | 216 | singleByte(midi[1]); |
hsgw | 24:5281ea9f6e68 | 217 | break; |
hsgw | 24:5281ea9f6e68 | 218 | default: break; |
hsgw | 24:5281ea9f6e68 | 219 | } |
hsgw | 24:5281ea9f6e68 | 220 | } |
hsgw | 24:5281ea9f6e68 | 221 | |
hsgw | 24:5281ea9f6e68 | 222 | // read another message |
hsgw | 24:5281ea9f6e68 | 223 | host->bulkRead(dev, bulk_in, buf, size_bulk_in, false); |
hsgw | 24:5281ea9f6e68 | 224 | } |
hsgw | 24:5281ea9f6e68 | 225 | } |
hsgw | 24:5281ea9f6e68 | 226 | } |
hsgw | 24:5281ea9f6e68 | 227 | |
hsgw | 24:5281ea9f6e68 | 228 | bool USBHostMIDI::sendMidiBuffer(uint8_t data0, uint8_t data1, uint8_t data2, uint8_t data3) { |
hsgw | 24:5281ea9f6e68 | 229 | if (bulk_out) { |
hsgw | 24:5281ea9f6e68 | 230 | uint8_t midi[4]; |
hsgw | 24:5281ea9f6e68 | 231 | |
hsgw | 24:5281ea9f6e68 | 232 | midi[0] = data0; |
hsgw | 24:5281ea9f6e68 | 233 | midi[1] = data1; |
hsgw | 24:5281ea9f6e68 | 234 | midi[2] = data2; |
hsgw | 24:5281ea9f6e68 | 235 | midi[3] = data3; |
hsgw | 24:5281ea9f6e68 | 236 | if (host->bulkWrite(dev, bulk_out, (uint8_t *)midi, 4) == USB_TYPE_OK) { |
hsgw | 24:5281ea9f6e68 | 237 | return true; |
hsgw | 24:5281ea9f6e68 | 238 | } |
hsgw | 24:5281ea9f6e68 | 239 | } |
hsgw | 24:5281ea9f6e68 | 240 | return false; |
hsgw | 24:5281ea9f6e68 | 241 | } |
hsgw | 24:5281ea9f6e68 | 242 | |
hsgw | 24:5281ea9f6e68 | 243 | bool USBHostMIDI::sendMiscellaneousFunctionCode(uint8_t data1, uint8_t data2, uint8_t data3) { |
hsgw | 24:5281ea9f6e68 | 244 | return sendMidiBuffer(0, data1, data2, data3); |
hsgw | 24:5281ea9f6e68 | 245 | } |
hsgw | 24:5281ea9f6e68 | 246 | |
hsgw | 24:5281ea9f6e68 | 247 | bool USBHostMIDI::sendCableEvent(uint8_t data1, uint8_t data2, uint8_t data3) { |
hsgw | 24:5281ea9f6e68 | 248 | return sendMidiBuffer(1, data1, data2, data3); |
hsgw | 24:5281ea9f6e68 | 249 | } |
hsgw | 24:5281ea9f6e68 | 250 | |
hsgw | 24:5281ea9f6e68 | 251 | bool USBHostMIDI::sendSystemCommmonTwoBytes(uint8_t data1, uint8_t data2) { |
hsgw | 24:5281ea9f6e68 | 252 | return sendMidiBuffer(2, data1, data2, 0); |
hsgw | 24:5281ea9f6e68 | 253 | } |
hsgw | 24:5281ea9f6e68 | 254 | |
hsgw | 24:5281ea9f6e68 | 255 | bool USBHostMIDI::sendSystemCommmonThreeBytes(uint8_t data1, uint8_t data2, uint8_t data3) { |
hsgw | 24:5281ea9f6e68 | 256 | return sendMidiBuffer(3, data1, data2, 0); |
hsgw | 24:5281ea9f6e68 | 257 | } |
hsgw | 24:5281ea9f6e68 | 258 | |
hsgw | 24:5281ea9f6e68 | 259 | bool USBHostMIDI::sendSystemExclusive(uint8_t *buffer, int length) { |
hsgw | 24:5281ea9f6e68 | 260 | uint8_t midi[64]; |
hsgw | 24:5281ea9f6e68 | 261 | int midiLength; |
hsgw | 24:5281ea9f6e68 | 262 | int midiPos; |
hsgw | 24:5281ea9f6e68 | 263 | if (bulk_out) { |
hsgw | 24:5281ea9f6e68 | 264 | for (int i = 0; i < length; i += 48) { |
hsgw | 24:5281ea9f6e68 | 265 | if (i + 48 >= length) { |
hsgw | 24:5281ea9f6e68 | 266 | // contains last data |
hsgw | 24:5281ea9f6e68 | 267 | midiLength = (((length - i) + 2) / 3) * 4; |
hsgw | 24:5281ea9f6e68 | 268 | for (int pos = i; pos < length; pos += 3) { |
hsgw | 24:5281ea9f6e68 | 269 | midiPos = (pos + 2) / 3 * 4; |
hsgw | 24:5281ea9f6e68 | 270 | if (pos + 3 >= length) { |
hsgw | 24:5281ea9f6e68 | 271 | // last data |
hsgw | 24:5281ea9f6e68 | 272 | switch (pos % 3) { |
hsgw | 24:5281ea9f6e68 | 273 | case 0: |
hsgw | 24:5281ea9f6e68 | 274 | midi[midiPos ] = 7; |
hsgw | 24:5281ea9f6e68 | 275 | midi[midiPos + 1] = buffer[pos ]; |
hsgw | 24:5281ea9f6e68 | 276 | midi[midiPos + 2] = buffer[pos + 1]; |
hsgw | 24:5281ea9f6e68 | 277 | midi[midiPos + 3] = buffer[pos + 2]; |
hsgw | 24:5281ea9f6e68 | 278 | break; |
hsgw | 24:5281ea9f6e68 | 279 | case 1: |
hsgw | 24:5281ea9f6e68 | 280 | midi[midiPos ] = 5; |
hsgw | 24:5281ea9f6e68 | 281 | midi[midiPos + 1] = buffer[pos ]; |
hsgw | 24:5281ea9f6e68 | 282 | midi[midiPos + 2] = 0; |
hsgw | 24:5281ea9f6e68 | 283 | midi[midiPos + 3] = 0; |
hsgw | 24:5281ea9f6e68 | 284 | break; |
hsgw | 24:5281ea9f6e68 | 285 | case 2: |
hsgw | 24:5281ea9f6e68 | 286 | midi[midiPos ] = 6; |
hsgw | 24:5281ea9f6e68 | 287 | midi[midiPos + 1] = buffer[pos ]; |
hsgw | 24:5281ea9f6e68 | 288 | midi[midiPos + 2] = buffer[pos + 1]; |
hsgw | 24:5281ea9f6e68 | 289 | midi[midiPos + 3] = 0; |
hsgw | 24:5281ea9f6e68 | 290 | break; |
hsgw | 24:5281ea9f6e68 | 291 | } |
hsgw | 24:5281ea9f6e68 | 292 | } else { |
hsgw | 24:5281ea9f6e68 | 293 | // has more data |
hsgw | 24:5281ea9f6e68 | 294 | midi[midiPos ] = 4; |
hsgw | 24:5281ea9f6e68 | 295 | midi[midiPos + 1] = buffer[pos ]; |
hsgw | 24:5281ea9f6e68 | 296 | midi[midiPos + 2] = buffer[pos + 1]; |
hsgw | 24:5281ea9f6e68 | 297 | midi[midiPos + 3] = buffer[pos + 2]; |
hsgw | 24:5281ea9f6e68 | 298 | } |
hsgw | 24:5281ea9f6e68 | 299 | } |
hsgw | 24:5281ea9f6e68 | 300 | } else { |
hsgw | 24:5281ea9f6e68 | 301 | // has more data |
hsgw | 24:5281ea9f6e68 | 302 | midiLength = 64; |
hsgw | 24:5281ea9f6e68 | 303 | for (int pos = i; pos < length; pos += 3) { |
hsgw | 24:5281ea9f6e68 | 304 | midiPos = (pos + 2) / 3 * 4; |
hsgw | 24:5281ea9f6e68 | 305 | midi[midiPos ] = 4; |
hsgw | 24:5281ea9f6e68 | 306 | midi[midiPos + 1] = buffer[pos ]; |
hsgw | 24:5281ea9f6e68 | 307 | midi[midiPos + 2] = buffer[pos + 1]; |
hsgw | 24:5281ea9f6e68 | 308 | midi[midiPos + 3] = buffer[pos + 2]; |
hsgw | 24:5281ea9f6e68 | 309 | } |
hsgw | 24:5281ea9f6e68 | 310 | } |
hsgw | 24:5281ea9f6e68 | 311 | |
hsgw | 24:5281ea9f6e68 | 312 | if (host->bulkWrite(dev, bulk_out, (uint8_t *)midi, midiLength) != USB_TYPE_OK) { |
hsgw | 24:5281ea9f6e68 | 313 | return false; |
hsgw | 24:5281ea9f6e68 | 314 | } |
hsgw | 24:5281ea9f6e68 | 315 | } |
hsgw | 24:5281ea9f6e68 | 316 | return true; |
hsgw | 24:5281ea9f6e68 | 317 | } |
hsgw | 24:5281ea9f6e68 | 318 | return false; |
hsgw | 24:5281ea9f6e68 | 319 | } |
hsgw | 24:5281ea9f6e68 | 320 | |
hsgw | 24:5281ea9f6e68 | 321 | bool USBHostMIDI::sendNoteOff(uint8_t channel, uint8_t note, uint8_t velocity) { |
hsgw | 24:5281ea9f6e68 | 322 | return sendMidiBuffer(8, channel & 0xf | 0x80, note & 0x7f, velocity & 0x7f); |
hsgw | 24:5281ea9f6e68 | 323 | } |
hsgw | 24:5281ea9f6e68 | 324 | |
hsgw | 24:5281ea9f6e68 | 325 | bool USBHostMIDI::sendNoteOn(uint8_t channel, uint8_t note, uint8_t velocity) { |
hsgw | 24:5281ea9f6e68 | 326 | return sendMidiBuffer(9, channel & 0xf | 0x90, note & 0x7f, velocity & 0x7f); |
hsgw | 24:5281ea9f6e68 | 327 | } |
hsgw | 24:5281ea9f6e68 | 328 | |
hsgw | 24:5281ea9f6e68 | 329 | bool USBHostMIDI::sendPolyKeyPress(uint8_t channel, uint8_t note, uint8_t pressure) { |
hsgw | 24:5281ea9f6e68 | 330 | return sendMidiBuffer(10, channel & 0xf | 0xa0, note & 0x7f, pressure & 0x7f); |
hsgw | 24:5281ea9f6e68 | 331 | } |
hsgw | 24:5281ea9f6e68 | 332 | |
hsgw | 24:5281ea9f6e68 | 333 | bool USBHostMIDI::sendControlChange(uint8_t channel, uint8_t key, uint8_t value) { |
hsgw | 24:5281ea9f6e68 | 334 | return sendMidiBuffer(11, channel & 0xf | 0xb0, key & 0x7f, value & 0x7f); |
hsgw | 24:5281ea9f6e68 | 335 | } |
hsgw | 24:5281ea9f6e68 | 336 | |
hsgw | 24:5281ea9f6e68 | 337 | bool USBHostMIDI::sendProgramChange(uint8_t channel, uint8_t program) { |
hsgw | 24:5281ea9f6e68 | 338 | return sendMidiBuffer(12, channel & 0xf | 0xc0, program & 0x7f, 0); |
hsgw | 24:5281ea9f6e68 | 339 | } |
hsgw | 24:5281ea9f6e68 | 340 | |
hsgw | 24:5281ea9f6e68 | 341 | bool USBHostMIDI::sendChannelPressure(uint8_t channel, uint8_t pressure) { |
hsgw | 24:5281ea9f6e68 | 342 | return sendMidiBuffer(13, channel & 0xf | 0xd0, pressure & 0x7f, 0); |
hsgw | 24:5281ea9f6e68 | 343 | } |
hsgw | 24:5281ea9f6e68 | 344 | |
hsgw | 24:5281ea9f6e68 | 345 | bool USBHostMIDI::sendPitchBend(uint8_t channel, uint16_t value) { |
hsgw | 24:5281ea9f6e68 | 346 | return sendMidiBuffer(14, channel & 0xf | 0xe0, value & 0x7f, (value >> 7) & 0x7f); |
hsgw | 24:5281ea9f6e68 | 347 | } |
hsgw | 24:5281ea9f6e68 | 348 | |
hsgw | 24:5281ea9f6e68 | 349 | bool USBHostMIDI::sendSingleByte(uint8_t data) { |
hsgw | 24:5281ea9f6e68 | 350 | return sendMidiBuffer(15, data, 0, 0); |
hsgw | 24:5281ea9f6e68 | 351 | } |
hsgw | 24:5281ea9f6e68 | 352 | |
hsgw | 27:23fa4e04b1db | 353 | void callback_dummy(uint8_t firstArg, ...){ |
hsgw | 27:23fa4e04b1db | 354 | USB_DBG("Not attached command comming! %d\n", firstArg); |
hsgw | 27:23fa4e04b1db | 355 | } |
hsgw | 27:23fa4e04b1db | 356 | |
hsgw | 24:5281ea9f6e68 | 357 | /*virtual*/ void USBHostMIDI::setVidPid(uint16_t vid, uint16_t pid) |
hsgw | 24:5281ea9f6e68 | 358 | { |
hsgw | 24:5281ea9f6e68 | 359 | // we don't check VID/PID for this driver |
hsgw | 24:5281ea9f6e68 | 360 | } |
hsgw | 24:5281ea9f6e68 | 361 | |
hsgw | 24:5281ea9f6e68 | 362 | /*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 | 363 | { |
hsgw | 24:5281ea9f6e68 | 364 | // USB MIDI class/subclass |
hsgw | 24:5281ea9f6e68 | 365 | if ((midi_intf == -1) && |
hsgw | 24:5281ea9f6e68 | 366 | (intf_class == AUDIO_CLASS) && |
hsgw | 24:5281ea9f6e68 | 367 | (intf_subclass == 0x03)) { |
hsgw | 24:5281ea9f6e68 | 368 | midi_intf = intf_nb; |
hsgw | 24:5281ea9f6e68 | 369 | return true; |
hsgw | 24:5281ea9f6e68 | 370 | } |
hsgw | 24:5281ea9f6e68 | 371 | |
hsgw | 24:5281ea9f6e68 | 372 | // vendor specific device |
hsgw | 24:5281ea9f6e68 | 373 | if ((midi_intf == -1) && |
hsgw | 24:5281ea9f6e68 | 374 | (intf_class == 0xff) && |
hsgw | 24:5281ea9f6e68 | 375 | (intf_subclass == 0x03)) { |
hsgw | 24:5281ea9f6e68 | 376 | midi_intf = intf_nb; |
hsgw | 24:5281ea9f6e68 | 377 | return true; |
hsgw | 24:5281ea9f6e68 | 378 | } |
hsgw | 24:5281ea9f6e68 | 379 | |
hsgw | 24:5281ea9f6e68 | 380 | return false; |
hsgw | 24:5281ea9f6e68 | 381 | } |
hsgw | 24:5281ea9f6e68 | 382 | |
hsgw | 24:5281ea9f6e68 | 383 | /*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 | 384 | { |
hsgw | 24:5281ea9f6e68 | 385 | if (intf_nb == midi_intf) { |
hsgw | 24:5281ea9f6e68 | 386 | if (type == BULK_ENDPOINT) { |
hsgw | 24:5281ea9f6e68 | 387 | midi_device_found = true; |
hsgw | 24:5281ea9f6e68 | 388 | return true; |
hsgw | 24:5281ea9f6e68 | 389 | } |
hsgw | 24:5281ea9f6e68 | 390 | } |
hsgw | 24:5281ea9f6e68 | 391 | return false; |
hsgw | 24:5281ea9f6e68 | 392 | } |
hsgw | 24:5281ea9f6e68 | 393 | |
hsgw | 24:5281ea9f6e68 | 394 | #endif |