Charles Tritt / Mbed 2 deprecated FlexiBarDemo

Dependencies:   mbed

Fork of BinaryCount by Charles Tritt

Committer:
CSTritt
Date:
Fri Oct 20 17:27:02 2017 +0000
Revision:
7:031078522195
Parent:
6:0c1ab2c11252
Child:
8:8a2d07e4a8c3
Initial version.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CSTritt 0:8ed2f4a2a2fe 1 /*
CSTritt 7:031078522195 2 Project: FlexiBarDemo
CSTritt 0:8ed2f4a2a2fe 3 File: main.cpp
CSTritt 7:031078522195 4
CSTritt 7:031078522195 5 Test harness for flexiBar bar graph display function.
CSTritt 7:031078522195 6
CSTritt 0:8ed2f4a2a2fe 7 Written by: Dr. C. S. Tritt
CSTritt 6:0c1ab2c11252 8 Created: 9/13/17 (v. 1.0)
CSTritt 0:8ed2f4a2a2fe 9 */
CSTritt 0:8ed2f4a2a2fe 10 #include "mbed.h"
CSTritt 0:8ed2f4a2a2fe 11
CSTritt 7:031078522195 12 // Create 10-bit BusOut object called barGraph.
CSTritt 7:031078522195 13 BusOut barGraph(D11, D10, D9, D8, D7, D6, D5, D4, D3, D2);
CSTritt 7:031078522195 14
CSTritt 7:031078522195 15 // Displays specified digit on theBar. Returns non-zero in case of errors. Mbed
CSTritt 7:031078522195 16 // objects generally must be based by reference. See function definition for
CSTritt 7:031078522195 17 // more information.
CSTritt 7:031078522195 18 int flexiBar(int value, BusOut &theBar, bool single, bool active);
CSTritt 0:8ed2f4a2a2fe 19
CSTritt 0:8ed2f4a2a2fe 20 int main() {
CSTritt 6:0c1ab2c11252 21
CSTritt 7:031078522195 22 printf("flexiBar Test Harness\n"); // ID software.
CSTritt 0:8ed2f4a2a2fe 23
CSTritt 7:031078522195 24 // Start by directly testing the display.
CSTritt 7:031078522195 25 barGraph = 0b1111111111; // Light all bars.
CSTritt 7:031078522195 26 wait(2.0);
CSTritt 7:031078522195 27 barGraph = 0; // All bars off.
CSTritt 7:031078522195 28 wait(2.0);
CSTritt 0:8ed2f4a2a2fe 29
CSTritt 7:031078522195 30 // Loop through all values in both modes.
CSTritt 0:8ed2f4a2a2fe 31 while(true) {
CSTritt 7:031078522195 32 bool single = true;
CSTritt 7:031078522195 33 for (int n = 0; n <= 9; n++) {
CSTritt 7:031078522195 34 flexiBar(n, barGraph, single, true);
CSTritt 7:031078522195 35 wait(1.0);
CSTritt 7:031078522195 36 }
CSTritt 7:031078522195 37 single = false;
CSTritt 7:031078522195 38 for (int n = 0; n <= 9; n++) {
CSTritt 7:031078522195 39 flexiBar(n, barGraph, single, true);
CSTritt 7:031078522195 40 wait(1.0);
CSTritt 7:031078522195 41 }
CSTritt 7:031078522195 42 // Test the special case of all bars off. Third argument is ignored.
CSTritt 7:031078522195 43 flexiBar(-1, barGraph, false, true);
CSTritt 7:031078522195 44 wait(2.0);
CSTritt 7:031078522195 45 }
CSTritt 7:031078522195 46 }
CSTritt 7:031078522195 47
CSTritt 7:031078522195 48 int flexiBar(int value, BusOut &theBar, bool single, bool active) {
CSTritt 7:031078522195 49 /*
CSTritt 7:031078522195 50 Function: flexiBar (v. 1.0)
CSTritt 7:031078522195 51 Created by: Dr. C. S. Tritt, 10/20/17
CSTritt 7:031078522195 52
CSTritt 7:031078522195 53 Displays value (range 0 to 9) on the specified bar graph display. If solid
CSTritt 7:031078522195 54 is true, all the lower order bars will be on. Otherwise they will be off.
CSTritt 7:031078522195 55 Active being true indicates the display is wired active high. Otherwise, it
CSTritt 7:031078522195 56 is assumed to be active low. To assure display is operational, the
CSTritt 7:031078522195 57 display will show at least one bar lit at all times (0 is indicated by a
CSTritt 7:031078522195 58 single bar and 9 by 10 bars). A special case is a value of -1 which will
CSTritt 7:031078522195 59 light no bars.
CSTritt 7:031078522195 60
CSTritt 7:031078522195 61 I previously thought pow would be better than left shift (<<), but now I see
CSTritt 7:031078522195 62 that left shift really is the best approach.
CSTritt 7:031078522195 63
CSTritt 7:031078522195 64 Mbed objects generally must be passed by reference (with &).
CSTritt 7:031078522195 65
CSTritt 7:031078522195 66 Pass theBar (rather than use a global symbol) for flexibility.
CSTritt 7:031078522195 67
CSTritt 7:031078522195 68 Note there are 2 returns in this function! One for value being out of range
CSTritt 7:031078522195 69 errors. The other for no error.
CSTritt 7:031078522195 70 */
CSTritt 7:031078522195 71 if (value < -1 || value > 9) return -1; // Value out of range error.
CSTritt 7:031078522195 72
CSTritt 7:031078522195 73 int output; // Use variable to make active low case easy to compute.
CSTritt 7:031078522195 74
CSTritt 7:031078522195 75 if (value == -1) {
CSTritt 7:031078522195 76 output = 0; // Special case of all bars off.
CSTritt 7:031078522195 77 } else {
CSTritt 7:031078522195 78 if (single) {
CSTritt 7:031078522195 79 output = 1 << value; // Light single bars by shifting 1 left.
CSTritt 7:031078522195 80 } else {
CSTritt 7:031078522195 81 output = (1 << (value + 1)) - 1; // Light a stack of bars.
CSTritt 2:5682a72277ed 82 }
CSTritt 0:8ed2f4a2a2fe 83 }
CSTritt 7:031078522195 84
CSTritt 7:031078522195 85 if (active) { // Deal with active low vs. active high.
CSTritt 7:031078522195 86 theBar = output;
CSTritt 7:031078522195 87 } else {
CSTritt 7:031078522195 88 theBar = ~output; // Invert bits for actived low.
CSTritt 7:031078522195 89 }
CSTritt 7:031078522195 90 return 0; //
CSTritt 7:031078522195 91 }