]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/control.c
firmware: buttons + hall probe via ADC
[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, braking;
102
103 void init_control()
104 {
105         dim_mode = 0;
106         towbar_mode = 0;
107 }
108
109 void brake_on()
110 {
111         braking = 1;
112         gpio_set(0, 1);
113         led_set_pattern(N_PWMLEDS, status_led_pattern_select());
114         // TODO brighten rear light
115 }
116
117 void brake_off()
118 {
119         braking = 0;
120         gpio_set(0, 0);
121         led_set_pattern(N_PWMLEDS, status_led_pattern_select());
122         // TODO dim rear light
123 }
124
125 void toggle_dim_mode()
126 {
127         dim_mode = !dim_mode;
128         pattern_reload();
129 }
130
131 void set_panic_mode()
132 {
133         if (!dim_mode)
134                 led_set_pattern(0, panic_pattern);
135
136         led_set_pattern(1, panic_pattern);
137         led_set_pattern(2, panic_pattern);
138         led_set_pattern(4, panic_pattern);
139 }
140
141 pattern_t *pwmled0_pattern_select()
142 {
143         if (battery_critical)
144                 return on1_pattern;
145
146         if (towbar_mode)
147                 return NULL;
148
149         switch (ambient_zone) {
150         case 0: return dim_mode ? NULL : on3_pattern;
151         case 1: return dim_mode ? NULL : normal3_pattern;
152         case 2: return dim_mode ? slow3_pattern : normal3_pattern;
153         case 3:
154         default: return dim_mode ? slow3_pattern : normal4_pattern;
155         }
156 }
157
158 pattern_t *pwmled1_pattern_select()
159 {
160 #ifndef TESTING_FW
161         return NULL;
162 #else
163         if (battery_critical)
164                 return on1_pattern;
165 #endif
166
167         if (towbar_mode) {
168                 switch (ambient_zone) {
169                 case 0:
170                 case 1:
171                         return dim_mode ? on2_pattern : on1_pattern;
172                 case 2: return dim_mode ? NULL : on2_pattern;
173                 case 3:
174                 default: return dim_mode ? NULL : on3_pattern;
175                 }
176         } else {
177                 switch (ambient_zone) {
178                 case 0: return dim_mode ? slow1_pattern : normal2_pattern;
179                 case 1: return dim_mode ? slow2_pattern : normal3_pattern;
180                 case 2: return dim_mode ? NULL : normal4_pattern;
181                 case 3:
182                 default: return NULL;
183                 }
184         }
185 }
186
187 pattern_t *pwmled2_pattern_select()
188 {
189 #ifndef TESTING_FW
190         if (battery_critical)
191                 return on1_pattern;
192 #endif
193
194         switch (ambient_zone) {
195         case 0: return dim_mode ? on2_pattern : on3_pattern;
196         case 1: return dim_mode ? slow1_pattern : normal2_pattern;
197         case 2:
198         case 3:
199         default:
200                 return dim_mode ? slow2_pattern : normal3_pattern;
201         }
202 }
203
204 pattern_t *status_led_pattern_select()
205 {
206         if (braking)
207                 return on1_pattern;
208
209         if (buttons_setup_in_progress())
210                 return buttons_setup_status0_pattern_select();
211
212         // FIXME: do something sane
213         return number_pattern(battery_gauge(), 0);
214 }
215
216 pattern_t *illumination_led_pattern_select()
217 {
218         if (battery_critical)
219                 return NULL;
220
221         switch (ambient_zone) {
222         case 0: return dim_mode
223                 ? number_pattern(1, 1)
224                 : on1_pattern;
225         case 1: return dim_mode
226                 ? number_pattern(2, 1)
227                 : number_pattern(3, 1);
228         case 2: return dim_mode
229                 ? number_pattern(1, 0)
230                 : number_pattern(2, 0);
231         case 3:
232         default: return dim_mode
233                 ? number_pattern(3, 0)
234                 : number_pattern(4, 0);
235         }
236 }
237
238 pattern_t *laser_pattern_select()
239 {
240         if (!dim_mode && ambient_zone <= 1)
241                 return number_pattern(2, 1);
242         else
243                 return NULL;
244 }