Clemens Valens
/
cocorico_ring_demo
Simple demo showing how to control the CoCo-ri-Co LED ring.
main.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 | #include "mbed.h" |
Clemo | 0:c6fa86a8f29b | 2 | #include "LedMatrix.h" |
Clemo | 0:c6fa86a8f29b | 3 | |
Clemo | 0:c6fa86a8f29b | 4 | |
Clemo | 0:c6fa86a8f29b | 5 | CLedMatrix matrix; |
Clemo | 0:c6fa86a8f29b | 6 | Ticker matrixRefresh; |
Clemo | 0:c6fa86a8f29b | 7 | DigitalOut led16(P0_10,0); |
Clemo | 0:c6fa86a8f29b | 8 | LedMatrixColor_t color_global; |
Clemo | 0:c6fa86a8f29b | 9 | uint32_t ms = 0; |
Clemo | 0:c6fa86a8f29b | 10 | uint32_t debug_ms = 0; |
Clemo | 0:c6fa86a8f29b | 11 | |
Clemo | 0:c6fa86a8f29b | 12 | |
Clemo | 0:c6fa86a8f29b | 13 | void tickLed(void) |
Clemo | 0:c6fa86a8f29b | 14 | { |
Clemo | 0:c6fa86a8f29b | 15 | static int i = 0; |
Clemo | 0:c6fa86a8f29b | 16 | // Color is global so the debug LED can use it too. |
Clemo | 0:c6fa86a8f29b | 17 | color_global = (i&0x10)? red : green; // Toggle color. |
Clemo | 0:c6fa86a8f29b | 18 | matrix.putPixel(i&0xf,color_global); // Set pixel. |
Clemo | 0:c6fa86a8f29b | 19 | i--; // Spin clockwise. |
Clemo | 0:c6fa86a8f29b | 20 | } |
Clemo | 0:c6fa86a8f29b | 21 | |
Clemo | 0:c6fa86a8f29b | 22 | |
Clemo | 0:c6fa86a8f29b | 23 | void systick(void) |
Clemo | 0:c6fa86a8f29b | 24 | { |
Clemo | 0:c6fa86a8f29b | 25 | matrix.tick(); |
Clemo | 0:c6fa86a8f29b | 26 | ms += 1; |
Clemo | 0:c6fa86a8f29b | 27 | |
Clemo | 0:c6fa86a8f29b | 28 | debug_ms += 1; |
Clemo | 0:c6fa86a8f29b | 29 | if (debug_ms==250) |
Clemo | 0:c6fa86a8f29b | 30 | { |
Clemo | 0:c6fa86a8f29b | 31 | matrix.debugLed(color_global==green?red:green); |
Clemo | 0:c6fa86a8f29b | 32 | } |
Clemo | 0:c6fa86a8f29b | 33 | else if (debug_ms==500) |
Clemo | 0:c6fa86a8f29b | 34 | { |
Clemo | 0:c6fa86a8f29b | 35 | matrix.debugLed(black); |
Clemo | 0:c6fa86a8f29b | 36 | } |
Clemo | 0:c6fa86a8f29b | 37 | else if (debug_ms>=1000) |
Clemo | 0:c6fa86a8f29b | 38 | { |
Clemo | 0:c6fa86a8f29b | 39 | debug_ms = 0; |
Clemo | 0:c6fa86a8f29b | 40 | } |
Clemo | 0:c6fa86a8f29b | 41 | } |
Clemo | 0:c6fa86a8f29b | 42 | |
Clemo | 0:c6fa86a8f29b | 43 | |
Clemo | 0:c6fa86a8f29b | 44 | int main(void) |
Clemo | 0:c6fa86a8f29b | 45 | { |
Clemo | 0:c6fa86a8f29b | 46 | uint8_t wait = 62; |
Clemo | 0:c6fa86a8f29b | 47 | matrix.debugLedEnable(true); |
Clemo | 0:c6fa86a8f29b | 48 | |
Clemo | 0:c6fa86a8f29b | 49 | // The debug LED uses a Ticker object, the LED ring uses wait_ms for timing. |
Clemo | 0:c6fa86a8f29b | 50 | |
Clemo | 0:c6fa86a8f29b | 51 | matrixRefresh.attach_us(&systick,1000); |
Clemo | 0:c6fa86a8f29b | 52 | |
Clemo | 0:c6fa86a8f29b | 53 | while (1) |
Clemo | 0:c6fa86a8f29b | 54 | { |
Clemo | 0:c6fa86a8f29b | 55 | tickLed(); |
Clemo | 0:c6fa86a8f29b | 56 | wait_ms(wait); |
Clemo | 0:c6fa86a8f29b | 57 | // 16 LEDs evenly distributed over 1000 ms requires |
Clemo | 0:c6fa86a8f29b | 58 | // an average on time of 62.5 ms per LED. |
Clemo | 0:c6fa86a8f29b | 59 | wait = wait==63? 62 : 63; |
Clemo | 0:c6fa86a8f29b | 60 | } |
Clemo | 0:c6fa86a8f29b | 61 | } |