Bmag incl gps rettelse

Dependencies:   mbed WDT MODSERIAL BME280

Committer:
gert_lauritsen
Date:
Thu Jun 20 07:13:18 2019 +0000
Revision:
56:df9052e3808c
change date stamp from gps, reads serialnumb from file.

Who changed what in which revision?

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