diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2012-09-29 17:01:07 +0200 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2012-09-29 17:01:07 +0200 |
commit | 5be48c81ef2958f9e1caaa5273f972f517bcd460 (patch) | |
tree | 848cc985986df17b26f427b918c40f35f86d0c77 | |
parent | 9a95c5b6673e3833a05fab376a31b534d900508d (diff) |
Added USB volume control.
-rw-r--r-- | main.cpp | 55 |
1 files changed, 46 insertions, 9 deletions
@@ -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) { |