Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: F401RE-USBHostMIDI_RecieveExample
Fork of F401RE-USBHost by
USBHostMIDI.h
00001 /* USBHostMidi library 00002 * Originaled by k.shoji 00003 * porting by Takuya Urakawa 00004 */ 00005 00006 /* mbed USBHost Library 00007 * Copyright (c) 2006-2013 ARM Limited 00008 * 00009 * Licensed under the Apache License, Version 2.0 (the "License"); 00010 * you may not use this file except in compliance with the License. 00011 * You may obtain a copy of the License at 00012 * 00013 * http://www.apache.org/licenses/LICENSE-2.0 00014 * 00015 * Unless required by applicable law or agreed to in writing, software 00016 * distributed under the License is distributed on an "AS IS" BASIS, 00017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00018 * See the License for the specific language governing permissions and 00019 * limitations under the License. 00020 */ 00021 00022 #ifndef USBHOSTMIDI_H 00023 #define USBHOSTMIDI_H 00024 00025 #include "USBHostConf.h" 00026 00027 #if USBHOST_MIDI 00028 00029 #include "USBHost.h" 00030 00031 /** 00032 * A class to communicate a USB MIDI device 00033 */ 00034 class USBHostMIDI : public IUSBEnumerator { 00035 public: 00036 /** 00037 * Constructor 00038 */ 00039 USBHostMIDI(); 00040 00041 /** 00042 * Check if a USB MIDI device is connected 00043 * 00044 * @returns true if a midi device is connected 00045 */ 00046 bool connected(); 00047 00048 /** 00049 * Try to connect a midi device 00050 * 00051 * @return true if connection was successful 00052 */ 00053 bool connect(); 00054 00055 /** 00056 * Attach a callback called when miscellaneous function code is received 00057 * @warning DISABLED 00058 * 00059 * @param ptr function pointer 00060 * prototype: void onMiscellaneousFunctionCode(uint8_t data1, uint8_t data2, uint8_t data3); 00061 */ 00062 inline void attachMiscellaneousFunctionCode(void (*fn)(uint8_t, uint8_t, uint8_t)) { 00063 miscellaneousFunctionCode = fn; 00064 } 00065 00066 /** 00067 * Attach a callback called when cable event is received 00068 * @warning DISABLED 00069 * 00070 * @param ptr function pointer 00071 * prototype: void onCableEvent(uint8_t data1, uint8_t data2, uint8_t data3); 00072 */ 00073 inline void attachCableEvent(void (*fn)(uint8_t, uint8_t, uint8_t)) { 00074 cableEvent = fn; 00075 } 00076 00077 /** 00078 * Attach a callback called when system exclusive is received 00079 * 00080 * @param ptr function pointer 00081 * prototype: void onSystemCommonTwoBytes(uint8_t data1, uint8_t data2); 00082 */ 00083 inline void attachSystemCommonTwoBytes(void (*fn)(uint8_t, uint8_t)) { 00084 systemCommonTwoBytes = fn; 00085 } 00086 00087 /** 00088 * Attach a callback called when system exclusive is received 00089 * 00090 * @param ptr function pointer 00091 * prototype: void onSystemCommonThreeBytes(uint8_t data1, uint8_t data2, uint8_t data3); 00092 */ 00093 inline void attachSystemCommonThreeBytes(void (*fn)(uint8_t, uint8_t, uint8_t)) { 00094 systemCommonThreeBytes = fn; 00095 } 00096 00097 /** 00098 * Attach a callback called when system exclusive is received 00099 * 00100 * @param ptr function pointer 00101 * prototype: void onSystemExclusive(uint8_t *data, uint16_t length, bool hasNextData); 00102 */ 00103 inline void attachSystemExclusive(void (*fn)(uint8_t *, uint16_t, bool)) { 00104 systemExclusive = fn; 00105 } 00106 00107 /** 00108 * Attach a callback called when note on is received 00109 * 00110 * @param ptr function pointer 00111 * prototype: void onNoteOn(uint8_t channel, uint8_t note, uint8_t velocity); 00112 */ 00113 inline void attachNoteOn(void (*fn)(uint8_t, uint8_t, uint8_t)) { 00114 noteOn = fn; 00115 } 00116 00117 /** 00118 * Attach a callback called when note off is received 00119 * 00120 * @param ptr function pointer 00121 * prototype: void onNoteOff(uint8_t channel, uint8_t note, uint8_t velocity); 00122 */ 00123 inline void attachNoteOff(void (*fn)(uint8_t, uint8_t, uint8_t)) { 00124 noteOff = fn; 00125 } 00126 00127 /** 00128 * Attach a callback called when poly keypress is received 00129 * 00130 * @param ptr function pointer 00131 * prototype: void onPolyKeyPress(uint8_t channel, uint8_t note, uint8_t pressure); 00132 */ 00133 inline void attachPolyKeyPress(void (*fn)(uint8_t, uint8_t, uint8_t)) { 00134 polyKeyPress = fn; 00135 } 00136 00137 /** 00138 * Attach a callback called when control change is received 00139 * 00140 * @param ptr function pointer 00141 * prototype: void onControlChange(uint8_t channel, uint8_t key, uint8_t value); 00142 */ 00143 inline void attachControlChange(void (*fn)(uint8_t, uint8_t, uint8_t)) { 00144 controlChange = fn; 00145 } 00146 00147 /** 00148 * Attach a callback called when program change is received 00149 * 00150 * @param ptr function pointer 00151 * prototype: void onProgramChange(uint8_t channel, uint8_t program); 00152 */ 00153 inline void attachProgramChange(void (*fn)(uint8_t, uint8_t)) { 00154 programChange = fn; 00155 } 00156 00157 /** 00158 * Attach a callback called when channel pressure is received 00159 * 00160 * @param ptr function pointer 00161 * prototype: void onChannelPressure(uint8_t channel, uint8_t pressure); 00162 */ 00163 inline void attachChannelPressure(void (*fn)(uint8_t, uint8_t)) { 00164 channelPressure = fn; 00165 } 00166 00167 /** 00168 * Attach a callback called when pitch bend is received 00169 * 00170 * @param ptr function pointer 00171 * prototype: void onPitchBend(uint8_t channel, uint16_t value); 00172 */ 00173 inline void attachPitchBend(void (*fn)(uint8_t, uint16_t)) { 00174 pitchBend = fn; 00175 } 00176 00177 /** 00178 * Attach a callback called when single byte is received 00179 * 00180 * @param ptr function pointer 00181 * prototype: void onSingleByte(uint8_t value); 00182 */ 00183 inline void attachSingleByte(void (*fn)(uint8_t)) { 00184 singleByte = fn; 00185 } 00186 00187 /** 00188 * Send a cable event with 3 bytes event 00189 * 00190 * @param data1 0-255 00191 * @param data2 0-255 00192 * @param data3 0-255 00193 * @return true if message sent successfully 00194 */ 00195 bool sendMiscellaneousFunctionCode(uint8_t data1, uint8_t data2, uint8_t data3); 00196 00197 /** 00198 * Send a cable event with 3 bytes event 00199 * 00200 * @param data1 0-255 00201 * @param data2 0-255 00202 * @param data3 0-255 00203 * @return true if message sent successfully 00204 */ 00205 bool sendCableEvent(uint8_t data1, uint8_t data2, uint8_t data3); 00206 00207 /** 00208 * Send a system common message with 2 bytes event 00209 * 00210 * @param data1 0-255 00211 * @param data2 0-255 00212 * @return true if message sent successfully 00213 */ 00214 bool sendSystemCommmonTwoBytes(uint8_t data1, uint8_t data2); 00215 00216 /** 00217 * Send a system common message with 3 bytes event 00218 * 00219 * @param data1 0-255 00220 * @param data2 0-255 00221 * @param data3 0-255 00222 * @return true if message sent successfully 00223 */ 00224 bool sendSystemCommmonThreeBytes(uint8_t data1, uint8_t data2, uint8_t data3); 00225 00226 /** 00227 * Send a system exclusive event 00228 * 00229 * @param buffer, starts with 0xF0, and end with 0xf7 00230 * @param length 00231 * @return true if message sent successfully 00232 */ 00233 bool sendSystemExclusive(uint8_t *buffer, int length); 00234 00235 /** 00236 * Send a note off event 00237 * 00238 * @param channel 0-15 00239 * @param note 0-127 00240 * @param velocity 0-127 00241 * @return true if message sent successfully 00242 */ 00243 bool sendNoteOff(uint8_t channel, uint8_t note, uint8_t velocity); 00244 00245 /** 00246 * Send a note on event 00247 * 00248 * @param channel 0-15 00249 * @param note 0-127 00250 * @param velocity 0-127 (0 means note off) 00251 * @return true if message sent successfully 00252 */ 00253 bool sendNoteOn(uint8_t channel, uint8_t note, uint8_t velocity); 00254 00255 /** 00256 * Send a poly keypress event 00257 * 00258 * @param channel 0-15 00259 * @param note 0-127 00260 * @param pressure 0-127 00261 * @return true if message sent successfully 00262 */ 00263 bool sendPolyKeyPress(uint8_t channel, uint8_t note, uint8_t pressure); 00264 00265 /** 00266 * Send a control change event 00267 * 00268 * @param channel 0-15 00269 * @param key 0-127 00270 * @param value 0-127 00271 * @return true if message sent successfully 00272 */ 00273 bool sendControlChange(uint8_t channel, uint8_t key, uint8_t value); 00274 00275 /** 00276 * Send a program change event 00277 * 00278 * @param channel 0-15 00279 * @param program 0-127 00280 * @return true if message sent successfully 00281 */ 00282 bool sendProgramChange(uint8_t channel, uint8_t program); 00283 00284 /** 00285 * Send a channel pressure event 00286 * 00287 * @param channel 0-15 00288 * @param pressure 0-127 00289 * @return true if message sent successfully 00290 */ 00291 bool sendChannelPressure(uint8_t channel, uint8_t pressure); 00292 00293 /** 00294 * Send a control change event 00295 * 00296 * @param channel 0-15 00297 * @param key 0(lower)-8191(center)-16383(higher) 00298 * @return true if message sent successfully 00299 */ 00300 bool sendPitchBend(uint8_t channel, uint16_t value); 00301 00302 /** 00303 * Send a single byte event 00304 * 00305 * @param data 0-255 00306 * @return true if message sent successfully 00307 */ 00308 bool sendSingleByte(uint8_t data); 00309 00310 protected: 00311 //From IUSBEnumerator 00312 virtual void setVidPid(uint16_t vid, uint16_t pid); 00313 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 00314 virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used 00315 00316 private: 00317 USBHost * host; 00318 USBDeviceConnected * dev; 00319 USBEndpoint * bulk_in; 00320 USBEndpoint * bulk_out; 00321 uint32_t size_bulk_in; 00322 uint32_t size_bulk_out; 00323 00324 bool dev_connected; 00325 00326 void init(); 00327 00328 uint8_t buf[64]; 00329 00330 void rxHandler(); 00331 00332 uint16_t sysExBufferPos; 00333 uint8_t sysExBuffer[64]; 00334 00335 void (*miscellaneousFunctionCode)(uint8_t, uint8_t, uint8_t); 00336 void (*cableEvent)(uint8_t, uint8_t, uint8_t); 00337 void (*systemCommonTwoBytes)(uint8_t, uint8_t); 00338 void (*systemCommonThreeBytes)(uint8_t, uint8_t, uint8_t); 00339 void (*systemExclusive)(uint8_t *, uint16_t, bool); 00340 void (*noteOff)(uint8_t, uint8_t, uint8_t); 00341 void (*noteOn)(uint8_t, uint8_t, uint8_t); 00342 void (*polyKeyPress)(uint8_t, uint8_t, uint8_t); 00343 void (*controlChange)(uint8_t, uint8_t, uint8_t); 00344 void (*programChange)(uint8_t, uint8_t); 00345 void (*channelPressure)(uint8_t, uint8_t); 00346 void (*pitchBend)(uint8_t, uint16_t); 00347 void (*singleByte)(uint8_t); 00348 00349 bool sendMidiBuffer(uint8_t data0, uint8_t data1, uint8_t data2, uint8_t data3); 00350 00351 // dummy callback function 00352 static void callbackDummysingleByte(uint8_t a) { 00353 USB_INFO("Not attached command comming! First byte %d\n", a); 00354 return; 00355 } 00356 00357 static void callbackDummy2Bytes(uint8_t a, uint8_t b){ 00358 callbackDummysingleByte(a); 00359 } 00360 00361 static void callbackDummy3Bytes(uint8_t a, uint8_t b, uint8_t c){ 00362 callbackDummysingleByte(a); 00363 } 00364 00365 static void callbackDummysystemExclusive(uint8_t *a, uint16_t b, bool c){ 00366 callbackDummysingleByte(a[0]); 00367 } 00368 static void callbackDummypitchBend(uint8_t a, uint16_t b){ 00369 callbackDummysingleByte(a); 00370 } 00371 00372 int midi_intf; 00373 bool midi_device_found; 00374 00375 }; 00376 00377 #endif 00378 00379 #endif
Generated on Thu Jul 14 2022 17:06:48 by
1.7.2
