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

Dependencies:   C12832_lcd USBDevice mbed

Files at this revision

API Documentation at this revision

Comitter:
jakowisp
Date:
Tue Jul 30 22:35:10 2013 +0000
Child:
1:948ffad3284f
Commit message:
AudioControl Finctional
;

Changed in this revision

C12832_lcd.lib Show annotated file Show diff for this revision Revisions of this file
MyDisplayClass.cpp Show annotated file Show diff for this revision Revisions of this file
MyDisplayClass.h Show annotated file Show diff for this revision Revisions of this file
USBAudioControl.cpp Show annotated file Show diff for this revision Revisions of this file
USBAudioControl.h Show annotated file Show diff for this revision Revisions of this file
USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
bargraph.cpp Show annotated file Show diff for this revision Revisions of this file
bargraph.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C12832_lcd.lib	Tue Jul 30 22:35:10 2013 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/dreschpe/code/C12832_lcd/#c9afe58d786a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MyDisplayClass.cpp	Tue Jul 30 22:35:10 2013 +0000
@@ -0,0 +1,74 @@
+#include "MyDisplayClass.h"
+
+MyDisplayClass::MyDisplayClass(){
+   lcd=new C12832_LCD();
+   volume=new bargraph(lcd,32);
+}
+
+void MyDisplayClass::drawNoConnection(C12832_LCD *lcd) {
+     lcd->fillrect(0, 31, 15,16, 0);
+     lcd->rect(0, 31, 15,16, 1);
+     lcd->line(2,29,13,18,1);
+     lcd->circle(7,24,4,1);
+}
+
+void MyDisplayClass::drawConnection(C12832_LCD *lcd) {
+     lcd->fillrect(0, 31, 15,16, 0);
+     lcd->rect(0, 31, 15,16, 1);
+     lcd->rect(5,26,10,21,1);
+     lcd->line(6,23,7,25,1);
+     lcd->line(7,25,14,17,1);
+}
+
+void MyDisplayClass::drawSuspend(C12832_LCD *lcd) {
+     lcd->fillrect(0, 31, 15,16, 0);
+     lcd->rect(0, 31, 15,16, 1);
+     lcd->line(4,20,11,20,1);
+     lcd->line(4,27,11,20,1);
+     lcd->line(4,27,11,27,1);
+}
+
+void MyDisplayClass::setLevel(int level){
+      volume->setLevel(level);
+   }
+   
+void MyDisplayClass::setMaxLevel(int level){
+      volume->setMaxLevel(level);
+   }
+
+void MyDisplayClass::UpdateStatus(int state){
+    switch(state) {
+       case 2:  drawSuspend(lcd); break;
+       case 3:  drawConnection(lcd); break;
+      default:  drawNoConnection(lcd); break;
+      }
+}
+
+void MyDisplayClass::UpdateTextStatus(int state){
+   
+       lcd->locate(3,12);
+       lcd->printf("         ");
+       lcd->locate(3,12);
+        switch(state) {
+           case 2:  lcd->printf("Suspended"); break;
+           case 3:  lcd->printf("Normal"); break;
+          default:  lcd->printf("Offline"); break;
+          }
+
+}
+
+void MyDisplayClass::update(int state,int features){
+      if((features&0x1) == 0x1)
+         if((features&0x2) == 0x2) {
+           volume->updateBargraph();
+         } else {
+           lcd->locate(3,3);
+           lcd->printf("Volume: %2d",volume->level); 
+         } 
+  
+        if((features&0x2) == 0x2)  
+           UpdateStatus(state);
+        else
+           UpdateTextStatus(state);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MyDisplayClass.h	Tue Jul 30 22:35:10 2013 +0000
@@ -0,0 +1,27 @@
+#ifndef MyDisplayClass_H
+#define MyDisplayClass_H
+
+#include "C12832_lcd.h"
+#include "bargraph.h"
+
+class MyDisplayClass {
+public:
+   MyDisplayClass(void);
+   void update(int state,int features);
+   void UpdateTextStatus(int state);
+   void UpdateStatus(int state);
+   void setLevel(int level);
+   void setMaxLevel(int level);
+   
+private:
+    void drawSuspend(C12832_LCD *lcd);
+    void drawConnection(C12832_LCD *lcd);
+    void drawNoConnection(C12832_LCD *lcd);
+    
+    C12832_LCD  *lcd;
+    bargraph   *volume;
+    //(&lcd,32);    
+
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBAudioControl.cpp	Tue Jul 30 22:35:10 2013 +0000
@@ -0,0 +1,50 @@
+#include "stdint.h"
+#include "USBAudioControl.h"
+
+void USBAudioControl::suspendStateChanged(unsigned int suspend){
+    if(callbackSuspendChange!=NULL)
+      (*callbackSuspendChange)(suspend);
+}
+
+unsigned int USBAudioControl::getConnectState(){
+   if (device.state != CONFIGURED)
+       return 0;
+   return 1;
+}
+
+uint8_t * USBAudioControl::reportDesc() {
+       static uint8_t reportDescriptor[] = {
+            USAGE_PAGE(2),      0x0c,0x00,       // Consumer
+            USAGE(1),           0x01,       // Remote Control
+            COLLECTION(1),      0x01,       // Application
+            LOGICAL_MINIMUM(1),     0x00,
+            LOGICAL_MAXIMUM(1),     0x01,
+    
+            USAGE(2),           0xe9,0x00,       // Vol+
+            USAGE(2),           0xea,0x00,       // Vol-
+            USAGE(2),           0xb5,0x00,       // NextTrack
+            USAGE(2),           0xb6,0x00,       // PrevTrack
+            USAGE(2),           0xcd,0x00,       // Pause/play
+            REPORT_COUNT(1),    0x05,
+            REPORT_SIZE(1),     0x01,
+            INPUT(1),           0x02,
+            REPORT_COUNT(1),    0x03,
+            REPORT_SIZE(1),     0x01,
+            INPUT(1),           0x01,
+            LOGICAL_MINIMUM(1), 0x00,                // logical minimum = 0
+            LOGICAL_MAXIMUM(2), 0xFF, 0x00,          // logical maximum = 255
+            REPORT_SIZE(1),     0x08,
+            REPORT_COUNT(1),    0x01,                   // report count
+            USAGE(1),           0x02,                          // usage
+            OUTPUT(1),          0x02,                         // Output (array)
+            REPORT_COUNT(1),    0x01,
+            USAGE(1),           0x03,
+            FEATURE(2),         0x02, 0x01,
+            END_COLLECTION(0),
+        };
+        reportLength = sizeof(reportDescriptor);
+        return reportDescriptor;
+}
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBAudioControl.h	Tue Jul 30 22:35:10 2013 +0000
@@ -0,0 +1,46 @@
+#ifndef USBaudiocontrol_H
+#define USBaudiocontrol_H
+
+#include "USBHID.h"
+
+
+/* Common usage */
+
+/**
+ *
+ * @endcode
+ */
+
+class USBAudioControl: public USBHID
+{
+    public:
+        
+        /**
+        *   Constructor
+        *
+        * @param vendor_id Your vendor_id (default: 0x1234)
+        * @param product_id Your product_id (default: 0x0001)
+        * @param product_release Your preoduct_release (default: 0x0001)
+        *
+        */
+        USBAudioControl( uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0006, uint16_t product_release = 0x0001): 
+            USBHID(1, 1, 1, vendor_id, product_id, product_release, false)
+            {        
+                callbackSuspendChange=NULL;
+            };
+        
+        
+        /*
+        * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
+        *
+        * @returns pointer to the report descriptor
+        */
+        virtual uint8_t * reportDesc();
+        
+        unsigned int getConnectState();
+        void (*callbackSuspendChange)(unsigned int connected);
+        virtual void suspendStateChanged(unsigned int suspend);
+
+};
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice.lib	Tue Jul 30 22:35:10 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/jakowisp/code/USBDevice/#00cd29199e0e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bargraph.cpp	Tue Jul 30 22:35:10 2013 +0000
@@ -0,0 +1,55 @@
+ #include "bargraph.h"
+ bargraph::bargraph(C12832_LCD *inlcd,int maxlevelsIn,int Xin,int Yin,int widthIn,int heightIn){
+      lcd=inlcd;
+      level=0;
+      lastLevel=0;
+      if(maxlevelsIn<=32 && maxlevelsIn >=4)
+         maxlevels=maxlevelsIn;
+      else
+         maxlevels=32;
+      if(Xin>=0 && Xin<=127-maxlevels)
+           x=Xin;
+      else
+           x=0;
+      if(Yin>=0 && Yin<=31-maxlevels)
+           y=Yin;
+      else
+           y=0;
+      if(widthIn>maxlevels && widthIn<128)
+        width=widthIn-1;
+      else
+        width=127;
+      if(heightIn>=(maxlevels-1) && heightIn<31)  
+         height=heightIn;
+      else 
+         height=31;
+      leveladjust=(height+1)/maxlevels;
+      levelwidth=(width+1)/maxlevels;
+   }
+   
+   void  bargraph::setMaxLevel(int maxlevels){
+      lcd->fillrect(x, y, x+width,y+height, 0);
+      leveladjust=(height+1)/maxlevels;
+      levelwidth=(width+1)/maxlevels;
+   }
+   
+   void bargraph::setLevel(int level){
+      this->level=level;
+   }
+   
+   void  bargraph::updateBargraph(){  
+      if(lcd!=NULL) {
+        if(level!=lastLevel) {
+          for(int i = 0 ; i<lastLevel;i++) {
+             lcd->fillrect(x+levelwidth*(i), y, x+levelwidth*(i+1)-1, y+i*leveladjust, 0);
+          }
+          for(int i = 0 ; i<level;i++) {
+             lcd->fillrect(x+levelwidth*(i), y, x+levelwidth*(i+1)-1, y+i*leveladjust, 1);
+          }
+          lastLevel=level;
+        }
+      }
+    };
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bargraph.h	Tue Jul 30 22:35:10 2013 +0000
@@ -0,0 +1,25 @@
+#include "C12832_lcd.h"
+
+class bargraph {
+public:
+   bargraph(C12832_LCD *inlcd,int maxlevelsIn=32,int Xin=0,int Yin=0,int widthIn=128,int heightIn=32);
+   
+   int level;
+   void updateBargraph();
+   void setMaxLevel(int maxlevels);
+   void setLevel(int level);
+
+private:
+   int x;
+   int y;
+   int width;
+   int height;
+   int maxlevels;
+   int leveladjust;
+   int levelwidth;
+   int lastLevel;
+   
+   C12832_LCD *lcd;
+};
+      
+
--- /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);
+    }
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Jul 30 22:35:10 2013 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/b3110cd2dd17
\ No newline at end of file