]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/pattern.c
d5720b0a304bee8cd7f5c9f9bc654a6a9b9834b9
[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, 0x10 },
25         { 0, 0x10 },
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, 0x1E },
51         PATTERN_END
52 };
53
54 static pattern_t pattern_invnum[] = {
55         { 1, 0x5 },
56         { 0, 0x1 }, /* 10 */
57         { 1, 0x5 },
58         { 0, 0x1 }, /*  9 */
59         { 1, 0x5 },
60         { 0, 0x1 }, /*  8 */
61         { 1, 0x5 },
62         { 0, 0x1 }, /*  7 */
63         { 1, 0x5 },
64         { 0, 0x1 }, /*  6 */
65         { 1, 0x5 },
66         { 0, 0x1 }, /*  5 */
67         { 1, 0x5 },
68         { 0, 0x1 }, /*  4 */
69         { 1, 0x5 },
70         { 0, 0x1 }, /*  3 */
71         { 1, 0x5 },
72         { 0, 0x1 }, /*  2 */
73         { 1, 0x5 },
74         { 0, 0x1 }, /*  1 */
75         { 1, 0x1E },
76         PATTERN_END
77 };
78
79 pattern_t off_pattern[] = {
80         { 0, 0x1E },
81         PATTERN_END
82 };
83
84 static void led_set_mode(unsigned char n, unsigned char mode)
85 {
86         if (n < N_PWMLEDS) {
87                 pwmled_set_mode(n, mode);
88         } else if (n < N_LEDS) {
89                 gpio_set(n - N_PWMLEDS, mode);
90         }
91 }
92
93 void led_set_pattern(unsigned char n, pattern_t *pattern)
94 {
95         if (!pattern)
96                 pattern = off_pattern;
97
98         led_patterns[n] = pattern;
99
100         led_counters[n] = pattern->duration;
101         led_set_mode(n, pattern->mode);
102 }
103
104 void init_pattern()
105 {
106         unsigned char i;
107
108         for (i = 0; i < N_LEDS; i++)
109                 led_set_pattern(i, NULL);
110
111         led_set_pattern(N_PWMLEDS+1, boot_pattern);
112 }
113
114 pattern_t *number_pattern(unsigned char num, unsigned char inv)
115 {
116         if (num >= 9)
117                 num = 9;
118
119         if (inv) {
120                 return pattern_invnum
121                         + sizeof(pattern_invnum)/sizeof(pattern_t)
122                         - 2 - 2*num;
123         } else {
124                 return pattern_num
125                         + sizeof(pattern_num)/sizeof(pattern_t)
126                         - 2 - 2*num;
127         }
128 }
129
130 static pattern_t *pattern_select(unsigned char n)
131 {
132         switch(n) {
133         case 0: return pwmled0_pattern_select();
134         case 1: return pwmled1_pattern_select();
135         case 2: return pwmled2_pattern_select();
136         case 3: return status_led_pattern_select();
137         case 4: return illumination_led_pattern_select();
138         default: return NULL;
139         }
140 }
141
142 void pattern_reload()
143 {
144         unsigned char i;
145
146         for (i = 0; i < N_LEDS; i++)
147                 led_set_pattern(i, pattern_select(i));
148 }
149
150 void patterns_next_tick()
151 {
152         unsigned char i;
153
154         for (i = 0; i < N_LEDS; i++) {
155                 if (!led_patterns[i])
156                         continue;
157
158                 if (led_counters[i] == 0) {
159                         pattern_t *p = led_patterns[i];
160                         p++;
161                         if (p->duration == 0) { // END
162                                 p = pattern_select(i);
163                         }
164                         led_set_pattern(i, p);
165                 }
166
167                 if (led_counters[i] < PATTERN_FOREVER)
168                         led_counters[i]--;
169         }
170 }
171