]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/control.c
Ability to quickly build testing firmware
[bike-lights.git] / firmware / control.c
1 #include <inttypes.h>
2 #include <stdlib.h> // for NULL
3
4 #include "lights.h"
5
6 static pattern_t panic_pattern[] = {
7         { 3, 1 }, // FIXME: will be 4, but let's be safe while testing
8         { 0, 1 },
9         { 3, 1 },
10         { 0, 1 },
11         { 3, 1 },
12         { 0, 1 },
13         { 3, 1 },
14         { 0, 1 },
15         { 3, 1 },
16         { 0, 1 },
17         { 3, 1 },
18         { 0, 1 },
19         { 3, 1 },
20         { 0, 1 },
21         { 3, 1 },
22         { 0, 1 },
23         { 3, 1 },
24         { 0, 1 },
25         { 3, 1 },
26         { 0, 1 },
27         { 3, 1 },
28         { 0, 1 },
29         PATTERN_END
30 };
31
32 pattern_t on1_pattern [] = {
33         { 1, PATTERN_FOREVER }
34 };
35
36 static pattern_t on2_pattern [] = {
37         { 2, PATTERN_FOREVER }
38 };
39
40 static pattern_t on3_pattern [] = {
41         { 3, PATTERN_FOREVER }
42 };
43
44 static pattern_t normal2_pattern[] = {
45         { 2, 0x1 },
46         { 0, 0x1 },
47         { 2, 0x1 },
48         { 0, 0x8 },
49         { 1, 0x1 },
50         { 0, 0x1 },
51         { 1, 0x1 },
52         { 0, 0x8 },
53         PATTERN_END
54 };
55
56 static pattern_t normal3_pattern[] = {
57         { 3, 0x1 },
58         { 0, 0x1 },
59         { 3, 0x1 },
60         { 0, 0x8 },
61         { 1, 0x1 },
62         { 0, 0x1 },
63         { 1, 0x1 },
64         { 0, 0x8 },
65         PATTERN_END
66 };
67
68 static pattern_t normal4_pattern[] = {
69         { 4, 0x1 },
70         { 0, 0x1 },
71         { 4, 0x1 },
72         { 0, 0x8 },
73         { 1, 0x1 },
74         { 0, 0x1 },
75         { 1, 0x1 },
76         { 0, 0x8 },
77         PATTERN_END
78 };
79
80 static pattern_t slow1_pattern[] = {
81         { 1, 0x01 },
82         { 0, 0x10 },
83         PATTERN_END
84 };
85
86 static pattern_t slow2_pattern[] = {
87         { 2, 0x01 },
88         { 0, 0x10 },
89         PATTERN_END
90 };
91
92 static pattern_t slow3_pattern[] = {
93         { 3, 0x01 },
94         { 0, 0x10 },
95         PATTERN_END
96 };
97
98 static unsigned char dim_mode, towbar_mode;
99
100 void init_control()
101 {
102         dim_mode = 0;
103         towbar_mode = 0;
104 }
105
106 void toggle_dim_mode()
107 {
108         dim_mode = !dim_mode;
109         pattern_reload();
110 }
111
112 void set_panic_mode()
113 {
114         if (!dim_mode)
115                 led_set_pattern(0, panic_pattern);
116
117         led_set_pattern(1, panic_pattern);
118         led_set_pattern(2, panic_pattern);
119         led_set_pattern(4, panic_pattern);
120 }
121
122 pattern_t *pwmled0_pattern_select()
123 {
124         // TODO: battery critical
125
126 #ifndef TESTING_FW
127         return normal3_pattern;
128 #endif
129
130         if (towbar_mode)
131                 return NULL;
132
133         switch (ambient_zone) {
134         case 0: return dim_mode ? NULL : on1_pattern;
135         case 1: return dim_mode ? NULL : slow2_pattern;
136         case 2: return dim_mode ? slow3_pattern : slow2_pattern;
137         case 3:
138         default: return dim_mode ? slow3_pattern : normal4_pattern;
139         }
140 }
141
142 pattern_t *pwmled1_pattern_select()
143 {
144         // TODO: battery critical
145
146 #ifndef TESTING_FW
147         return off_pattern;
148 #endif
149         if (towbar_mode) {
150                 switch (ambient_zone) {
151                 case 0:
152                 case 1:
153                         return dim_mode ? on2_pattern : on1_pattern;
154                 case 2: return dim_mode ? NULL : on2_pattern;
155                 case 3:
156                 default: return dim_mode ? NULL : on3_pattern;
157                 }
158         } else {
159                 switch (ambient_zone) {
160                 case 0: return dim_mode ? slow1_pattern : normal2_pattern;
161                 case 1: return dim_mode ? slow2_pattern : normal3_pattern;
162                 case 2: return dim_mode ? NULL : normal4_pattern;
163                 case 3:
164                 default: return NULL;
165                 }
166         }
167 }
168
169 pattern_t *pwmled2_pattern_select()
170 {
171         // TODO: battery critical
172
173 #ifndef TESTING_FW
174         return on3_pattern;
175 #endif
176         switch (ambient_zone) {
177         case 0: return dim_mode ? on2_pattern : on3_pattern;
178         case 1: return dim_mode ? slow1_pattern : normal2_pattern;
179         case 2:
180         case 3:
181         default:
182                 return dim_mode ? slow2_pattern : normal3_pattern;
183         }
184 }
185
186 pattern_t *status_led_pattern_select()
187 {
188         if (buttons_setup_in_progress())
189                 return buttons_setup_status0_pattern_select();
190
191         // FIXME: do something sane
192         return number_pattern(1 + ambient_zone, 0);
193 }
194
195 pattern_t *illumination_led_pattern_select()
196 {
197         // todo: battery critical
198
199         switch (ambient_zone) {
200         case 0: return dim_mode
201                 ? number_pattern(1, 1)
202                 : on1_pattern;
203         case 1: return dim_mode
204                 ? number_pattern(2, 1)
205                 : number_pattern(3, 1);
206         case 2: return dim_mode
207                 ? number_pattern(3, 0)
208                 : number_pattern(4, 0);
209         case 3:
210         default: return dim_mode
211                 ? number_pattern(5, 0)
212                 : number_pattern(6, 0);
213         }
214 }
215