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