]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/pattern.c
firmware: control logic moved to a separate module
[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 static void led_set_mode(unsigned char n, unsigned char mode)
80 {
81         if (n < N_PWMLEDS) {
82                 pwmled_set_mode(n, mode);
83         } else if (n < N_LEDS) {
84                 gpio_set(n - N_PWMLEDS, mode);
85         }
86 }
87
88 void led_set_pattern(unsigned char n, pattern_t *pattern)
89 {
90         led_patterns[n] = pattern;
91
92         if (!pattern) {
93                 led_set_mode(n, 0);
94                 return;
95         }
96
97         led_counters[n] = pattern->duration;
98         led_set_mode(n, pattern->mode);
99 }
100
101 void init_pattern()
102 {
103         unsigned char i;
104
105         for (i = 0; i < N_LEDS; i++)
106                 led_set_pattern(i, NULL);
107
108         led_set_pattern(N_PWMLEDS+1, boot_pattern);
109 }
110
111 pattern_t *number_pattern(unsigned char num, unsigned char inv)
112 {
113         if (num >= 9)
114                 num = 9;
115
116         if (inv) {
117                 return pattern_invnum
118                         + sizeof(pattern_invnum)/sizeof(pattern_t)
119                         - 2 - 2*num;
120         } else {
121                 return pattern_num
122                         + sizeof(pattern_num)/sizeof(pattern_t)
123                         - 2 - 2*num;
124         }
125 }
126
127 static pattern_t *pattern_select(unsigned char n)
128 {
129         switch(n) {
130         case 0: return pwmled0_pattern_select();
131         case 1: return pwmled1_pattern_select();
132         case 2: return pwmled2_pattern_select();
133         case 3: return status_led_pattern_select();
134         case 4: return illumination_led_pattern_select();
135         default: return NULL;
136         }
137 }
138
139 void pattern_reload()
140 {
141         unsigned char i;
142
143         for (i = 0; i < N_LEDS; i++)
144                 led_set_pattern(i, pattern_select(i));
145 }
146
147 void patterns_next_tick()
148 {
149         unsigned char i;
150
151         for (i = 0; i < N_LEDS; i++) {
152                 if (!led_patterns[i])
153                         continue;
154
155                 if (led_counters[i] == 0) {
156                         pattern_t *p = led_patterns[i];
157                         p++;
158                         if (p->duration == 0) { // END
159                                 p = pattern_select(i);
160                         }
161                         led_set_pattern(i, p);
162                 }
163
164                 if (led_counters[i] < PATTERN_FOREVER)
165                         led_counters[i]--;
166         }
167 }
168