Clemens Valens
/
cocorico_ring_demo
Simple demo showing how to control the CoCo-ri-Co LED ring.
LedMatrix.cpp@0:c6fa86a8f29b, 2016-06-21 (annotated)
- Committer:
- Clemo
- Date:
- Tue Jun 21 07:45:06 2016 +0000
- Revision:
- 0:c6fa86a8f29b
Demo program showing how to control the CoCo-ri-Co's LED ring.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Clemo | 0:c6fa86a8f29b | 1 | /* |
Clemo | 0:c6fa86a8f29b | 2 | * LedMatrix.cpp |
Clemo | 0:c6fa86a8f29b | 3 | * |
Clemo | 0:c6fa86a8f29b | 4 | * Created on: 25 january 2016 |
Clemo | 0:c6fa86a8f29b | 5 | * Author: CPV |
Clemo | 0:c6fa86a8f29b | 6 | */ |
Clemo | 0:c6fa86a8f29b | 7 | |
Clemo | 0:c6fa86a8f29b | 8 | #include "mbed.h" |
Clemo | 0:c6fa86a8f29b | 9 | #include "LedMatrix.h" |
Clemo | 0:c6fa86a8f29b | 10 | |
Clemo | 0:c6fa86a8f29b | 11 | #define MATRIX_ROWS (4) |
Clemo | 0:c6fa86a8f29b | 12 | #define MATRIX_COLUMNS (4) |
Clemo | 0:c6fa86a8f29b | 13 | |
Clemo | 0:c6fa86a8f29b | 14 | |
Clemo | 0:c6fa86a8f29b | 15 | DigitalOut led17g(CENTER_LED_X,1); |
Clemo | 0:c6fa86a8f29b | 16 | DigitalOut led17r(CENTER_LED_Y,1); |
Clemo | 0:c6fa86a8f29b | 17 | |
Clemo | 0:c6fa86a8f29b | 18 | |
Clemo | 0:c6fa86a8f29b | 19 | DigitalInOut ledMatrixRows[MATRIX_ROWS] = |
Clemo | 0:c6fa86a8f29b | 20 | { |
Clemo | 0:c6fa86a8f29b | 21 | DigitalInOut(P0_15,PIN_INPUT,PullNone,0), |
Clemo | 0:c6fa86a8f29b | 22 | DigitalInOut(P0_13,PIN_INPUT,PullNone,0), |
Clemo | 0:c6fa86a8f29b | 23 | DigitalInOut(P0_16,PIN_INPUT,PullNone,0), |
Clemo | 0:c6fa86a8f29b | 24 | DigitalInOut(P0_7,PIN_INPUT,PullNone,0) |
Clemo | 0:c6fa86a8f29b | 25 | }; |
Clemo | 0:c6fa86a8f29b | 26 | /*DigitalInOut row0(P0_15,PIN_INPUT,PullNone,0); |
Clemo | 0:c6fa86a8f29b | 27 | DigitalInOut row1(P0_13,PIN_INPUT,PullNone,0); |
Clemo | 0:c6fa86a8f29b | 28 | DigitalInOut row2(P0_16,PIN_INPUT,PullNone,0); |
Clemo | 0:c6fa86a8f29b | 29 | DigitalInOut row3(P0_7,PIN_INPUT,PullNone,0);*/ |
Clemo | 0:c6fa86a8f29b | 30 | |
Clemo | 0:c6fa86a8f29b | 31 | |
Clemo | 0:c6fa86a8f29b | 32 | DigitalInOut ledMatrixColumns[MATRIX_COLUMNS] = |
Clemo | 0:c6fa86a8f29b | 33 | { |
Clemo | 0:c6fa86a8f29b | 34 | DigitalInOut(P0_9,PIN_INPUT,PullNone,0), |
Clemo | 0:c6fa86a8f29b | 35 | DigitalInOut(P0_8,PIN_INPUT,PullNone,0), |
Clemo | 0:c6fa86a8f29b | 36 | DigitalInOut(P0_17,PIN_INPUT,PullNone,0), |
Clemo | 0:c6fa86a8f29b | 37 | DigitalInOut(P0_14,PIN_INPUT,PullNone,0) |
Clemo | 0:c6fa86a8f29b | 38 | }; |
Clemo | 0:c6fa86a8f29b | 39 | /*DigitalInOut col0(P0_9,PIN_INPUT,PullNone,0); |
Clemo | 0:c6fa86a8f29b | 40 | DigitalInOut col1(P0_8,PIN_INPUT,PullNone,0); |
Clemo | 0:c6fa86a8f29b | 41 | DigitalInOut col2(P0_17,PIN_INPUT,PullNone,0); |
Clemo | 0:c6fa86a8f29b | 42 | DigitalInOut col3(P0_14,PIN_INPUT,PullNone,0);*/ |
Clemo | 0:c6fa86a8f29b | 43 | |
Clemo | 0:c6fa86a8f29b | 44 | |
Clemo | 0:c6fa86a8f29b | 45 | CLedMatrixPixel pixels[PIXELS] = |
Clemo | 0:c6fa86a8f29b | 46 | { |
Clemo | 0:c6fa86a8f29b | 47 | CLedMatrixPixel(0,0), // LED1 |
Clemo | 0:c6fa86a8f29b | 48 | CLedMatrixPixel(0,1), // LED2 |
Clemo | 0:c6fa86a8f29b | 49 | CLedMatrixPixel(0,2), // LED3 |
Clemo | 0:c6fa86a8f29b | 50 | CLedMatrixPixel(0,3), // LED4 |
Clemo | 0:c6fa86a8f29b | 51 | CLedMatrixPixel(1,0), // LED5 |
Clemo | 0:c6fa86a8f29b | 52 | CLedMatrixPixel(1,1), // LED6 |
Clemo | 0:c6fa86a8f29b | 53 | CLedMatrixPixel(1,2), // LED7 |
Clemo | 0:c6fa86a8f29b | 54 | CLedMatrixPixel(1,3), // LED8 |
Clemo | 0:c6fa86a8f29b | 55 | CLedMatrixPixel(2,0), // LED9 |
Clemo | 0:c6fa86a8f29b | 56 | CLedMatrixPixel(2,1), // LED10 |
Clemo | 0:c6fa86a8f29b | 57 | CLedMatrixPixel(2,2), // LED11 |
Clemo | 0:c6fa86a8f29b | 58 | CLedMatrixPixel(2,3), // LED12 |
Clemo | 0:c6fa86a8f29b | 59 | CLedMatrixPixel(3,0), // LED13 |
Clemo | 0:c6fa86a8f29b | 60 | CLedMatrixPixel(3,1), // LED14 |
Clemo | 0:c6fa86a8f29b | 61 | CLedMatrixPixel(3,2), // LED15 |
Clemo | 0:c6fa86a8f29b | 62 | CLedMatrixPixel(3,3), // LED16 |
Clemo | 0:c6fa86a8f29b | 63 | CLedMatrixPixel(MATRIX_ROWS,MATRIX_COLUMNS), // LED17 (not in the matrix) |
Clemo | 0:c6fa86a8f29b | 64 | }; |
Clemo | 0:c6fa86a8f29b | 65 | |
Clemo | 0:c6fa86a8f29b | 66 | |
Clemo | 0:c6fa86a8f29b | 67 | void CLedMatrix::initialise(void) |
Clemo | 0:c6fa86a8f29b | 68 | { |
Clemo | 0:c6fa86a8f29b | 69 | m_acitvePixel = 0; |
Clemo | 0:c6fa86a8f29b | 70 | m_maxPixel = PIXELS; |
Clemo | 0:c6fa86a8f29b | 71 | } |
Clemo | 0:c6fa86a8f29b | 72 | |
Clemo | 0:c6fa86a8f29b | 73 | |
Clemo | 0:c6fa86a8f29b | 74 | void CLedMatrix::putPixel(uint8_t nr, LedMatrixColor_t color) |
Clemo | 0:c6fa86a8f29b | 75 | { |
Clemo | 0:c6fa86a8f29b | 76 | if (nr<PIXELS) pixels[nr] = color; |
Clemo | 0:c6fa86a8f29b | 77 | } |
Clemo | 0:c6fa86a8f29b | 78 | |
Clemo | 0:c6fa86a8f29b | 79 | |
Clemo | 0:c6fa86a8f29b | 80 | void CLedMatrix::set(CLedMatrixPixel pixel, LedMatrixColor_t color) |
Clemo | 0:c6fa86a8f29b | 81 | { |
Clemo | 0:c6fa86a8f29b | 82 | if (pixel.x()>=MATRIX_ROWS && pixel.x()>=MATRIX_COLUMNS) |
Clemo | 0:c6fa86a8f29b | 83 | { |
Clemo | 0:c6fa86a8f29b | 84 | // Special treats for CENTER_LED. |
Clemo | 0:c6fa86a8f29b | 85 | setCenterLed(color); |
Clemo | 0:c6fa86a8f29b | 86 | } |
Clemo | 0:c6fa86a8f29b | 87 | else if (color==black) |
Clemo | 0:c6fa86a8f29b | 88 | { |
Clemo | 0:c6fa86a8f29b | 89 | // Black is achieved by making the pixel pins inputs. |
Clemo | 0:c6fa86a8f29b | 90 | ledMatrixRows[pixel.x()].input(); |
Clemo | 0:c6fa86a8f29b | 91 | ledMatrixColumns[pixel.y()].input(); |
Clemo | 0:c6fa86a8f29b | 92 | } |
Clemo | 0:c6fa86a8f29b | 93 | else |
Clemo | 0:c6fa86a8f29b | 94 | { |
Clemo | 0:c6fa86a8f29b | 95 | // Red, green & orange require the pixel pins to be outputs. |
Clemo | 0:c6fa86a8f29b | 96 | ledMatrixRows[pixel.x()].output(); |
Clemo | 0:c6fa86a8f29b | 97 | ledMatrixColumns[pixel.y()].output(); |
Clemo | 0:c6fa86a8f29b | 98 | switch (color) |
Clemo | 0:c6fa86a8f29b | 99 | { |
Clemo | 0:c6fa86a8f29b | 100 | case red: |
Clemo | 0:c6fa86a8f29b | 101 | ledMatrixRows[pixel.x()] = 1; |
Clemo | 0:c6fa86a8f29b | 102 | ledMatrixColumns[pixel.y()] = 0; |
Clemo | 0:c6fa86a8f29b | 103 | break; |
Clemo | 0:c6fa86a8f29b | 104 | |
Clemo | 0:c6fa86a8f29b | 105 | case green: |
Clemo | 0:c6fa86a8f29b | 106 | ledMatrixRows[pixel.x()] = 0; |
Clemo | 0:c6fa86a8f29b | 107 | ledMatrixColumns[pixel.y()] = 1; |
Clemo | 0:c6fa86a8f29b | 108 | break; |
Clemo | 0:c6fa86a8f29b | 109 | |
Clemo | 0:c6fa86a8f29b | 110 | case orange: |
Clemo | 0:c6fa86a8f29b | 111 | ledMatrixRows[pixel.x()] = 1; |
Clemo | 0:c6fa86a8f29b | 112 | ledMatrixColumns[pixel.y()] = 1; |
Clemo | 0:c6fa86a8f29b | 113 | break; |
Clemo | 0:c6fa86a8f29b | 114 | } |
Clemo | 0:c6fa86a8f29b | 115 | } |
Clemo | 0:c6fa86a8f29b | 116 | } |
Clemo | 0:c6fa86a8f29b | 117 | |
Clemo | 0:c6fa86a8f29b | 118 | |
Clemo | 0:c6fa86a8f29b | 119 | void CLedMatrix::setCenterLed(LedMatrixColor_t color) |
Clemo | 0:c6fa86a8f29b | 120 | { |
Clemo | 0:c6fa86a8f29b | 121 | switch (color) |
Clemo | 0:c6fa86a8f29b | 122 | { |
Clemo | 0:c6fa86a8f29b | 123 | case black: |
Clemo | 0:c6fa86a8f29b | 124 | led17r = 1; |
Clemo | 0:c6fa86a8f29b | 125 | led17g = 1; |
Clemo | 0:c6fa86a8f29b | 126 | break; |
Clemo | 0:c6fa86a8f29b | 127 | |
Clemo | 0:c6fa86a8f29b | 128 | case red: |
Clemo | 0:c6fa86a8f29b | 129 | led17r = 0; |
Clemo | 0:c6fa86a8f29b | 130 | led17g = 1; |
Clemo | 0:c6fa86a8f29b | 131 | break; |
Clemo | 0:c6fa86a8f29b | 132 | |
Clemo | 0:c6fa86a8f29b | 133 | case green: |
Clemo | 0:c6fa86a8f29b | 134 | led17r = 1; |
Clemo | 0:c6fa86a8f29b | 135 | led17g = 0; |
Clemo | 0:c6fa86a8f29b | 136 | break; |
Clemo | 0:c6fa86a8f29b | 137 | |
Clemo | 0:c6fa86a8f29b | 138 | case orange: |
Clemo | 0:c6fa86a8f29b | 139 | led17r = 0; |
Clemo | 0:c6fa86a8f29b | 140 | led17g = 0; |
Clemo | 0:c6fa86a8f29b | 141 | break; |
Clemo | 0:c6fa86a8f29b | 142 | } |
Clemo | 0:c6fa86a8f29b | 143 | } |
Clemo | 0:c6fa86a8f29b | 144 | |
Clemo | 0:c6fa86a8f29b | 145 | |
Clemo | 0:c6fa86a8f29b | 146 | void CLedMatrix::tick(void) |
Clemo | 0:c6fa86a8f29b | 147 | { |
Clemo | 0:c6fa86a8f29b | 148 | set(pixels[m_acitvePixel],black); // Deactivate current LED. |
Clemo | 0:c6fa86a8f29b | 149 | m_acitvePixel += 1; // Increment LED counter. |
Clemo | 0:c6fa86a8f29b | 150 | if (m_acitvePixel>=m_maxPixel) m_acitvePixel = 0; // Wrap around if needed. |
Clemo | 0:c6fa86a8f29b | 151 | set(pixels[m_acitvePixel],pixels[m_acitvePixel].color()); // Activate new LED. |
Clemo | 0:c6fa86a8f29b | 152 | } |