/* * Copyright (c) 2024 Titouan Christophe * * SPDX-License-Identifier: Apache-2.0 * * @file * @brief Sample application for USB MIDI 2.0 device class */ #include #include #include #include #include #include LOG_MODULE_REGISTER(sample_usb_midi, LOG_LEVEL_INF); static const struct device *const midi = DEVICE_DT_GET(DT_NODELABEL(usb_midi)); static struct gpio_dt_spec led = GPIO_DT_SPEC_GET_OR(DT_ALIAS(led0), gpios, {0}); static void key_press(struct input_event *evt, void *user_data) { /* Only handle key presses in the 7bit MIDI range */ if (evt->type != INPUT_EV_KEY || evt->code > 0x7f) { return; } uint8_t command = evt->value ? UMP_MIDI_NOTE_ON : UMP_MIDI_NOTE_OFF; uint8_t channel = 0; uint8_t note = evt->code; uint8_t velocity = 100; struct midi_ump ump = UMP_MIDI1_CHANNEL_VOICE(0, command, channel, note, velocity); usbd_midi_send(midi, ump); } INPUT_CALLBACK_DEFINE(NULL, key_press, NULL); static void on_midi_packet(const struct device *dev, const struct midi_ump ump) { LOG_INF("Received MIDI packet (MT=%X)", UMP_MT(ump)); /* Only send MIDI1 channel voice messages back to the host */ if (UMP_MT(ump) == UMP_MT_MIDI1_CHANNEL_VOICE) { LOG_INF("Send back MIDI1 message %02X %02X %02X", UMP_MIDI_STATUS(ump), UMP_MIDI1_P1(ump), UMP_MIDI1_P2(ump)); usbd_midi_send(dev, ump); } } static void on_device_ready(const struct device *dev, const bool ready) { /* Light up the LED (if any) when USB-MIDI2.0 is enabled */ if (led.port) { gpio_pin_set_dt(&led, ready); } } static const struct usbd_midi_ops ops = { .rx_packet_cb = on_midi_packet, .ready_cb = on_device_ready, }; int main(void) { struct usbd_context *sample_usbd; if (!device_is_ready(midi)) { LOG_ERR("MIDI device not ready"); return -1; } if (led.port) { if (gpio_pin_configure_dt(&led, GPIO_OUTPUT)) { LOG_ERR("Unable to setup LED, not using it"); memset(&led, 0, sizeof(led)); } } usbd_midi_set_ops(midi, &ops); sample_usbd = sample_usbd_init_device(NULL); if (sample_usbd == NULL) { LOG_ERR("Failed to initialize USB device"); return -1; } if (usbd_enable(sample_usbd)) { LOG_ERR("Failed to enable device support"); return -1; } LOG_INF("USB device support enabled"); return 0; }