| /* |
| * Copyright (c) 2020 Nordic Semiconductor ASA |
| * |
| * SPDX-License-Identifier: Apache-2.0 |
| */ |
| |
| /** |
| * @file |
| * @brief Sample app for Audio class |
| */ |
| |
| #include <zephyr/kernel.h> |
| #include <zephyr/logging/log.h> |
| #include <zephyr/usb/usb_device.h> |
| #include <zephyr/usb/class/usb_audio.h> |
| |
| LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); |
| |
| static void data_received(const struct device *dev, |
| struct net_buf *buffer, |
| size_t size) |
| { |
| int ret; |
| |
| if (!buffer || !size) { |
| /* This should never happen */ |
| return; |
| } |
| |
| LOG_DBG("Received %d data, buffer %p", size, buffer); |
| |
| /* Check if the device OUT buffer can be used for input */ |
| if (size == usb_audio_get_in_frame_size(dev)) { |
| ret = usb_audio_send(dev, buffer, size); |
| if (ret) { |
| net_buf_unref(buffer); |
| } |
| } else { |
| net_buf_unref(buffer); |
| } |
| } |
| |
| static void feature_update(const struct device *dev, |
| const struct usb_audio_fu_evt *evt) |
| { |
| int16_t volume = 0; |
| |
| LOG_DBG("Control selector %d for channel %d updated", |
| evt->cs, evt->channel); |
| switch (evt->cs) { |
| case USB_AUDIO_FU_MUTE_CONTROL: |
| break; |
| case USB_AUDIO_FU_VOLUME_CONTROL: |
| volume = *((int16_t *)(evt->val)); |
| LOG_INF("set volume: %d", volume); |
| break; |
| default: |
| break; |
| } |
| } |
| |
| static const struct usb_audio_ops ops = { |
| .data_received_cb = data_received, |
| .feature_update_cb = feature_update, |
| }; |
| |
| int main(void) |
| { |
| const struct device *hs_dev; |
| int ret; |
| |
| LOG_INF("Entered %s", __func__); |
| hs_dev = DEVICE_DT_GET_ONE(usb_audio_hs); |
| |
| if (!device_is_ready(hs_dev)) { |
| LOG_ERR("Device USB Headset is not ready"); |
| return 0; |
| } |
| |
| LOG_INF("Found USB Headset Device"); |
| |
| usb_audio_register(hs_dev, &ops); |
| |
| ret = usb_enable(NULL); |
| if (ret != 0) { |
| LOG_ERR("Failed to enable USB"); |
| return 0; |
| } |
| |
| LOG_INF("USB enabled"); |
| return 0; |
| } |