-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathubit_led_matrix.c
More file actions
180 lines (171 loc) · 2.99 KB
/
ubit_led_matrix.c
File metadata and controls
180 lines (171 loc) · 2.99 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdint.h>
#include "ubit_led_matrix.h"
#include "gpio.h"
static void ubit_led_matrix_helper_disable_all_pins();
static void ubit_led_matrix_helper_translate_x_y_to_ground_supply(int * data);
void ubit_led_matrix_init(){
for(int i = 4; i <= 15; i++){
GPIO->DIRSET = (1 << i);
GPIO->OUTCLR = (1 << i);
}
}
void ubit_led_matrix_light_only_at(int x, int y){
ubit_led_matrix_helper_disable_all_pins();
int ground_supply[2] = {x, y};
ubit_led_matrix_helper_translate_x_y_to_ground_supply(ground_supply);
int ground_pin = ground_supply[0];
int supply_pin = ground_supply[1];
GPIO->OUTCLR = (1 << ground_pin);
GPIO->OUTSET = (1 << supply_pin);
}
static void ubit_led_matrix_helper_disable_all_pins(){
/*
* These magic numbers come from (1 << 13) | ... | (1 << 15)
* and (1 << 4) | ... | (1 << 12), respectively. They set the
* ground pins high, and the supply pins low.
*/
GPIO->OUTCLR = 57344;
GPIO->OUTSET = 8176;
}
static void ubit_led_matrix_helper_translate_x_y_to_ground_supply(int * data){
/*
* Expects the input to be an integer array of [x, y], will
* alter this array to contain [ground pin, supply pin].
*/
int x = data[0];
int y = data[1];
if(x > 2)
x = 2;
else if(x < -2)
x = -2;
if(y > 2)
y = 2;
else if(y < -2)
y = -2;
int ground;
int supply;
switch(y){
case -2:
switch(x){
case -2:
ground = 6;
supply = 15;
break;
case -1:
ground = 10;
supply = 14;
break;
case 0:
ground = 4;
supply = 15;
break;
case 1:
ground = 9;
supply = 14;
break;
case 2:
ground = 5;
supply = 15;
break;
}
break;
case -1:
switch(x){
case -2:
ground = 11;
supply = 13;
break;
case -1:
ground = 10;
supply = 13;
break;
case 0:
ground = 9;
supply = 13;
break;
case 1:
ground = 8;
supply = 13;
break;
case 2:
ground = 7;
supply = 13;
break;
}
break;
case 0:
switch(x){
case -2:
ground = 5;
supply = 14;
break;
case -1:
ground = 12;
supply = 13;
break;
case 0:
ground = 6;
supply = 14;
break;
case 1:
ground = 12;
supply = 15;
break;
case 2:
ground = 4;
supply = 14;
break;
}
break;
case 1:
switch(x){
case -2:
ground = 7;
supply = 15;
break;
case -1:
ground = 8;
supply = 15;
break;
case 0:
ground = 9;
supply = 15;
break;
case 1:
ground = 10;
supply = 15;
break;
case 2:
ground = 11;
supply = 15;
break;
}
break;
case 2:
switch(x){
case -2:
ground = 4;
supply = 13;
break;
case -1:
ground = 7;
supply = 14;
break;
case 0:
ground = 5;
supply = 13;
break;
case 1:
ground = 8;
supply = 14;
break;
case 2:
ground = 6;
supply = 13;
break;
}
break;
}
data[0] = ground;
data[1] = supply;
}