simple GUI elements for drawing on a canvas, works with adafruit graphics lib

Dependents:   ezSBC_MPU9250

Files at this revision

API Documentation at this revision

Comitter:
JojoS
Date:
Mon Apr 24 16:21:53 2017 +0000
Commit message:
simple GUI elements

Changed in this revision

Controls.cpp Show annotated file Show diff for this revision Revisions of this file
Controls.h Show annotated file Show diff for this revision Revisions of this file
SmallGUIBase.cpp Show annotated file Show diff for this revision Revisions of this file
SmallGUIBase.h Show annotated file Show diff for this revision Revisions of this file
SmallGUI_config.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r ccdf1edcbba6 Controls.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Controls.cpp	Mon Apr 24 16:21:53 2017 +0000
@@ -0,0 +1,49 @@
+#include "Controls.h"
+
+#if (USE_BARGRAPH == 1)
+barGraph::barGraph(bgOrientation _orientation, int16_t _x0, int16_t _y0, int16_t _x1, int16_t _y1) :
+    baseControl()
+{
+    orientation = _orientation;
+    x0 = _x0;
+    y0 = _y0;
+    x1 = _x1;
+    y1 = _y1;
+}
+
+void barGraph::setScale(float _valMin, float _valMax)
+{
+    valMin = _valMin;
+    valMax = _valMax;
+    if (orientation == bgHorizontal) {
+        valScaleFactor = (x1 - x0) / (valMax - valMin);
+        zeroPos = x0 - valScaleFactor * valMin;
+    } else {
+        valScaleFactor = (y1 - y0) / (valMax - valMin);
+        zeroPos = y0 - valScaleFactor * valMin;
+    }
+}
+
+void barGraph::setValue(float _val)
+{
+    val = _val;
+}
+
+void barGraph::draw(Adafruit_GFX &canvas)
+{
+    int16_t p;
+    int16_t w;
+    
+    if (orientation == bgHorizontal) {
+        p = x0 + valScaleFactor * (val - valMin);
+        for (w = y0; w <= y1; w++)
+            canvas.drawLine(zeroPos, w, p, w, WHITE);
+        
+    } else {
+        p = y0 + valScaleFactor * (val - valMin);
+        for (w = x0; w <= x1; w++)
+            canvas.drawLine(zeroPos, w, p, w, WHITE);
+    }
+}
+
+#endif
diff -r 000000000000 -r ccdf1edcbba6 Controls.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Controls.h	Mon Apr 24 16:21:53 2017 +0000
@@ -0,0 +1,35 @@
+#ifndef _Controls_h_
+#define _Controls_h_
+
+#include "mbed.h"
+#include "SmallGUIBase.h"
+
+#if (USE_BARGRAPH == 1)
+class barGraph : public baseControl
+{
+    public:
+    enum bgOrientation {bgHorizontal, bgVertical};
+    
+    barGraph(bgOrientation orientation, int16_t x0, int16_t y0, int16_t x1, int16_t y1);
+    
+    void setScale(float valMin, float valMax);
+    void setValue(float val);
+    
+    virtual void draw(Adafruit_GFX &canvas);
+    
+    private:
+    bgOrientation   orientation;
+    int16_t x0;
+    int16_t y0;
+    int16_t x1;
+    int16_t y1;
+    int16_t zeroPos;
+    float valMin;
+    float valMax;
+    float valScaleFactor;
+    float val;
+    
+};
+#endif
+
+#endif
diff -r 000000000000 -r ccdf1edcbba6 SmallGUIBase.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SmallGUIBase.cpp	Mon Apr 24 16:21:53 2017 +0000
@@ -0,0 +1,23 @@
+#include "SmallGUIBase.h"
+
+Canvas::Canvas(Adafruit_GFX &_gfxDevice) :
+    gfxDevice(_gfxDevice)
+{
+    ctrlCount = 0;
+}
+
+void Canvas::draw()
+{
+    // call draw() on all controls
+    int i;
+    for (i=0; i < ctrlCount; i++)
+        ctrlList[i]->draw(gfxDevice);
+}
+
+void Canvas::addControl(baseControl &bc)
+{
+    if (ctrlCount < MAX_CONTROLS_PER_CANVAS) {
+        ctrlList[ctrlCount] = &bc;
+        ctrlCount++;
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r ccdf1edcbba6 SmallGUIBase.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SmallGUIBase.h	Mon Apr 24 16:21:53 2017 +0000
@@ -0,0 +1,28 @@
+#ifndef _SMALLGUI_BASE_H_
+#define _SMALLGUI_BASE_H_
+
+#include "mbed.h"
+#include "SmallGUI_config.h"
+
+class baseControl
+{
+    public:
+    baseControl() {};
+    virtual void draw(Adafruit_GFX &_gfxDevice) {};
+};
+    
+class Canvas
+{
+    public:
+    Canvas(Adafruit_GFX &_gfxDevice);
+    void draw();
+    void addControl(baseControl &ctrl);
+    
+    private:
+    Adafruit_GFX &gfxDevice;
+    int ctrlCount;
+    baseControl *ctrlList[MAX_CONTROLS_PER_CANVAS];
+};
+    
+
+#endif
diff -r 000000000000 -r ccdf1edcbba6 SmallGUI_config.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SmallGUI_config.h	Mon Apr 24 16:21:53 2017 +0000
@@ -0,0 +1,5 @@
+// include basic graphic functions
+#include "Adafruit_GFX.h"
+
+#define MAX_CONTROLS_PER_CANVAS     20
+#define USE_BARGRAPH    1