Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

LcdBarGraph.cpp

By: a guest on Jul 31st, 2011  |  syntax: C++  |  size: 4.00 KB  |  views: 83  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * File: LcdBarGraph.cpp
  3.  * Description:
  4.  * LcdBarGraph is an Arduino library for displaying analog values in LCD display,
  5.  *   which is previously initialized. This library uses LiquedCrystal library for displaying.
  6.  *
  7.  * Author: Balazs Kelemen
  8.  * Contact: prampec+arduino@gmail.com
  9.  * Copyright: 2010 Balazs Kelemen
  10.  * Copying permission statement:
  11.     This file is part of LcdBarGraph.
  12.  
  13.     LcdBarGraph is free software: you can redistribute it and/or modify
  14.     it under the terms of the GNU General Public License as published by
  15.     the Free Software Foundation, either version 3 of the License, or
  16.     (at your option) any later version.
  17.  
  18.     This program is distributed in the hope that it will be useful,
  19.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.     GNU General Public License for more details.
  22.  
  23.     You should have received a copy of the GNU General Public License
  24.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  25.  
  26. */
  27.  
  28. #include "WProgram.h"
  29. #include "LcdBarGraph.h"
  30.  
  31. #include <LiquidCrystal.h>
  32.  
  33.  
  34. // -- initializing bar segment characters
  35. // -- filled character
  36. byte LcdBarGraph::_level0[8] = {
  37.     B11111,
  38.     B11111,
  39.     B11111,
  40.     B11111,
  41.     B11111,
  42.     B11111,
  43.     B11111,
  44.     B11111
  45. };
  46. // -- character with one bar
  47. byte LcdBarGraph::_level1[8] = {
  48.     B10000,
  49.     B10000,
  50.     B10000,
  51.     B10000,
  52.     B10000,
  53.     B10000,
  54.     B10000,
  55.     B10000
  56. };
  57. // -- character with two bars
  58. byte LcdBarGraph::_level2[8] = {
  59.     B11000,
  60.     B11000,
  61.     B11000,
  62.     B11000,
  63.     B11000,
  64.     B11000,
  65.     B11000,
  66.     B11000
  67. };
  68. // -- character with three bars
  69. byte LcdBarGraph::_level3[8] = {
  70.     B11100,
  71.     B11100,
  72.     B11100,
  73.     B11100,
  74.     B11100,
  75.     B11100,
  76.     B11100,
  77.     B11100
  78. };
  79. // -- character with four bars
  80. byte LcdBarGraph::_level4[8] = {
  81.     B11110,
  82.     B11110,
  83.     B11110,
  84.     B11110,
  85.     B11110,
  86.     B11110,
  87.     B11110,
  88.     B11110
  89. };
  90.  
  91. // -- constructor
  92. LcdBarGraph::LcdBarGraph(LiquidCrystal* lcd, byte numCols, byte startCol, byte startRow)
  93. {
  94.     // -- setting fields
  95.     _lcd = lcd;
  96.     _numCols = numCols;
  97.     _startCol = startCol;
  98.     _startRow = startRow;
  99.     // -- creating characters
  100.     _lcd->createChar(0, this->_level0);
  101.     _lcd->createChar(1, this->_level1);
  102.     _lcd->createChar(2, this->_level2);
  103.     _lcd->createChar(3, this->_level3);
  104.     _lcd->createChar(4, this->_level4);
  105.     // -- setting initial values
  106.     this->_prevValue = 0; // -- cached value
  107.     this->_lastFullChars = 0; // -- cached value
  108. }
  109.  
  110. // -- the draw function
  111. void LcdBarGraph::drawValue(int value, int maxValue) {
  112.     // -- calculate full (filled) character count
  113.     byte fullChars = value * _numCols / maxValue;
  114.     // -- calculate partial character bar count
  115.     byte mod = ((long)value * _numCols * 5 / maxValue) % 5;
  116.  
  117.     // -- if value does not change, do not draw anything
  118.     int normalizedValue = (int)fullChars * 5 + mod;
  119.     if(this->_prevValue != normalizedValue) {
  120.         // -- do not clear the display to eliminate flickers
  121.         _lcd->setCursor(_startCol,_startRow);
  122.        
  123.         // -- write filled characters
  124.         for(byte i=0; i<fullChars; i++) {
  125.             _lcd->write(0);
  126.         }
  127.        
  128.         // -- write the partial character
  129.         if(mod > 0) {
  130.             _lcd->write(mod); // -- index the right partial character
  131.             ++fullChars;
  132.         }
  133.        
  134.         // -- clear characters left over the previous draw
  135.         for(byte i=fullChars;i<this->_lastFullChars;i++) {
  136.             _lcd->write(' ');
  137.         }
  138.        
  139.         // -- save cache
  140.         this->_lastFullChars = fullChars;
  141.         this->_prevValue = normalizedValue;
  142.     }
  143.     /*
  144.     // debug info
  145.     _lcd->setCursor(0,1);
  146.     _lcd->write('[');
  147.     _lcd->print((int)value);
  148.     _lcd->write(' ');
  149.     _lcd->print(normalizedValue);
  150.     _lcd->write(' ');
  151.     _lcd->print((int)mod);
  152.     _lcd->write(']');
  153.     */
  154. }