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.

Committer:
RichardE
Date:
Sun Nov 11 17:06:06 2012 +0000
Revision:
3:6607ad01c5a1
Parent:
0:d4ba843a0e6a
BarIndicator constructor now defaults to using character codes 128 to 137 when drawing indicators.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RichardE 0:d4ba843a0e6a 1 /*
RichardE 0:d4ba843a0e6a 2 * SOURCE FILE : BarIndicator.cpp
RichardE 0:d4ba843a0e6a 3 *
RichardE 0:d4ba843a0e6a 4 * Definition of class BarIndicator.
RichardE 0:d4ba843a0e6a 5 *
RichardE 0:d4ba843a0e6a 6 */
RichardE 0:d4ba843a0e6a 7
RichardE 0:d4ba843a0e6a 8 #include "BarIndicator.h"
RichardE 0:d4ba843a0e6a 9
RichardE 0:d4ba843a0e6a 10 /** Constructor.
RichardE 0:d4ba843a0e6a 11 */
RichardE 0:d4ba843a0e6a 12 BarIndicator::BarIndicator() :
RichardE 3:6607ad01c5a1 13 startCode( 128 ),
RichardE 0:d4ba843a0e6a 14 min( (float)0 ),
RichardE 0:d4ba843a0e6a 15 max( (float)100 ),
RichardE 0:d4ba843a0e6a 16 xco( 0 ),
RichardE 0:d4ba843a0e6a 17 yco( 0 ),
RichardE 0:d4ba843a0e6a 18 length( 10 )
RichardE 0:d4ba843a0e6a 19 {
RichardE 0:d4ba843a0e6a 20 }
RichardE 0:d4ba843a0e6a 21
RichardE 0:d4ba843a0e6a 22 /** Destructor.
RichardE 0:d4ba843a0e6a 23 */
RichardE 0:d4ba843a0e6a 24 BarIndicator::~BarIndicator() {
RichardE 0:d4ba843a0e6a 25 }
RichardE 0:d4ba843a0e6a 26
RichardE 0:d4ba843a0e6a 27 /** Draw bar indicator.
RichardE 0:d4ba843a0e6a 28 * Invalid readings are drawn using a greyed out indicator.
RichardE 0:d4ba843a0e6a 29 * @param gd Pointer to Gameduino to use for drawing.
RichardE 0:d4ba843a0e6a 30 * @param reading Reading to display on indicator.
RichardE 0:d4ba843a0e6a 31 * @param valid True if reading is valid, False if not.
RichardE 0:d4ba843a0e6a 32 */
RichardE 0:d4ba843a0e6a 33 void BarIndicator::Draw( Gameduino *gd, float reading, bool valid ) {
RichardE 0:d4ba843a0e6a 34 char text[] = "X";
RichardE 0:d4ba843a0e6a 35 if( valid ) {
RichardE 0:d4ba843a0e6a 36 // Work out fraction of indicator link that is filled.
RichardE 0:d4ba843a0e6a 37 float frac = ( reading - min ) / ( max - min );
RichardE 0:d4ba843a0e6a 38 // Don't allow fraction to stray outside of range 0.0 to 1.0.
RichardE 0:d4ba843a0e6a 39 if( frac < (float)0 ) {
RichardE 0:d4ba843a0e6a 40 frac = (float)0;
RichardE 0:d4ba843a0e6a 41 }
RichardE 0:d4ba843a0e6a 42 else if( frac > (float)1 ) {
RichardE 0:d4ba843a0e6a 43 frac = (float)1;
RichardE 0:d4ba843a0e6a 44 }
RichardE 0:d4ba843a0e6a 45 // Work out character position of character that is partially filled.
RichardE 0:d4ba843a0e6a 46 UInt8 pos = (UInt8)( frac * (float)length );
RichardE 0:d4ba843a0e6a 47 // Draw filled part of indicator.
RichardE 0:d4ba843a0e6a 48 UInt8 x;
RichardE 0:d4ba843a0e6a 49 text[ 0 ] = (char)( startCode + 8 );
RichardE 0:d4ba843a0e6a 50 for( x = xco; x < xco + pos; ++x ) {
RichardE 0:d4ba843a0e6a 51 gd->putstr( x, yco, text );
RichardE 0:d4ba843a0e6a 52 }
RichardE 0:d4ba843a0e6a 53 // Don't do rest if indicator is at maximum.
RichardE 0:d4ba843a0e6a 54 if( pos < length ) {
RichardE 0:d4ba843a0e6a 55 // Work out how much of next character needs to be filled.
RichardE 0:d4ba843a0e6a 56 UInt8 rem = (UInt8)( (int)floor( frac * (float)length * (float)8 ) & 7 );
RichardE 0:d4ba843a0e6a 57 // Draw partially filled character.
RichardE 0:d4ba843a0e6a 58 text[ 0 ] = startCode + rem;
RichardE 0:d4ba843a0e6a 59 gd->putstr( xco + pos, yco, text );
RichardE 0:d4ba843a0e6a 60 // Draw unfilled characters for remainder of indicator.
RichardE 0:d4ba843a0e6a 61 text[ 0 ] = (char)( startCode + 0 );
RichardE 0:d4ba843a0e6a 62 for( x = xco + pos + 1; x < xco + length; ++x ) {
RichardE 0:d4ba843a0e6a 63 gd->putstr( x, yco, text );
RichardE 0:d4ba843a0e6a 64 }
RichardE 0:d4ba843a0e6a 65 }
RichardE 0:d4ba843a0e6a 66 }
RichardE 0:d4ba843a0e6a 67 else {
RichardE 0:d4ba843a0e6a 68 // Draw greyed out characters for whole indicator.
RichardE 0:d4ba843a0e6a 69 text[ 0 ] = (char)( startCode + 9 );
RichardE 0:d4ba843a0e6a 70 for( UInt8 x = xco; x < xco + length; ++x ) {
RichardE 0:d4ba843a0e6a 71 gd->putstr( x, yco, text );
RichardE 0:d4ba843a0e6a 72 }
RichardE 0:d4ba843a0e6a 73 }
RichardE 0:d4ba843a0e6a 74 }