A class to display a value as a bar on either the on-board LEDs, or using LEDs connected to the pwm pins.

Files at this revision

API Documentation at this revision

Comitter:
EricWieser
Date:
Tue Nov 09 19:30:45 2010 +0000
Child:
1:6b38423b75db
Commit message:
First commit

Changed in this revision

BarChart.cpp Show annotated file Show diff for this revision Revisions of this file
BarChart.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BarChart.cpp	Tue Nov 09 19:30:45 2010 +0000
@@ -0,0 +1,74 @@
+#include "BarChart.h"
+#include "mbed.h"
+
+
+BarChart::~BarChart() {
+    for(int i = 0; i < _numPins; i++)
+    {
+        delete _outputs[i];
+    }
+    delete [] _outputs;
+}
+
+BarChart::BarChart() {
+    PwmOut ** tmp = _outputs = new PwmOut*[4];
+    *tmp++ = new PwmOut(LED1);
+    *tmp++ = new PwmOut(LED2);
+    *tmp++ = new PwmOut(LED3);
+    *tmp++ = new PwmOut(LED4);
+    _numPins = 4;
+    _init();
+}
+
+BarChart::BarChart(PinName p1,  PinName p2 = NC,  PinName p3 = NC,  PinName p4 = NC,
+                   PinName p5 = NC,  PinName p6 = NC,  PinName p7 = NC,  PinName p8 = NC,
+                   PinName p9 = NC,  PinName p10 = NC, PinName p11 = NC, PinName p12 = NC,
+                   PinName p13 = NC, PinName p14 = NC, PinName p15 = NC, PinName p16 = NC) {
+    PinName all[16] = {p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16};
+
+    _numPins = 0;
+    for (int i = 0; i < 16; i++)
+        if (all[i] != NC)
+            _numPins++;
+
+    _outputs = new PwmOut*[_numPins];
+
+    _numPins = 0;
+    for (int i = 0; i < 16; i++)
+        if (all[i] != NC)
+            _outputs[_numPins++] = new PwmOut(all[i]);
+
+    _init();
+}
+
+void BarChart::_init() {
+    setInputLimits(0, 1);
+    setOutputLimits(0, 1);
+}
+
+void BarChart::setInputLimits(float min, float max) {
+    _minInput = min;
+    _maxInput = max;
+}
+
+void BarChart::setOutputLimits(float min, float max) {
+    _minOutput = min;
+    _maxOutput = max;
+}
+
+float BarChart::_linearScale(float value) {
+    return (value - _minInput)/(_maxInput - _minInput);
+}
+
+void BarChart::show(float value) {
+    value = _linearScale(value) * _numPins;
+
+    for (int i = 0; i < _numPins; i++) {
+       if (i < value - 1)
+            *_outputs[i] = _maxOutput;
+        else if (i > value)
+            *_outputs[i] = _minOutput;
+        else
+            *_outputs[i] = _minOutput + (value - i)*(_maxOutput - _minOutput);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BarChart.h	Tue Nov 09 19:30:45 2010 +0000
@@ -0,0 +1,75 @@
+#ifndef MBED_BAR_CHART_H
+#define MBED_BAR_CHART_H
+
+#include "mbed.h"
+/** Bar chart output class, based on an array of PwmOuts
+*
+* Example:
+ * @code
+ * // Display the voltage at pin 20 as a bar chart on the on-board LEDs
+ * #include "mbed.h"
+ * #include "BarChart.h"
+ *
+ * BarChart b;
+ * AnalogIn input(p20);
+
+ * int main() {
+ *    //Set the maximum and minimum LED brightness
+ *    b.setOutputLimits(0.1,0.9);
+ *
+ *    while(1) {
+ *        b = input;
+ *        //equivalent to b.show(in);
+ *
+ *        wait(0.01);
+ *    }
+ * }
+ * @endcode
+ */
+class BarChart {
+public:
+    /** Create a Bar chart from the 4 LEDs mounted on the board */
+    BarChart();
+    /** Create a Bar chart from the (PWM) pins specified
+     *
+     * @param pxx the pins to use, lowest bar first
+     */
+    BarChart(PinName p1,  PinName p2,  PinName p3,  PinName p4,
+             PinName p5,  PinName p6,  PinName p7,  PinName p8,
+             PinName p9,  PinName p10, PinName p11, PinName p12,
+             PinName p13, PinName p14, PinName p15, PinName p16);
+    /** Set the range of input that will be provided to the BarChart
+     *
+     * @param min The lowest value that the barchart will display
+     * @param max The highest value that the barchart will display
+     */
+    void setInputLimits(float min, float max);
+    /** Set the range of output that will be provided to the PWM pins
+     *
+     * @param min The lowest duty cycle of the PwmOuts
+     * @param max The highest duty cycle of the PwmOuts
+     */
+    void setOutputLimits(float min, float max);
+    /** Output the value to the PwmOuts
+     *
+     * @param value The value to display
+     */
+    void show(float value);
+    /** Shorthand for the show function */
+    BarChart& operator = (float value) {
+        show(value);
+        return *this;
+    };
+    ~BarChart();
+private:
+    PwmOut **_outputs;
+    int _numPins;
+    float _maxInput;
+    float _minInput;
+    float _maxOutput;
+    float _minOutput;
+    float _linearScale(float value);
+    void _init();
+};
+
+#endif
\ No newline at end of file