blob: 4c10f55b03bab922b90a457197e8b2e33cd375bf [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//--------------------------------------------------------------------+
34// MACRO CONSTANT TYPEDEF PROTYPES
35//--------------------------------------------------------------------+
36
37/* Blink pattern
38 * - 250 ms : device not mounted
39 * - 1000 ms : device mounted
40 * - 2500 ms : device is suspended
41 */
42enum {
43 BLINK_NOT_MOUNTED = 250,
44 BLINK_MOUNTED = 1000,
45 BLINK_SUSPENDED = 2500,
46};
47
48static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
49
50void led_blinking_task(void);
51void cdc_task(void);
52void midi_task(void);
53
54/*------------- MAIN -------------*/
55int main(void)
56{
57 board_init();
58 tusb_init();
59
60 while (1)
61 {
62 tud_task(); // tinyusb device task
63 led_blinking_task();
64 cdc_task();
65 midi_task();
66 }
67
68 return 0;
69}
70
71//--------------------------------------------------------------------+
72// Device callbacks
73//--------------------------------------------------------------------+
74
75// Invoked when device is mounted
76void tud_mount_cb(void)
77{
78 blink_interval_ms = BLINK_MOUNTED;
79}
80
81// Invoked when device is unmounted
82void tud_umount_cb(void)
83{
84 blink_interval_ms = BLINK_NOT_MOUNTED;
85}
86
87// Invoked when usb bus is suspended
88// remote_wakeup_en : if host allow us to perform remote wakeup
89// Within 7ms, device must draw an average of current less than 2.5 mA from bus
90void tud_suspend_cb(bool remote_wakeup_en)
91{
92 (void) remote_wakeup_en;
93 blink_interval_ms = BLINK_SUSPENDED;
94}
95
96// Invoked when usb bus is resumed
97void tud_resume_cb(void)
98{
99 blink_interval_ms = BLINK_MOUNTED;
100}
101
102
103//--------------------------------------------------------------------+
104// USB CDC
105//--------------------------------------------------------------------+
106void cdc_task(void)
107{
108 if ( tud_cdc_connected() )
109 {
110 // connected and there are data available
111 if ( tud_cdc_available() )
112 {
113 uint8_t buf[64];
114
115 // read and echo back
116 uint32_t count = tud_cdc_read(buf, sizeof(buf));
117
118 for(uint32_t i=0; i<count; i++)
119 {
120 tud_cdc_write_char(buf[i]);
121
122 if ( buf[i] == '\r' ) tud_cdc_write_char('\n');
123 }
124
125 tud_cdc_write_flush();
126 }
127 }
128}
129
130// Invoked when cdc when line state changed e.g connected/disconnected
131void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
132{
133 (void) itf;
134
135 // connected
136 if ( dtr && rts )
137 {
138 // print initial message when connected
139 tud_cdc_write_str("\r\nTinyUSB CDC MSC device example\r\n");
140 }
141}
142
143// Invoked when CDC interface received data from host
144void tud_cdc_rx_cb(uint8_t itf)
145{
146 (void) itf;
147}
148
149//--------------------------------------------------------------------+
150// MIDI Task
151//--------------------------------------------------------------------+
152
153// Variable that holds the current position in the sequence.
154uint32_t note_pos = 0;
155
156// Store example melody as an array of note values
157static const uint8_t note_sequence[] =
158{
159 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,
160 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,
161 56,61,64,68,74,78,81,86,90,93,98,102
162};
163
164void midi_task(void)
165{
166 static uint32_t start_ms = 0;
167
168 uint8_t const cable_num = 0; // MIDI jack associated with USB endpoint
169 uint8_t const channel = 0; // 0 for channel 1
170
171 // The MIDI interface always creates input and output port/jack descriptors
172 // regardless of these being used or not. Therefore incoming traffic should be read
173 // (possibly just discarded) to avoid the sender blocking in IO
174 uint8_t packet[4];
175 while( tud_midi_available() ) tud_midi_packet_read(packet);
176
177 // send note every 1000 ms
178 if (board_millis() - start_ms < 286) return; // not enough time
179 start_ms += 286;
180
181 // Previous positions in the note sequence.
182 int previous = note_pos - 1;
183
184 // If we currently are at position 0, set the
185 // previous position to the last note in the sequence.
186 if (previous < 0) previous = sizeof(note_sequence) - 1;
187
188 // Send Note On for current position at full velocity (127) on channel 1.
189 uint8_t note_on[3] = { 0x90 | channel, note_sequence[note_pos], 127 };
190 tud_midi_stream_write(cable_num, note_on, 3);
191
192 // Send Note Off for previous note.
193 uint8_t note_off[3] = { 0x80 | channel, note_sequence[previous], 0};
194 tud_midi_stream_write(cable_num, note_off, 3);
195
196 // Increment position
197 note_pos++;
198
199 // If we are at the end of the sequence, start over.
200 if (note_pos >= sizeof(note_sequence)) note_pos = 0;
201}
202
203//--------------------------------------------------------------------+
204// BLINKING TASK
205//--------------------------------------------------------------------+
206void led_blinking_task(void)
207{
208 static uint32_t start_ms = 0;
209 static bool led_state = false;
210
211 // Blink every interval ms
212 if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
213 start_ms += blink_interval_ms;
214
215 board_led_write(led_state);
216 led_state = 1 - led_state; // toggle
217}