My Fork of F401RE-USBHost. Add USBHostMIDI functions (originaled by Kaoru Shoji http://mbed.org/users/kshoji/code/USBHostMIDI/)

Dependencies:   FATFileSystem

Dependents:   F401RE-USBHostMIDI_RecieveExample

Fork of F401RE-USBHost by Norimasa Okamoto

Committer:
hsgw
Date:
Thu Sep 18 12:32:33 2014 +0000
Revision:
26:077ab26227c6
Parent:
24:5281ea9f6e68
Child:
27:23fa4e04b1db
fix miscellaneous function and cable event.

Who changed what in which revision?

UserRevisionLine numberNew 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 26:077ab26227c6 94
hsgw 26:077ab26227c6 95 int length = bulk_in->getLengthTransferred(); // why does this method always return 64?
hsgw 24:5281ea9f6e68 96 if (bulk_in->getState() == USB_TYPE_IDLE || bulk_in->getState() == USB_TYPE_FREE) {
hsgw 24:5281ea9f6e68 97 // MIDI event handling
hsgw 24:5281ea9f6e68 98 for (int i = 0; i < length; i += 4) {
hsgw 24:5281ea9f6e68 99 if (i + 4 > length) {
hsgw 24:5281ea9f6e68 100 // length shortage, ignored.
hsgw 24:5281ea9f6e68 101 break;
hsgw 24:5281ea9f6e68 102 }
hsgw 24:5281ea9f6e68 103
hsgw 24:5281ea9f6e68 104 // read each four bytes
hsgw 24:5281ea9f6e68 105 midi = &buf[i];
hsgw 26:077ab26227c6 106
hsgw 24:5281ea9f6e68 107 // process MIDI message
hsgw 26:077ab26227c6 108 if (midi[0] == 0 && midi[1] == 0) {
hsgw 26:077ab26227c6 109 // {0,0,0,0} may be not collect data
hsgw 26:077ab26227c6 110 continue;
hsgw 26:077ab26227c6 111 }
hsgw 26:077ab26227c6 112
hsgw 26:077ab26227c6 113 USB_DBG("raw: %d, %d, %d, %d", midi[0]&0xf, midi[1], midi[2], midi[3]);
hsgw 24:5281ea9f6e68 114 // switch by code index number
hsgw 24:5281ea9f6e68 115 switch (midi[0] & 0xf) {
hsgw 24:5281ea9f6e68 116 case 0: // miscellaneous function codes
hsgw 26:077ab26227c6 117 if(midi[1] == 0) break;
hsgw 26:077ab26227c6 118 miscellaneousFunctionCode(midi[1], midi[2], midi[3]);
hsgw 24:5281ea9f6e68 119 break;
hsgw 24:5281ea9f6e68 120 case 1: // cable events
hsgw 26:077ab26227c6 121 cableEvent(midi[1], midi[2], midi[3]);
hsgw 24:5281ea9f6e68 122 break;
hsgw 24:5281ea9f6e68 123 case 2: // two bytes system common messages
hsgw 24:5281ea9f6e68 124 systemCommonTwoBytes(midi[1], midi[2]);
hsgw 24:5281ea9f6e68 125 break;
hsgw 24:5281ea9f6e68 126 case 3: // three bytes system common messages
hsgw 24:5281ea9f6e68 127 systemCommonThreeBytes(midi[1], midi[2], midi[3]);
hsgw 24:5281ea9f6e68 128 break;
hsgw 24:5281ea9f6e68 129 case 4: // SysEx starts or continues
hsgw 24:5281ea9f6e68 130 sysExBuffer[sysExBufferPos++] = midi[1];
hsgw 24:5281ea9f6e68 131 if (sysExBufferPos >= 64) {
hsgw 24:5281ea9f6e68 132 systemExclusive(sysExBuffer, sysExBufferPos, true);
hsgw 24:5281ea9f6e68 133 sysExBufferPos = 0;
hsgw 24:5281ea9f6e68 134 }
hsgw 24:5281ea9f6e68 135 sysExBuffer[sysExBufferPos++] = midi[2];
hsgw 24:5281ea9f6e68 136 if (sysExBufferPos >= 64) {
hsgw 24:5281ea9f6e68 137 systemExclusive(sysExBuffer, sysExBufferPos, true);
hsgw 24:5281ea9f6e68 138 sysExBufferPos = 0;
hsgw 24:5281ea9f6e68 139 }
hsgw 24:5281ea9f6e68 140 sysExBuffer[sysExBufferPos++] = midi[3];
hsgw 24:5281ea9f6e68 141 // SysEx continues. don't send
hsgw 24:5281ea9f6e68 142 break;
hsgw 24:5281ea9f6e68 143 case 5: // SysEx ends with single byte
hsgw 24:5281ea9f6e68 144 sysExBuffer[sysExBufferPos++] = midi[1];
hsgw 24:5281ea9f6e68 145 systemExclusive(sysExBuffer, sysExBufferPos, false);
hsgw 24:5281ea9f6e68 146 sysExBufferPos = 0;
hsgw 24:5281ea9f6e68 147 break;
hsgw 24:5281ea9f6e68 148 case 6: // SysEx ends with two bytes
hsgw 24:5281ea9f6e68 149 sysExBuffer[sysExBufferPos++] = midi[1];
hsgw 24:5281ea9f6e68 150 if (sysExBufferPos >= 64) {
hsgw 24:5281ea9f6e68 151 systemExclusive(sysExBuffer, sysExBufferPos, true);
hsgw 24:5281ea9f6e68 152 sysExBufferPos = 0;
hsgw 24:5281ea9f6e68 153 }
hsgw 24:5281ea9f6e68 154 sysExBuffer[sysExBufferPos++] = midi[2];
hsgw 24:5281ea9f6e68 155 systemExclusive(sysExBuffer, sysExBufferPos, false);
hsgw 24:5281ea9f6e68 156 sysExBufferPos = 0;
hsgw 24:5281ea9f6e68 157 break;
hsgw 24:5281ea9f6e68 158 case 7: // SysEx ends with three bytes
hsgw 24:5281ea9f6e68 159 sysExBuffer[sysExBufferPos++] = midi[1];
hsgw 24:5281ea9f6e68 160 if (sysExBufferPos >= 64) {
hsgw 24:5281ea9f6e68 161 systemExclusive(sysExBuffer, sysExBufferPos, true);
hsgw 24:5281ea9f6e68 162 sysExBufferPos = 0;
hsgw 24:5281ea9f6e68 163 }
hsgw 24:5281ea9f6e68 164 sysExBuffer[sysExBufferPos++] = midi[2];
hsgw 24:5281ea9f6e68 165 if (sysExBufferPos >= 64) {
hsgw 24:5281ea9f6e68 166 systemExclusive(sysExBuffer, sysExBufferPos, true);
hsgw 24:5281ea9f6e68 167 sysExBufferPos = 0;
hsgw 24:5281ea9f6e68 168 }
hsgw 24:5281ea9f6e68 169 sysExBuffer[sysExBufferPos++] = midi[3];
hsgw 24:5281ea9f6e68 170 systemExclusive(sysExBuffer, sysExBufferPos, false);
hsgw 24:5281ea9f6e68 171 sysExBufferPos = 0;
hsgw 24:5281ea9f6e68 172 break;
hsgw 24:5281ea9f6e68 173 case 8:
hsgw 24:5281ea9f6e68 174 noteOff(midi[1] & 0xf, midi[2], midi[3]);
hsgw 24:5281ea9f6e68 175 break;
hsgw 24:5281ea9f6e68 176 case 9:
hsgw 24:5281ea9f6e68 177 if (midi[3]) {
hsgw 24:5281ea9f6e68 178 noteOn(midi[1] & 0xf, midi[2], midi[3]);
hsgw 24:5281ea9f6e68 179 } else {
hsgw 24:5281ea9f6e68 180 noteOff(midi[1] & 0xf, midi[2], midi[3]);
hsgw 24:5281ea9f6e68 181 }
hsgw 24:5281ea9f6e68 182 break;
hsgw 24:5281ea9f6e68 183 case 10:
hsgw 24:5281ea9f6e68 184 polyKeyPress(midi[1] & 0xf, midi[2], midi[3]);
hsgw 24:5281ea9f6e68 185 break;
hsgw 24:5281ea9f6e68 186 case 11:
hsgw 24:5281ea9f6e68 187 controlChange(midi[1] & 0xf, midi[2], midi[3]);
hsgw 24:5281ea9f6e68 188 break;
hsgw 24:5281ea9f6e68 189 case 12:
hsgw 24:5281ea9f6e68 190 programChange(midi[1] & 0xf, midi[2]);
hsgw 24:5281ea9f6e68 191 break;
hsgw 24:5281ea9f6e68 192 case 13:
hsgw 24:5281ea9f6e68 193 channelPressure(midi[1] & 0xf, midi[2]);
hsgw 24:5281ea9f6e68 194 break;
hsgw 24:5281ea9f6e68 195 case 14:
hsgw 24:5281ea9f6e68 196 pitchBend(midi[1] & 0xf, midi[2] | (midi[3] << 7));
hsgw 24:5281ea9f6e68 197 break;
hsgw 24:5281ea9f6e68 198 case 15:
hsgw 24:5281ea9f6e68 199 singleByte(midi[1]);
hsgw 24:5281ea9f6e68 200 break;
hsgw 24:5281ea9f6e68 201 default: break;
hsgw 24:5281ea9f6e68 202 }
hsgw 24:5281ea9f6e68 203 }
hsgw 24:5281ea9f6e68 204
hsgw 24:5281ea9f6e68 205 // read another message
hsgw 24:5281ea9f6e68 206 host->bulkRead(dev, bulk_in, buf, size_bulk_in, false);
hsgw 24:5281ea9f6e68 207 }
hsgw 24:5281ea9f6e68 208 }
hsgw 24:5281ea9f6e68 209 }
hsgw 24:5281ea9f6e68 210
hsgw 24:5281ea9f6e68 211 bool USBHostMIDI::sendMidiBuffer(uint8_t data0, uint8_t data1, uint8_t data2, uint8_t data3) {
hsgw 24:5281ea9f6e68 212 if (bulk_out) {
hsgw 24:5281ea9f6e68 213 uint8_t midi[4];
hsgw 24:5281ea9f6e68 214
hsgw 24:5281ea9f6e68 215 midi[0] = data0;
hsgw 24:5281ea9f6e68 216 midi[1] = data1;
hsgw 24:5281ea9f6e68 217 midi[2] = data2;
hsgw 24:5281ea9f6e68 218 midi[3] = data3;
hsgw 24:5281ea9f6e68 219 if (host->bulkWrite(dev, bulk_out, (uint8_t *)midi, 4) == USB_TYPE_OK) {
hsgw 24:5281ea9f6e68 220 return true;
hsgw 24:5281ea9f6e68 221 }
hsgw 24:5281ea9f6e68 222 }
hsgw 24:5281ea9f6e68 223 return false;
hsgw 24:5281ea9f6e68 224 }
hsgw 24:5281ea9f6e68 225
hsgw 24:5281ea9f6e68 226 bool USBHostMIDI::sendMiscellaneousFunctionCode(uint8_t data1, uint8_t data2, uint8_t data3) {
hsgw 24:5281ea9f6e68 227 return sendMidiBuffer(0, data1, data2, data3);
hsgw 24:5281ea9f6e68 228 }
hsgw 24:5281ea9f6e68 229
hsgw 24:5281ea9f6e68 230 bool USBHostMIDI::sendCableEvent(uint8_t data1, uint8_t data2, uint8_t data3) {
hsgw 24:5281ea9f6e68 231 return sendMidiBuffer(1, data1, data2, data3);
hsgw 24:5281ea9f6e68 232 }
hsgw 24:5281ea9f6e68 233
hsgw 24:5281ea9f6e68 234 bool USBHostMIDI::sendSystemCommmonTwoBytes(uint8_t data1, uint8_t data2) {
hsgw 24:5281ea9f6e68 235 return sendMidiBuffer(2, data1, data2, 0);
hsgw 24:5281ea9f6e68 236 }
hsgw 24:5281ea9f6e68 237
hsgw 24:5281ea9f6e68 238 bool USBHostMIDI::sendSystemCommmonThreeBytes(uint8_t data1, uint8_t data2, uint8_t data3) {
hsgw 24:5281ea9f6e68 239 return sendMidiBuffer(3, data1, data2, 0);
hsgw 24:5281ea9f6e68 240 }
hsgw 24:5281ea9f6e68 241
hsgw 24:5281ea9f6e68 242 bool USBHostMIDI::sendSystemExclusive(uint8_t *buffer, int length) {
hsgw 24:5281ea9f6e68 243 uint8_t midi[64];
hsgw 24:5281ea9f6e68 244 int midiLength;
hsgw 24:5281ea9f6e68 245 int midiPos;
hsgw 24:5281ea9f6e68 246 if (bulk_out) {
hsgw 24:5281ea9f6e68 247 for (int i = 0; i < length; i += 48) {
hsgw 24:5281ea9f6e68 248 if (i + 48 >= length) {
hsgw 24:5281ea9f6e68 249 // contains last data
hsgw 24:5281ea9f6e68 250 midiLength = (((length - i) + 2) / 3) * 4;
hsgw 24:5281ea9f6e68 251 for (int pos = i; pos < length; pos += 3) {
hsgw 24:5281ea9f6e68 252 midiPos = (pos + 2) / 3 * 4;
hsgw 24:5281ea9f6e68 253 if (pos + 3 >= length) {
hsgw 24:5281ea9f6e68 254 // last data
hsgw 24:5281ea9f6e68 255 switch (pos % 3) {
hsgw 24:5281ea9f6e68 256 case 0:
hsgw 24:5281ea9f6e68 257 midi[midiPos ] = 7;
hsgw 24:5281ea9f6e68 258 midi[midiPos + 1] = buffer[pos ];
hsgw 24:5281ea9f6e68 259 midi[midiPos + 2] = buffer[pos + 1];
hsgw 24:5281ea9f6e68 260 midi[midiPos + 3] = buffer[pos + 2];
hsgw 24:5281ea9f6e68 261 break;
hsgw 24:5281ea9f6e68 262 case 1:
hsgw 24:5281ea9f6e68 263 midi[midiPos ] = 5;
hsgw 24:5281ea9f6e68 264 midi[midiPos + 1] = buffer[pos ];
hsgw 24:5281ea9f6e68 265 midi[midiPos + 2] = 0;
hsgw 24:5281ea9f6e68 266 midi[midiPos + 3] = 0;
hsgw 24:5281ea9f6e68 267 break;
hsgw 24:5281ea9f6e68 268 case 2:
hsgw 24:5281ea9f6e68 269 midi[midiPos ] = 6;
hsgw 24:5281ea9f6e68 270 midi[midiPos + 1] = buffer[pos ];
hsgw 24:5281ea9f6e68 271 midi[midiPos + 2] = buffer[pos + 1];
hsgw 24:5281ea9f6e68 272 midi[midiPos + 3] = 0;
hsgw 24:5281ea9f6e68 273 break;
hsgw 24:5281ea9f6e68 274 }
hsgw 24:5281ea9f6e68 275 } else {
hsgw 24:5281ea9f6e68 276 // has more data
hsgw 24:5281ea9f6e68 277 midi[midiPos ] = 4;
hsgw 24:5281ea9f6e68 278 midi[midiPos + 1] = buffer[pos ];
hsgw 24:5281ea9f6e68 279 midi[midiPos + 2] = buffer[pos + 1];
hsgw 24:5281ea9f6e68 280 midi[midiPos + 3] = buffer[pos + 2];
hsgw 24:5281ea9f6e68 281 }
hsgw 24:5281ea9f6e68 282 }
hsgw 24:5281ea9f6e68 283 } else {
hsgw 24:5281ea9f6e68 284 // has more data
hsgw 24:5281ea9f6e68 285 midiLength = 64;
hsgw 24:5281ea9f6e68 286 for (int pos = i; pos < length; pos += 3) {
hsgw 24:5281ea9f6e68 287 midiPos = (pos + 2) / 3 * 4;
hsgw 24:5281ea9f6e68 288 midi[midiPos ] = 4;
hsgw 24:5281ea9f6e68 289 midi[midiPos + 1] = buffer[pos ];
hsgw 24:5281ea9f6e68 290 midi[midiPos + 2] = buffer[pos + 1];
hsgw 24:5281ea9f6e68 291 midi[midiPos + 3] = buffer[pos + 2];
hsgw 24:5281ea9f6e68 292 }
hsgw 24:5281ea9f6e68 293 }
hsgw 24:5281ea9f6e68 294
hsgw 24:5281ea9f6e68 295 if (host->bulkWrite(dev, bulk_out, (uint8_t *)midi, midiLength) != USB_TYPE_OK) {
hsgw 24:5281ea9f6e68 296 return false;
hsgw 24:5281ea9f6e68 297 }
hsgw 24:5281ea9f6e68 298 }
hsgw 24:5281ea9f6e68 299 return true;
hsgw 24:5281ea9f6e68 300 }
hsgw 24:5281ea9f6e68 301 return false;
hsgw 24:5281ea9f6e68 302 }
hsgw 24:5281ea9f6e68 303
hsgw 24:5281ea9f6e68 304 bool USBHostMIDI::sendNoteOff(uint8_t channel, uint8_t note, uint8_t velocity) {
hsgw 24:5281ea9f6e68 305 return sendMidiBuffer(8, channel & 0xf | 0x80, note & 0x7f, velocity & 0x7f);
hsgw 24:5281ea9f6e68 306 }
hsgw 24:5281ea9f6e68 307
hsgw 24:5281ea9f6e68 308 bool USBHostMIDI::sendNoteOn(uint8_t channel, uint8_t note, uint8_t velocity) {
hsgw 24:5281ea9f6e68 309 return sendMidiBuffer(9, channel & 0xf | 0x90, note & 0x7f, velocity & 0x7f);
hsgw 24:5281ea9f6e68 310 }
hsgw 24:5281ea9f6e68 311
hsgw 24:5281ea9f6e68 312 bool USBHostMIDI::sendPolyKeyPress(uint8_t channel, uint8_t note, uint8_t pressure) {
hsgw 24:5281ea9f6e68 313 return sendMidiBuffer(10, channel & 0xf | 0xa0, note & 0x7f, pressure & 0x7f);
hsgw 24:5281ea9f6e68 314 }
hsgw 24:5281ea9f6e68 315
hsgw 24:5281ea9f6e68 316 bool USBHostMIDI::sendControlChange(uint8_t channel, uint8_t key, uint8_t value) {
hsgw 24:5281ea9f6e68 317 return sendMidiBuffer(11, channel & 0xf | 0xb0, key & 0x7f, value & 0x7f);
hsgw 24:5281ea9f6e68 318 }
hsgw 24:5281ea9f6e68 319
hsgw 24:5281ea9f6e68 320 bool USBHostMIDI::sendProgramChange(uint8_t channel, uint8_t program) {
hsgw 24:5281ea9f6e68 321 return sendMidiBuffer(12, channel & 0xf | 0xc0, program & 0x7f, 0);
hsgw 24:5281ea9f6e68 322 }
hsgw 24:5281ea9f6e68 323
hsgw 24:5281ea9f6e68 324 bool USBHostMIDI::sendChannelPressure(uint8_t channel, uint8_t pressure) {
hsgw 24:5281ea9f6e68 325 return sendMidiBuffer(13, channel & 0xf | 0xd0, pressure & 0x7f, 0);
hsgw 24:5281ea9f6e68 326 }
hsgw 24:5281ea9f6e68 327
hsgw 24:5281ea9f6e68 328 bool USBHostMIDI::sendPitchBend(uint8_t channel, uint16_t value) {
hsgw 24:5281ea9f6e68 329 return sendMidiBuffer(14, channel & 0xf | 0xe0, value & 0x7f, (value >> 7) & 0x7f);
hsgw 24:5281ea9f6e68 330 }
hsgw 24:5281ea9f6e68 331
hsgw 24:5281ea9f6e68 332 bool USBHostMIDI::sendSingleByte(uint8_t data) {
hsgw 24:5281ea9f6e68 333 return sendMidiBuffer(15, data, 0, 0);
hsgw 24:5281ea9f6e68 334 }
hsgw 24:5281ea9f6e68 335
hsgw 24:5281ea9f6e68 336 /*virtual*/ void USBHostMIDI::setVidPid(uint16_t vid, uint16_t pid)
hsgw 24:5281ea9f6e68 337 {
hsgw 24:5281ea9f6e68 338 // we don't check VID/PID for this driver
hsgw 24:5281ea9f6e68 339 }
hsgw 24:5281ea9f6e68 340
hsgw 24:5281ea9f6e68 341 /*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 342 {
hsgw 24:5281ea9f6e68 343 // USB MIDI class/subclass
hsgw 24:5281ea9f6e68 344 if ((midi_intf == -1) &&
hsgw 24:5281ea9f6e68 345 (intf_class == AUDIO_CLASS) &&
hsgw 24:5281ea9f6e68 346 (intf_subclass == 0x03)) {
hsgw 24:5281ea9f6e68 347 midi_intf = intf_nb;
hsgw 24:5281ea9f6e68 348 return true;
hsgw 24:5281ea9f6e68 349 }
hsgw 24:5281ea9f6e68 350
hsgw 24:5281ea9f6e68 351 // vendor specific device
hsgw 24:5281ea9f6e68 352 if ((midi_intf == -1) &&
hsgw 24:5281ea9f6e68 353 (intf_class == 0xff) &&
hsgw 24:5281ea9f6e68 354 (intf_subclass == 0x03)) {
hsgw 24:5281ea9f6e68 355 midi_intf = intf_nb;
hsgw 24:5281ea9f6e68 356 return true;
hsgw 24:5281ea9f6e68 357 }
hsgw 24:5281ea9f6e68 358
hsgw 24:5281ea9f6e68 359 return false;
hsgw 24:5281ea9f6e68 360 }
hsgw 24:5281ea9f6e68 361
hsgw 24:5281ea9f6e68 362 /*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 363 {
hsgw 24:5281ea9f6e68 364 if (intf_nb == midi_intf) {
hsgw 24:5281ea9f6e68 365 if (type == BULK_ENDPOINT) {
hsgw 24:5281ea9f6e68 366 midi_device_found = true;
hsgw 24:5281ea9f6e68 367 return true;
hsgw 24:5281ea9f6e68 368 }
hsgw 24:5281ea9f6e68 369 }
hsgw 24:5281ea9f6e68 370 return false;
hsgw 24:5281ea9f6e68 371 }
hsgw 24:5281ea9f6e68 372
hsgw 24:5281ea9f6e68 373 #endif