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
flexiBar.cpp@8:8a2d07e4a8c3, 2017-10-23 (annotated)
- Committer:
- CSTritt
- Date:
- Mon Oct 23 19:10:39 2017 +0000
- Revision:
- 8:8a2d07e4a8c3
- Child:
- 9:d87a0d02a0ac
Improved opening comments. Changed out-of-range error indication from -1 to 1.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| CSTritt | 8:8a2d07e4a8c3 | 1 | /* |
| CSTritt | 8:8a2d07e4a8c3 | 2 | Project: FlexiBarDemo |
| CSTritt | 8:8a2d07e4a8c3 | 3 | File: flexiBar.cpp |
| CSTritt | 8:8a2d07e4a8c3 | 4 | Function: flexiBar (v. 1.1) |
| CSTritt | 8:8a2d07e4a8c3 | 5 | Created by: Dr. C. S. Tritt, 10/22/17 |
| CSTritt | 8:8a2d07e4a8c3 | 6 | |
| CSTritt | 8:8a2d07e4a8c3 | 7 | This function displays an integer value (range 0 to 9) on the specified bar |
| CSTritt | 8:8a2d07e4a8c3 | 8 | graph display. |
| CSTritt | 8:8a2d07e4a8c3 | 9 | |
| CSTritt | 8:8a2d07e4a8c3 | 10 | Precondition(s): |
| CSTritt | 8:8a2d07e4a8c3 | 11 | |
| CSTritt | 8:8a2d07e4a8c3 | 12 | Physical bar graph display correctly wired to digital output pins. |
| CSTritt | 8:8a2d07e4a8c3 | 13 | BusOut object that collects display output pins. |
| CSTritt | 8:8a2d07e4a8c3 | 14 | |
| CSTritt | 8:8a2d07e4a8c3 | 15 | Postcondition(s): |
| CSTritt | 8:8a2d07e4a8c3 | 16 | |
| CSTritt | 8:8a2d07e4a8c3 | 17 | Bar graph display is left showing the appropriate output. |
| CSTritt | 8:8a2d07e4a8c3 | 18 | |
| CSTritt | 8:8a2d07e4a8c3 | 19 | Arguments/Parameters: |
| CSTritt | 8:8a2d07e4a8c3 | 20 | |
| CSTritt | 8:8a2d07e4a8c3 | 21 | int value - Value to be displayed. Range -1 to 9, inclusive. |
| CSTritt | 8:8a2d07e4a8c3 | 22 | BusOut &theBar - Bus created from connected GPIO output pins. |
| CSTritt | 8:8a2d07e4a8c3 | 23 | bool single - Single vs. stacked display format. True indicates single. |
| CSTritt | 8:8a2d07e4a8c3 | 24 | bool active - Active high flag. True indicates active high. |
| CSTritt | 8:8a2d07e4a8c3 | 25 | |
| CSTritt | 8:8a2d07e4a8c3 | 26 | Returns (& Error Handling): |
| CSTritt | 8:8a2d07e4a8c3 | 27 | |
| CSTritt | 8:8a2d07e4a8c3 | 28 | 0 - Normal operation. |
| CSTritt | 8:8a2d07e4a8c3 | 29 | 1 - Error, value out of range. |
| CSTritt | 8:8a2d07e4a8c3 | 30 | |
| CSTritt | 8:8a2d07e4a8c3 | 31 | Notes: |
| CSTritt | 8:8a2d07e4a8c3 | 32 | |
| CSTritt | 8:8a2d07e4a8c3 | 33 | To assure display is operational, at least one bar will always be lit |
| CSTritt | 8:8a2d07e4a8c3 | 34 | (0 is indicated by a single bar and 9 by 10 bars). A special case is the |
| CSTritt | 8:8a2d07e4a8c3 | 35 | the value -1, which will light no bars. |
| CSTritt | 8:8a2d07e4a8c3 | 36 | |
| CSTritt | 8:8a2d07e4a8c3 | 37 | I previously thought pow would be better than left shift (<<), but now I see |
| CSTritt | 8:8a2d07e4a8c3 | 38 | that left shift really is the best approach. |
| CSTritt | 8:8a2d07e4a8c3 | 39 | |
| CSTritt | 8:8a2d07e4a8c3 | 40 | Mbed objects generally must be passed by reference (with &). This prevents |
| CSTritt | 8:8a2d07e4a8c3 | 41 | there possibly two or more instances of the objects possibly having |
| CSTritt | 8:8a2d07e4a8c3 | 42 | conflicting states. |
| CSTritt | 8:8a2d07e4a8c3 | 43 | |
| CSTritt | 8:8a2d07e4a8c3 | 44 | Pass theBar (rather than using the global symbol) for flexibility. |
| CSTritt | 8:8a2d07e4a8c3 | 45 | |
| CSTritt | 8:8a2d07e4a8c3 | 46 | Note there are 2 returns in this function! One for value being out of range |
| CSTritt | 8:8a2d07e4a8c3 | 47 | errors. The other for no error. |
| CSTritt | 8:8a2d07e4a8c3 | 48 | */ |
| CSTritt | 8:8a2d07e4a8c3 | 49 | #include "mbed.h" |
| CSTritt | 8:8a2d07e4a8c3 | 50 | |
| CSTritt | 8:8a2d07e4a8c3 | 51 | int flexiBar(int value, BusOut &theBar, bool single, bool active) |
| CSTritt | 8:8a2d07e4a8c3 | 52 | { |
| CSTritt | 8:8a2d07e4a8c3 | 53 | |
| CSTritt | 8:8a2d07e4a8c3 | 54 | if (value < -1 || value > 9) return 1; // Indicate value out-of-range error. |
| CSTritt | 8:8a2d07e4a8c3 | 55 | |
| CSTritt | 8:8a2d07e4a8c3 | 56 | int output; // Use variable to make active low case easy to compute. |
| CSTritt | 8:8a2d07e4a8c3 | 57 | |
| CSTritt | 8:8a2d07e4a8c3 | 58 | if (value == -1) { |
| CSTritt | 8:8a2d07e4a8c3 | 59 | output = 0; // Special case of all bars off. |
| CSTritt | 8:8a2d07e4a8c3 | 60 | } else { |
| CSTritt | 8:8a2d07e4a8c3 | 61 | if (single) { |
| CSTritt | 8:8a2d07e4a8c3 | 62 | output = 1 << value; // Light single bars by shifting 1 left. |
| CSTritt | 8:8a2d07e4a8c3 | 63 | } else { |
| CSTritt | 8:8a2d07e4a8c3 | 64 | output = (1 << (value + 1)) - 1; // Light a stack of bars. |
| CSTritt | 8:8a2d07e4a8c3 | 65 | } |
| CSTritt | 8:8a2d07e4a8c3 | 66 | } |
| CSTritt | 8:8a2d07e4a8c3 | 67 | |
| CSTritt | 8:8a2d07e4a8c3 | 68 | if (active) { // Deal with active low vs. active high. |
| CSTritt | 8:8a2d07e4a8c3 | 69 | theBar = output; |
| CSTritt | 8:8a2d07e4a8c3 | 70 | } else { |
| CSTritt | 8:8a2d07e4a8c3 | 71 | theBar = ~output; // Invert bits for active low. |
| CSTritt | 8:8a2d07e4a8c3 | 72 | } |
| CSTritt | 8:8a2d07e4a8c3 | 73 | return 0; // |
| CSTritt | 8:8a2d07e4a8c3 | 74 | } |
