blob: 193748477f2084614747deee0b47088e6e3a3473 [file] [log] [blame]
Austin Schuh41baf202022-01-01 14:33:40 -08001/*
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
26#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29
30#include "bsp/board.h"
31#include "tusb.h"
32
33/* This MIDI example send sequence of note (on/off) repeatedly. To test on PC, you need to install
34 * synth software and midi connection management software. On
35 * - Linux (Ubuntu): install qsynth, qjackctl. Then connect TinyUSB output port to FLUID Synth input port
36 * - Windows: install MIDI-OX
37 * - MacOS: SimpleSynth
38 */
39
40//--------------------------------------------------------------------+
41// MACRO CONSTANT TYPEDEF PROTYPES
42//--------------------------------------------------------------------+
43
44/* Blink pattern
45 * - 250 ms : device not mounted
46 * - 1000 ms : device mounted
47 * - 2500 ms : device is suspended
48 */
49enum {
50 BLINK_NOT_MOUNTED = 250,
51 BLINK_MOUNTED = 1000,
52 BLINK_SUSPENDED = 2500,
53};
54
55static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
56
57void led_blinking_task(void);
58void midi_task(void);
59
60/*------------- MAIN -------------*/
61int main(void)
62{
63 board_init();
64
65 tusb_init();
66
67 while (1)
68 {
69 tud_task(); // tinyusb device task
70 led_blinking_task();
71 midi_task();
72 }
73
74
75 return 0;
76}
77
78//--------------------------------------------------------------------+
79// Device callbacks
80//--------------------------------------------------------------------+
81
82// Invoked when device is mounted
83void tud_mount_cb(void)
84{
85 blink_interval_ms = BLINK_MOUNTED;
86}
87
88// Invoked when device is unmounted
89void tud_umount_cb(void)
90{
91 blink_interval_ms = BLINK_NOT_MOUNTED;
92}
93
94// Invoked when usb bus is suspended
95// remote_wakeup_en : if host allow us to perform remote wakeup
96// Within 7ms, device must draw an average of current less than 2.5 mA from bus
97void tud_suspend_cb(bool remote_wakeup_en)
98{
99 (void) remote_wakeup_en;
100 blink_interval_ms = BLINK_SUSPENDED;
101}
102
103// Invoked when usb bus is resumed
104void tud_resume_cb(void)
105{
106 blink_interval_ms = BLINK_MOUNTED;
107}
108
109//--------------------------------------------------------------------+
110// MIDI Task
111//--------------------------------------------------------------------+
112
113// Variable that holds the current position in the sequence.
114uint32_t note_pos = 0;
115
116// Store example melody as an array of note values
117uint8_t note_sequence[] =
118{
119 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,
120 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,
121 56,61,64,68,74,78,81,86,90,93,98,102
122};
123
124void midi_task(void)
125{
126 static uint32_t start_ms = 0;
127
128 uint8_t const cable_num = 0; // MIDI jack associated with USB endpoint
129 uint8_t const channel = 0; // 0 for channel 1
130
131 // The MIDI interface always creates input and output port/jack descriptors
132 // regardless of these being used or not. Therefore incoming traffic should be read
133 // (possibly just discarded) to avoid the sender blocking in IO
134 uint8_t packet[4];
135 while ( tud_midi_available() ) tud_midi_packet_read(packet);
136
137 // send note periodically
138 if (board_millis() - start_ms < 286) return; // not enough time
139 start_ms += 286;
140
141 // Previous positions in the note sequence.
142 int previous = note_pos - 1;
143
144 // If we currently are at position 0, set the
145 // previous position to the last note in the sequence.
146 if (previous < 0) previous = sizeof(note_sequence) - 1;
147
148 // Send Note On for current position at full velocity (127) on channel 1.
149 uint8_t note_on[3] = { 0x90 | channel, note_sequence[note_pos], 127 };
150 tud_midi_stream_write(cable_num, note_on, 3);
151
152 // Send Note Off for previous note.
153 uint8_t note_off[3] = { 0x80 | channel, note_sequence[previous], 0};
154 tud_midi_stream_write(cable_num, note_off, 3);
155
156 // Increment position
157 note_pos++;
158
159 // If we are at the end of the sequence, start over.
160 if (note_pos >= sizeof(note_sequence)) note_pos = 0;
161}
162
163//--------------------------------------------------------------------+
164// BLINKING TASK
165//--------------------------------------------------------------------+
166void led_blinking_task(void)
167{
168 static uint32_t start_ms = 0;
169 static bool led_state = false;
170
171 // Blink every interval ms
172 if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
173 start_ms += blink_interval_ms;
174
175 board_led_write(led_state);
176 led_state = 1 - led_state; // toggle
177}