]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/USBHost/USBHostMIDI/USBHostMIDI.h
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / USBHost / USBHostMIDI / USBHostMIDI.h
1 /* Copyright (c) 2014 mbed.org, MIT License
2 *
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:
8 *
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
11 *
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.
17 */
18
19 #ifndef USBHOSTMIDI_H
20 #define USBHOSTMIDI_H
21
22 #include "USBHostConf.h"
23
24 #if USBHOST_MIDI
25
26 #include "USBHost.h"
27
28 /**
29 * A class to communicate a USB MIDI device
30 */
31 class USBHostMIDI : public IUSBEnumerator {
32 public:
33 /**
34 * Constructor
35 */
36 USBHostMIDI();
37
38 /**
39 * Check if a USB MIDI device is connected
40 *
41 * @returns true if a midi device is connected
42 */
43 bool connected();
44
45 /**
46 * Try to connect a midi device
47 *
48 * @return true if connection was successful
49 */
50 bool connect();
51
52 /**
53 * Attach a callback called when miscellaneous function code is received
54 *
55 * @param ptr function pointer
56 * prototype: void onMiscellaneousFunctionCode(uint8_t data1, uint8_t data2, uint8_t data3);
57 */
58 inline void attachMiscellaneousFunctionCode(void (*fn)(uint8_t, uint8_t, uint8_t)) {
59 miscellaneousFunctionCode = fn;
60 }
61
62 /**
63 * Attach a callback called when cable event is received
64 *
65 * @param ptr function pointer
66 * prototype: void onCableEvent(uint8_t data1, uint8_t data2, uint8_t data3);
67 */
68 inline void attachCableEvent(void (*fn)(uint8_t, uint8_t, uint8_t)) {
69 cableEvent = fn;
70 }
71
72 /**
73 * Attach a callback called when system exclusive is received
74 *
75 * @param ptr function pointer
76 * prototype: void onSystemCommonTwoBytes(uint8_t data1, uint8_t data2);
77 */
78 inline void attachSystemCommonTwoBytes(void (*fn)(uint8_t, uint8_t)) {
79 systemCommonTwoBytes = fn;
80 }
81
82 /**
83 * Attach a callback called when system exclusive is received
84 *
85 * @param ptr function pointer
86 * prototype: void onSystemCommonThreeBytes(uint8_t data1, uint8_t data2, uint8_t data3);
87 */
88 inline void attachSystemCommonThreeBytes(void (*fn)(uint8_t, uint8_t, uint8_t)) {
89 systemCommonThreeBytes = fn;
90 }
91
92 /**
93 * Attach a callback called when system exclusive is received
94 *
95 * @param ptr function pointer
96 * prototype: void onSystemExclusive(uint8_t *data, uint16_t length, bool hasNextData);
97 */
98 inline void attachSystemExclusive(void (*fn)(uint8_t *, uint16_t, bool)) {
99 systemExclusive = fn;
100 }
101
102 /**
103 * Attach a callback called when note on is received
104 *
105 * @param ptr function pointer
106 * prototype: void onNoteOn(uint8_t channel, uint8_t note, uint8_t velocity);
107 */
108 inline void attachNoteOn(void (*fn)(uint8_t, uint8_t, uint8_t)) {
109 noteOn = fn;
110 }
111
112 /**
113 * Attach a callback called when note off is received
114 *
115 * @param ptr function pointer
116 * prototype: void onNoteOff(uint8_t channel, uint8_t note, uint8_t velocity);
117 */
118 inline void attachNoteOff(void (*fn)(uint8_t, uint8_t, uint8_t)) {
119 noteOff = fn;
120 }
121
122 /**
123 * Attach a callback called when poly keypress is received
124 *
125 * @param ptr function pointer
126 * prototype: void onPolyKeyPress(uint8_t channel, uint8_t note, uint8_t pressure);
127 */
128 inline void attachPolyKeyPress(void (*fn)(uint8_t, uint8_t, uint8_t)) {
129 polyKeyPress = fn;
130 }
131
132 /**
133 * Attach a callback called when control change is received
134 *
135 * @param ptr function pointer
136 * prototype: void onControlChange(uint8_t channel, uint8_t key, uint8_t value);
137 */
138 inline void attachControlChange(void (*fn)(uint8_t, uint8_t, uint8_t)) {
139 controlChange = fn;
140 }
141
142 /**
143 * Attach a callback called when program change is received
144 *
145 * @param ptr function pointer
146 * prototype: void onProgramChange(uint8_t channel, uint8_t program);
147 */
148 inline void attachProgramChange(void (*fn)(uint8_t, uint8_t)) {
149 programChange = fn;
150 }
151
152 /**
153 * Attach a callback called when channel pressure is received
154 *
155 * @param ptr function pointer
156 * prototype: void onChannelPressure(uint8_t channel, uint8_t pressure);
157 */
158 inline void attachChannelPressure(void (*fn)(uint8_t, uint8_t)) {
159 channelPressure = fn;
160 }
161
162 /**
163 * Attach a callback called when pitch bend is received
164 *
165 * @param ptr function pointer
166 * prototype: void onPitchBend(uint8_t channel, uint16_t value);
167 */
168 inline void attachPitchBend(void (*fn)(uint8_t, uint16_t)) {
169 pitchBend = fn;
170 }
171
172 /**
173 * Attach a callback called when single byte is received
174 *
175 * @param ptr function pointer
176 * prototype: void onSingleByte(uint8_t value);
177 */
178 inline void attachSingleByte(void (*fn)(uint8_t)) {
179 singleByte = fn;
180 }
181
182 /**
183 * Send a cable event with 3 bytes event
184 *
185 * @param data1 0-255
186 * @param data2 0-255
187 * @param data3 0-255
188 * @return true if message sent successfully
189 */
190 bool sendMiscellaneousFunctionCode(uint8_t data1, uint8_t data2, uint8_t data3);
191
192 /**
193 * Send a cable event with 3 bytes event
194 *
195 * @param data1 0-255
196 * @param data2 0-255
197 * @param data3 0-255
198 * @return true if message sent successfully
199 */
200 bool sendCableEvent(uint8_t data1, uint8_t data2, uint8_t data3);
201
202 /**
203 * Send a system common message with 2 bytes event
204 *
205 * @param data1 0-255
206 * @param data2 0-255
207 * @return true if message sent successfully
208 */
209 bool sendSystemCommmonTwoBytes(uint8_t data1, uint8_t data2);
210
211 /**
212 * Send a system common message with 3 bytes event
213 *
214 * @param data1 0-255
215 * @param data2 0-255
216 * @param data3 0-255
217 * @return true if message sent successfully
218 */
219 bool sendSystemCommmonThreeBytes(uint8_t data1, uint8_t data2, uint8_t data3);
220
221 /**
222 * Send a system exclusive event
223 *
224 * @param buffer, starts with 0xF0, and end with 0xf7
225 * @param length
226 * @return true if message sent successfully
227 */
228 bool sendSystemExclusive(uint8_t *buffer, int length);
229
230 /**
231 * Send a note off event
232 *
233 * @param channel 0-15
234 * @param note 0-127
235 * @param velocity 0-127
236 * @return true if message sent successfully
237 */
238 bool sendNoteOff(uint8_t channel, uint8_t note, uint8_t velocity);
239
240 /**
241 * Send a note on event
242 *
243 * @param channel 0-15
244 * @param note 0-127
245 * @param velocity 0-127 (0 means note off)
246 * @return true if message sent successfully
247 */
248 bool sendNoteOn(uint8_t channel, uint8_t note, uint8_t velocity);
249
250 /**
251 * Send a poly keypress event
252 *
253 * @param channel 0-15
254 * @param note 0-127
255 * @param pressure 0-127
256 * @return true if message sent successfully
257 */
258 bool sendPolyKeyPress(uint8_t channel, uint8_t note, uint8_t pressure);
259
260 /**
261 * Send a control change event
262 *
263 * @param channel 0-15
264 * @param key 0-127
265 * @param value 0-127
266 * @return true if message sent successfully
267 */
268 bool sendControlChange(uint8_t channel, uint8_t key, uint8_t value);
269
270 /**
271 * Send a program change event
272 *
273 * @param channel 0-15
274 * @param program 0-127
275 * @return true if message sent successfully
276 */
277 bool sendProgramChange(uint8_t channel, uint8_t program);
278
279 /**
280 * Send a channel pressure event
281 *
282 * @param channel 0-15
283 * @param pressure 0-127
284 * @return true if message sent successfully
285 */
286 bool sendChannelPressure(uint8_t channel, uint8_t pressure);
287
288 /**
289 * Send a control change event
290 *
291 * @param channel 0-15
292 * @param key 0(lower)-8191(center)-16383(higher)
293 * @return true if message sent successfully
294 */
295 bool sendPitchBend(uint8_t channel, uint16_t value);
296
297 /**
298 * Send a single byte event
299 *
300 * @param data 0-255
301 * @return true if message sent successfully
302 */
303 bool sendSingleByte(uint8_t data);
304
305 protected:
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
310
311 private:
312 USBHost * host;
313 USBDeviceConnected * dev;
314 USBEndpoint * bulk_in;
315 USBEndpoint * bulk_out;
316 uint32_t size_bulk_in;
317 uint32_t size_bulk_out;
318
319 bool dev_connected;
320
321 void init();
322
323 uint8_t buf[64];
324
325 void rxHandler();
326
327 uint16_t sysExBufferPos;
328 uint8_t sysExBuffer[64];
329
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);
343
344 bool sendMidiBuffer(uint8_t data0, uint8_t data1, uint8_t data2, uint8_t data3);
345
346 int midi_intf;
347 bool midi_device_found;
348
349 };
350
351 #endif /* USBHOST_MIDI */
352
353 #endif /* USBHOSTMIDI_H */
Imprint / Impressum