Used to draw horizontal bar indicators using characters on a Gameduino display. Requires Gameduino library.

This is used to display moving bar style indicators on a Gameduino VGA display. You will require the Gameduino library in order to use this. The indicators are made up of a horizontal strip of characters. The left side of the indicator represents the minimum possible reading and the right side represents the maximum possible reading. When you draw the indicator you specify what reading to display and the code works out which characters to display so that a bar is drawn starting at the left and extending right up to an appropriate position. The BarIndicator class allows you to specify the location of the indicator on the screen, the length of the indicator (the number of characters to use) and the minimum and maximum values for the bar. You can also change which section of the character set to use when drawing the bar.

Files at this revision

API Documentation at this revision

Comitter:
RichardE
Date:
Sun Nov 11 13:01:20 2012 +0000
Child:
1:2d2a358a8f7d
Commit message:
First commit. Uses characters '0' to '9' to make indicators by default.

Changed in this revision

BarIndicator.cpp Show annotated file Show diff for this revision Revisions of this file
BarIndicator.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BarIndicator.cpp	Sun Nov 11 13:01:20 2012 +0000
@@ -0,0 +1,74 @@
+/*
+ * SOURCE FILE : BarIndicator.cpp
+ *
+ * Definition of class BarIndicator.
+ *
+ */
+
+#include "BarIndicator.h"
+
+/** Constructor.
+ */
+BarIndicator::BarIndicator() :
+    startCode( (UInt8)'0' ),
+    min( (float)0 ),
+    max( (float)100 ),
+    xco( 0 ),
+    yco( 0 ),
+    length( 10 )
+{
+}
+
+/** Destructor.
+ */
+BarIndicator::~BarIndicator() {
+}
+
+/** Draw bar indicator.
+ * Invalid readings are drawn using a greyed out indicator.
+ * @param gd Pointer to Gameduino to use for drawing.
+ * @param reading Reading to display on indicator.
+ * @param valid True if reading is valid, False if not.
+ */
+void BarIndicator::Draw( Gameduino *gd, float reading, bool valid ) {
+    char text[] = "X";
+    if( valid ) {
+        // Work out fraction of indicator link that is filled.
+        float frac = ( reading - min ) / ( max - min );
+        // Don't allow fraction to stray outside of range 0.0 to 1.0.
+        if( frac < (float)0 ) {
+          frac = (float)0;
+        }
+        else if( frac > (float)1 ) {
+          frac = (float)1;
+        }
+        // Work out character position of character that is partially filled.
+        UInt8 pos = (UInt8)( frac * (float)length );
+        // Draw filled part of indicator.
+        UInt8 x;
+        text[ 0 ] = (char)( startCode + 8 );
+        for( x = xco; x < xco + pos; ++x ) {
+          gd->putstr( x, yco, text );
+        }
+        // Don't do rest if indicator is at maximum.
+        if( pos < length ) {
+            // Work out how much of next character needs to be filled.
+            UInt8 rem = (UInt8)( (int)floor( frac * (float)length * (float)8 ) & 7 );
+            // Draw partially filled character.
+            text[ 0 ] = startCode + rem;
+            gd->putstr( xco + pos, yco, text );
+            // Draw unfilled characters for remainder of indicator.
+            text[ 0 ] = (char)( startCode + 0 );
+            for( x = xco + pos + 1; x < xco + length; ++x ) {
+              gd->putstr( x, yco, text );
+            }
+        }
+    }
+    else {
+        // Draw greyed out characters for whole indicator.
+        text[ 0 ] = (char)( startCode + 9 );
+        for( UInt8 x = xco; x < xco + length; ++x ) {
+          gd->putstr( x, yco, text );
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BarIndicator.h	Sun Nov 11 13:01:20 2012 +0000
@@ -0,0 +1,98 @@
+/*
+ * SOURCE FILE : BarIndicator.h
+ *
+ * Definition of class BarIndicator.
+ *
+ */
+
+#ifndef BarIndicatorDefined
+
+  #define BarIndicatorDefined
+
+  #include "Gameduino.h"
+  
+  /** Used to draw moving bar indicators on a Gameduino display.
+   * The indicators are constructed using suitably defined characters.
+   */
+  class BarIndicator {
+
+  public :
+
+    /** Constructor.
+     */
+    BarIndicator();
+
+    /** Destructor.
+     */
+    virtual ~BarIndicator();
+
+    /** Set character code at which thermometer characters start.
+     * There should be 10 characters defined starting at this code, in this order:
+     * 0 : Empty
+     * 1 : 1/8 full
+     * 2 : 1/0 full
+     * 3 : 3/8 full
+     * 4 : 1/2 full
+     * 5 : 5/8 full
+     * 6 : 3/4 full
+     * 7 : 7/8 full
+     * 8 : Full
+     * 9 : Greyed out (used when reading is invalid).
+     * @params code Character code at which thermometer characters start.
+     */
+    void SetStartCode( UInt8 sc ) {
+      startCode = sc;
+    }
+     
+    /** Set minimum and maximum readings.
+     * @param min Smallest reading that can be shown on bar indicator.
+     * @param max Largest reading that can be shown on bar indicator.
+     */
+    void SetLimits( float min, float max ) {
+      this->min = min;
+      this->max = max;
+    }
+    
+    /** Set location of indicator on screen using character coordinates.
+     * @param x X coordinate for indicator.
+     * @param y Y coordinate for indicator.
+     */
+    void SetLocation( UInt8 x, UInt8 y ) {
+      xco = x;
+      yco = y;
+    }
+    
+    /** Set length of indicator (in characters).
+     * @param len Length of indicator.
+     */
+    void SetLength( UInt8 len ) {
+      length = len;
+    }
+    
+    /** Draw bar indicator.
+     * Invalid readings are drawn using a greyed out indicator.
+     * @param gd Pointer to Gameduino to use for drawing.
+     * @param reading Reading to display on indicator.
+     * @param valid True if reading is valid, False if not.
+     */
+    void Draw( Gameduino *gd, float reading, bool valid );
+    
+  private :
+  
+    // Character code at which thermometer characters start.
+    UInt8 startCode;
+    
+    // Minimum and maximum readings on thermometer.
+    float min, max;
+
+    // Coordinates of indicator.
+    UInt8 xco, yco;
+       
+    // Length of indicator.
+    UInt8 length;
+     
+  };
+
+#endif
+
+/* END of BarIndicator.h */