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:
Mon Aug 05 02:02:58 2013 +0000
Parent:
2:dec5e78d579b
Commit message:
Fixed several known issues to complete the assignment

Changed in this revision

MyDisplayClass.cpp 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
diff -r dec5e78d579b -r 6da430f4818a MyDisplayClass.cpp
--- a/MyDisplayClass.cpp	Thu Aug 01 20:12:41 2013 +0000
+++ b/MyDisplayClass.cpp	Mon Aug 05 02:02:58 2013 +0000
@@ -1,8 +1,11 @@
 #include "MyDisplayClass.h"
-
+//Constructor
 MyDisplayClass::MyDisplayClass(){
+   //Create a new LCD instance
    lcd=new C12832_LCD();
+   //Create a new bargraph instance
    volume=new bargraph(lcd,32);
+   //Set the default feature modes. Volme Display off. Graphic mode off.
    volumeDisplayEnable=false;
    graphicModeEnable=false;
 }
@@ -38,6 +41,7 @@
       volume->setMaxLevel(level);
    }
 
+//When in Graphic mode update USB status with pictures.
 void MyDisplayClass::UpdateStatus(int state){
     switch(state) {
        case 2:  drawSuspend(lcd); break;
@@ -46,8 +50,8 @@
       }
 }
 
+//When in text mode update the USB status based on state
 void MyDisplayClass::UpdateTextStatus(int state){
-   
        lcd->locate(3,12);
        lcd->printf("         ");
        lcd->locate(3,12);
@@ -59,15 +63,19 @@
 
 }
 
+//Update the volume control display bsed on feature enable state, USB state and volume
 void MyDisplayClass::update(int state){
+      //Is volume display enabled
       if(volumeDisplayEnable)
+         //Volume:Is Graphic mode enabled or should messages be in text
          if(graphicModeEnable) {
+           //Call Graphic mode update function
            volume->updateBargraph();
          } else {
            lcd->locate(3,3);
-           lcd->printf("Volume: %2d",volume->level); 
+           lcd->printf("Volume: %3d PCT%",(volume->level * 100)/volume->maxlevels); 
          } 
-  
+         //USB:Is Graphic mode enabled or should messages be in text  
         if(graphicModeEnable)  
            UpdateStatus(state);
         else
diff -r dec5e78d579b -r 6da430f4818a bargraph.cpp
--- a/bargraph.cpp	Thu Aug 01 20:12:41 2013 +0000
+++ b/bargraph.cpp	Mon Aug 05 02:02:58 2013 +0000
@@ -1,12 +1,17 @@
  #include "bargraph.h"
+ /*
+ * Constructor
+ */
  bargraph::bargraph(C12832_LCD *inlcd,int maxlevelsIn,int Xin,int Yin,int widthIn,int heightIn){
       this->lcd=inlcd;
       this->level=0;
       this->lastLevel=0;
+      //Make sure maxlevel is a valid level
       if(maxlevelsIn<=32 && maxlevelsIn >=4)
          this->maxlevels=maxlevelsIn;
       else
          this->maxlevels=32;
+      //make sure all initial values for size and location are valid   
       if(Xin>=0 && Xin<=127-this->maxlevels)
            this->x=Xin;
       else
@@ -23,23 +28,31 @@
          this->height=heightIn;
       else 
          this->height=31;
+      //calculate how wide and tall each bar shall be.
       this->leveladjust=(this->height+1)/this->maxlevels;
       this->levelwidth=(this->width+1)/this->maxlevels;
    }
    
-   void  bargraph::setMaxLevel(int maxlevels){
-      //TODO: need to defensive check maxlevels
+   void  bargraph::setMaxLevel(unsigned int maxlevelsIn) {
+//Defensively set maxlevels to a new format level and clear the level/lastlevel values.
+      if(maxlevelsIn<=32 && maxlevelsIn >=4)
+         this->maxlevels=maxlevels;
+      else
+         this->maxlevels=32;
       this->lcd->fillrect(this->x, this->y, this->x+this->width,this->y+this->height, 0);
-      this->lastLevel=0;    
+      this->lastLevel=0;
+      this->level=0;    
       this->leveladjust=(this->height+1)/maxlevels;
       this->levelwidth=(this->width+1)/maxlevels;
    }
    
-   void bargraph::setLevel(int level){
-      this->level=level;
+   void bargraph::setLevel(unsigned int level){       
+   //Set level to a normalized value relative to maxlevels
+      this->level=(this->maxlevels * level)/0xff;     
    }
    
    void  bargraph::updateBargraph(){  
+   //Erase than draw the bars for the graph.
       if(this->lcd!=NULL) {
         if(this->level!=this->lastLevel) {
           for(int i = 0 ; i<this->lastLevel;i++) {
@@ -49,6 +62,8 @@
                                  this->y+i*this->leveladjust, 
                                  0);
           }
+          if (this->level > this->maxlevels)
+              this->level=this->maxlevels;
           for(int i = 0 ; i<level;i++) {
              this->lcd->fillrect(this->x+this->levelwidth*(i), 
                                  this->y, 
diff -r dec5e78d579b -r 6da430f4818a bargraph.h
--- a/bargraph.h	Thu Aug 01 20:12:41 2013 +0000
+++ b/bargraph.h	Mon Aug 05 02:02:58 2013 +0000
@@ -2,22 +2,37 @@
 
 class bargraph {
 public:
+   /*
+   * Constructor , Requires a pointer to the LCD
+   */
    bargraph(C12832_LCD *inlcd,int maxlevelsIn=32,int Xin=0,int Yin=0,int widthIn=128,int heightIn=32);
    
-   int level;
+   // This variable is used to draw the current volume level
+   unsigned int level;
+   // This variable represents the maximum volume  Volume leveles are percents of maxlevel when graphed.
+   unsigned int maxlevels;
+
+   //This function will redraw the Bar graph
    void updateBargraph();
-   void setMaxLevel(int maxlevels);
-   void setLevel(int level);
-
+   
+   //this fuction will reset the maxlevels and clea the current level
+   void setMaxLevel(unsigned int maxlevels);
+   //This function will set the level value.  The input is 0x00 to 0xff and is normalized to a percent of maximum level.
+   void setLevel(unsigned int level);
+ 
 private:
+   //How wide and tall the grpah is as where as where it should start.
    int x;
    int y;
    int width;
    int height;
-   int maxlevels;
+   
+   //When drawing bars how tall and wide should they be.
    int leveladjust;
    int levelwidth;
-   int lastLevel;
+   
+   //What was the last drawn state so we delete only what was drawn
+   unsigned int lastLevel;
    
    C12832_LCD *lcd;
 };