main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "bsp/board.h"
  29. #include "tusb.h"
  30. /* This MIDI example send sequence of note (on/off) repeatedly. To test on PC, you need to install
  31. * synth software and midi connection management software. On
  32. * - Linux (Ubuntu): install qsynth, qjackctl. Then connect TinyUSB output port to FLUID Synth input port
  33. * - Windows: install MIDI-OX
  34. * - MacOS: SimpleSynth
  35. */
  36. //--------------------------------------------------------------------+
  37. // MACRO CONSTANT TYPEDEF PROTYPES
  38. //--------------------------------------------------------------------+
  39. /* Blink pattern
  40. * - 250 ms : device not mounted
  41. * - 1000 ms : device mounted
  42. * - 2500 ms : device is suspended
  43. */
  44. enum {
  45. BLINK_NOT_MOUNTED = 250,
  46. BLINK_MOUNTED = 1000,
  47. BLINK_SUSPENDED = 2500,
  48. };
  49. static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
  50. void led_blinking_task(void);
  51. void midi_task(void);
  52. /*------------- MAIN -------------*/
  53. int main(void)
  54. {
  55. board_init();
  56. // init device stack on configured roothub port
  57. tud_init(BOARD_TUD_RHPORT);
  58. while (1)
  59. {
  60. tud_task(); // tinyusb device task
  61. led_blinking_task();
  62. midi_task();
  63. }
  64. return 0;
  65. }
  66. //--------------------------------------------------------------------+
  67. // Device callbacks
  68. //--------------------------------------------------------------------+
  69. // Invoked when device is mounted
  70. void tud_mount_cb(void)
  71. {
  72. blink_interval_ms = BLINK_MOUNTED;
  73. }
  74. // Invoked when device is unmounted
  75. void tud_umount_cb(void)
  76. {
  77. blink_interval_ms = BLINK_NOT_MOUNTED;
  78. }
  79. // Invoked when usb bus is suspended
  80. // remote_wakeup_en : if host allow us to perform remote wakeup
  81. // Within 7ms, device must draw an average of current less than 2.5 mA from bus
  82. void tud_suspend_cb(bool remote_wakeup_en)
  83. {
  84. (void) remote_wakeup_en;
  85. blink_interval_ms = BLINK_SUSPENDED;
  86. }
  87. // Invoked when usb bus is resumed
  88. void tud_resume_cb(void)
  89. {
  90. blink_interval_ms = BLINK_MOUNTED;
  91. }
  92. //--------------------------------------------------------------------+
  93. // MIDI Task
  94. //--------------------------------------------------------------------+
  95. // Variable that holds the current position in the sequence.
  96. uint32_t note_pos = 0;
  97. // Store example melody as an array of note values
  98. uint8_t note_sequence[] =
  99. {
  100. 74,78,81,86,90,93,98,102,57,61,66,69,73,78,81,85,88,92,97,100,97,92,88,85,81,78,
  101. 74,69,66,62,57,62,66,69,74,78,81,86,90,93,97,102,97,93,90,85,81,78,73,68,64,61,
  102. 56,61,64,68,74,78,81,86,90,93,98,102
  103. };
  104. void midi_task(void)
  105. {
  106. static uint32_t start_ms = 0;
  107. uint8_t const cable_num = 0; // MIDI jack associated with USB endpoint
  108. uint8_t const channel = 0; // 0 for channel 1
  109. // The MIDI interface always creates input and output port/jack descriptors
  110. // regardless of these being used or not. Therefore incoming traffic should be read
  111. // (possibly just discarded) to avoid the sender blocking in IO
  112. uint8_t packet[4];
  113. while ( tud_midi_available() ) tud_midi_packet_read(packet);
  114. // send note periodically
  115. if (board_millis() - start_ms < 286) return; // not enough time
  116. start_ms += 286;
  117. // Previous positions in the note sequence.
  118. int previous = (int) (note_pos - 1);
  119. // If we currently are at position 0, set the
  120. // previous position to the last note in the sequence.
  121. if (previous < 0) previous = sizeof(note_sequence) - 1;
  122. // Send Note On for current position at full velocity (127) on channel 1.
  123. uint8_t note_on[3] = { 0x90 | channel, note_sequence[note_pos], 127 };
  124. tud_midi_stream_write(cable_num, note_on, 3);
  125. // Send Note Off for previous note.
  126. uint8_t note_off[3] = { 0x80 | channel, note_sequence[previous], 0};
  127. tud_midi_stream_write(cable_num, note_off, 3);
  128. // Increment position
  129. note_pos++;
  130. // If we are at the end of the sequence, start over.
  131. if (note_pos >= sizeof(note_sequence)) note_pos = 0;
  132. }
  133. //--------------------------------------------------------------------+
  134. // BLINKING TASK
  135. //--------------------------------------------------------------------+
  136. void led_blinking_task(void)
  137. {
  138. static uint32_t start_ms = 0;
  139. static bool led_state = false;
  140. // Blink every interval ms
  141. if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
  142. start_ms += blink_interval_ms;
  143. board_led_write(led_state);
  144. led_state = 1 - led_state; // toggle
  145. }