]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/control.c
firmware: control logic moved to a separate module
[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         { 2, 0x1 },
58         { 0, 0x1 },
59         { 2, 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         { 2, 0x1 },
70         { 0, 0x1 },
71         { 2, 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         if (towbar_mode)
127                 return NULL;
128
129         switch (ambient_zone) {
130         case 0: return dim_mode ? NULL : on1_pattern;
131         case 1: return dim_mode ? NULL : slow2_pattern;
132         case 2: return dim_mode ? slow3_pattern : slow2_pattern;
133         case 3:
134         default: return dim_mode ? slow3_pattern : normal4_pattern;
135         }
136 }
137
138 pattern_t *pwmled1_pattern_select()
139 {
140         // TODO: battery critical
141
142         if (towbar_mode) {
143                 switch (ambient_zone) {
144                 case 0:
145                 case 1:
146                         return dim_mode ? on2_pattern : on1_pattern;
147                 case 2: return dim_mode ? NULL : on2_pattern;
148                 case 3:
149                 default: return dim_mode ? NULL : on3_pattern;
150                 }
151         } else {
152                 switch (ambient_zone) {
153                 case 0: return dim_mode ? slow1_pattern : normal2_pattern;
154                 case 1: return dim_mode ? slow2_pattern : normal3_pattern;
155                 case 2: return dim_mode ? NULL : normal4_pattern;
156                 case 3:
157                 default: return NULL;
158                 }
159         }
160 }
161
162 pattern_t *pwmled2_pattern_select()
163 {
164         // TODO: battery critical
165
166         switch (ambient_zone) {
167         case 0: return dim_mode ? on2_pattern : on3_pattern;
168         case 1: return dim_mode ? slow1_pattern : normal2_pattern;
169         case 2:
170         case 3:
171         default:
172                 return dim_mode ? slow2_pattern : normal3_pattern;
173         }
174 }
175
176 pattern_t *status_led_pattern_select()
177 {
178         if (buttons_setup_in_progress())
179                 return buttons_setup_status0_pattern_select();
180
181         // FIXME: do something sane
182         return number_pattern(1 + ambient_zone, 1);
183 }
184
185 pattern_t *illumination_led_pattern_select()
186 {
187         // todo: battery critical
188
189         switch (ambient_zone) {
190         case 0: return dim_mode
191                 ? number_pattern(1, 1)
192                 : on1_pattern;
193         case 1: return dim_mode
194                 ? number_pattern(2, 1)
195                 : number_pattern(3, 1);
196         case 2: return dim_mode
197                 ? number_pattern(3, 0)
198                 : number_pattern(4, 0);
199         case 3:
200         default: return dim_mode
201                 ? number_pattern(5, 0)
202                 : number_pattern(6, 0);
203         }
204 }
205