Charles Tritt / Mbed 2 deprecated FlexiBarDemo

Dependencies:   mbed

Fork of BinaryCount by Charles Tritt

main.cpp

Committer:
CSTritt
Date:
2017-10-20
Revision:
7:031078522195
Parent:
6:0c1ab2c11252
Child:
8:8a2d07e4a8c3

File content as of revision 7:031078522195:

/*
    Project: FlexiBarDemo
    File: main.cpp

    Test harness for flexiBar bar graph display function.

    Written by: Dr. C. S. Tritt
    Created: 9/13/17 (v. 1.0)
*/
#include "mbed.h"

// Create 10-bit BusOut object called barGraph.
BusOut barGraph(D11, D10, D9, D8, D7, D6, D5, D4, D3, D2);

// Displays specified digit on theBar. Returns non-zero in case of errors. Mbed 
// objects generally must be based by reference. See function definition for 
// more information.
int flexiBar(int value, BusOut &theBar, bool single, bool active);

int main() {

    printf("flexiBar Test Harness\n"); // ID software.
    
    // Start by directly testing the display.
    barGraph = 0b1111111111;  // Light all bars.
    wait(2.0);
    barGraph = 0;  // All bars off.
    wait(2.0);
    
    // Loop through all values in both modes.
    while(true) {
        bool single = true;
        for (int n = 0; n <= 9; n++) {
            flexiBar(n, barGraph, single, true);
            wait(1.0);
        }
        single = false;
        for (int n = 0; n <= 9; n++) {
            flexiBar(n, barGraph, single, true);
            wait(1.0);
        }
        // Test the special case of all bars off. Third argument is ignored.
        flexiBar(-1, barGraph, false, true);
        wait(2.0);   
    }
}

int flexiBar(int value, BusOut &theBar, bool single, bool active) {
/*
    Function: flexiBar (v. 1.0)
    Created by: Dr. C. S. Tritt, 10/20/17
    
    Displays value (range 0 to 9) on the specified bar graph display. If solid 
    is true, all the lower order bars will be on. Otherwise they will be off. 
    Active being true indicates the display is wired active high. Otherwise, it
    is assumed to be active low. To assure display is operational, the 
    display will show at least one bar lit at all times (0 is indicated by a 
    single bar and 9 by 10 bars). A special case is a value of -1 which will 
    light no bars.
    
    I previously thought pow would be better than left shift (<<), but now I see
    that left shift really is the best approach.
    
    Mbed objects generally must be passed by reference (with &).
    
    Pass theBar (rather than use a global symbol) for flexibility.
    
    Note there are 2 returns in this function! One for value being out of range
    errors. The other for no error.
*/
    if (value < -1 || value > 9) return -1; // Value out of range error.
    
    int output; // Use variable to make active low case easy to compute.
    
    if (value == -1) {
        output = 0;  // Special case of all bars off.
    } else {
        if (single) {
             output = 1 << value; // Light single bars by shifting 1 left.
        } else {
             output = (1 << (value + 1)) - 1; // Light a stack of bars.
        }
    }
    
    if (active) { // Deal with active low vs. active high.
        theBar = output;
    } else {
        theBar = ~output; // Invert bits for actived low.
    }
    return 0; // 
}