Test program for the microbit LED matrix
Dependencies: mbed
main.cpp@0:a1f9bf062855, 2020-09-14 (annotated)
- Committer:
- f3d
- Date:
- Mon Sep 14 14:30:15 2020 +0000
- Revision:
- 0:a1f9bf062855
Tested with new pattern
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
f3d | 0:a1f9bf062855 | 1 | #include "mbed.h" |
f3d | 0:a1f9bf062855 | 2 | /* |
f3d | 0:a1f9bf062855 | 3 | * All the LEDs on the micro:bit are part of the LED Matrix, |
f3d | 0:a1f9bf062855 | 4 | * In order to get simple blinking behaviour, we set column 0 |
f3d | 0:a1f9bf062855 | 5 | * to be permanently at ground. If you want to use the LEDs as |
f3d | 0:a1f9bf062855 | 6 | * a screen, there is a display driver in the micro:bit 'DAL', |
f3d | 0:a1f9bf062855 | 7 | */ |
f3d | 0:a1f9bf062855 | 8 | // Columns are active low |
f3d | 0:a1f9bf062855 | 9 | // Rows are active high |
f3d | 0:a1f9bf062855 | 10 | // Initialize all LED matrix connections in an inactive state |
f3d | 0:a1f9bf062855 | 11 | DigitalOut col1(P0_4,1); |
f3d | 0:a1f9bf062855 | 12 | DigitalOut col2(P0_5,1); |
f3d | 0:a1f9bf062855 | 13 | DigitalOut col3(P0_6,1); |
f3d | 0:a1f9bf062855 | 14 | DigitalOut col4(P0_7,1); |
f3d | 0:a1f9bf062855 | 15 | DigitalOut col5(P0_8,1); |
f3d | 0:a1f9bf062855 | 16 | DigitalOut col6(P0_9,1); |
f3d | 0:a1f9bf062855 | 17 | DigitalOut col7(P0_10,1); |
f3d | 0:a1f9bf062855 | 18 | DigitalOut col8(P0_11,1); |
f3d | 0:a1f9bf062855 | 19 | DigitalOut col9(P0_12,1); |
f3d | 0:a1f9bf062855 | 20 | DigitalOut row1(P0_13,0); |
f3d | 0:a1f9bf062855 | 21 | DigitalOut row2(P0_14,0); |
f3d | 0:a1f9bf062855 | 22 | DigitalOut row3(P0_15,0); |
f3d | 0:a1f9bf062855 | 23 | |
f3d | 0:a1f9bf062855 | 24 | |
f3d | 0:a1f9bf062855 | 25 | |
f3d | 0:a1f9bf062855 | 26 | Ticker DisplayThreadObject; |
f3d | 0:a1f9bf062855 | 27 | typedef struct { |
f3d | 0:a1f9bf062855 | 28 | DigitalOut & row; |
f3d | 0:a1f9bf062855 | 29 | DigitalOut & col; |
f3d | 0:a1f9bf062855 | 30 | int state; |
f3d | 0:a1f9bf062855 | 31 | } LEDStruct; |
f3d | 0:a1f9bf062855 | 32 | LEDStruct FrameBuffer[5][5] = { {{row1,col1,0}, {row2,col4,0}, {row1,col2,0}, {row2,col5,0}, {row1,col3,0}}, |
f3d | 0:a1f9bf062855 | 33 | {{row3,col4,0}, {row3,col5,0}, {row3,col6,0}, {row3,col7,0}, {row3,col8,0}}, |
f3d | 0:a1f9bf062855 | 34 | {{row2,col2,0}, {row1,col9,0}, {row2,col3,0}, {row3,col9,0}, {row2,col1,0}}, |
f3d | 0:a1f9bf062855 | 35 | {{row1,col8,0}, {row1,col7,0}, {row1,col6,0}, {row1,col5,0}, {row1,col4,0}}, |
f3d | 0:a1f9bf062855 | 36 | {{row3,col3,0}, {row2,col7,0}, {row3,col1,0}, {row2,col6,0}, {row3,col2,0}} |
f3d | 0:a1f9bf062855 | 37 | }; |
f3d | 0:a1f9bf062855 | 38 | |
f3d | 0:a1f9bf062855 | 39 | |
f3d | 0:a1f9bf062855 | 40 | void DisplayThreadTask(void) |
f3d | 0:a1f9bf062855 | 41 | { |
f3d | 0:a1f9bf062855 | 42 | static int row=0; |
f3d | 0:a1f9bf062855 | 43 | static int col=0; |
f3d | 0:a1f9bf062855 | 44 | |
f3d | 0:a1f9bf062855 | 45 | // Turn off all LEDs from previous state |
f3d | 0:a1f9bf062855 | 46 | for (int row=0;row<5;row++) |
f3d | 0:a1f9bf062855 | 47 | { |
f3d | 0:a1f9bf062855 | 48 | for (int col=0;col<5;col++) |
f3d | 0:a1f9bf062855 | 49 | { |
f3d | 0:a1f9bf062855 | 50 | FrameBuffer[row][col].row=0; |
f3d | 0:a1f9bf062855 | 51 | FrameBuffer[row][col].col=1; |
f3d | 0:a1f9bf062855 | 52 | } |
f3d | 0:a1f9bf062855 | 53 | } |
f3d | 0:a1f9bf062855 | 54 | |
f3d | 0:a1f9bf062855 | 55 | if (FrameBuffer[row][col].state==1) |
f3d | 0:a1f9bf062855 | 56 | { |
f3d | 0:a1f9bf062855 | 57 | // Turn LED on |
f3d | 0:a1f9bf062855 | 58 | FrameBuffer[row][col].row = 1; |
f3d | 0:a1f9bf062855 | 59 | FrameBuffer[row][col].col = 0; |
f3d | 0:a1f9bf062855 | 60 | } |
f3d | 0:a1f9bf062855 | 61 | else |
f3d | 0:a1f9bf062855 | 62 | { |
f3d | 0:a1f9bf062855 | 63 | // Turn LED off |
f3d | 0:a1f9bf062855 | 64 | FrameBuffer[row][col].row = 0; |
f3d | 0:a1f9bf062855 | 65 | FrameBuffer[row][col].col = 1; |
f3d | 0:a1f9bf062855 | 66 | } |
f3d | 0:a1f9bf062855 | 67 | |
f3d | 0:a1f9bf062855 | 68 | col++; |
f3d | 0:a1f9bf062855 | 69 | if (col > 4) |
f3d | 0:a1f9bf062855 | 70 | { |
f3d | 0:a1f9bf062855 | 71 | col = 0; |
f3d | 0:a1f9bf062855 | 72 | row ++ ; |
f3d | 0:a1f9bf062855 | 73 | if (row > 4) |
f3d | 0:a1f9bf062855 | 74 | row = 0; |
f3d | 0:a1f9bf062855 | 75 | } |
f3d | 0:a1f9bf062855 | 76 | |
f3d | 0:a1f9bf062855 | 77 | } |
f3d | 0:a1f9bf062855 | 78 | int main() { |
f3d | 0:a1f9bf062855 | 79 | |
f3d | 0:a1f9bf062855 | 80 | DisplayThreadObject.attach(DisplayThreadTask,0.001); // update the display once LED per millisecond |
f3d | 0:a1f9bf062855 | 81 | |
f3d | 0:a1f9bf062855 | 82 | FrameBuffer[0][0].state = 1; |
f3d | 0:a1f9bf062855 | 83 | FrameBuffer[0][1].state = 1; |
f3d | 0:a1f9bf062855 | 84 | FrameBuffer[0][2].state = 1; |
f3d | 0:a1f9bf062855 | 85 | FrameBuffer[0][3].state = 1; |
f3d | 0:a1f9bf062855 | 86 | FrameBuffer[0][4].state = 1; |
f3d | 0:a1f9bf062855 | 87 | |
f3d | 0:a1f9bf062855 | 88 | FrameBuffer[2][2].state = 1; |
f3d | 0:a1f9bf062855 | 89 | |
f3d | 0:a1f9bf062855 | 90 | FrameBuffer[4][0].state = 1; |
f3d | 0:a1f9bf062855 | 91 | FrameBuffer[4][1].state = 1; |
f3d | 0:a1f9bf062855 | 92 | FrameBuffer[4][2].state = 1; |
f3d | 0:a1f9bf062855 | 93 | FrameBuffer[4][3].state = 1; |
f3d | 0:a1f9bf062855 | 94 | FrameBuffer[4][4].state = 1; |
f3d | 0:a1f9bf062855 | 95 | |
f3d | 0:a1f9bf062855 | 96 | |
f3d | 0:a1f9bf062855 | 97 | while(1) { |
f3d | 0:a1f9bf062855 | 98 | |
f3d | 0:a1f9bf062855 | 99 | } |
f3d | 0:a1f9bf062855 | 100 | } |