From 5be48c81ef2958f9e1caaa5273f972f517bcd460 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sat, 29 Sep 2012 17:01:07 +0200 Subject: Added USB volume control. --- main.cpp | 55 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/main.cpp b/main.cpp index 1cee450..13dac75 100644 --- a/main.cpp +++ b/main.cpp @@ -119,6 +119,9 @@ USB_otg usb(OTG_FS, dev_desc_p, conf_desc_p); class USB_Audio : public USB_class_driver { private: USB_generic& usb; + + enum ControlState {None, VolLeft, VolRight}; + ControlState control_state; public: USB_Audio(USB_generic& usbd) : usb(usbd) { @@ -127,34 +130,52 @@ class USB_Audio : public USB_class_driver { protected: virtual SetupStatus handle_setup(uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength) { + control_state = ControlState::None; + + // GET_CUR if(bmRequestType == 0xa1 && bRequest == 0x81) { - uint32_t nullw = 0; - usb.write(0, &nullw, wLength); + uint32_t res = 0; + usb.write(0, &res, wLength); return SetupStatus::Ok; } + // GET_MIN if(bmRequestType == 0xa1 && bRequest == 0x82) { - uint32_t nullw = 0; - usb.write(0, &nullw, wLength); + uint32_t res = 0; + usb.write(0, &res, wLength); return SetupStatus::Ok; } + // GET_MAX if(bmRequestType == 0xa1 && bRequest == 0x83) { - uint32_t nullw = 0; - usb.write(0, &nullw, wLength); + uint32_t res = 127; + usb.write(0, &res, wLength); return SetupStatus::Ok; } + // GET_RES if(bmRequestType == 0xa1 && bRequest == 0x84) { - uint32_t nullw = 0; - usb.write(0, &nullw, wLength); + uint32_t res = 1; + usb.write(0, &res, wLength); return SetupStatus::Ok; } + // SET_CUR if(bmRequestType == 0x21 && bRequest == 0x01) { + switch(wValue) { + case 0x0201: + control_state = ControlState::VolLeft; + break; + + case 0x0202: + control_state = ControlState::VolRight; + break; + } + return SetupStatus::Ok; } + // SET_CUR if(bmRequestType == 0x22 && bRequest == 0x01) { return SetupStatus::Ok; } @@ -170,7 +191,23 @@ class USB_Audio : public USB_class_driver { } virtual void handle_out(uint8_t ep, uint32_t len) { - if(ep == 0) { + if(ep == 0 && len > 0) { + uint32_t buf; + usb.read(0, &buf, len > 4 ? 4 : len); + + switch(control_state) { + case ControlState::VolLeft: + dac.set_volume(0, buf & 0xff); + break; + + case ControlState::VolRight: + dac.set_volume(1, buf & 0xff); + break; + + default: + break; + } + usb.write(0, nullptr, 0); } else if(ep == 1) { -- cgit v1.2.3