Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BinaryCount by
Diff: main.cpp
- Revision:
- 7:031078522195
- Parent:
- 6:0c1ab2c11252
- Child:
- 8:8a2d07e4a8c3
--- a/main.cpp Wed Sep 13 10:32:11 2017 +0000 +++ b/main.cpp Fri Oct 20 17:27:02 2017 +0000 @@ -1,29 +1,91 @@ /* - Project: BinaryCount + Project: FlexiBarDemo File: main.cpp - - Displays 4 bit binary count on bar graph display. - + + Test harness for flexiBar bar graph display function. + Written by: Dr. C. S. Tritt Created: 9/13/17 (v. 1.0) */ #include "mbed.h" -BusOut barGraph(D5, D4, D3, D2); // Create barGraph BusOut object. -DigitalOut pinD7(D7); // Needed to force this pin off. +// 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("Binary count example\n"); // ID software. + printf("flexiBar Test Harness\n"); // ID software. - pinD7 = 0; // Force pin D7 off. This shouldn't be necessary. + // Start by directly testing the display. + barGraph = 0b1111111111; // Light all bars. + wait(2.0); + barGraph = 0; // All bars off. + wait(2.0); - barGraph = 0; // Light top bar. + // Loop through all values in both modes. while(true) { - barGraph = barGraph + 1; // Add one to count. - wait(1.0); - if (barGraph == 15) { - barGraph = 0; + 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. } } -} \ No newline at end of file + + if (active) { // Deal with active low vs. active high. + theBar = output; + } else { + theBar = ~output; // Invert bits for actived low. + } + return 0; // +} \ No newline at end of file