![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Usb Device Interface, protocol, and programming homework #4 Audio Control device
Dependencies: C12832_lcd USBDevice mbed
Diff: main.cpp
- Revision:
- 0:69eb9d19fb91
- Child:
- 1:948ffad3284f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Jul 30 22:35:10 2013 +0000 @@ -0,0 +1,77 @@ +#include "mbed.h" + +#include "USBAudioControl.h" +#include "MyDisplayClass.h" + +#define PAUSEKEY 16 +#define NUMSECONDSTOHOLD 5 + +BusIn btnArray(p15,p12,p13,p16,p14); +DigitalOut toggle(LED1); + +MyDisplayClass display; +USBAudioControl hid(0x1234,0x0006,0x0001); +HID_REPORT tempReport; + +unsigned int features=0; +unsigned int USBstate=0; + +void SetLevel(HID_REPORT *report){ + display.setLevel(report->data[0]); +} + +void SetFeatures(HID_REPORT *report){ + features=(report->data[1]&0x30)>>4; + display.setMaxLevel((report->data[1]&0x0f)<<2); +} + +void WriteConnectedBit(unsigned int connected){ + USBstate = (connected << 1) | (USBstate & 0x1); +} + +void WriteSuspendBit(unsigned int suspend){ + USBstate = (suspend) | (USBstate & 0x2); +} + +void PollInputs(HID_REPORT *tempReport) { + tempReport->data[0]=btnArray; +} + +void CheckForHeldPauseKey(HID_REPORT * tempreport, USBAudioControl * hid, DigitalOut * toggle){ + static int holdCount=0; + + //Provide Feedback to user holding the key by flashing an LED when the key is held + *toggle=(holdCount>0)?(holdCount /5)%2 :0; + //Count how long the PAUSEKEY is held + if(tempReport.data[0]==PAUSEKEY) { + holdCount++; + } else + holdCount=0; + //If Key is held # of seconds, The hid will diconnect/connect the USB device + if(holdCount>=NUMSECONDSTOHOLD*10) { + holdCount=0; + if(hid->getConnectState()) { + hid->disconnect(); + } else + hid->connect(); + } +} + +int main() { + + hid.callbackSetOutputReport=&SetLevel; + hid.callbackSetFeatureReport=&SetFeatures; + hid.callbackSuspendChange=&WriteSuspendBit; + tempReport.length=1; + + while(1) { + PollInputs(&tempReport); + WriteConnectedBit(hid.getConnectState()); + CheckForHeldPauseKey(&tempReport,&hid,&toggle); + hid.FillInputReport(&tempReport); + display.update(USBstate,features); + wait(0.1); + } +} + +