]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/control.c
pattern: laser diodes as gpio 4
[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         if (towbar_mode)
130                 return NULL;
131
132         switch (ambient_zone) {
133         case 0: return dim_mode ? NULL : on3_pattern;
134         case 1: return dim_mode ? NULL : normal3_pattern;
135         case 2: return dim_mode ? slow3_pattern : normal3_pattern;
136         case 3:
137         default: return dim_mode ? slow3_pattern : normal4_pattern;
138         }
139 }
140
141 pattern_t *pwmled1_pattern_select()
142 {
143         // TODO: battery critical
144
145 #ifndef TESTING_FW
146         return NULL;
147 #endif
148
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(1, 0)
208                 : number_pattern(2, 0);
209         case 3:
210         default: return dim_mode
211                 ? number_pattern(3, 0)
212                 : number_pattern(4, 0);
213         }
214 }
215
216 pattern_t *laser_pattern_select()
217 {
218         if (!dim_mode && ambient_zone <= 1)
219                 return number_pattern(2, 1);
220         else
221                 return NULL;
222 }