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