Skip to content

Commit 77158ba

Browse files
Examples: Simplify blinky loop.
1 parent 5e01b52 commit 77158ba

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

source/main.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,46 +16,45 @@
1616
*/
1717
#include "mbed.h"
1818

19+
#define ROW_COUNT 5
20+
#define COL_COUNT 5
21+
1922
#if defined(MICROBIT_TARGET)
2023
// Main micro:bit target has access to the LED matrix and on-board features
21-
DigitalOut rows[5] = {
24+
DigitalOut rows[ROW_COUNT] = {
2225
DigitalOut(ROW_1, 0),
2326
DigitalOut(ROW_2, 0),
2427
DigitalOut(ROW_3, 0),
2528
DigitalOut(ROW_4, 0),
2629
DigitalOut(ROW_5, 0)
2730
};
28-
DigitalOut cols[5] = {
31+
DigitalOut cols[COL_COUNT] = {
2932
DigitalOut(COL_1, 1),
3033
DigitalOut(COL_2, 1),
3134
DigitalOut(COL_3, 1),
3235
DigitalOut(COL_4, 1),
3336
DigitalOut(COL_5, 1)
3437
};
35-
bool led1;
36-
bool led2;
38+
bool led1 = false;
39+
bool led2 = false;
3740
#else
3841
// The nRF52833 DK and micro:bit V2.2 IF MCU have access to direct LEDs
39-
bool rows[5];
40-
bool cols[5];
42+
bool rows[ROW_COUNT];
43+
bool cols[COL_COUNT];
4144
DigitalOut led1(LED1, 0);
4245
DigitalOut led2(LED2, 1);
4346
#endif
4447

4548
int main(void) {
4649
// Infinite loop to blink the matrix LEDs if built for the target MCU or
47-
// to toggle the orange and red USB LEDs if built for the interface MCU
50+
// to toggle the orange and red USB LEDs if built for the interface MCU
4851
while (true) {
49-
for (int i = 0; i < 5; i++) {
50-
rows[i] = 1;
51-
int prev_i = i - 1;
52-
if (prev_i < 0) prev_i = 4;
53-
rows[prev_i] = 0;
54-
for (int j = 0; j < 5; j++) {
55-
cols[j] = 0;
56-
int prev_j = j - 1;
57-
if (prev_j < 0) prev_j = 4;
58-
cols[prev_j] = 1;
52+
for (int i = 0, prev_i = ROW_COUNT - 1; i < ROW_COUNT; ++i, prev_i = i - 1) {
53+
rows[i] = true;
54+
rows[prev_i] = false;
55+
for (int j = 0, prev_j = COL_COUNT - 1; j < COL_COUNT; ++j, prev_j = j - 1) {
56+
cols[j] = false;
57+
cols[prev_j] = true;
5958

6059
led1 = !led1;
6160
led2 = !led2;

0 commit comments

Comments
 (0)