140955da859ff4ec88b47f07c0f221344f68dac3
[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_active;
190 static uint8_t          ao_lco_drag_beep_count;
191 static uint8_t          ao_lco_drag_beep_on;
192 static uint16_t         ao_lco_drag_beep_time;
193 static uint16_t         ao_lco_drag_warn_time;
194
195 #define AO_LCO_DRAG_BEEP_TIME   AO_MS_TO_TICKS(50)
196 #define AO_LCO_DRAG_WARN_TIME   AO_SEC_TO_TICKS(5)
197
198 static void
199 ao_lco_drag_beep_start(void)
200 {
201         ao_beep(AO_BEEP_HIGH);
202         PRINTD("beep start\n");
203         ao_lco_drag_beep_on = 1;
204         ao_lco_drag_beep_time = ao_time() + AO_LCO_DRAG_BEEP_TIME;
205 }
206
207 static void
208 ao_lco_drag_beep_stop(void)
209 {
210         ao_beep(0);
211         PRINTD("beep stop\n");
212         ao_lco_drag_beep_on = 0;
213         if (ao_lco_drag_beep_count) {
214                 --ao_lco_drag_beep_count;
215                 if (ao_lco_drag_beep_count)
216                         ao_lco_drag_beep_time = ao_time() + AO_LCO_DRAG_BEEP_TIME;
217         }
218 }
219
220 static void
221 ao_lco_drag_beep(uint8_t beeps)
222 {
223         PRINTD("beep %d\n", beeps);
224         if (!ao_lco_drag_beep_count)
225                 ao_lco_drag_beep_start();
226         ao_lco_drag_beep_count += beeps;
227 }
228
229 static uint16_t
230 ao_lco_drag_beep_check(uint16_t now, uint16_t delay)
231 {
232         PRINTD("beep check count %d delta %d\n",
233                ao_lco_drag_beep_count,
234                (int16_t) (now - ao_lco_drag_beep_time));
235         if (ao_lco_drag_beep_count) {
236                 if ((int16_t) (now - ao_lco_drag_beep_time) >= 0) {
237                         if (ao_lco_drag_beep_on)
238                                 ao_lco_drag_beep_stop();
239                         else
240                                 ao_lco_drag_beep_start();
241                 }
242         }
243
244         if (ao_lco_drag_beep_count) {
245                 if (delay > AO_LCO_DRAG_BEEP_TIME)
246                         delay = AO_LCO_DRAG_BEEP_TIME;
247         }
248         return delay;
249 }
250
251 static void
252 ao_lco_drag_enable(void)
253 {
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_beep(5);
259         ao_lco_set_display();
260 }
261
262 static void
263 ao_lco_drag_disable(void)
264 {
265         PRINTD("Drag disable\n");
266         ao_lco_drag_race = 0;
267         ao_led_off(AO_LED_DRAG);
268         memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
269         ao_lco_drag_beep(2);
270         ao_lco_set_display();
271 }
272
273 static uint16_t
274 ao_lco_drag_warn_check(uint16_t now, uint16_t delay)
275 {
276         uint16_t        warn_delay = ~0;
277
278         if (ao_lco_drag_race) {
279                 if ((int16_t) (now - ao_lco_drag_warn_time) >= 0) {
280                         ao_lco_drag_beep(1);
281                         ao_lco_drag_warn_time = now + AO_LCO_DRAG_WARN_TIME;
282                 }
283                 warn_delay = ao_lco_drag_warn_time - now;
284         }
285         if (delay > warn_delay)
286                 delay = warn_delay;
287         return delay;
288 }
289
290 static void
291 ao_lco_drag_monitor(void)
292 {
293         uint16_t        delay = ~0;
294         uint16_t        now;
295
296         for (;;) {
297                 PRINTD("Drag monitor active %d delay %d\n", ao_lco_drag_active, delay);
298                 if (delay == (uint16_t) ~0)
299                         ao_sleep(&ao_lco_drag_active);
300                 else
301                         ao_sleep_for(&ao_lco_drag_active, delay);
302
303                 delay = ~0;
304                 if (!ao_lco_drag_active)
305                         continue;
306
307                 now = ao_time();
308                 delay = ao_lco_drag_warn_check(now, delay);
309                 delay = ao_lco_drag_beep_check(now, delay);
310
311                 /* check to see if there's anything left to do here */
312                 if (!ao_lco_drag_race && !ao_lco_drag_beep_count) {
313                         delay = ~0;
314                         ao_lco_drag_active = 0;
315                 }
316         }
317 }
318
319 static void
320 ao_lco_input(void)
321 {
322         static struct ao_event  event;
323         int8_t          dir, new_pad;
324         int16_t         new_box;
325
326         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
327         for (;;) {
328                 ao_event_get(&event);
329                 PRINTD("event type %d unit %d value %d\n",
330                        event.type, event.unit, event.value);
331                 switch (event.type) {
332                 case AO_EVENT_QUADRATURE:
333                         switch (event.unit) {
334                         case AO_QUADRATURE_SELECT:
335                                 if (!ao_lco_armed) {
336                                         switch (ao_lco_select_mode) {
337                                         case AO_LCO_SELECT_PAD:
338                                                 dir = (int8_t) event.value;
339                                                 new_pad = ao_lco_pad;
340                                                 do {
341                                                         new_pad += dir;
342                                                         if (new_pad > AO_PAD_MAX_CHANNELS)
343                                                                 new_pad = 0;
344                                                         if (new_pad < 0)
345                                                                 new_pad = AO_PAD_MAX_CHANNELS;
346                                                         if (new_pad == ao_lco_pad)
347                                                                 break;
348                                                 } while (!ao_lco_pad_present(ao_lco_box, new_pad));
349                                                 if (new_pad != ao_lco_pad) {
350                                                         ao_lco_pad = new_pad;
351                                                         ao_lco_set_display();
352                                                 }
353                                                 break;
354                                         case AO_LCO_SELECT_BOX:
355                                                 dir = (int8_t) event.value;
356                                                 new_box = ao_lco_box;
357                                                 do {
358                                                         new_box += dir;
359                                                         if (new_box > ao_lco_max_box)
360                                                                 new_box = ao_lco_min_box;
361                                                         else if (new_box < ao_lco_min_box)
362                                                                 new_box = ao_lco_max_box;
363                                                         if (new_box == ao_lco_box)
364                                                                 break;
365                                                 } while (!ao_lco_box_present(new_box));
366                                                 if (ao_lco_box != new_box) {
367                                                         ao_lco_box = new_box;
368                                                         ao_lco_pad = 1;
369                                                         ao_lco_channels[ao_lco_box] = 0;
370                                                         ao_lco_set_display();
371                                                 }
372                                                 break;
373                                         default:
374                                                 break;
375                                         }
376                                 }
377                                 break;
378                         }
379                         break;
380                 case AO_EVENT_BUTTON:
381                         switch (event.unit) {
382                         case AO_BUTTON_ARM:
383                                 ao_lco_armed = event.value;
384                                 PRINTD("Armed %d\n", ao_lco_armed);
385                                 if (ao_lco_armed) {
386                                         if (ao_lco_drag_race) {
387                                                 uint8_t box;
388
389                                                 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++)
390                                                         if (ao_lco_selected[box])
391                                                                 break;
392                                                 if (box > ao_lco_max_box)
393                                                         ao_lco_armed = 0;
394                                         } else {
395                                                 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
396                                                 if (ao_lco_pad != 0)
397                                                         ao_lco_selected[ao_lco_box] = (1 << (ao_lco_pad - 1));
398                                                 else
399                                                         ao_lco_armed = 0;
400                                         }
401                                 }
402                                 ao_lco_set_select();
403                                 ao_wakeup(&ao_lco_armed);
404                                 break;
405                         case AO_BUTTON_FIRE:
406                                 if (ao_lco_armed) {
407                                         ao_lco_firing = event.value;
408                                         PRINTD("Firing %d\n", ao_lco_firing);
409                                         ao_wakeup(&ao_lco_armed);
410                                 }
411                                 break;
412                         case AO_BUTTON_DRAG_SELECT:
413                                 if (event.value && ao_lco_drag_race) {
414                                         if (ao_lco_pad != 0) {
415                                                 ao_lco_selected[ao_lco_box] ^= (1 << (ao_lco_pad - 1));
416                                                 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
417                                                        ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
418                                                 ao_lco_drag_beep(ao_lco_pad);
419                                         }
420                                 }
421                                 break;
422                         case AO_BUTTON_DRAG_MODE:
423                                 if (event.value)
424                                         ao_lco_drag_enable();
425                                 else
426                                         ao_lco_drag_disable();
427                                 break;
428                         case AO_BUTTON_ENCODER_SELECT:
429                                 if (event.value) {
430                                         if (!ao_lco_armed) {
431                                                 ao_lco_select_mode = 1 - ao_lco_select_mode;
432                                                 ao_lco_set_select();
433                                         }
434                                 }
435                                 break;
436                         }
437                         break;
438                 }
439         }
440 }
441
442 static AO_LED_TYPE      continuity_led[AO_LED_CONTINUITY_NUM] = {
443 #ifdef AO_LED_CONTINUITY_0
444         AO_LED_CONTINUITY_0,
445 #endif
446 #ifdef AO_LED_CONTINUITY_1
447         AO_LED_CONTINUITY_1,
448 #endif
449 #ifdef AO_LED_CONTINUITY_2
450         AO_LED_CONTINUITY_2,
451 #endif
452 #ifdef AO_LED_CONTINUITY_3
453         AO_LED_CONTINUITY_3,
454 #endif
455 #ifdef AO_LED_CONTINUITY_4
456         AO_LED_CONTINUITY_4,
457 #endif
458 #ifdef AO_LED_CONTINUITY_5
459         AO_LED_CONTINUITY_5,
460 #endif
461 #ifdef AO_LED_CONTINUITY_6
462         AO_LED_CONTINUITY_6,
463 #endif
464 #ifdef AO_LED_CONTINUITY_7
465         AO_LED_CONTINUITY_7,
466 #endif
467 };
468
469 static uint8_t
470 ao_lco_get_channels(uint8_t box, struct ao_pad_query *query)
471 {
472         int8_t                  r;
473
474         r = ao_lco_query(box, query, &ao_lco_tick_offset[box]);
475         if (r == AO_RADIO_CMAC_OK) {
476                 ao_lco_channels[box] = query->channels;
477                 ao_lco_valid[box] = AO_LCO_VALID_LAST | AO_LCO_VALID_EVER;
478         } else
479                 ao_lco_valid[box] &= ~AO_LCO_VALID_LAST;
480         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]);
481         ao_wakeup(&ao_pad_query);
482         return ao_lco_valid[box];
483 }
484
485 static void
486 ao_lco_update(void)
487 {
488         uint8_t previous_valid = ao_lco_valid[ao_lco_box];
489
490         if (ao_lco_get_channels(ao_lco_box, &ao_pad_query) & AO_LCO_VALID_LAST) {
491                 if (!(previous_valid & AO_LCO_VALID_EVER)) {
492                         if (ao_lco_pad != 0)
493                                 ao_lco_pad = ao_lco_pad_first(ao_lco_box);
494                         ao_lco_set_display();
495                 }
496                 if (ao_lco_pad == 0)
497                         ao_lco_set_display();
498         }
499 }
500
501 static void
502 ao_lco_box_reset_present(void)
503 {
504         ao_lco_min_box = 0xff;
505         ao_lco_max_box = 0x00;
506         memset(ao_lco_box_mask, 0, sizeof (ao_lco_box_mask));
507 }
508
509 static void
510 ao_lco_box_set_present(uint8_t box)
511 {
512         if (box < ao_lco_min_box)
513                 ao_lco_min_box = box;
514         if (box > ao_lco_max_box)
515                 ao_lco_max_box = box;
516         if (box >= AO_PAD_MAX_BOXES)
517                 return;
518         ao_lco_box_mask[MASK_ID(box)] |= 1 << MASK_SHIFT(box);
519 }
520
521 static void
522 ao_lco_search(void)
523 {
524         int8_t          r;
525         int8_t          try;
526         uint8_t         box;
527         uint8_t         boxes = 0;
528
529         ao_lco_box_reset_present();
530         ao_lco_set_pad(0);
531         for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
532                 if ((box % 10) == 0)
533                         ao_lco_set_box(box);
534                 for (try = 0; try < 3; try++) {
535                         ao_lco_tick_offset[box] = 0;
536                         r = ao_lco_query(box, &ao_pad_query, &ao_lco_tick_offset[box]);
537                         PRINTD("box %d result %d offset %d\n", box, r, ao_lco_tick_offset[box]);
538                         if (r == AO_RADIO_CMAC_OK) {
539                                 ++boxes;
540                                 ao_lco_box_set_present(box);
541                                 ao_lco_set_pad(boxes % 10);
542                                 ao_delay(AO_MS_TO_TICKS(30));
543                                 break;
544                         }
545                 }
546         }
547         if (ao_lco_min_box <= ao_lco_max_box)
548                 ao_lco_box = ao_lco_min_box;
549         else
550                 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
551         memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
552         memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
553         ao_lco_pad = 1;
554         ao_lco_set_display();
555 }
556
557 static void
558 ao_lco_igniter_status(void)
559 {
560         uint8_t         c;
561         uint8_t         t = 0;
562
563         for (;;) {
564                 ao_sleep(&ao_pad_query);
565                 PRINTD("RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_valid[ao_lco_box]);
566                 if (!(ao_lco_valid[ao_lco_box] & AO_LCO_VALID_LAST)) {
567                         ao_led_on(AO_LED_RED);
568                         ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
569                         continue;
570                 }
571                 if (ao_radio_cmac_rssi < -90) {
572                         ao_led_on(AO_LED_AMBER);
573                         ao_led_off(AO_LED_RED|AO_LED_GREEN);
574                 } else {
575                         ao_led_on(AO_LED_GREEN);
576                         ao_led_off(AO_LED_RED|AO_LED_AMBER);
577                 }
578                 if (ao_pad_query.arm_status)
579                         ao_led_on(AO_LED_REMOTE_ARM);
580                 else
581                         ao_led_off(AO_LED_REMOTE_ARM);
582
583                 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
584                         uint8_t status;
585
586                         if (ao_pad_query.channels & (1 << c))
587                                 status = ao_pad_query.igniter_status[c];
588                         else
589                                 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
590
591                         if (ao_lco_drag_race && (ao_lco_selected[ao_lco_box] & (1 << c))) {
592                                 uint8_t on = 0;
593                                 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN) {
594                                         if (t)
595                                                 on = 1;
596                                 } else {
597                                         if (t == 1)
598                                                 on = 1;
599                                 }
600                                 if (on)
601                                         ao_led_on(continuity_led[c]);
602                                 else
603                                         ao_led_off(continuity_led[c]);
604                         } else {
605                                 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
606                                         ao_led_on(continuity_led[c]);
607                                 else
608                                         ao_led_off(continuity_led[c]);
609                         }
610                 }
611                 t = (t + 1) & 3;
612         }
613 }
614
615 static void
616 ao_lco_arm_warn(void)
617 {
618         for (;;) {
619                 while (!ao_lco_armed) {
620                         ao_led_off(AO_LED_FIRE);
621                         ao_sleep(&ao_lco_armed);
622                 }
623                 ao_led_on(AO_LED_FIRE);
624                 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
625                 ao_delay(AO_MS_TO_TICKS(200));
626         }
627 }
628
629 static void
630 ao_lco_batt_voltage(void)
631 {
632         struct ao_adc   packet;
633         int16_t         decivolt;
634
635         ao_adc_single_get(&packet);
636         decivolt = ao_battery_decivolt(packet.v_batt);
637         ao_lco_set_voltage(decivolt);
638         ao_delay(AO_MS_TO_TICKS(1000));
639 }
640
641 static struct ao_task ao_lco_input_task;
642 static struct ao_task ao_lco_monitor_task;
643 static struct ao_task ao_lco_arm_warn_task;
644 static struct ao_task ao_lco_igniter_status_task;
645
646 static void
647 ao_lco_monitor(void)
648 {
649         uint16_t                delay;
650         uint8_t                 box;
651
652         ao_lco_batt_voltage();
653         ao_lco_search();
654         ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
655         ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
656         ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
657         ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
658         for (;;) {
659                 PRINTD("monitor armed %d firing %d\n",
660                        ao_lco_armed, ao_lco_firing);
661
662                 if (ao_lco_armed && ao_lco_firing) {
663                         ao_lco_ignite(AO_PAD_FIRE);
664                 } else {
665                         ao_lco_update();
666                         if (ao_lco_armed) {
667                                 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
668                                         if (ao_lco_selected[box]) {
669                                                 PRINTD("Arming box %d pads %x\n",
670                                                        box, ao_lco_selected[box]);
671                                                 if (ao_lco_valid[box] & AO_LCO_VALID_EVER) {
672                                                         ao_lco_arm(box, ao_lco_selected[box], ao_lco_tick_offset[box]);
673                                                         ao_delay(AO_MS_TO_TICKS(10));
674                                                 }
675                                         }
676                                 }
677                         }
678                 }
679                 if (ao_lco_armed && ao_lco_firing)
680                         delay = AO_MS_TO_TICKS(100);
681                 else
682                         delay = AO_SEC_TO_TICKS(1);
683                 ao_sleep_for(&ao_lco_armed, delay);
684         }
685 }
686
687 #if DEBUG
688 void
689 ao_lco_set_debug(void)
690 {
691         ao_cmd_decimal();
692         if (ao_cmd_status == ao_cmd_success)
693                 ao_lco_debug = ao_cmd_lex_i != 0;
694 }
695
696 __code struct ao_cmds ao_lco_cmds[] = {
697         { ao_lco_set_debug,     "D <0 off, 1 on>\0Debug" },
698         { ao_lco_search,        "s\0Search for pad boxes" },
699         { 0, NULL }
700 };
701 #endif
702
703 void
704 ao_lco_init(void)
705 {
706         ao_add_task(&ao_lco_monitor_task, ao_lco_monitor, "lco monitor");
707 #if DEBUG
708         ao_cmd_register(&ao_lco_cmds[0]);
709 #endif
710 }