Simple demo showing how to control the CoCo-ri-Co LED ring.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Clemo
Date:
Tue Jun 21 07:45:06 2016 +0000
Commit message:
Demo program showing how to control the CoCo-ri-Co's LED ring.

Changed in this revision

LedMatrix.cpp Show annotated file Show diff for this revision Revisions of this file
LedMatrix.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r c6fa86a8f29b LedMatrix.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LedMatrix.cpp	Tue Jun 21 07:45:06 2016 +0000
@@ -0,0 +1,152 @@
+/*
+ * LedMatrix.cpp
+ *
+ *  Created on: 25 january 2016
+ *      Author: CPV
+ */
+
+#include "mbed.h"
+#include "LedMatrix.h"
+
+#define MATRIX_ROWS  (4)
+#define MATRIX_COLUMNS  (4)
+
+
+DigitalOut led17g(CENTER_LED_X,1);
+DigitalOut led17r(CENTER_LED_Y,1);
+
+
+DigitalInOut ledMatrixRows[MATRIX_ROWS] = 
+{
+    DigitalInOut(P0_15,PIN_INPUT,PullNone,0),
+    DigitalInOut(P0_13,PIN_INPUT,PullNone,0),
+    DigitalInOut(P0_16,PIN_INPUT,PullNone,0),
+    DigitalInOut(P0_7,PIN_INPUT,PullNone,0)
+};
+/*DigitalInOut row0(P0_15,PIN_INPUT,PullNone,0);
+DigitalInOut row1(P0_13,PIN_INPUT,PullNone,0);
+DigitalInOut row2(P0_16,PIN_INPUT,PullNone,0);
+DigitalInOut row3(P0_7,PIN_INPUT,PullNone,0);*/
+
+
+DigitalInOut ledMatrixColumns[MATRIX_COLUMNS] = 
+{
+    DigitalInOut(P0_9,PIN_INPUT,PullNone,0),
+    DigitalInOut(P0_8,PIN_INPUT,PullNone,0),
+    DigitalInOut(P0_17,PIN_INPUT,PullNone,0),
+    DigitalInOut(P0_14,PIN_INPUT,PullNone,0)
+};
+/*DigitalInOut col0(P0_9,PIN_INPUT,PullNone,0);
+DigitalInOut col1(P0_8,PIN_INPUT,PullNone,0);
+DigitalInOut col2(P0_17,PIN_INPUT,PullNone,0);
+DigitalInOut col3(P0_14,PIN_INPUT,PullNone,0);*/
+
+
+CLedMatrixPixel pixels[PIXELS] =
+{
+    CLedMatrixPixel(0,0), // LED1
+    CLedMatrixPixel(0,1), // LED2
+    CLedMatrixPixel(0,2), // LED3
+    CLedMatrixPixel(0,3), // LED4
+    CLedMatrixPixel(1,0), // LED5
+    CLedMatrixPixel(1,1), // LED6
+    CLedMatrixPixel(1,2), // LED7
+    CLedMatrixPixel(1,3), // LED8
+    CLedMatrixPixel(2,0), // LED9
+    CLedMatrixPixel(2,1), // LED10
+    CLedMatrixPixel(2,2), // LED11
+    CLedMatrixPixel(2,3), // LED12
+    CLedMatrixPixel(3,0), // LED13
+    CLedMatrixPixel(3,1), // LED14
+    CLedMatrixPixel(3,2), // LED15
+    CLedMatrixPixel(3,3), // LED16
+    CLedMatrixPixel(MATRIX_ROWS,MATRIX_COLUMNS), // LED17 (not in the matrix)
+};
+
+
+void CLedMatrix::initialise(void)
+{
+    m_acitvePixel = 0;
+    m_maxPixel = PIXELS;
+}
+
+
+void CLedMatrix::putPixel(uint8_t nr, LedMatrixColor_t color) 
+{ 
+    if (nr<PIXELS) pixels[nr] = color; 
+}
+
+
+void CLedMatrix::set(CLedMatrixPixel pixel, LedMatrixColor_t color)
+{
+    if (pixel.x()>=MATRIX_ROWS && pixel.x()>=MATRIX_COLUMNS)
+    {
+        // Special treats for CENTER_LED.
+        setCenterLed(color);
+    }
+    else if (color==black)
+    {
+        // Black is achieved by making the pixel pins inputs.
+        ledMatrixRows[pixel.x()].input();
+        ledMatrixColumns[pixel.y()].input();
+    }
+    else
+    {
+        // Red, green & orange require the pixel pins to be outputs.
+        ledMatrixRows[pixel.x()].output();
+        ledMatrixColumns[pixel.y()].output();
+        switch (color)
+        {
+            case red:
+                ledMatrixRows[pixel.x()] = 1;
+                ledMatrixColumns[pixel.y()] = 0;
+                break;
+        
+            case green:
+                ledMatrixRows[pixel.x()] = 0;
+                ledMatrixColumns[pixel.y()] = 1;
+                break;
+        
+            case orange:
+                ledMatrixRows[pixel.x()] = 1;
+                ledMatrixColumns[pixel.y()] = 1;
+                break;
+        }
+    }
+}
+
+
+void CLedMatrix::setCenterLed(LedMatrixColor_t color)
+{
+    switch (color)
+    {
+    case black:
+        led17r = 1;
+        led17g = 1;
+        break;
+
+    case red:
+        led17r = 0;
+        led17g = 1;
+        break;
+
+    case green:
+        led17r = 1;
+        led17g = 0;
+        break;
+
+    case orange:
+        led17r = 0;
+        led17g = 0;
+        break;
+    }
+}
+
+
+void CLedMatrix::tick(void)
+{
+    set(pixels[m_acitvePixel],black); // Deactivate current LED.
+    m_acitvePixel += 1; // Increment LED counter.
+    if (m_acitvePixel>=m_maxPixel) m_acitvePixel = 0; // Wrap around if needed.
+    set(pixels[m_acitvePixel],pixels[m_acitvePixel].color()); // Activate new LED.
+}
diff -r 000000000000 -r c6fa86a8f29b LedMatrix.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LedMatrix.h	Tue Jun 21 07:45:06 2016 +0000
@@ -0,0 +1,79 @@
+/*
+ * LedMatrix.h
+ *
+ *  Created on: 25 january 2016
+ *      Author: CPV
+ */
+
+#ifndef __LEDMATRIX_H__
+#define __LEDMATRIX_H__
+
+#include "stdint.h"
+
+#define PIXELS  (17)
+#define CENTER_LED  (PIXELS-1)
+
+#define CENTER_LED_X  (P0_10)
+#define CENTER_LED_Y  (P0_11)
+
+
+typedef enum
+{
+    black = 0,
+    red,
+    green,
+    orange
+}
+LedMatrixColor_t;
+
+
+class CLedMatrixPixel
+{
+public:
+    CLedMatrixPixel(uint8_t x, uint8_t y, LedMatrixColor_t color=black) 
+    { 
+        m_x = x; 
+        m_y = y; 
+        m_color = color;
+    }
+    
+    uint8_t x(void) { return m_x; }
+    uint8_t y(void) { return m_y; }
+
+    LedMatrixColor_t color(void) { return m_color; }
+    CLedMatrixPixel& operator= (LedMatrixColor_t color) { m_color = color; return *this; }
+    
+private:
+    uint8_t m_x;
+    uint8_t m_y;
+    LedMatrixColor_t m_color;
+};
+
+
+class CLedMatrix
+{
+public:
+    CLedMatrix(void) { initialise(); }
+    void putPixel(uint8_t nr, LedMatrixColor_t color);
+    void tick(void);
+    
+    void debugLedEnable(bool enable) { m_maxPixel = enable==true? PIXELS-1 : PIXELS; }
+    void debugLed(LedMatrixColor_t color) 
+    { 
+        if (m_maxPixel!=PIXELS) setCenterLed(color); 
+    }
+    
+private:
+    void initialise(void);
+    void set(CLedMatrixPixel pixel, LedMatrixColor_t color);
+    void setCenterLed(LedMatrixColor_t color);
+    
+    uint8_t m_acitvePixel;
+    uint8_t m_maxPixel;
+};
+
+
+extern DigitalOut led17g;
+extern DigitalOut led17r;
+
+#endif /* __LEDMATRIX_H__ */
diff -r 000000000000 -r c6fa86a8f29b main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Jun 21 07:45:06 2016 +0000
@@ -0,0 +1,61 @@
+#include "mbed.h"
+#include "LedMatrix.h"
+ 
+
+CLedMatrix matrix;
+Ticker matrixRefresh;
+DigitalOut led16(P0_10,0);
+LedMatrixColor_t color_global;
+uint32_t ms = 0;
+uint32_t debug_ms = 0;
+
+
+void tickLed(void)
+{
+    static int i = 0;
+    // Color is global so the debug LED can use it too.
+    color_global = (i&0x10)? red : green; // Toggle color.
+    matrix.putPixel(i&0xf,color_global); // Set pixel.
+    i--; // Spin clockwise.
+}
+
+
+void systick(void)
+{
+    matrix.tick();
+    ms += 1;
+
+    debug_ms += 1;
+    if (debug_ms==250)
+    {
+        matrix.debugLed(color_global==green?red:green);
+    }
+    else if (debug_ms==500)
+    {
+        matrix.debugLed(black);
+    }
+    else if (debug_ms>=1000)
+    {
+        debug_ms = 0;
+    }
+}
+
+ 
+int main(void) 
+{
+    uint8_t wait = 62;
+    matrix.debugLedEnable(true);
+    
+    // The debug LED uses a Ticker object, the LED ring uses wait_ms for timing.
+    
+    matrixRefresh.attach_us(&systick,1000);
+    
+    while (1)
+    {
+        tickLed();
+        wait_ms(wait);
+        // 16 LEDs evenly distributed over 1000 ms requires
+        // an average on time of 62.5 ms per LED.
+        wait = wait==63? 62 : 63;
+    }
+}
diff -r 000000000000 -r c6fa86a8f29b mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Jun 21 07:45:06 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/6f327212ef96
\ No newline at end of file