Usb Device Interface, protocol, and programming homework #4 Audio Control device

Dependencies:   C12832_lcd USBDevice mbed

Committer:
jakowisp
Date:
Mon Aug 05 02:02:58 2013 +0000
Revision:
3:6da430f4818a
Parent:
2:dec5e78d579b
Fixed several known issues to complete the assignment

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jakowisp 0:69eb9d19fb91 1 #include "mbed.h"
jakowisp 0:69eb9d19fb91 2
jakowisp 0:69eb9d19fb91 3 #include "USBAudioControl.h"
jakowisp 0:69eb9d19fb91 4 #include "MyDisplayClass.h"
jakowisp 0:69eb9d19fb91 5
jakowisp 1:948ffad3284f 6 //Define Key words for the special disconnect/connect functionality
jakowisp 1:948ffad3284f 7 #define PAUSEKEY 0x10
jakowisp 0:69eb9d19fb91 8 #define NUMSECONDSTOHOLD 5
jakowisp 0:69eb9d19fb91 9
jakowisp 1:948ffad3284f 10 //Joystick array for the audio control functions
jakowisp 0:69eb9d19fb91 11 BusIn btnArray(p15,p12,p13,p16,p14);
jakowisp 1:948ffad3284f 12 //Display LED to provide feedback during the hold button sequence
jakowisp 0:69eb9d19fb91 13 DigitalOut toggle(LED1);
jakowisp 0:69eb9d19fb91 14
jakowisp 1:948ffad3284f 15 //Group all the display items in a class. This class creates a LCD object, and bargraph object
jakowisp 0:69eb9d19fb91 16 MyDisplayClass display;
jakowisp 1:948ffad3284f 17
jakowisp 1:948ffad3284f 18 //Set up the USB HID device
jakowisp 0:69eb9d19fb91 19 USBAudioControl hid(0x1234,0x0006,0x0001);
jakowisp 1:948ffad3284f 20 //temporary structure to hold data before filling the input report
jakowisp 2:dec5e78d579b 21 HID_REPORT tempReport(1);
jakowisp 0:69eb9d19fb91 22
jakowisp 1:948ffad3284f 23
jakowisp 1:948ffad3284f 24 //Variable to hold USB state
jakowisp 1:948ffad3284f 25 // Bit 0: suspended=0,normal=1
jakowisp 1:948ffad3284f 26 // Bit 1: 1=USB configured, 0 USB not configured
jakowisp 0:69eb9d19fb91 27 unsigned int USBstate=0;
jakowisp 0:69eb9d19fb91 28
jakowisp 1:948ffad3284f 29 //Callback funciton: When an Output Report is received, set the current volume level.
jakowisp 0:69eb9d19fb91 30 void SetLevel(HID_REPORT *report){
jakowisp 0:69eb9d19fb91 31 display.setLevel(report->data[0]);
jakowisp 0:69eb9d19fb91 32 }
jakowisp 0:69eb9d19fb91 33
jakowisp 1:948ffad3284f 34 //Callback Function: When a feature report is sent, change the feature bits, and the scale
jakowisp 0:69eb9d19fb91 35 void SetFeatures(HID_REPORT *report){
jakowisp 1:948ffad3284f 36 display.volumeDisplayEnable=((report->data[1]&0x10)>>4)==0x1;
jakowisp 1:948ffad3284f 37 display.graphicModeEnable=((report->data[1]&0x20)>>5)==0x1;
jakowisp 0:69eb9d19fb91 38 display.setMaxLevel((report->data[1]&0x0f)<<2);
jakowisp 0:69eb9d19fb91 39 }
jakowisp 0:69eb9d19fb91 40
jakowisp 1:948ffad3284f 41 //Function to write the USB configured bit, this is polled.
jakowisp 0:69eb9d19fb91 42 void WriteConnectedBit(unsigned int connected){
jakowisp 0:69eb9d19fb91 43 USBstate = (connected << 1) | (USBstate & 0x1);
jakowisp 0:69eb9d19fb91 44 }
jakowisp 0:69eb9d19fb91 45
jakowisp 1:948ffad3284f 46 //Callback function: When the Suspend bit is changed, update USBstate
jakowisp 0:69eb9d19fb91 47 void WriteSuspendBit(unsigned int suspend){
jakowisp 0:69eb9d19fb91 48 USBstate = (suspend) | (USBstate & 0x2);
jakowisp 0:69eb9d19fb91 49 }
jakowisp 1:948ffad3284f 50
jakowisp 1:948ffad3284f 51 //Fill a temp report with the button array state
jakowisp 0:69eb9d19fb91 52 void PollInputs(HID_REPORT *tempReport) {
jakowisp 0:69eb9d19fb91 53 tempReport->data[0]=btnArray;
jakowisp 0:69eb9d19fb91 54 }
jakowisp 0:69eb9d19fb91 55
jakowisp 1:948ffad3284f 56 //Provide a check for a special button combination to connect and disconnect.
jakowisp 0:69eb9d19fb91 57 void CheckForHeldPauseKey(HID_REPORT * tempreport, USBAudioControl * hid, DigitalOut * toggle){
jakowisp 0:69eb9d19fb91 58 static int holdCount=0;
jakowisp 0:69eb9d19fb91 59
jakowisp 0:69eb9d19fb91 60 //Provide Feedback to user holding the key by flashing an LED when the key is held
jakowisp 0:69eb9d19fb91 61 *toggle=(holdCount>0)?(holdCount /5)%2 :0;
jakowisp 0:69eb9d19fb91 62 //Count how long the PAUSEKEY is held
jakowisp 0:69eb9d19fb91 63 if(tempReport.data[0]==PAUSEKEY) {
jakowisp 0:69eb9d19fb91 64 holdCount++;
jakowisp 0:69eb9d19fb91 65 } else
jakowisp 0:69eb9d19fb91 66 holdCount=0;
jakowisp 0:69eb9d19fb91 67 //If Key is held # of seconds, The hid will diconnect/connect the USB device
jakowisp 0:69eb9d19fb91 68 if(holdCount>=NUMSECONDSTOHOLD*10) {
jakowisp 0:69eb9d19fb91 69 holdCount=0;
jakowisp 0:69eb9d19fb91 70 if(hid->getConnectState()) {
jakowisp 0:69eb9d19fb91 71 hid->disconnect();
jakowisp 0:69eb9d19fb91 72 } else
jakowisp 0:69eb9d19fb91 73 hid->connect();
jakowisp 0:69eb9d19fb91 74 }
jakowisp 0:69eb9d19fb91 75 }
jakowisp 0:69eb9d19fb91 76
jakowisp 1:948ffad3284f 77
jakowisp 1:948ffad3284f 78 //Main Function
jakowisp 0:69eb9d19fb91 79 int main() {
jakowisp 0:69eb9d19fb91 80
jakowisp 1:948ffad3284f 81 //Assign callback functions for USB events
jakowisp 0:69eb9d19fb91 82 hid.callbackSetOutputReport=&SetLevel;
jakowisp 0:69eb9d19fb91 83 hid.callbackSetFeatureReport=&SetFeatures;
jakowisp 0:69eb9d19fb91 84 hid.callbackSuspendChange=&WriteSuspendBit;
jakowisp 2:dec5e78d579b 85
jakowisp 0:69eb9d19fb91 86
jakowisp 1:948ffad3284f 87 //Main Loop
jakowisp 0:69eb9d19fb91 88 while(1) {
jakowisp 1:948ffad3284f 89 //Poll Inputs
jakowisp 0:69eb9d19fb91 90 PollInputs(&tempReport);
jakowisp 1:948ffad3284f 91 //Poll USB Configured
jakowisp 0:69eb9d19fb91 92 WriteConnectedBit(hid.getConnectState());
jakowisp 1:948ffad3284f 93 //Hidden Configureation option
jakowisp 0:69eb9d19fb91 94 CheckForHeldPauseKey(&tempReport,&hid,&toggle);
jakowisp 1:948ffad3284f 95 //Set up the input data for a send
jakowisp 0:69eb9d19fb91 96 hid.FillInputReport(&tempReport);
jakowisp 1:948ffad3284f 97 //Update the display
jakowisp 1:948ffad3284f 98 display.update(USBstate);
jakowisp 0:69eb9d19fb91 99 wait(0.1);
jakowisp 0:69eb9d19fb91 100 }
jakowisp 0:69eb9d19fb91 101 }
jakowisp 0:69eb9d19fb91 102
jakowisp 0:69eb9d19fb91 103
jakowisp 1:948ffad3284f 104