-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathled.c
More file actions
82 lines (64 loc) · 2.03 KB
/
Copy pathled.c
File metadata and controls
82 lines (64 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "led.h"
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include "hardware/gpio.h"
const uint32_t LED_Y_BASE = 0;
const uint32_t LED_X_BASE = 3;
bool led_state[21] = {0};
const uint32_t led_count = 20;
void led_set_state(unsigned led_num, bool state) {
if(led_num > led_count) {
return;
}
led_state[led_num] = state;
}
void led_setup() {
for(int i=0; i < 7; i++) {
gpio_init(LED_X_BASE + i);
gpio_set_dir(LED_X_BASE + i, GPIO_OUT);
gpio_put(LED_X_BASE + i, 1);
}
for(int i=0; i < 3; i++) {
gpio_init(LED_Y_BASE + i);
gpio_set_dir(LED_Y_BASE + i, GPIO_OUT);
gpio_put(LED_Y_BASE + i, 0);
}
}
void led_task(void) {
while(1) {
for(uint32_t y = 0; y < 3; y++) {
// Continue if no LED is on here.
uint32_t led_idx = y * 7;
if(!led_state[led_idx++] &&
!led_state[led_idx++] &&
!led_state[led_idx++] &&
!led_state[led_idx++] &&
!led_state[led_idx++] &&
!led_state[led_idx++] &&
!led_state[led_idx++]) {
continue;
}
// output state of each LED in row
uint32_t x_idx = 0;
led_idx = y * 7;
// printf("LED IDX: %d %d\n", led_idx, led_state[led_idx]);
// printf("IO PUT %d %d\n", LED_X_BASE + x_idx, !led_state[led_idx]);
gpio_put(LED_X_BASE + x_idx++, !led_state[led_idx++]);
gpio_put(LED_X_BASE + x_idx++, !led_state[led_idx++]);
gpio_put(LED_X_BASE + x_idx++, !led_state[led_idx++]);
gpio_put(LED_X_BASE + x_idx++, !led_state[led_idx++]);
gpio_put(LED_X_BASE + x_idx++, !led_state[led_idx++]);
gpio_put(LED_X_BASE + x_idx++, !led_state[led_idx++]);
gpio_put(LED_X_BASE + x_idx++, !led_state[led_idx++]);
// enable output row
gpio_put(LED_Y_BASE + y, 1);
sleep_ms(3);
// disable output row
gpio_put(LED_Y_BASE + y, 0);
// disable columns
x_idx = 0;
}
}
}