altos/telelco: Adjust drag race UI
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <ao.h>
19 #include <ao_lco.h>
20 #include <ao_event.h>
21 #include <ao_seven_segment.h>
22 #include <ao_quadrature.h>
23 #include <ao_lco_func.h>
24 #include <ao_radio_cmac.h>
25
26 #define DEBUG   1
27
28 #if DEBUG
29 static uint8_t  ao_lco_debug;
30 #define PRINTD(...) do { if (!ao_lco_debug) break; printf ("\r%5u %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } while(0)
31 #else
32 #define PRINTD(...) 
33 #endif
34
35 #define AO_LCO_PAD_DIGIT        0
36 #define AO_LCO_BOX_DIGIT_1      1
37 #define AO_LCO_BOX_DIGIT_10     2
38
39 #define AO_LCO_DRAG_RACE_START_TIME     AO_SEC_TO_TICKS(5)
40 #define AO_LCO_DRAG_RACE_STOP_TIME      AO_SEC_TO_TICKS(2)
41
42 static uint8_t  ao_lco_min_box, ao_lco_max_box;
43 static uint8_t  ao_lco_selected[AO_PAD_MAX_BOXES];
44 static uint8_t  ao_lco_valid[AO_PAD_MAX_BOXES];
45 static uint8_t  ao_lco_channels[AO_PAD_MAX_BOXES];
46 static uint16_t ao_lco_tick_offset[AO_PAD_MAX_BOXES];
47
48 /* UI values */
49 static uint8_t  ao_lco_armed;
50 static uint8_t  ao_lco_firing;
51 static uint16_t ao_lco_fire_tick;
52 static uint8_t  ao_lco_fire_down;
53 static uint8_t  ao_lco_drag_race;
54 static uint8_t  ao_lco_pad;
55 static int16_t  ao_lco_box;
56
57 #define AO_LCO_BOX_DRAG         0x1000
58
59 static struct ao_pad_query      ao_pad_query;
60
61 static uint8_t  ao_lco_display_mutex;
62
63 static void
64 ao_lco_set_pad(uint8_t pad)
65 {
66         ao_mutex_get(&ao_lco_display_mutex);
67         ao_seven_segment_set(AO_LCO_PAD_DIGIT, pad | (ao_lco_drag_race << 4));
68         ao_mutex_put(&ao_lco_display_mutex);
69 }
70
71 #define SEVEN_SEGMENT_d         ((0 << 0) |     \
72                                  (0 << 1) |     \
73                                  (1 << 2) |     \
74                                  (1 << 3) |     \
75                                  (1 << 4) |     \
76                                  (1 << 5) |     \
77                                  (1 << 6))
78
79
80 #define SEVEN_SEGMENT_r         ((0 << 0) |     \
81                                  (0 << 1) |     \
82                                  (0 << 2) |     \
83                                  (1 << 3) |     \
84                                  (1 << 4) |     \
85                                  (0 << 5) |     \
86                                  (0 << 6))
87
88 static void
89 ao_lco_set_box(uint16_t box)
90 {
91         ao_mutex_get(&ao_lco_display_mutex);
92         if (box == AO_LCO_BOX_DRAG) {
93                 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_10, SEVEN_SEGMENT_d | (ao_lco_drag_race << 7));
94                 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_1, SEVEN_SEGMENT_r | (ao_lco_drag_race << 7));
95         } else {
96                 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, box % 10 | (ao_lco_drag_race << 4));
97                 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, box / 10 | (ao_lco_drag_race << 4));
98         }
99         ao_mutex_put(&ao_lco_display_mutex);
100 }
101
102 static void
103 ao_lco_set_voltage(uint16_t decivolts)
104 {
105         uint8_t tens, ones, tenths;
106
107         tenths = decivolts % 10;
108         ones = (decivolts / 10) % 10;
109         tens = (decivolts / 100) % 10;
110         ao_mutex_get(&ao_lco_display_mutex);
111         ao_seven_segment_set(AO_LCO_PAD_DIGIT, tenths);
112         ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ones | 0x10);
113         ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, tens);
114         ao_mutex_put(&ao_lco_display_mutex);
115 }
116
117 static void
118 ao_lco_set_display(void)
119 {
120         if (ao_lco_pad == 0 && ao_lco_box != AO_LCO_BOX_DRAG) {
121                 ao_lco_set_voltage(ao_pad_query.battery);
122         } else {
123                 if (ao_lco_box == AO_LCO_BOX_DRAG)
124                         ao_lco_set_pad(ao_lco_drag_race);
125                 else
126                         ao_lco_set_pad(ao_lco_pad);
127                 ao_lco_set_box(ao_lco_box);
128         }
129 }
130
131 #define MASK_SIZE(n)    (((n) + 7) >> 3)
132 #define MASK_ID(n)      ((n) >> 3)
133 #define MASK_SHIFT(n)   ((n) & 7)
134
135 static uint8_t  ao_lco_box_mask[MASK_SIZE(AO_PAD_MAX_BOXES)];
136
137 static uint8_t
138 ao_lco_box_present(uint16_t box)
139 {
140         if (box == AO_LCO_BOX_DRAG)
141                 return 1;
142
143         if (box >= AO_PAD_MAX_BOXES)
144                 return 0;
145         return (ao_lco_box_mask[MASK_ID(box)] >> MASK_SHIFT(box)) & 1;
146 }
147
148 static uint8_t
149 ao_lco_pad_present(uint8_t box, uint8_t pad)
150 {
151         /* voltage measurement is always valid */
152         if (pad == 0)
153                 return 1;
154         if (!ao_lco_channels[box])
155                 return 0;
156         if (pad > AO_PAD_MAX_CHANNELS)
157                 return 0;
158         return (ao_lco_channels[box] >> (pad - 1)) & 1;
159 }
160
161 static uint8_t
162 ao_lco_pad_first(uint8_t box)
163 {
164         uint8_t pad;
165
166         for (pad = 1; pad <= AO_PAD_MAX_CHANNELS; pad++)
167                 if (ao_lco_pad_present(box, pad))
168                         return pad;
169         return 0;
170 }
171
172 static struct ao_task   ao_lco_drag_task;
173 static uint8_t          ao_lco_drag_active;
174 static uint8_t          ao_lco_drag_beep_count;
175 static uint8_t          ao_lco_drag_beep_on;
176 static uint16_t         ao_lco_drag_beep_time;
177 static uint16_t         ao_lco_drag_warn_time;
178
179 #define AO_LCO_DRAG_BEEP_TIME   AO_MS_TO_TICKS(50)
180 #define AO_LCO_DRAG_WARN_TIME   AO_SEC_TO_TICKS(5)
181
182 static void
183 ao_lco_drag_beep_start(void)
184 {
185         ao_beep(AO_BEEP_HIGH);
186         PRINTD("beep start\n");
187         ao_lco_drag_beep_on = 1;
188         ao_lco_drag_beep_time = ao_time() + AO_LCO_DRAG_BEEP_TIME;
189 }
190
191 static void
192 ao_lco_drag_beep_stop(void)
193 {
194         ao_beep(0);
195         PRINTD("beep stop\n");
196         ao_lco_drag_beep_on = 0;
197         if (ao_lco_drag_beep_count) {
198                 --ao_lco_drag_beep_count;
199                 if (ao_lco_drag_beep_count)
200                         ao_lco_drag_beep_time = ao_time() + AO_LCO_DRAG_BEEP_TIME;
201         }
202 }
203
204 static void
205 ao_lco_drag_beep(uint8_t beeps)
206 {
207         PRINTD("beep %d\n", beeps);
208         if (!ao_lco_drag_beep_count)
209                 ao_lco_drag_beep_start();
210         ao_lco_drag_beep_count += beeps;
211 }
212
213 static uint16_t
214 ao_lco_drag_beep_check(uint16_t now, uint16_t delay)
215 {
216         PRINTD("beep check count %d delta %d\n",
217                ao_lco_drag_beep_count,
218                (int16_t) (now - ao_lco_drag_beep_time));
219         if (ao_lco_drag_beep_count) {
220                 if ((int16_t) (now - ao_lco_drag_beep_time) >= 0) {
221                         if (ao_lco_drag_beep_on)
222                                 ao_lco_drag_beep_stop();
223                         else
224                                 ao_lco_drag_beep_start();
225                 }
226         }
227
228         if (ao_lco_drag_beep_count) {
229                 if (delay > AO_LCO_DRAG_BEEP_TIME)
230                         delay = AO_LCO_DRAG_BEEP_TIME;
231         }
232         return delay;
233 }
234
235 static void
236 ao_lco_drag_enable(void)
237 {
238         PRINTD("Drag enable\n");
239         ao_lco_drag_race = 1;
240         memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
241         ao_lco_drag_beep(5);
242         ao_lco_set_display();
243         ao_lco_fire_down = 0;
244 }
245
246 static void
247 ao_lco_drag_disable(void)
248 {
249         PRINTD("Drag disable\n");
250         ao_lco_drag_race = 0;
251         memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
252         ao_lco_drag_beep(2);
253         ao_lco_set_display();
254         ao_lco_fire_down = 0;
255 }
256
257 static uint16_t
258 ao_lco_drag_button_check(uint16_t now, uint16_t delay)
259 {
260         uint16_t        button_delay = ~0;
261
262         /*
263          * Check to see if the button has been held down long enough
264          * to switch in/out of drag race mode
265          */
266         if (ao_lco_fire_down) {
267                 if (ao_lco_drag_race) {
268                         if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_STOP_TIME)
269                                 ao_lco_drag_disable();
270                         else
271                                 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_STOP_TIME - now;
272                 } else {
273                         if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_START_TIME)
274                                 ao_lco_drag_enable();
275                         else
276                                 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_START_TIME - now;
277                 }
278                 if (delay > button_delay)
279                         delay = button_delay;
280         }
281         return delay;
282 }
283
284 static uint16_t
285 ao_lco_drag_warn_check(uint16_t now, uint16_t delay)
286 {
287         uint16_t        warn_delay = ~0;
288
289         if (ao_lco_drag_race) {
290                 if ((int16_t) (now - ao_lco_drag_warn_time) >= 0) {
291                         ao_lco_drag_beep(1);
292                         ao_lco_drag_warn_time = now + AO_LCO_DRAG_WARN_TIME;
293                 }
294                 warn_delay = ao_lco_drag_warn_time - now;
295         }
296         if (delay > warn_delay)
297                 delay = warn_delay;
298         return delay;
299 }
300
301 static void
302 ao_lco_drag_monitor(void)
303 {
304         uint16_t        delay = ~0;
305         uint16_t        now;
306
307         for (;;) {
308                 PRINTD("Drag monitor active %d delay %d\n", ao_lco_drag_active, delay);
309                 if (delay == (uint16_t) ~0)
310                         ao_sleep(&ao_lco_drag_active);
311                 else
312                         ao_sleep_for(&ao_lco_drag_active, delay);
313
314                 delay = ~0;
315                 if (!ao_lco_drag_active)
316                         continue;
317
318                 now = ao_time();
319                 delay = ao_lco_drag_button_check(now, delay);
320                 delay = ao_lco_drag_warn_check(now, delay);
321                 delay = ao_lco_drag_beep_check(now, delay);
322
323                 /* check to see if there's anything left to do here */
324                 if (!ao_lco_fire_down && !ao_lco_drag_race && !ao_lco_drag_beep_count) {
325                         delay = ~0;
326                         ao_lco_drag_active = 0;
327                 }
328         }
329 }
330
331 static void
332 ao_lco_input(void)
333 {
334         static struct ao_event  event;
335         int8_t          dir, new_pad;
336         int16_t         new_box;
337
338         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
339         for (;;) {
340                 ao_event_get(&event);
341                 PRINTD("event type %d unit %d value %d\n",
342                        event.type, event.unit, event.value);
343                 switch (event.type) {
344                 case AO_EVENT_QUADRATURE:
345                         switch (event.unit) {
346                         case AO_QUADRATURE_PAD:
347                                 if (!ao_lco_armed) {
348                                         dir = (int8_t) event.value;
349                                         new_pad = ao_lco_pad;
350                                         do {
351                                                 new_pad += dir;
352                                                 if (new_pad > AO_PAD_MAX_CHANNELS)
353                                                         new_pad = 0;
354                                                 if (new_pad < 0)
355                                                         new_pad = AO_PAD_MAX_CHANNELS;
356                                                 if (new_pad == ao_lco_pad)
357                                                         break;
358                                         } while (!ao_lco_pad_present(ao_lco_box, new_pad));
359                                         if (new_pad != ao_lco_pad) {
360                                                 ao_lco_pad = new_pad;
361                                                 ao_lco_set_display();
362                                         }
363                                 }
364                                 break;
365                         case AO_QUADRATURE_BOX:
366                                 if (!ao_lco_armed) {
367                                         dir = (int8_t) event.value;
368                                         new_box = ao_lco_box;
369                                         do {
370                                                 if (new_box == AO_LCO_BOX_DRAG) {
371                                                         if (dir < 0)
372                                                                 new_box = ao_lco_max_box;
373                                                         else
374                                                                 new_box = ao_lco_min_box;
375                                                 } else {
376                                                         new_box += dir;
377                                                         if (new_box > ao_lco_max_box)
378                                                                 new_box = AO_LCO_BOX_DRAG;
379                                                         else if (new_box < ao_lco_min_box)
380                                                                 new_box = AO_LCO_BOX_DRAG;
381                                                 }
382                                                 if (new_box == ao_lco_box)
383                                                         break;
384                                         } while (!ao_lco_box_present(new_box));
385                                         if (ao_lco_box != new_box) {
386                                                 ao_lco_box = new_box;
387                                                 ao_lco_pad = 1;
388                                                 if (ao_lco_box != AO_LCO_BOX_DRAG)
389                                                         ao_lco_channels[ao_lco_box] = 0;
390                                                 ao_lco_set_display();
391                                         }
392                                 }
393                                 break;
394                         }
395                         break;
396                 case AO_EVENT_BUTTON:
397                         switch (event.unit) {
398                         case AO_BUTTON_ARM:
399                                 ao_lco_armed = event.value;
400                                 PRINTD("Armed %d\n", ao_lco_armed);
401                                 if (ao_lco_armed) {
402                                         if (ao_lco_drag_race) {
403                                                 uint8_t box;
404
405                                                 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
406                                                         if (ao_lco_selected[box]) {
407                                                                 ao_wakeup(&ao_lco_armed);
408                                                                 break;
409                                                         }
410                                                 }
411                                         } else {
412                                                 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
413                                                 if (ao_lco_pad != 0 && ao_lco_box != AO_LCO_BOX_DRAG)
414                                                         ao_lco_selected[ao_lco_box] = (1 << (ao_lco_pad - 1));
415                                                 else
416                                                         ao_lco_armed = 0;
417                                         }
418                                 }
419                                 ao_wakeup(&ao_lco_armed);
420                                 break;
421                         case AO_BUTTON_FIRE:
422                                 if (ao_lco_armed) {
423                                         ao_lco_fire_down = 0;
424                                         ao_lco_firing = event.value;
425                                         PRINTD("Firing %d\n", ao_lco_firing);
426                                         ao_wakeup(&ao_lco_armed);
427                                 } else {
428                                         if (event.value) {
429                                                 if (ao_lco_box == AO_LCO_BOX_DRAG) {
430                                                         ao_lco_fire_down = 1;
431                                                         ao_lco_fire_tick = ao_time();
432                                                         ao_lco_drag_active = 1;
433                                                 }
434                                                 if (ao_lco_drag_race) {
435                                                         if (ao_lco_pad != 0 && ao_lco_box != AO_LCO_BOX_DRAG) {
436                                                                 ao_lco_selected[ao_lco_box] ^= (1 << (ao_lco_pad - 1));
437                                                                 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
438                                                                        ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
439                                                                 ao_lco_drag_beep(ao_lco_pad);
440                                                         }
441                                                 }
442                                                 ao_wakeup(&ao_lco_drag_active);
443                                         } else {
444                                                 ao_lco_fire_down = 0;
445                                                 if (ao_lco_drag_active)
446                                                         ao_wakeup(&ao_lco_drag_active);
447                                         }
448                                 }
449                                 break;
450                         }
451                         break;
452                 }
453         }
454 }
455
456 static AO_LED_TYPE      continuity_led[AO_LED_CONTINUITY_NUM] = {
457 #ifdef AO_LED_CONTINUITY_0
458         AO_LED_CONTINUITY_0,
459 #endif
460 #ifdef AO_LED_CONTINUITY_1
461         AO_LED_CONTINUITY_1,
462 #endif
463 #ifdef AO_LED_CONTINUITY_2
464         AO_LED_CONTINUITY_2,
465 #endif
466 #ifdef AO_LED_CONTINUITY_3
467         AO_LED_CONTINUITY_3,
468 #endif
469 #ifdef AO_LED_CONTINUITY_4
470         AO_LED_CONTINUITY_4,
471 #endif
472 #ifdef AO_LED_CONTINUITY_5
473         AO_LED_CONTINUITY_5,
474 #endif
475 #ifdef AO_LED_CONTINUITY_6
476         AO_LED_CONTINUITY_6,
477 #endif
478 #ifdef AO_LED_CONTINUITY_7
479         AO_LED_CONTINUITY_7,
480 #endif
481 };
482
483 static uint8_t
484 ao_lco_get_channels(uint8_t box, struct ao_pad_query *query)
485 {
486         int8_t                  r;
487
488         r = ao_lco_query(box, query, &ao_lco_tick_offset[box]);
489         if (r == AO_RADIO_CMAC_OK) {
490                 ao_lco_channels[box] = query->channels;
491                 ao_lco_valid[box] = 1;
492         } else
493                 ao_lco_valid[box] = 0;
494         PRINTD("ao_lco_get_channels(%d) rssi %d valid %d ret %d\n", box, ao_radio_cmac_rssi, ao_lco_valid[box], r);
495         ao_wakeup(&ao_pad_query);
496         return ao_lco_valid[box];
497 }
498
499 static void
500 ao_lco_update(void)
501 {
502         if (ao_lco_box != AO_LCO_BOX_DRAG) {
503                 uint8_t previous_valid = ao_lco_valid[ao_lco_box];
504
505                 if (ao_lco_get_channels(ao_lco_box, &ao_pad_query)) {
506                         if (!previous_valid) {
507                                 if (ao_lco_pad != 0)
508                                         ao_lco_pad = ao_lco_pad_first(ao_lco_box);
509                                 ao_lco_set_display();
510                         }
511                         if (ao_lco_pad == 0)
512                                 ao_lco_set_display();
513                 }
514         }
515 }
516
517 static void
518 ao_lco_box_reset_present(void)
519 {
520         ao_lco_min_box = 0xff;
521         ao_lco_max_box = 0x00;
522         memset(ao_lco_box_mask, 0, sizeof (ao_lco_box_mask));
523 }
524
525 static void
526 ao_lco_box_set_present(uint8_t box)
527 {
528         if (box < ao_lco_min_box)
529                 ao_lco_min_box = box;
530         if (box > ao_lco_max_box)
531                 ao_lco_max_box = box;
532         if (box >= AO_PAD_MAX_BOXES)
533                 return;
534         ao_lco_box_mask[MASK_ID(box)] |= 1 << MASK_SHIFT(box);
535 }
536
537 static void
538 ao_lco_search(void)
539 {
540         uint16_t        tick_offset;
541         int8_t          r;
542         int8_t          try;
543         uint8_t         box;
544         uint8_t         boxes = 0;
545
546         ao_lco_box_reset_present();
547         ao_lco_set_pad(0);
548         for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
549                 if ((box % 10) == 0)
550                         ao_lco_set_box(box);
551                 for (try = 0; try < 3; try++) {
552                         tick_offset = 0;
553                         r = ao_lco_query(box, &ao_pad_query, &tick_offset);
554                         PRINTD("box %d result %d\n", box, r);
555                         if (r == AO_RADIO_CMAC_OK) {
556                                 ++boxes;
557                                 ao_lco_box_set_present(box);
558                                 ao_lco_set_pad(boxes % 10);
559                                 ao_delay(AO_MS_TO_TICKS(30));
560                                 break;
561                         }
562                 }
563         }
564         if (ao_lco_min_box <= ao_lco_max_box)
565                 ao_lco_box = ao_lco_min_box;
566         else
567                 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
568         memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
569         memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
570         ao_lco_pad = 1;
571         ao_lco_set_display();
572 }
573
574 static void
575 ao_lco_igniter_status(void)
576 {
577         uint8_t         c;
578         uint8_t         t = 0;
579
580         for (;;) {
581                 ao_sleep(&ao_pad_query);
582                 PRINTD("RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_box == AO_LCO_BOX_DRAG ? -1 : ao_lco_valid[ao_lco_box]);
583                 if (ao_lco_box == AO_LCO_BOX_DRAG) {
584                         ao_led_off(AO_LED_RED|AO_LED_GREEN|AO_LED_AMBER);
585                         for (c = 0; c < AO_LED_CONTINUITY_NUM; c++)
586                                 ao_led_off(continuity_led[c]);
587                 } else {
588                         if (!ao_lco_valid[ao_lco_box]) {
589                                 ao_led_on(AO_LED_RED);
590                                 ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
591                                 continue;
592                         }
593                         if (ao_radio_cmac_rssi < -90) {
594                                 ao_led_on(AO_LED_AMBER);
595                                 ao_led_off(AO_LED_RED|AO_LED_GREEN);
596                         } else {
597                                 ao_led_on(AO_LED_GREEN);
598                                 ao_led_off(AO_LED_RED|AO_LED_AMBER);
599                         }
600                         if (ao_pad_query.arm_status)
601                                 ao_led_on(AO_LED_REMOTE_ARM);
602                         else
603                                 ao_led_off(AO_LED_REMOTE_ARM);
604
605                         for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
606                                 uint8_t status;
607
608                                 if (ao_lco_drag_race) {
609                                         if (ao_lco_selected[ao_lco_box] & (1 << c) && t)
610                                                 ao_led_on(continuity_led[c]);
611                                         else
612                                                 ao_led_off(continuity_led[c]);
613                                 } else {
614                                         if (ao_pad_query.channels & (1 << c))
615                                                 status = ao_pad_query.igniter_status[c];
616                                         else
617                                                 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
618                                         if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
619                                                 ao_led_on(continuity_led[c]);
620                                         else
621                                                 ao_led_off(continuity_led[c]);
622                                 }
623                         }
624                         t = 1-t;
625                 }
626         }
627 }
628
629 static void
630 ao_lco_arm_warn(void)
631 {
632         for (;;) {
633                 while (!ao_lco_armed)
634                         ao_sleep(&ao_lco_armed);
635                 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
636                 ao_delay(AO_MS_TO_TICKS(200));
637         }
638 }
639
640 static struct ao_task ao_lco_input_task;
641 static struct ao_task ao_lco_monitor_task;
642 static struct ao_task ao_lco_arm_warn_task;
643 static struct ao_task ao_lco_igniter_status_task;
644
645 static void
646 ao_lco_monitor(void)
647 {
648         uint16_t                delay;
649         uint8_t                 box;
650
651         ao_lco_search();
652         ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
653         ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
654         ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
655         ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
656         for (;;) {
657                 PRINTD("monitor armed %d firing %d\n",
658                        ao_lco_armed, ao_lco_firing);
659
660                 if (ao_lco_armed && ao_lco_firing) {
661                         ao_lco_ignite();
662                 } else {
663                         ao_lco_update();
664                         if (ao_lco_armed) {
665                                 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
666                                         if (ao_lco_selected[box]) {
667                                                 PRINTD("Arming box %d pads %x\n",
668                                                        box, ao_lco_selected[box]);
669                                                 if (ao_lco_valid[box])
670                                                         ao_lco_arm(box, ao_lco_selected[box], ao_lco_tick_offset[box]);
671                                         }
672                                 }
673                         }
674                 }
675                 if (ao_lco_armed && ao_lco_firing)
676                         delay = AO_MS_TO_TICKS(100);
677                 else
678                         delay = AO_SEC_TO_TICKS(1);
679                 ao_sleep_for(&ao_lco_armed, delay);
680         }
681 }
682
683 #if DEBUG
684 void
685 ao_lco_set_debug(void)
686 {
687         ao_cmd_decimal();
688         if (ao_cmd_status == ao_cmd_success)
689                 ao_lco_debug = ao_cmd_lex_i != 0;
690 }
691
692 __code struct ao_cmds ao_lco_cmds[] = {
693         { ao_lco_set_debug,     "D <0 off, 1 on>\0Debug" },
694         { ao_lco_search,        "s\0Search for pad boxes" },
695         { 0, NULL }
696 };
697 #endif
698
699 void
700 ao_lco_init(void)
701 {
702         ao_add_task(&ao_lco_monitor_task, ao_lco_monitor, "lco monitor");
703 #if DEBUG
704         ao_cmd_register(&ao_lco_cmds[0]);
705 #endif
706 }