Bmag incl gps rettelse

Dependencies:   mbed WDT MODSERIAL BME280

Committer:
gert_lauritsen
Date:
Fri Jul 05 12:55:53 2019 +0000
Branch:
MbedBMAGThrRev
Revision:
61:52a6d0ffd8e5
dato rettel og ID i file lagt ind i version 5

Who changed what in which revision?

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