1 /* Copyright (c) 2014 mbed.org, MIT License
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include "USBHostConf.h"
29 * A class to communicate a USB MIDI device
31 class USBHostMIDI
: public IUSBEnumerator
{
39 * Check if a USB MIDI device is connected
41 * @returns true if a midi device is connected
46 * Try to connect a midi device
48 * @return true if connection was successful
53 * Attach a callback called when miscellaneous function code is received
55 * @param ptr function pointer
56 * prototype: void onMiscellaneousFunctionCode(uint8_t data1, uint8_t data2, uint8_t data3);
58 inline void attachMiscellaneousFunctionCode(void (*fn
)(uint8_t, uint8_t, uint8_t)) {
59 miscellaneousFunctionCode
= fn
;
63 * Attach a callback called when cable event is received
65 * @param ptr function pointer
66 * prototype: void onCableEvent(uint8_t data1, uint8_t data2, uint8_t data3);
68 inline void attachCableEvent(void (*fn
)(uint8_t, uint8_t, uint8_t)) {
73 * Attach a callback called when system exclusive is received
75 * @param ptr function pointer
76 * prototype: void onSystemCommonTwoBytes(uint8_t data1, uint8_t data2);
78 inline void attachSystemCommonTwoBytes(void (*fn
)(uint8_t, uint8_t)) {
79 systemCommonTwoBytes
= fn
;
83 * Attach a callback called when system exclusive is received
85 * @param ptr function pointer
86 * prototype: void onSystemCommonThreeBytes(uint8_t data1, uint8_t data2, uint8_t data3);
88 inline void attachSystemCommonThreeBytes(void (*fn
)(uint8_t, uint8_t, uint8_t)) {
89 systemCommonThreeBytes
= fn
;
93 * Attach a callback called when system exclusive is received
95 * @param ptr function pointer
96 * prototype: void onSystemExclusive(uint8_t *data, uint16_t length, bool hasNextData);
98 inline void attachSystemExclusive(void (*fn
)(uint8_t *, uint16_t, bool)) {
103 * Attach a callback called when note on is received
105 * @param ptr function pointer
106 * prototype: void onNoteOn(uint8_t channel, uint8_t note, uint8_t velocity);
108 inline void attachNoteOn(void (*fn
)(uint8_t, uint8_t, uint8_t)) {
113 * Attach a callback called when note off is received
115 * @param ptr function pointer
116 * prototype: void onNoteOff(uint8_t channel, uint8_t note, uint8_t velocity);
118 inline void attachNoteOff(void (*fn
)(uint8_t, uint8_t, uint8_t)) {
123 * Attach a callback called when poly keypress is received
125 * @param ptr function pointer
126 * prototype: void onPolyKeyPress(uint8_t channel, uint8_t note, uint8_t pressure);
128 inline void attachPolyKeyPress(void (*fn
)(uint8_t, uint8_t, uint8_t)) {
133 * Attach a callback called when control change is received
135 * @param ptr function pointer
136 * prototype: void onControlChange(uint8_t channel, uint8_t key, uint8_t value);
138 inline void attachControlChange(void (*fn
)(uint8_t, uint8_t, uint8_t)) {
143 * Attach a callback called when program change is received
145 * @param ptr function pointer
146 * prototype: void onProgramChange(uint8_t channel, uint8_t program);
148 inline void attachProgramChange(void (*fn
)(uint8_t, uint8_t)) {
153 * Attach a callback called when channel pressure is received
155 * @param ptr function pointer
156 * prototype: void onChannelPressure(uint8_t channel, uint8_t pressure);
158 inline void attachChannelPressure(void (*fn
)(uint8_t, uint8_t)) {
159 channelPressure
= fn
;
163 * Attach a callback called when pitch bend is received
165 * @param ptr function pointer
166 * prototype: void onPitchBend(uint8_t channel, uint16_t value);
168 inline void attachPitchBend(void (*fn
)(uint8_t, uint16_t)) {
173 * Attach a callback called when single byte is received
175 * @param ptr function pointer
176 * prototype: void onSingleByte(uint8_t value);
178 inline void attachSingleByte(void (*fn
)(uint8_t)) {
183 * Send a cable event with 3 bytes event
188 * @return true if message sent successfully
190 bool sendMiscellaneousFunctionCode(uint8_t data1
, uint8_t data2
, uint8_t data3
);
193 * Send a cable event with 3 bytes event
198 * @return true if message sent successfully
200 bool sendCableEvent(uint8_t data1
, uint8_t data2
, uint8_t data3
);
203 * Send a system common message with 2 bytes event
207 * @return true if message sent successfully
209 bool sendSystemCommmonTwoBytes(uint8_t data1
, uint8_t data2
);
212 * Send a system common message with 3 bytes event
217 * @return true if message sent successfully
219 bool sendSystemCommmonThreeBytes(uint8_t data1
, uint8_t data2
, uint8_t data3
);
222 * Send a system exclusive event
224 * @param buffer, starts with 0xF0, and end with 0xf7
226 * @return true if message sent successfully
228 bool sendSystemExclusive(uint8_t *buffer
, int length
);
231 * Send a note off event
233 * @param channel 0-15
235 * @param velocity 0-127
236 * @return true if message sent successfully
238 bool sendNoteOff(uint8_t channel
, uint8_t note
, uint8_t velocity
);
241 * Send a note on event
243 * @param channel 0-15
245 * @param velocity 0-127 (0 means note off)
246 * @return true if message sent successfully
248 bool sendNoteOn(uint8_t channel
, uint8_t note
, uint8_t velocity
);
251 * Send a poly keypress event
253 * @param channel 0-15
255 * @param pressure 0-127
256 * @return true if message sent successfully
258 bool sendPolyKeyPress(uint8_t channel
, uint8_t note
, uint8_t pressure
);
261 * Send a control change event
263 * @param channel 0-15
266 * @return true if message sent successfully
268 bool sendControlChange(uint8_t channel
, uint8_t key
, uint8_t value
);
271 * Send a program change event
273 * @param channel 0-15
274 * @param program 0-127
275 * @return true if message sent successfully
277 bool sendProgramChange(uint8_t channel
, uint8_t program
);
280 * Send a channel pressure event
282 * @param channel 0-15
283 * @param pressure 0-127
284 * @return true if message sent successfully
286 bool sendChannelPressure(uint8_t channel
, uint8_t pressure
);
289 * Send a control change event
291 * @param channel 0-15
292 * @param key 0(lower)-8191(center)-16383(higher)
293 * @return true if message sent successfully
295 bool sendPitchBend(uint8_t channel
, uint16_t value
);
298 * Send a single byte event
301 * @return true if message sent successfully
303 bool sendSingleByte(uint8_t data
);
306 //From IUSBEnumerator
307 virtual void setVidPid(uint16_t vid
, uint16_t pid
);
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
309 virtual bool useEndpoint(uint8_t intf_nb
, ENDPOINT_TYPE type
, ENDPOINT_DIRECTION dir
); //Must return true if the endpoint will be used
313 USBDeviceConnected
* dev
;
314 USBEndpoint
* bulk_in
;
315 USBEndpoint
* bulk_out
;
316 uint32_t size_bulk_in
;
317 uint32_t size_bulk_out
;
327 uint16_t sysExBufferPos
;
328 uint8_t sysExBuffer
[64];
330 void (*miscellaneousFunctionCode
)(uint8_t, uint8_t, uint8_t);
331 void (*cableEvent
)(uint8_t, uint8_t, uint8_t);
332 void (*systemCommonTwoBytes
)(uint8_t, uint8_t);
333 void (*systemCommonThreeBytes
)(uint8_t, uint8_t, uint8_t);
334 void (*systemExclusive
)(uint8_t *, uint16_t, bool);
335 void (*noteOff
)(uint8_t, uint8_t, uint8_t);
336 void (*noteOn
)(uint8_t, uint8_t, uint8_t);
337 void (*polyKeyPress
)(uint8_t, uint8_t, uint8_t);
338 void (*controlChange
)(uint8_t, uint8_t, uint8_t);
339 void (*programChange
)(uint8_t, uint8_t);
340 void (*channelPressure
)(uint8_t, uint8_t);
341 void (*pitchBend
)(uint8_t, uint16_t);
342 void (*singleByte
)(uint8_t);
344 bool sendMidiBuffer(uint8_t data0
, uint8_t data1
, uint8_t data2
, uint8_t data3
);
347 bool midi_device_found
;
351 #endif /* USBHOST_MIDI */
353 #endif /* USBHOSTMIDI_H */