altos: Fix telelcotwo build for new ao_lco_bits.c file
[fw/altos] / src / drivers / ao_lco.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_lco.h>
21 #include <ao_event.h>
22 #include <ao_seven_segment.h>
23 #include <ao_quadrature.h>
24 #include <ao_lco_func.h>
25 #include <ao_radio_cmac.h>
26 #if HAS_ADC_SINGLE
27 #include <ao_adc_single.h>
28 #endif
29
30 #define AO_LCO_PAD_DIGIT        0
31 #define AO_LCO_BOX_DIGIT_1      1
32 #define AO_LCO_BOX_DIGIT_10     2
33
34 #define AO_LCO_DRAG_RACE_START_TIME     AO_SEC_TO_TICKS(5)
35 #define AO_LCO_DRAG_RACE_STOP_TIME      AO_SEC_TO_TICKS(2)
36
37 #define AO_LCO_VALID_LAST       1
38 #define AO_LCO_VALID_EVER       2
39
40 /* UI values */
41 static uint16_t ao_lco_fire_tick;
42 static uint8_t  ao_lco_fire_down;
43
44 static uint8_t  ao_lco_display_mutex;
45
46 void
47 ao_lco_show_pad(uint8_t pad)
48 {
49         ao_mutex_get(&ao_lco_display_mutex);
50         ao_seven_segment_set(AO_LCO_PAD_DIGIT, pad | (ao_lco_drag_race << 4));
51         ao_mutex_put(&ao_lco_display_mutex);
52 }
53
54 #define SEVEN_SEGMENT_d         ((0 << 0) |     \
55                                  (0 << 1) |     \
56                                  (1 << 2) |     \
57                                  (1 << 3) |     \
58                                  (1 << 4) |     \
59                                  (1 << 5) |     \
60                                  (1 << 6))
61
62
63 #define SEVEN_SEGMENT_r         ((0 << 0) |     \
64                                  (0 << 1) |     \
65                                  (0 << 2) |     \
66                                  (1 << 3) |     \
67                                  (1 << 4) |     \
68                                  (0 << 5) |     \
69                                  (0 << 6))
70
71 void
72 ao_lco_show_box(uint16_t box)
73 {
74         ao_mutex_get(&ao_lco_display_mutex);
75         if (box == AO_LCO_BOX_DRAG) {
76                 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_10, SEVEN_SEGMENT_d | (ao_lco_drag_race << 7));
77                 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_1, SEVEN_SEGMENT_r | (ao_lco_drag_race << 7));
78         } else {
79                 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, box % 10 | (ao_lco_drag_race << 4));
80                 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, box / 10 | (ao_lco_drag_race << 4));
81         }
82         ao_mutex_put(&ao_lco_display_mutex);
83 }
84
85 void
86 ao_lco_show_voltage(uint16_t decivolts)
87 {
88         uint8_t tens, ones, tenths;
89
90         tenths = decivolts % 10;
91         ones = (decivolts / 10) % 10;
92         tens = (decivolts / 100) % 10;
93         ao_mutex_get(&ao_lco_display_mutex);
94         ao_seven_segment_set(AO_LCO_PAD_DIGIT, tenths);
95         ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ones | 0x10);
96         ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, tens);
97         ao_mutex_put(&ao_lco_display_mutex);
98 }
99
100 void
101 ao_lco_show_display(void)
102 {
103         if (ao_lco_pad == AO_LCO_PAD_VOLTAGE && ao_lco_box != AO_LCO_BOX_DRAG) {
104                 ao_lco_show_voltage(ao_pad_query.battery);
105         } else {
106                 if (ao_lco_box == AO_LCO_BOX_DRAG)
107                         ao_lco_show_pad(ao_lco_drag_race);
108                 else
109                         ao_lco_show_pad(ao_lco_pad);
110                 ao_lco_show_box(ao_lco_box);
111         }
112 }
113
114 uint8_t
115 ao_lco_box_present(uint16_t box)
116 {
117         if (box == AO_LCO_BOX_DRAG)
118                 return 1;
119
120         if (box >= AO_PAD_MAX_BOXES)
121                 return 0;
122         return (ao_lco_box_mask[AO_LCO_MASK_ID(box)] >> AO_LCO_MASK_SHIFT(box)) & 1;
123 }
124
125 static struct ao_task   ao_lco_drag_task;
126 static uint8_t          ao_lco_drag_active;
127
128 static uint16_t
129 ao_lco_drag_button_check(uint16_t now, uint16_t delay)
130 {
131         uint16_t        button_delay = ~0;
132
133         /*
134          * Check to see if the button has been held down long enough
135          * to switch in/out of drag race mode
136          */
137         if (ao_lco_fire_down) {
138                 if (ao_lco_drag_race) {
139                         if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_STOP_TIME) {
140                                 ao_lco_drag_disable();
141                                 ao_lco_fire_down = 0;
142                         }
143                         else
144                                 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_STOP_TIME - now;
145                 } else {
146                         if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_START_TIME) {
147                                 ao_lco_drag_enable();
148                                 ao_lco_fire_down = 0;
149                         }
150                         else
151                                 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_START_TIME - now;
152                 }
153                 if (delay > button_delay)
154                         delay = button_delay;
155         }
156         return delay;
157 }
158
159 static void
160 ao_lco_drag_monitor(void)
161 {
162         uint16_t        delay = ~0;
163         uint16_t        now;
164
165         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
166         for (;;) {
167                 PRINTD("Drag monitor count %d active %d delay %d\n",
168                        ao_lco_drag_beep_count, ao_lco_drag_active, delay);
169                 if (delay == (uint16_t) ~0)
170                         ao_sleep(&ao_lco_drag_beep_count);
171                 else
172                         ao_sleep_for(&ao_lco_drag_beep_count, delay);
173
174                 delay = ~0;
175                 if (!ao_lco_drag_active)
176                         continue;
177
178                 now = ao_time();
179                 delay = ao_lco_drag_button_check(now, delay);
180                 delay = ao_lco_drag_warn_check(now, delay);
181                 delay = ao_lco_drag_beep_check(now, delay);
182
183                 /* check to see if there's anything left to do here */
184                 if (!ao_lco_fire_down && !ao_lco_drag_race && !ao_lco_drag_beep_count) {
185                         delay = ~0;
186                         ao_lco_drag_active = 0;
187                 }
188         }
189 }
190
191 static void
192 ao_lco_step_box(int8_t dir)
193 {
194         int16_t new_box = ao_lco_box;
195         do {
196                 if (new_box == AO_LCO_BOX_DRAG) {
197                         if (dir < 0)
198                                 new_box = ao_lco_max_box;
199                         else
200                                 new_box = ao_lco_min_box;
201                 } else {
202                         new_box += dir;
203                         if (new_box > ao_lco_max_box)
204                                 new_box = AO_LCO_BOX_DRAG;
205                         else if (new_box < ao_lco_min_box)
206                                 new_box = AO_LCO_BOX_DRAG;
207                 }
208                 if (new_box == ao_lco_box)
209                         break;
210         } while (!ao_lco_box_present(new_box));
211         ao_lco_set_box(new_box);
212 }
213
214 static void
215 ao_lco_input(void)
216 {
217         static struct ao_event  event;
218
219         for (;;) {
220                 ao_event_get(&event);
221                 PRINTD("event type %d unit %d value %d\n",
222                        event.type, event.unit, event.value);
223                 switch (event.type) {
224                 case AO_EVENT_QUADRATURE:
225                         switch (event.unit) {
226                         case AO_QUADRATURE_PAD:
227                                 if (!ao_lco_armed)
228                                         ao_lco_step_pad((int8_t) event.value);
229                                 break;
230                         case AO_QUADRATURE_BOX:
231                                 if (!ao_lco_armed)
232                                         ao_lco_step_box((int8_t) event.value);
233                                 break;
234                         }
235                         break;
236                 case AO_EVENT_BUTTON:
237                         switch (event.unit) {
238                         case AO_BUTTON_ARM:
239                                 ao_lco_set_armed(event.value);
240                                 break;
241                         case AO_BUTTON_FIRE:
242                                 if (ao_lco_armed) {
243                                         ao_lco_fire_down = 0;
244                                         ao_lco_set_firing(event.value);
245                                 } else {
246                                         if (event.value) {
247                                                 if (ao_lco_box == AO_LCO_BOX_DRAG) {
248                                                         ao_lco_fire_down = 1;
249                                                         ao_lco_fire_tick = ao_time();
250                                                         ao_lco_drag_active = 1;
251                                                         ao_wakeup(&ao_lco_drag_beep_count);
252                                                 } else {
253                                                         ao_lco_toggle_drag();
254                                                 }
255                                         } else {
256                                                 ao_lco_fire_down = 0;
257                                                 if (ao_lco_drag_active)
258                                                         ao_wakeup(&ao_lco_drag_beep_count);
259                                         }
260                                 }
261                                 break;
262                         }
263                         break;
264                 }
265         }
266 }
267
268 /*
269  * Light up everything for a second at power on to let the user
270  * visually inspect the system for correct operation
271  */
272 static void
273 ao_lco_display_test()
274 {
275         ao_mutex_get(&ao_lco_display_mutex);
276         ao_seven_segment_set(AO_LCO_PAD_DIGIT, 8 | 0x10);
277         ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, 8 | 0x10);
278         ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, 8 | 0x10);
279         ao_mutex_put(&ao_lco_display_mutex);
280         ao_led_on(LEDS_AVAILABLE);
281         ao_delay(AO_MS_TO_TICKS(1000));
282         ao_led_off(LEDS_AVAILABLE);
283 }
284
285 #if HAS_ADC_SINGLE
286 static void
287 ao_lco_batt_voltage(void)
288 {
289         struct ao_adc   packet;
290         int16_t         decivolt;
291
292         ao_adc_single_get(&packet);
293         decivolt = ao_battery_decivolt(packet.v_batt);
294         ao_lco_show_voltage(decivolt);
295         ao_delay(AO_MS_TO_TICKS(1000));
296 }
297 #endif
298
299 static struct ao_task ao_lco_input_task;
300 static struct ao_task ao_lco_monitor_task;
301 static struct ao_task ao_lco_arm_warn_task;
302 static struct ao_task ao_lco_igniter_status_task;
303
304 static void
305 ao_lco_main(void)
306 {
307         ao_lco_display_test();
308 #if HAS_ADC_SINGLE
309         ao_lco_batt_voltage();
310 #endif
311         ao_lco_search();
312         ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
313         ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
314         ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
315         ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
316         ao_lco_monitor();
317 }
318
319 #if DEBUG
320 void
321 ao_lco_set_debug(void)
322 {
323         ao_cmd_decimal();
324         if (ao_cmd_status == ao_cmd_success)
325                 ao_lco_debug = ao_cmd_lex_i != 0;
326 }
327
328 __code struct ao_cmds ao_lco_cmds[] = {
329         { ao_lco_set_debug,     "D <0 off, 1 on>\0Debug" },
330         { ao_lco_search,        "s\0Search for pad boxes" },
331         { 0, NULL }
332 };
333 #endif
334
335 void
336 ao_lco_init(void)
337 {
338         ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
339 #if DEBUG
340         ao_cmd_register(&ao_lco_cmds[0]);
341 #endif
342 }