Charles Tritt / Mbed 2 deprecated FlexiBarDemo

Dependencies:   mbed

Fork of BinaryCount by Charles Tritt

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers flexiBar.cpp Source File

flexiBar.cpp

00001 /*
00002     Project: FlexiBarDemo
00003     File: flexiBar.cpp
00004     Function: flexiBar (v. 1.2)
00005     Created by: Dr. C. S. Tritt, 10/22/17
00006 
00007     This function displays an integer value (range 0 to 9) on the specified bar
00008     graph display. 
00009     
00010     Precondition(s):
00011     
00012         Physical bar graph display correctly wired to GPIO pins.
00013         BusOut object that collects display GPIO pins.
00014     
00015     Postcondition(s):
00016     
00017         Bar graph display is left showing the appropriate output.
00018     
00019     Arguments/Parameters:
00020     
00021         int value - Value to be displayed. Range -1 to 9, inclusive.
00022         BusOut &theBar - Bus created from connected GPIO output pins.
00023         bool single - Single vs. stacked display format. True indicates single.
00024         bool active - Active high flag. True indicates active high.
00025     
00026     Returns (& Error Handling):
00027     
00028         0 - Normal operation.
00029         1 - Error, value out of range.
00030         
00031     Notes:
00032     
00033     To assure display is operational, at least one bar will always be lit
00034     (0 is indicated by a single bar and 9 by 10 bars). A special case is the 
00035     the value -1, which will light no bars.
00036 
00037     I previously thought pow would be better than left shift (<<), but now I see
00038     that left shift really is the best approach.
00039 
00040     Mbed objects generally must be passed by reference (with &). This prevents
00041     there possibly two or more instances of the objects possibly having 
00042     conflicting states.
00043 
00044     Pass theBar (rather than using the global symbol) for flexibility.
00045 
00046     Note there are 2 returns in this function! One for value being out of range
00047     errors. The other for no error.
00048 */
00049 #include "mbed.h"
00050 
00051 int flexiBar(int value, BusOut &theBar, bool single, bool active)
00052 {
00053 
00054     if (value < -1 || value > 9) return 1; // Indicate value out-of-range error.
00055 
00056     int output; // Use variable to make active low case easy to compute.
00057 
00058     if (value == -1) {
00059         output = 0;  // Special case of all bars off.
00060     } else {
00061         if (single) {
00062             output = 1 << value; // Light single bars by shifting 1 left.
00063         } else {
00064             output = (1 << (value + 1)) - 1; // Light a stack of bars.
00065         }
00066     }
00067 
00068     if (active) { // Deal with active low vs. active high.
00069         theBar = output;
00070     } else {
00071         theBar = ~output; // Invert bits for active low.
00072     }
00073     return 0; //
00074 }