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