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

Dependencies:   C12832_lcd USBDevice mbed

Revision:
3:6da430f4818a
Parent:
1:948ffad3284f
--- 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,