mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /* Copyright (c) 2014 mbed.org, MIT License
elessair 0:f269e3021894 2 *
elessair 0:f269e3021894 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
elessair 0:f269e3021894 4 * and associated documentation files (the "Software"), to deal in the Software without
elessair 0:f269e3021894 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
elessair 0:f269e3021894 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
elessair 0:f269e3021894 7 * Software is furnished to do so, subject to the following conditions:
elessair 0:f269e3021894 8 *
elessair 0:f269e3021894 9 * The above copyright notice and this permission notice shall be included in all copies or
elessair 0:f269e3021894 10 * substantial portions of the Software.
elessair 0:f269e3021894 11 *
elessair 0:f269e3021894 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
elessair 0:f269e3021894 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
elessair 0:f269e3021894 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
elessair 0:f269e3021894 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
elessair 0:f269e3021894 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
elessair 0:f269e3021894 17 */
elessair 0:f269e3021894 18
elessair 0:f269e3021894 19 #ifndef USBHOSTMIDI_H
elessair 0:f269e3021894 20 #define USBHOSTMIDI_H
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22 #include "USBHostConf.h"
elessair 0:f269e3021894 23
elessair 0:f269e3021894 24 #if USBHOST_MIDI
elessair 0:f269e3021894 25
elessair 0:f269e3021894 26 #include "USBHost.h"
elessair 0:f269e3021894 27
elessair 0:f269e3021894 28 /**
elessair 0:f269e3021894 29 * A class to communicate a USB MIDI device
elessair 0:f269e3021894 30 */
elessair 0:f269e3021894 31 class USBHostMIDI : public IUSBEnumerator {
elessair 0:f269e3021894 32 public:
elessair 0:f269e3021894 33 /**
elessair 0:f269e3021894 34 * Constructor
elessair 0:f269e3021894 35 */
elessair 0:f269e3021894 36 USBHostMIDI();
elessair 0:f269e3021894 37
elessair 0:f269e3021894 38 /**
elessair 0:f269e3021894 39 * Check if a USB MIDI device is connected
elessair 0:f269e3021894 40 *
elessair 0:f269e3021894 41 * @returns true if a midi device is connected
elessair 0:f269e3021894 42 */
elessair 0:f269e3021894 43 bool connected();
elessair 0:f269e3021894 44
elessair 0:f269e3021894 45 /**
elessair 0:f269e3021894 46 * Try to connect a midi device
elessair 0:f269e3021894 47 *
elessair 0:f269e3021894 48 * @return true if connection was successful
elessair 0:f269e3021894 49 */
elessair 0:f269e3021894 50 bool connect();
elessair 0:f269e3021894 51
elessair 0:f269e3021894 52 /**
elessair 0:f269e3021894 53 * Attach a callback called when miscellaneous function code is received
elessair 0:f269e3021894 54 *
elessair 0:f269e3021894 55 * @param ptr function pointer
elessair 0:f269e3021894 56 * prototype: void onMiscellaneousFunctionCode(uint8_t data1, uint8_t data2, uint8_t data3);
elessair 0:f269e3021894 57 */
elessair 0:f269e3021894 58 inline void attachMiscellaneousFunctionCode(void (*fn)(uint8_t, uint8_t, uint8_t)) {
elessair 0:f269e3021894 59 miscellaneousFunctionCode = fn;
elessair 0:f269e3021894 60 }
elessair 0:f269e3021894 61
elessair 0:f269e3021894 62 /**
elessair 0:f269e3021894 63 * Attach a callback called when cable event is received
elessair 0:f269e3021894 64 *
elessair 0:f269e3021894 65 * @param ptr function pointer
elessair 0:f269e3021894 66 * prototype: void onCableEvent(uint8_t data1, uint8_t data2, uint8_t data3);
elessair 0:f269e3021894 67 */
elessair 0:f269e3021894 68 inline void attachCableEvent(void (*fn)(uint8_t, uint8_t, uint8_t)) {
elessair 0:f269e3021894 69 cableEvent = fn;
elessair 0:f269e3021894 70 }
elessair 0:f269e3021894 71
elessair 0:f269e3021894 72 /**
elessair 0:f269e3021894 73 * Attach a callback called when system exclusive is received
elessair 0:f269e3021894 74 *
elessair 0:f269e3021894 75 * @param ptr function pointer
elessair 0:f269e3021894 76 * prototype: void onSystemCommonTwoBytes(uint8_t data1, uint8_t data2);
elessair 0:f269e3021894 77 */
elessair 0:f269e3021894 78 inline void attachSystemCommonTwoBytes(void (*fn)(uint8_t, uint8_t)) {
elessair 0:f269e3021894 79 systemCommonTwoBytes = fn;
elessair 0:f269e3021894 80 }
elessair 0:f269e3021894 81
elessair 0:f269e3021894 82 /**
elessair 0:f269e3021894 83 * Attach a callback called when system exclusive is received
elessair 0:f269e3021894 84 *
elessair 0:f269e3021894 85 * @param ptr function pointer
elessair 0:f269e3021894 86 * prototype: void onSystemCommonThreeBytes(uint8_t data1, uint8_t data2, uint8_t data3);
elessair 0:f269e3021894 87 */
elessair 0:f269e3021894 88 inline void attachSystemCommonThreeBytes(void (*fn)(uint8_t, uint8_t, uint8_t)) {
elessair 0:f269e3021894 89 systemCommonThreeBytes = fn;
elessair 0:f269e3021894 90 }
elessair 0:f269e3021894 91
elessair 0:f269e3021894 92 /**
elessair 0:f269e3021894 93 * Attach a callback called when system exclusive is received
elessair 0:f269e3021894 94 *
elessair 0:f269e3021894 95 * @param ptr function pointer
elessair 0:f269e3021894 96 * prototype: void onSystemExclusive(uint8_t *data, uint16_t length, bool hasNextData);
elessair 0:f269e3021894 97 */
elessair 0:f269e3021894 98 inline void attachSystemExclusive(void (*fn)(uint8_t *, uint16_t, bool)) {
elessair 0:f269e3021894 99 systemExclusive = fn;
elessair 0:f269e3021894 100 }
elessair 0:f269e3021894 101
elessair 0:f269e3021894 102 /**
elessair 0:f269e3021894 103 * Attach a callback called when note on is received
elessair 0:f269e3021894 104 *
elessair 0:f269e3021894 105 * @param ptr function pointer
elessair 0:f269e3021894 106 * prototype: void onNoteOn(uint8_t channel, uint8_t note, uint8_t velocity);
elessair 0:f269e3021894 107 */
elessair 0:f269e3021894 108 inline void attachNoteOn(void (*fn)(uint8_t, uint8_t, uint8_t)) {
elessair 0:f269e3021894 109 noteOn = fn;
elessair 0:f269e3021894 110 }
elessair 0:f269e3021894 111
elessair 0:f269e3021894 112 /**
elessair 0:f269e3021894 113 * Attach a callback called when note off is received
elessair 0:f269e3021894 114 *
elessair 0:f269e3021894 115 * @param ptr function pointer
elessair 0:f269e3021894 116 * prototype: void onNoteOff(uint8_t channel, uint8_t note, uint8_t velocity);
elessair 0:f269e3021894 117 */
elessair 0:f269e3021894 118 inline void attachNoteOff(void (*fn)(uint8_t, uint8_t, uint8_t)) {
elessair 0:f269e3021894 119 noteOff = fn;
elessair 0:f269e3021894 120 }
elessair 0:f269e3021894 121
elessair 0:f269e3021894 122 /**
elessair 0:f269e3021894 123 * Attach a callback called when poly keypress is received
elessair 0:f269e3021894 124 *
elessair 0:f269e3021894 125 * @param ptr function pointer
elessair 0:f269e3021894 126 * prototype: void onPolyKeyPress(uint8_t channel, uint8_t note, uint8_t pressure);
elessair 0:f269e3021894 127 */
elessair 0:f269e3021894 128 inline void attachPolyKeyPress(void (*fn)(uint8_t, uint8_t, uint8_t)) {
elessair 0:f269e3021894 129 polyKeyPress = fn;
elessair 0:f269e3021894 130 }
elessair 0:f269e3021894 131
elessair 0:f269e3021894 132 /**
elessair 0:f269e3021894 133 * Attach a callback called when control change is received
elessair 0:f269e3021894 134 *
elessair 0:f269e3021894 135 * @param ptr function pointer
elessair 0:f269e3021894 136 * prototype: void onControlChange(uint8_t channel, uint8_t key, uint8_t value);
elessair 0:f269e3021894 137 */
elessair 0:f269e3021894 138 inline void attachControlChange(void (*fn)(uint8_t, uint8_t, uint8_t)) {
elessair 0:f269e3021894 139 controlChange = fn;
elessair 0:f269e3021894 140 }
elessair 0:f269e3021894 141
elessair 0:f269e3021894 142 /**
elessair 0:f269e3021894 143 * Attach a callback called when program change is received
elessair 0:f269e3021894 144 *
elessair 0:f269e3021894 145 * @param ptr function pointer
elessair 0:f269e3021894 146 * prototype: void onProgramChange(uint8_t channel, uint8_t program);
elessair 0:f269e3021894 147 */
elessair 0:f269e3021894 148 inline void attachProgramChange(void (*fn)(uint8_t, uint8_t)) {
elessair 0:f269e3021894 149 programChange = fn;
elessair 0:f269e3021894 150 }
elessair 0:f269e3021894 151
elessair 0:f269e3021894 152 /**
elessair 0:f269e3021894 153 * Attach a callback called when channel pressure is received
elessair 0:f269e3021894 154 *
elessair 0:f269e3021894 155 * @param ptr function pointer
elessair 0:f269e3021894 156 * prototype: void onChannelPressure(uint8_t channel, uint8_t pressure);
elessair 0:f269e3021894 157 */
elessair 0:f269e3021894 158 inline void attachChannelPressure(void (*fn)(uint8_t, uint8_t)) {
elessair 0:f269e3021894 159 channelPressure = fn;
elessair 0:f269e3021894 160 }
elessair 0:f269e3021894 161
elessair 0:f269e3021894 162 /**
elessair 0:f269e3021894 163 * Attach a callback called when pitch bend is received
elessair 0:f269e3021894 164 *
elessair 0:f269e3021894 165 * @param ptr function pointer
elessair 0:f269e3021894 166 * prototype: void onPitchBend(uint8_t channel, uint16_t value);
elessair 0:f269e3021894 167 */
elessair 0:f269e3021894 168 inline void attachPitchBend(void (*fn)(uint8_t, uint16_t)) {
elessair 0:f269e3021894 169 pitchBend = fn;
elessair 0:f269e3021894 170 }
elessair 0:f269e3021894 171
elessair 0:f269e3021894 172 /**
elessair 0:f269e3021894 173 * Attach a callback called when single byte is received
elessair 0:f269e3021894 174 *
elessair 0:f269e3021894 175 * @param ptr function pointer
elessair 0:f269e3021894 176 * prototype: void onSingleByte(uint8_t value);
elessair 0:f269e3021894 177 */
elessair 0:f269e3021894 178 inline void attachSingleByte(void (*fn)(uint8_t)) {
elessair 0:f269e3021894 179 singleByte = fn;
elessair 0:f269e3021894 180 }
elessair 0:f269e3021894 181
elessair 0:f269e3021894 182 /**
elessair 0:f269e3021894 183 * Send a cable event with 3 bytes event
elessair 0:f269e3021894 184 *
elessair 0:f269e3021894 185 * @param data1 0-255
elessair 0:f269e3021894 186 * @param data2 0-255
elessair 0:f269e3021894 187 * @param data3 0-255
elessair 0:f269e3021894 188 * @return true if message sent successfully
elessair 0:f269e3021894 189 */
elessair 0:f269e3021894 190 bool sendMiscellaneousFunctionCode(uint8_t data1, uint8_t data2, uint8_t data3);
elessair 0:f269e3021894 191
elessair 0:f269e3021894 192 /**
elessair 0:f269e3021894 193 * Send a cable event with 3 bytes event
elessair 0:f269e3021894 194 *
elessair 0:f269e3021894 195 * @param data1 0-255
elessair 0:f269e3021894 196 * @param data2 0-255
elessair 0:f269e3021894 197 * @param data3 0-255
elessair 0:f269e3021894 198 * @return true if message sent successfully
elessair 0:f269e3021894 199 */
elessair 0:f269e3021894 200 bool sendCableEvent(uint8_t data1, uint8_t data2, uint8_t data3);
elessair 0:f269e3021894 201
elessair 0:f269e3021894 202 /**
elessair 0:f269e3021894 203 * Send a system common message with 2 bytes event
elessair 0:f269e3021894 204 *
elessair 0:f269e3021894 205 * @param data1 0-255
elessair 0:f269e3021894 206 * @param data2 0-255
elessair 0:f269e3021894 207 * @return true if message sent successfully
elessair 0:f269e3021894 208 */
elessair 0:f269e3021894 209 bool sendSystemCommmonTwoBytes(uint8_t data1, uint8_t data2);
elessair 0:f269e3021894 210
elessair 0:f269e3021894 211 /**
elessair 0:f269e3021894 212 * Send a system common message with 3 bytes event
elessair 0:f269e3021894 213 *
elessair 0:f269e3021894 214 * @param data1 0-255
elessair 0:f269e3021894 215 * @param data2 0-255
elessair 0:f269e3021894 216 * @param data3 0-255
elessair 0:f269e3021894 217 * @return true if message sent successfully
elessair 0:f269e3021894 218 */
elessair 0:f269e3021894 219 bool sendSystemCommmonThreeBytes(uint8_t data1, uint8_t data2, uint8_t data3);
elessair 0:f269e3021894 220
elessair 0:f269e3021894 221 /**
elessair 0:f269e3021894 222 * Send a system exclusive event
elessair 0:f269e3021894 223 *
elessair 0:f269e3021894 224 * @param buffer, starts with 0xF0, and end with 0xf7
elessair 0:f269e3021894 225 * @param length
elessair 0:f269e3021894 226 * @return true if message sent successfully
elessair 0:f269e3021894 227 */
elessair 0:f269e3021894 228 bool sendSystemExclusive(uint8_t *buffer, int length);
elessair 0:f269e3021894 229
elessair 0:f269e3021894 230 /**
elessair 0:f269e3021894 231 * Send a note off event
elessair 0:f269e3021894 232 *
elessair 0:f269e3021894 233 * @param channel 0-15
elessair 0:f269e3021894 234 * @param note 0-127
elessair 0:f269e3021894 235 * @param velocity 0-127
elessair 0:f269e3021894 236 * @return true if message sent successfully
elessair 0:f269e3021894 237 */
elessair 0:f269e3021894 238 bool sendNoteOff(uint8_t channel, uint8_t note, uint8_t velocity);
elessair 0:f269e3021894 239
elessair 0:f269e3021894 240 /**
elessair 0:f269e3021894 241 * Send a note on event
elessair 0:f269e3021894 242 *
elessair 0:f269e3021894 243 * @param channel 0-15
elessair 0:f269e3021894 244 * @param note 0-127
elessair 0:f269e3021894 245 * @param velocity 0-127 (0 means note off)
elessair 0:f269e3021894 246 * @return true if message sent successfully
elessair 0:f269e3021894 247 */
elessair 0:f269e3021894 248 bool sendNoteOn(uint8_t channel, uint8_t note, uint8_t velocity);
elessair 0:f269e3021894 249
elessair 0:f269e3021894 250 /**
elessair 0:f269e3021894 251 * Send a poly keypress event
elessair 0:f269e3021894 252 *
elessair 0:f269e3021894 253 * @param channel 0-15
elessair 0:f269e3021894 254 * @param note 0-127
elessair 0:f269e3021894 255 * @param pressure 0-127
elessair 0:f269e3021894 256 * @return true if message sent successfully
elessair 0:f269e3021894 257 */
elessair 0:f269e3021894 258 bool sendPolyKeyPress(uint8_t channel, uint8_t note, uint8_t pressure);
elessair 0:f269e3021894 259
elessair 0:f269e3021894 260 /**
elessair 0:f269e3021894 261 * Send a control change event
elessair 0:f269e3021894 262 *
elessair 0:f269e3021894 263 * @param channel 0-15
elessair 0:f269e3021894 264 * @param key 0-127
elessair 0:f269e3021894 265 * @param value 0-127
elessair 0:f269e3021894 266 * @return true if message sent successfully
elessair 0:f269e3021894 267 */
elessair 0:f269e3021894 268 bool sendControlChange(uint8_t channel, uint8_t key, uint8_t value);
elessair 0:f269e3021894 269
elessair 0:f269e3021894 270 /**
elessair 0:f269e3021894 271 * Send a program change event
elessair 0:f269e3021894 272 *
elessair 0:f269e3021894 273 * @param channel 0-15
elessair 0:f269e3021894 274 * @param program 0-127
elessair 0:f269e3021894 275 * @return true if message sent successfully
elessair 0:f269e3021894 276 */
elessair 0:f269e3021894 277 bool sendProgramChange(uint8_t channel, uint8_t program);
elessair 0:f269e3021894 278
elessair 0:f269e3021894 279 /**
elessair 0:f269e3021894 280 * Send a channel pressure event
elessair 0:f269e3021894 281 *
elessair 0:f269e3021894 282 * @param channel 0-15
elessair 0:f269e3021894 283 * @param pressure 0-127
elessair 0:f269e3021894 284 * @return true if message sent successfully
elessair 0:f269e3021894 285 */
elessair 0:f269e3021894 286 bool sendChannelPressure(uint8_t channel, uint8_t pressure);
elessair 0:f269e3021894 287
elessair 0:f269e3021894 288 /**
elessair 0:f269e3021894 289 * Send a control change event
elessair 0:f269e3021894 290 *
elessair 0:f269e3021894 291 * @param channel 0-15
elessair 0:f269e3021894 292 * @param key 0(lower)-8191(center)-16383(higher)
elessair 0:f269e3021894 293 * @return true if message sent successfully
elessair 0:f269e3021894 294 */
elessair 0:f269e3021894 295 bool sendPitchBend(uint8_t channel, uint16_t value);
elessair 0:f269e3021894 296
elessair 0:f269e3021894 297 /**
elessair 0:f269e3021894 298 * Send a single byte event
elessair 0:f269e3021894 299 *
elessair 0:f269e3021894 300 * @param data 0-255
elessair 0:f269e3021894 301 * @return true if message sent successfully
elessair 0:f269e3021894 302 */
elessair 0:f269e3021894 303 bool sendSingleByte(uint8_t data);
elessair 0:f269e3021894 304
elessair 0:f269e3021894 305 protected:
elessair 0:f269e3021894 306 //From IUSBEnumerator
elessair 0:f269e3021894 307 virtual void setVidPid(uint16_t vid, uint16_t pid);
elessair 0:f269e3021894 308 virtual bool 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
elessair 0:f269e3021894 309 virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used
elessair 0:f269e3021894 310
elessair 0:f269e3021894 311 private:
elessair 0:f269e3021894 312 USBHost * host;
elessair 0:f269e3021894 313 USBDeviceConnected * dev;
elessair 0:f269e3021894 314 USBEndpoint * bulk_in;
elessair 0:f269e3021894 315 USBEndpoint * bulk_out;
elessair 0:f269e3021894 316 uint32_t size_bulk_in;
elessair 0:f269e3021894 317 uint32_t size_bulk_out;
elessair 0:f269e3021894 318
elessair 0:f269e3021894 319 bool dev_connected;
elessair 0:f269e3021894 320
elessair 0:f269e3021894 321 void init();
elessair 0:f269e3021894 322
elessair 0:f269e3021894 323 uint8_t buf[64];
elessair 0:f269e3021894 324
elessair 0:f269e3021894 325 void rxHandler();
elessair 0:f269e3021894 326
elessair 0:f269e3021894 327 uint16_t sysExBufferPos;
elessair 0:f269e3021894 328 uint8_t sysExBuffer[64];
elessair 0:f269e3021894 329
elessair 0:f269e3021894 330 void (*miscellaneousFunctionCode)(uint8_t, uint8_t, uint8_t);
elessair 0:f269e3021894 331 void (*cableEvent)(uint8_t, uint8_t, uint8_t);
elessair 0:f269e3021894 332 void (*systemCommonTwoBytes)(uint8_t, uint8_t);
elessair 0:f269e3021894 333 void (*systemCommonThreeBytes)(uint8_t, uint8_t, uint8_t);
elessair 0:f269e3021894 334 void (*systemExclusive)(uint8_t *, uint16_t, bool);
elessair 0:f269e3021894 335 void (*noteOff)(uint8_t, uint8_t, uint8_t);
elessair 0:f269e3021894 336 void (*noteOn)(uint8_t, uint8_t, uint8_t);
elessair 0:f269e3021894 337 void (*polyKeyPress)(uint8_t, uint8_t, uint8_t);
elessair 0:f269e3021894 338 void (*controlChange)(uint8_t, uint8_t, uint8_t);
elessair 0:f269e3021894 339 void (*programChange)(uint8_t, uint8_t);
elessair 0:f269e3021894 340 void (*channelPressure)(uint8_t, uint8_t);
elessair 0:f269e3021894 341 void (*pitchBend)(uint8_t, uint16_t);
elessair 0:f269e3021894 342 void (*singleByte)(uint8_t);
elessair 0:f269e3021894 343
elessair 0:f269e3021894 344 bool sendMidiBuffer(uint8_t data0, uint8_t data1, uint8_t data2, uint8_t data3);
elessair 0:f269e3021894 345
elessair 0:f269e3021894 346 int midi_intf;
elessair 0:f269e3021894 347 bool midi_device_found;
elessair 0:f269e3021894 348
elessair 0:f269e3021894 349 };
elessair 0:f269e3021894 350
elessair 0:f269e3021894 351 #endif /* USBHOST_MIDI */
elessair 0:f269e3021894 352
elessair 0:f269e3021894 353 #endif /* USBHOSTMIDI_H */