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+
26+ #include <stdlib.h>
27+ #include <stdio.h>
28+ #include <string.h>
29+
30+ #include "bsp/board.h"
31+ #include "tusb.h"
32+ #include "class/midi/midi_host.h"
33+
34+
35+ #if CFG_TUH_MIDI
36+
37+ //--------------------------------------------------------------------+
38+ // STATIC GLOBALS DECLARATION
39+ //--------------------------------------------------------------------+
40+ static uint8_t midi_dev_addr = 0 ;
41+
42+ //--------------------------------------------------------------------+
43+ // MACRO CONSTANT TYPEDEF PROTYPES
44+ //--------------------------------------------------------------------+
45+ void led_blinking_task (void );
46+ void midi_host_rx_task (void );
47+
48+ /*------------- MAIN -------------*/
49+ int main (void )
50+ {
51+ board_init ();
52+
53+ printf ("TinyUSB Host MIDI Example\r\n" );
54+
55+ tusb_init ();
56+
57+ while (1 )
58+ {
59+ tuh_task ();
60+ led_blinking_task ();
61+ midi_host_rx_task ();
62+ }
63+
64+ return 0 ;
65+ }
66+
67+ #endif
68+
69+ //--------------------------------------------------------------------+
70+ // Blinking Task
71+ //--------------------------------------------------------------------+
72+ void led_blinking_task (void )
73+ {
74+ const uint32_t interval_ms = 1000 ;
75+ static uint32_t start_ms = 0 ;
76+
77+ static bool led_state = false;
78+
79+ // Blink every interval ms
80+ if ( board_millis () - start_ms < interval_ms ) return ; // not enough time
81+ start_ms += interval_ms ;
82+
83+ board_led_write (led_state );
84+ led_state = 1 - led_state ; // toggle
85+ }
86+
87+ //--------------------------------------------------------------------+
88+ // MIDI host receive task
89+ //--------------------------------------------------------------------+
90+ void midi_host_rx_task (void )
91+ {
92+ // device must be attached and have at least one endpoint ready to receive a message
93+ if (!midi_dev_addr || !tuh_midi_configured (midi_dev_addr ))
94+ {
95+ return ;
96+ }
97+ if (tuh_midih_get_num_rx_cables (midi_dev_addr ) < 1 )
98+ {
99+ return ;
100+ }
101+ tuh_midi_read_poll (midi_dev_addr );
102+ }
103+
104+ //--------------------------------------------------------------------+
105+ // TinyUSB Callbacks
106+ //--------------------------------------------------------------------+
107+
108+ // Invoked when device with hid interface is mounted
109+ // Report descriptor is also available for use. tuh_hid_parse_report_descriptor()
110+ // can be used to parse common/simple enough descriptor.
111+ // Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, it will be skipped
112+ // therefore report_desc = NULL, desc_len = 0
113+ void tuh_midi_mount_cb (uint8_t dev_addr , uint8_t in_ep , uint8_t out_ep , uint8_t num_cables_rx , uint16_t num_cables_tx )
114+ {
115+ TU_LOG1 ("MIDI device address = %u, IN endpoint %u has %u cables, OUT endpoint %u has %u cables\r\n" ,
116+ dev_addr , in_ep & 0xf , num_cables_rx , out_ep & 0xf , num_cables_tx );
117+
118+ midi_dev_addr = dev_addr ;
119+ }
120+
121+ // Invoked when device with hid interface is un-mounted
122+ void tuh_midi_umount_cb (uint8_t dev_addr , uint8_t instance )
123+ {
124+ TU_LOG1 ("MIDI device address = %d, instance = %d is unmounted\r\n" , dev_addr , instance );
125+
126+ midi_dev_addr = 0 ;
127+ }
128+
129+ void tuh_midi_rx_cb (uint8_t dev_addr , uint32_t num_packets )
130+ {
131+ if (midi_dev_addr != dev_addr )
132+ {
133+ return ;
134+ }
135+
136+ if (num_packets == 0 )
137+ {
138+ return ;
139+ }
140+
141+ uint8_t cable_num ;
142+ uint8_t buffer [48 ];
143+ uint32_t bytes_read = tuh_midi_stream_read (dev_addr , & cable_num , buffer , sizeof (buffer ));
144+
145+ TU_LOG1 ("Read bytes %u cable %u" , bytes_read , cable_num );
146+ TU_LOG1_MEM (buffer , bytes_read , 2 );
147+ }
148+
149+ void tuh_midi_tx_cb (uint8_t dev_addr )
150+ {
151+ (void ) dev_addr ;
152+ }
0 commit comments