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.

USBMIDI.cpp 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* Copyright (c) 2010-2011 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. #include "stdint.h"
  19. #include "USBMIDI.h"
  20. USBMIDI::USBMIDI(uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
  21. : USBDevice(vendor_id, product_id, product_release), cur_data(0), data_end(true)
  22. {
  23. midi_evt = NULL;
  24. USBDevice::connect();
  25. }
  26. // write plain MIDIMessage that will be converted to USBMidi event packet
  27. void USBMIDI::write(MIDIMessage m) {
  28. // first byte keeped for retro-compatibility
  29. for(int p=1; p < m.length; p+=3) {
  30. uint8_t buf[4];
  31. // Midi message to USBMidi event packet
  32. buf[0]=m.data[1] >> 4;
  33. // SysEx
  34. if(buf[0] == 0xF) {
  35. if((m.length - p) > 3) {
  36. // SysEx start or continue
  37. buf[0]=0x4;
  38. } else {
  39. switch(m.length - p) {
  40. case 1:
  41. // SysEx end with one byte
  42. buf[0]=0x5;
  43. break;
  44. case 2:
  45. // SysEx end with two bytes
  46. buf[0]=0x6;
  47. break;
  48. case 3:
  49. // SysEx end with three bytes
  50. buf[0]=0x7;
  51. break;
  52. }
  53. }
  54. }
  55. buf[1]=m.data[p];
  56. if(p+1 < m.length)
  57. buf[2]=m.data[p+1];
  58. else
  59. buf[2]=0;
  60. if(p+2 < m.length)
  61. buf[3]=m.data[p+2];
  62. else
  63. buf[3]=0;
  64. USBDevice::write(EPBULK_IN, buf, 4, MAX_PACKET_SIZE_EPBULK);
  65. }
  66. }
  67. void USBMIDI::attach(void (*fptr)(MIDIMessage)) {
  68. midi_evt = fptr;
  69. }
  70. bool USBMIDI::EPBULK_OUT_callback() {
  71. uint8_t buf[64];
  72. uint32_t len;
  73. readEP(EPBULK_OUT, buf, &len, 64);
  74. if (midi_evt != NULL) {
  75. for (uint32_t i=0; i<len; i+=4) {
  76. uint8_t data_read;
  77. data_end=true;
  78. switch(buf[i]) {
  79. case 0x2:
  80. // Two-bytes System Common Message - undefined in USBMidi 1.0
  81. data_read=2;
  82. break;
  83. case 0x4:
  84. // SysEx start or continue
  85. data_end=false;
  86. data_read=3;
  87. break;
  88. case 0x5:
  89. // Single-byte System Common Message or SysEx end with one byte
  90. data_read=1;
  91. break;
  92. case 0x6:
  93. // SysEx end with two bytes
  94. data_read=2;
  95. break;
  96. case 0xC:
  97. // Program change
  98. data_read=2;
  99. break;
  100. case 0xD:
  101. // Channel pressure
  102. data_read=2;
  103. break;
  104. case 0xF:
  105. // Single byte
  106. data_read=1;
  107. break;
  108. default:
  109. // Others three-bytes messages
  110. data_read=3;
  111. break;
  112. }
  113. for(uint8_t j=1;j<data_read+1;j++) {
  114. data[cur_data]=buf[i+j];
  115. cur_data++;
  116. }
  117. if(data_end) {
  118. midi_evt(MIDIMessage(data,cur_data));
  119. cur_data=0;
  120. }
  121. }
  122. }
  123. // We reactivate the endpoint to receive next characters
  124. readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
  125. return true;
  126. }
  127. // Called in ISR context
  128. // Set configuration. Return false if the
  129. // configuration is not supported.
  130. bool USBMIDI::USBCallback_setConfiguration(uint8_t configuration) {
  131. if (configuration != DEFAULT_CONFIGURATION) {
  132. return false;
  133. }
  134. // Configure endpoints > 0
  135. addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
  136. addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
  137. // We activate the endpoint to be able to receive data
  138. readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
  139. return true;
  140. }
  141. uint8_t * USBMIDI::stringIinterfaceDesc() {
  142. static uint8_t stringIinterfaceDescriptor[] = {
  143. 0x0c, //bLength
  144. STRING_DESCRIPTOR, //bDescriptorType 0x03
  145. 'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio
  146. };
  147. return stringIinterfaceDescriptor;
  148. }
  149. uint8_t * USBMIDI::stringIproductDesc() {
  150. static uint8_t stringIproductDescriptor[] = {
  151. 0x16, //bLength
  152. STRING_DESCRIPTOR, //bDescriptorType 0x03
  153. 'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio
  154. };
  155. return stringIproductDescriptor;
  156. }
  157. uint8_t * USBMIDI::configurationDesc() {
  158. static uint8_t configDescriptor[] = {
  159. // configuration descriptor
  160. 0x09, 0x02, 0x65, 0x00, 0x02, 0x01, 0x00, 0xc0, 0x50,
  161. // The Audio Interface Collection
  162. 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, // Standard AC Interface Descriptor
  163. 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, // Class-specific AC Interface Descriptor
  164. 0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, // MIDIStreaming Interface Descriptors
  165. 0x07, 0x24, 0x01, 0x00, 0x01, 0x41, 0x00, // Class-Specific MS Interface Header Descriptor
  166. // MIDI IN JACKS
  167. 0x06, 0x24, 0x02, 0x01, 0x01, 0x00,
  168. 0x06, 0x24, 0x02, 0x02, 0x02, 0x00,
  169. // MIDI OUT JACKS
  170. 0x09, 0x24, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00,
  171. 0x09, 0x24, 0x03, 0x02, 0x06, 0x01, 0x01, 0x01, 0x00,
  172. // OUT endpoint descriptor
  173. 0x09, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
  174. 0x05, 0x25, 0x01, 0x01, 0x01,
  175. // IN endpoint descriptor
  176. 0x09, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
  177. 0x05, 0x25, 0x01, 0x01, 0x03,
  178. };
  179. return configDescriptor;
  180. }