]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/pattern.c
patterns: 3 bits for duration, 5 bits for mode
[bike-lights.git] / firmware / pattern.c
1 #include <avr/io.h>
2 #include <stdlib.h> // for NULL
3
4 #include "lights.h"
5
6 static unsigned char led_counters[N_LEDS];
7 static pattern_t *led_patterns[N_LEDS];
8
9 static pattern_t boot_pattern[] = {
10         { 1, 0x6 },
11         { 0, 0x6 },
12         { 1, 0x3 },
13         { 0, 0x3 },
14         { 1, 0x2 },
15         { 0, 0x2 },
16         { 1, 0x1 },
17         { 0, 0x1 },
18         { 1, 0x1 },
19         { 0, 0x1 },
20         { 1, 0x1 },
21         { 0, 0x1 },
22         { 1, 0x1 },
23         { 0, 0x1 },
24         { 1, 0x7 },
25         { 0, 0x7 },
26         PATTERN_END
27 };
28
29 static pattern_t pattern_num[] = {
30         { 0, 0x5 },
31         { 1, 0x1 }, /* 10 */
32         { 0, 0x5 },
33         { 1, 0x1 }, /*  9 */
34         { 0, 0x5 },
35         { 1, 0x1 }, /*  8 */
36         { 0, 0x5 },
37         { 1, 0x1 }, /*  7 */
38         { 0, 0x5 },
39         { 1, 0x1 }, /*  6 */
40         { 0, 0x5 },
41         { 1, 0x1 }, /*  5 */
42         { 0, 0x5 },
43         { 1, 0x1 }, /*  4 */
44         { 0, 0x5 },
45         { 1, 0x1 }, /*  3 */
46         { 0, 0x5 },
47         { 1, 0x1 }, /*  2 */
48         { 0, 0x5 },
49         { 1, 0x1 }, /*  1 */
50         { 0, 0x7 },
51         { 0, 0x7 },
52         PATTERN_END
53 };
54
55 static pattern_t pattern_invnum[] = {
56         { 1, 0x5 },
57         { 0, 0x1 }, /* 10 */
58         { 1, 0x5 },
59         { 0, 0x1 }, /*  9 */
60         { 1, 0x5 },
61         { 0, 0x1 }, /*  8 */
62         { 1, 0x5 },
63         { 0, 0x1 }, /*  7 */
64         { 1, 0x5 },
65         { 0, 0x1 }, /*  6 */
66         { 1, 0x5 },
67         { 0, 0x1 }, /*  5 */
68         { 1, 0x5 },
69         { 0, 0x1 }, /*  4 */
70         { 1, 0x5 },
71         { 0, 0x1 }, /*  3 */
72         { 1, 0x5 },
73         { 0, 0x1 }, /*  2 */
74         { 1, 0x5 },
75         { 0, 0x1 }, /*  1 */
76         { 1, 0x7 },
77         { 1, 0x7 },
78         PATTERN_END
79 };
80
81 pattern_t off_pattern[] = {
82         { 0, 0x1 },
83         PATTERN_END
84 };
85
86 static void led_set_mode(unsigned char n, unsigned char mode)
87 {
88         if (n < N_PWMLEDS) {
89                 pwmled_set_mode(n, mode);
90         } else if (n < N_LEDS) {
91                 gpio_set(n - N_PWMLEDS, mode);
92         }
93 }
94
95 void led_set_pattern(unsigned char n, pattern_t *pattern)
96 {
97         if (!pattern)
98                 pattern = off_pattern;
99
100         led_patterns[n] = pattern;
101
102         led_counters[n] = pattern->duration;
103         led_set_mode(n, pattern->mode);
104 }
105
106 void init_pattern()
107 {
108         unsigned char i;
109
110         for (i = 0; i < N_LEDS; i++)
111                 led_set_pattern(i, NULL);
112
113         led_set_pattern(N_PWMLEDS+1, boot_pattern);
114 }
115
116 pattern_t *number_pattern(unsigned char num, unsigned char inv)
117 {
118         if (num >= 10)
119                 num = 10;
120
121         if (inv) {
122                 return pattern_invnum
123                         + sizeof(pattern_invnum)/sizeof(pattern_t)
124                         - 2 - 2*num;
125         } else {
126                 return pattern_num
127                         + sizeof(pattern_num)/sizeof(pattern_t)
128                         - 2 - 2*num;
129         }
130 }
131
132 static pattern_t *pattern_select(unsigned char n)
133 {
134         switch(n) {
135         case 0: return pwmled0_pattern_select();
136         case 1: return pwmled1_pattern_select();
137         case 2: return pwmled2_pattern_select();
138         case 3: return status_led_pattern_select();
139         case 4: return illumination_led_pattern_select();
140         case 6: return laser_pattern_select();
141         default: return NULL;
142         }
143 }
144
145 void pattern_reload()
146 {
147         unsigned char i;
148
149         for (i = 0; i < N_LEDS; i++)
150                 led_set_pattern(i, pattern_select(i));
151 }
152
153 static void inline pattern_finished(unsigned char n)
154 {
155         unsigned char i;
156
157         led_patterns[n] = NULL;
158
159         if (n < N_PWMLEDS) {
160                 for (i = 0; i < N_PWMLEDS; i++)
161                         if (led_patterns[i])
162                                 return;
163
164                 /* all pwmleds finished; restart them */
165                 for (i = 0; i < N_PWMLEDS; i++)
166                         led_set_pattern(i, pattern_select(i));
167         } else if (n == 3) {
168                 if (!led_patterns[4])
169                         led_set_pattern(4, pattern_select(4));
170         } else if (n == 4) {
171                 if (!led_patterns[3])
172                         led_set_pattern(3, pattern_select(3));
173         } else {
174                 led_set_pattern(n, pattern_select(n));
175         }
176 }
177
178 void patterns_next_tick()
179 {
180         unsigned char i;
181
182         for (i = 0; i < N_LEDS; i++) {
183                 if (!led_patterns[i]) {
184                         pattern_finished(i);
185                         continue;
186                 }
187
188                 if (--led_counters[i] == 0) {
189                         pattern_t *p = led_patterns[i];
190                         p++;
191                         if (p->duration == 0) { // END
192                                 /* Keep the last state, wait for others */
193                                 pattern_finished(i);
194                                 continue;
195                         }
196                         led_set_pattern(i, p);
197                 }
198
199         }
200 }
201