Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

251 linhas
7.1KB

  1. /* Receive Incoming USB Host MIDI using functions. As usbMIDI
  2. reads incoming messages, handler functions are run.
  3. See the InputRead example for the non-function alterative.
  4. This very long example demonstrates all possible handler
  5. functions. Most applications need only some of these.
  6. This example is meant to allow easy copy-and-paste of the
  7. desired functions.
  8. Use the Arduino Serial Monitor to view the messages
  9. as Teensy receives them by USB MIDI
  10. You must select MIDI from the "Tools > USB Type" menu
  11. This example code is in the public domain.
  12. */
  13. #include <USBHost_t36.h>
  14. USBHost myusb;
  15. USBHub hub1(myusb);
  16. USBHub hub2(myusb);
  17. MIDIDevice midi1(myusb);
  18. void setup() {
  19. Serial.begin(115200);
  20. // Wait 1.5 seconds before turning on USB Host. If connected USB devices
  21. // use too much power, Teensy at least completes USB enumeration, which
  22. // makes isolating the power issue easier.
  23. delay(1500);
  24. Serial.println("USB Host InputFunctions example");
  25. delay(10);
  26. myusb.begin();
  27. midi1.setHandleNoteOn(myNoteOn);
  28. midi1.setHandleNoteOff(myNoteOff);
  29. midi1.setHandleAfterTouchPoly(myAfterTouchPoly);
  30. midi1.setHandleControlChange(myControlChange);
  31. midi1.setHandleProgramChange(myProgramChange);
  32. midi1.setHandleAfterTouchChannel(myAfterTouchChannel);
  33. midi1.setHandlePitchChange(myPitchChange);
  34. // Only one of these System Exclusive handlers will actually be
  35. // used. See the comments below for the difference between them.
  36. midi1.setHandleSystemExclusive(mySystemExclusiveChunk);
  37. midi1.setHandleSystemExclusive(mySystemExclusive);
  38. midi1.setHandleTimeCodeQuarterFrame(myTimeCodeQuarterFrame);
  39. midi1.setHandleSongPosition(mySongPosition);
  40. midi1.setHandleSongSelect(mySongSelect);
  41. midi1.setHandleTuneRequest(myTuneRequest);
  42. midi1.setHandleClock(myClock);
  43. midi1.setHandleStart(myStart);
  44. midi1.setHandleContinue(myContinue);
  45. midi1.setHandleStop(myStop);
  46. midi1.setHandleActiveSensing(myActiveSensing);
  47. midi1.setHandleSystemReset(mySystemReset);
  48. // This generic System Real Time handler is only used if the
  49. // more specific ones are not set.
  50. midi1.setHandleRealTimeSystem(myRealTimeSystem);
  51. }
  52. void loop() {
  53. // The handler functions are called when midi1 reads data. They
  54. // will not be called automatically. You must call midi1.read()
  55. // regularly from loop() for midi1 to actually read incoming
  56. // data and run the handler functions as messages arrive.
  57. myusb.Task();
  58. midi1.read();
  59. }
  60. void myNoteOn(byte channel, byte note, byte velocity) {
  61. // When a USB device with multiple virtual cables is used,
  62. // midi1.getCable() can be used to read which of the virtual
  63. // MIDI cables received this message.
  64. Serial.print("Note On, ch=");
  65. Serial.print(channel, DEC);
  66. Serial.print(", note=");
  67. Serial.print(note, DEC);
  68. Serial.print(", velocity=");
  69. Serial.println(velocity, DEC);
  70. }
  71. void myNoteOff(byte channel, byte note, byte velocity) {
  72. Serial.print("Note Off, ch=");
  73. Serial.print(channel, DEC);
  74. Serial.print(", note=");
  75. Serial.print(note, DEC);
  76. Serial.print(", velocity=");
  77. Serial.println(velocity, DEC);
  78. }
  79. void myAfterTouchPoly(byte channel, byte note, byte velocity) {
  80. Serial.print("AfterTouch Change, ch=");
  81. Serial.print(channel, DEC);
  82. Serial.print(", note=");
  83. Serial.print(note, DEC);
  84. Serial.print(", velocity=");
  85. Serial.println(velocity, DEC);
  86. }
  87. void myControlChange(byte channel, byte control, byte value) {
  88. Serial.print("Control Change, ch=");
  89. Serial.print(channel, DEC);
  90. Serial.print(", control=");
  91. Serial.print(control, DEC);
  92. Serial.print(", value=");
  93. Serial.println(value, DEC);
  94. }
  95. void myProgramChange(byte channel, byte program) {
  96. Serial.print("Program Change, ch=");
  97. Serial.print(channel, DEC);
  98. Serial.print(", program=");
  99. Serial.println(program, DEC);
  100. }
  101. void myAfterTouchChannel(byte channel, byte pressure) {
  102. Serial.print("After Touch, ch=");
  103. Serial.print(channel, DEC);
  104. Serial.print(", pressure=");
  105. Serial.println(pressure, DEC);
  106. }
  107. void myPitchChange(byte channel, int pitch) {
  108. Serial.print("Pitch Change, ch=");
  109. Serial.print(channel, DEC);
  110. Serial.print(", pitch=");
  111. Serial.println(pitch, DEC);
  112. }
  113. // This 3-input System Exclusive function is more complex, but allows you to
  114. // process very large messages which do not fully fit within the midi1's
  115. // internal buffer. Large messages are given to you in chunks, with the
  116. // 3rd parameter to tell you which is the last chunk. This function is
  117. // a Teensy extension, not available in the Arduino MIDI library.
  118. //
  119. void mySystemExclusiveChunk(const byte *data, uint16_t length, bool last) {
  120. Serial.print("SysEx Message: ");
  121. printBytes(data, length);
  122. if (last) {
  123. Serial.println(" (end)");
  124. } else {
  125. Serial.println(" (to be continued)");
  126. }
  127. }
  128. // This simpler 2-input System Exclusive function can only receive messages
  129. // up to the size of the internal buffer. Larger messages are truncated, with
  130. // no way to receive the data which did not fit in the buffer. If both types
  131. // of SysEx functions are set, the 3-input version will be called by midi1.
  132. //
  133. void mySystemExclusive(byte *data, unsigned int length) {
  134. Serial.print("SysEx Message: ");
  135. printBytes(data, length);
  136. Serial.println();
  137. }
  138. void myTimeCodeQuarterFrame(byte data) {
  139. static char SMPTE[8]={'0','0','0','0','0','0','0','0'};
  140. static byte fps=0;
  141. byte index = data >> 4;
  142. byte number = data & 15;
  143. if (index == 7) {
  144. fps = (number >> 1) & 3;
  145. number = number & 1;
  146. }
  147. if (index < 8 || number < 10) {
  148. SMPTE[index] = number + '0';
  149. Serial.print("TimeCode: "); // perhaps only print when index == 7
  150. Serial.print(SMPTE[7]);
  151. Serial.print(SMPTE[6]);
  152. Serial.print(':');
  153. Serial.print(SMPTE[5]);
  154. Serial.print(SMPTE[4]);
  155. Serial.print(':');
  156. Serial.print(SMPTE[3]);
  157. Serial.print(SMPTE[2]);
  158. Serial.print('.');
  159. Serial.print(SMPTE[1]); // perhaps add 2 to compensate for MIDI latency?
  160. Serial.print(SMPTE[0]);
  161. switch (fps) {
  162. case 0: Serial.println(" 24 fps"); break;
  163. case 1: Serial.println(" 25 fps"); break;
  164. case 2: Serial.println(" 29.97 fps"); break;
  165. case 3: Serial.println(" 30 fps"); break;
  166. }
  167. } else {
  168. Serial.print("TimeCode: invalid data = ");
  169. Serial.println(data, HEX);
  170. }
  171. }
  172. void mySongPosition(uint16_t beats) {
  173. Serial.print("Song Position, beat=");
  174. Serial.println(beats);
  175. }
  176. void mySongSelect(byte songNumber) {
  177. Serial.print("Song Select, song=");
  178. Serial.println(songNumber, DEC);
  179. }
  180. void myTuneRequest() {
  181. Serial.println("Tune Request");
  182. }
  183. void myClock() {
  184. Serial.println("Clock");
  185. }
  186. void myStart() {
  187. Serial.println("Start");
  188. }
  189. void myContinue() {
  190. Serial.println("Continue");
  191. }
  192. void myStop() {
  193. Serial.println("Stop");
  194. }
  195. void myActiveSensing() {
  196. Serial.println("Actvice Sensing");
  197. }
  198. void mySystemReset() {
  199. Serial.println("System Reset");
  200. }
  201. void myRealTimeSystem(uint8_t realtimebyte) {
  202. Serial.print("Real Time Message, code=");
  203. Serial.println(realtimebyte, HEX);
  204. }
  205. void printBytes(const byte *data, unsigned int size) {
  206. while (size > 0) {
  207. byte b = *data++;
  208. if (b < 16) Serial.print('0');
  209. Serial.print(b, HEX);
  210. if (size > 1) Serial.print(' ');
  211. size = size - 1;
  212. }
  213. }