This repository has been archived by the owner on Jan 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblink_kit.c
290 lines (259 loc) · 6.09 KB
/
blink_kit.c
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "blink_kit.h"
#include "button.h"
#include "effect.h"
/*! \addtogroup blink_kit
* @{
*
*/
/*! \brief Limit on how many effects can be registered.
*/
#define MAX_EFFECTS 20
/*! \brief Default array for #values.
*/
uint8_t led_array[NUM_LEDS];
/*! \brief Array of / pointer to the LED intensities manipulated by
* the \ref blink_kit module.
*/
uint8_t* values;
effect_function effects[MAX_EFFECTS];
uint8_t effect_count;
uint8_t current_effect;
/*! \brief Initialize global variables.
*/
void blink_kit_init(void) {
values = led_array;
effect_count = 0;
current_effect = -1;
}
/*! \brief Get the array that is currently used for the leds.
*/
uint8_t* get_led_array(void)
{
return values;
}
/*! \brief Set the array that is currently used for the leds.
*/
void set_led_array(uint8_t* a)
{
values = a;
}
/*! \brief Run the next effect.
*
* This function cannot be called from within an effect. Return from
* the effect instead.
*/
void run_next_effect(void)
{
current_effect++;
if (current_effect >= effect_count) {
current_effect = 0;
}
effects[current_effect]();
}
/*! \brief Should the currenlty running effect exit?
*
* This function must be polled regularly from all effects. If it
* returns true, the effect function must return.
*/
uint8_t should_exit(void)
{
if (button_pressed) {
button_pressed = 0;
return 1;
} else {
return 0;
}
}
/*! \brief Reset the contents of each element of the led array to
* value.
*
* Example with NUM_LEDS = NUM_INTENSITIES = 6:
*
* After: [0, 0, 0, 0, 0, 0]
*/
void clear(uint8_t value)
{
uint8_t i;
for (i = 0; i < NUM_LEDS; i++) {
values[i] = value;
}
}
/*! \brief Initialize the led array to a rising ramp-shaped intensity
* distribution.
*
* Example with NUM_LEDS = NUM_INTENSITIES = 6:
*
* After: [0, 1, 2, 3, 4, 5]
*/
void ramp_right(void) {
uint8_t i;
uint16_t level;
for (i = 0; i < NUM_LEDS; i++) {
level = (i * MAX_INTENSITY) / NUM_LEDS;
values[i] = level;
}
}
/*! \brief Initialize the led array to a falling ramp-shaped intensity
* distribution.
*
* Example with NUM_LEDS = NUM_INTENSITIES = 6:
*
* After: [5, 4, 3, 2, 1, 0]
*/
void ramp_left(void) {
uint8_t i;
uint16_t level;
for (i = 0; i < NUM_LEDS; i++) {
level = ((NUM_LEDS - i - 1) * MAX_INTENSITY) / NUM_LEDS;
values[i] = level;
}
}
/*! \brief Initialize the led array to two mirrored ramp-shaped
* intensity distributions.
*
* Example with NUM_LEDS = NUM_INTENSITIES = 6:
*
* After: [0, 1, 3, 3, 1, 0]
*/
void triangle(void) {
uint8_t i;
uint16_t level;
for (i = 0; i < NUM_LEDS/2; i++) {
level = (i * MAX_INTENSITY * 2) / NUM_LEDS;
values[i] = level;
}
for (i = NUM_LEDS/2; i < NUM_LEDS; i++) {
level = ((NUM_LEDS - i - 1) * MAX_INTENSITY * 2) / NUM_LEDS;
values[i] = level;
}
}
/*! \brief Shift all intensities one step to the right, using the
* rightmost intensity as the new leftmost.
*
* Example with NUM_LEDS = NUM_INTENSITIES = 6:
*
* Before: [0, 1, 2, 3, 4, 5]
* After: [5, 0, 1, 2, 3, 4]
*/
void rotate_right(void)
{
uint8_t i;
uint8_t last;
last = values[NUM_LEDS - 1];
for (i = NUM_LEDS - 1; i > 0; i--) {
values[i] = values[i - 1];
}
values[0] = last;
}
/*! \brief Shift all intensities one step to the left, using the
* leftmost intensitiy as the new rightmost.
*
* Example with NUM_LEDS = NUM_INTENSITIES = 6:
*
* Before: [0, 1, 2, 3, 4, 5]
* After: [1, 2, 3, 4, 5, 0]
*/
void rotate_left(void)
{
uint8_t i;
uint8_t left;
left = values[0];
for (i = 1; i < NUM_LEDS; i++) {
values[i - 1] = values[i];
}
values[NUM_LEDS - 1] = left;
}
/*! \brief Get the current intensity of the rightmost LED.
*
* Example with NUM_LEDS = NUM_INTENSITIES = 6:
*
* Before: [0, 1, 2, 3, 4, 5]
* Returned: 5
*/
uint8_t peek_right(void) {
return values[NUM_LEDS - 1];
}
/*! \brief Get the current intensity of the leftmost LED.
*
* Example with NUM_LEDS = NUM_INTENSITIES = 6:
*
* Before: [0, 1, 2, 3, 4, 5]
* Returned: 0
*/
uint8_t peek_left(void) {
return values[0];
}
/*! \brief Shift all intensities one step to the right, using the the
* provided intensity as the new leftmost.
*
* Example with NUM_LEDS = NUM_INTENSITIES = 6:
*
* Before: [0, 1, 2, 3, 4, 5]
* New left: 3
* After: [3, 0, 1, 2, 3, 4]
*/
uint8_t shift_right(uint8_t left)
{
uint8_t i;
uint8_t right;
right = values[NUM_LEDS - 1];
for (i = NUM_LEDS - 1; i > 0; i--) {
values[i] = values[i - 1];
}
values[0] = left;
return right;
}
/*! \brief Shift all intensities one step to the left, using the the
* provided intensity as the new rightmost.
*
* Example with NUM_LEDS = NUM_INTENSITIES = 6:
*
* Before: [0, 1, 2, 3, 4, 5]
* New right: 3
* After: [1, 2, 3, 4, 5, 3]
*/
uint8_t shift_left(uint8_t right)
{
uint8_t i;
uint8_t left;
left = values[0];
for (i = 1; i < NUM_LEDS; i++) {
values[i - 1] = values[i];
}
values[NUM_LEDS - 1] = right;
return left;
}
/*! \brief Shift all intensities one step to the left, using the the
* provided intensity as the new rightmost.
*
* Example with NUM_LEDS = NUM_INTENSITIES = 6:
*
* Before: [0, 0, 1, 1, 3, 5]
* After: [5, 3, 1, 1, 0, 0]
*/
void flip(void)
{
uint8_t i, j;
uint8_t temp;
for (i = 0; i < NUM_LEDS/2; i++) {
j = NUM_LEDS-i-1;
temp = values[i];
values[i] = values[j];
values[j] = temp;
}
}
/*! \brief Register a new effect.
*
* This function should only be called once per effect and only at
* program initialization. When the button is pressed, the registered
* effects will cycled through in the order their #add_effect calls
* were executed during the initialization.
*/
void add_effect(effect_function effect)
{
if (effect_count != MAX_EFFECTS) {
effects[effect_count] = effect;
effect_count++;
}
}
/*! @} */