Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

USBHostMIDI.h 10KB

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