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

Dependencies:   C12832_lcd USBDevice mbed

bargraph.cpp

Committer:
jakowisp
Date:
2013-07-31
Revision:
1:948ffad3284f
Parent:
0:69eb9d19fb91
Child:
3:6da430f4818a

File content as of revision 1:948ffad3284f:

 #include "bargraph.h"
 bargraph::bargraph(C12832_LCD *inlcd,int maxlevelsIn,int Xin,int Yin,int widthIn,int heightIn){
      this->lcd=inlcd;
      this->level=0;
      this->lastLevel=0;
      if(maxlevelsIn<=32 && maxlevelsIn >=4)
         this->maxlevels=maxlevelsIn;
      else
         this->maxlevels=32;
      if(Xin>=0 && Xin<=127-this->maxlevels)
           this->x=Xin;
      else
           this->x=0;
      if(Yin>=0 && Yin<=31-this->maxlevels)
           this->y=Yin;
      else
           this->y=0;
      if(widthIn>maxlevels && widthIn<128)
        this->width=widthIn-1;
      else
        this->width=127;
      if(heightIn>=(maxlevels-1) && heightIn<31)  
         this->height=heightIn;
      else 
         this->height=31;
      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
      this->lcd->fillrect(this->x, this->y, this->x+this->width,this->y+this->height, 0);
      this->lastLevel=0;    
      this->leveladjust=(this->height+1)/maxlevels;
      this->levelwidth=(this->width+1)/maxlevels;
   }
   
   void bargraph::setLevel(int level){
      this->level=level;
   }
   
   void  bargraph::updateBargraph(){  
      if(this->lcd!=NULL) {
        if(this->level!=this->lastLevel) {
          for(int i = 0 ; i<this->lastLevel;i++) {
             this->lcd->fillrect(this->x+this->levelwidth*(i), 
                                 this->y, 
                                 this->x+this->levelwidth*(i+1)-1,
                                 this->y+i*this->leveladjust, 
                                 0);
          }
          for(int i = 0 ; i<level;i++) {
             this->lcd->fillrect(this->x+this->levelwidth*(i), 
                                 this->y, 
                                 this->x+this->levelwidth*(i+1)-1,
                                 this->y+i*this->leveladjust, 
                                 1);
          }
          this->lastLevel = this->level;
        }
      }
    };