2 * Copyright © 2018 Keith Packard <keithp@keithp.com>
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.
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.
17 #include <ao_radio_cmac.h>
24 uint8_t ao_lco_armed; /* arm active */
25 uint8_t ao_lco_firing; /* fire active */
27 uint16_t ao_lco_min_box, ao_lco_max_box;
29 uint8_t ao_lco_pretending;
32 uint8_t ao_lco_drag_race;
35 struct ao_pad_query ao_pad_query; /* latest query response */
37 static uint8_t ao_lco_channels[AO_PAD_MAX_BOXES]; /* pad channels available on each box */
38 static uint16_t ao_lco_tick_offset[AO_PAD_MAX_BOXES]; /* 16 bit offset from local to remote tick count */
39 static uint8_t ao_lco_selected[AO_PAD_MAX_BOXES]; /* pads selected to fire */
41 #define AO_LCO_VALID_LAST 1
42 #define AO_LCO_VALID_EVER 2
44 static uint8_t ao_lco_valid[AO_PAD_MAX_BOXES]; /* AO_LCO_VALID bits per box */
46 static const AO_LED_TYPE continuity_led[AO_LED_CONTINUITY_NUM] = {
47 #ifdef AO_LED_CONTINUITY_0
50 #ifdef AO_LED_CONTINUITY_1
53 #ifdef AO_LED_CONTINUITY_2
56 #ifdef AO_LED_CONTINUITY_3
59 #ifdef AO_LED_CONTINUITY_4
62 #ifdef AO_LED_CONTINUITY_5
65 #ifdef AO_LED_CONTINUITY_6
68 #ifdef AO_LED_CONTINUITY_7
73 /* Set LEDs to match remote box status */
75 ao_lco_igniter_status(void)
83 ao_sleep_for(&ao_pad_query, AO_MS_TO_TICKS(50));
86 ao_sleep(&ao_pad_query);
87 if (ao_lco_box_pseudo(ao_lco_box)) {
88 ao_led_off(AO_LED_GREEN|AO_LED_AMBER|AO_LED_RED);
91 PRINTD("RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_valid[ao_lco_box]);
92 if (!(ao_lco_valid[ao_lco_box] & AO_LCO_VALID_LAST)) {
93 ao_led_on(AO_LED_RED);
94 ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
97 if (ao_radio_cmac_rssi < -90) {
98 ao_led_on(AO_LED_AMBER);
99 ao_led_off(AO_LED_RED|AO_LED_GREEN);
101 ao_led_on(AO_LED_GREEN);
102 ao_led_off(AO_LED_RED|AO_LED_AMBER);
104 if (ao_pad_query.arm_status)
105 ao_led_on(AO_LED_REMOTE_ARM);
107 ao_led_off(AO_LED_REMOTE_ARM);
109 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
112 if (ao_pad_query.channels & (1 << c))
113 status = ao_pad_query.igniter_status[c];
115 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
118 if (ao_lco_drag_race && (ao_lco_selected[ao_lco_box] & (1 << c))) {
120 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN) {
128 ao_led_on(continuity_led[c]);
130 ao_led_off(continuity_led[c]);
134 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
135 ao_led_on(continuity_led[c]);
137 ao_led_off(continuity_led[c]);
145 ao_lco_pad_present(uint16_t box, uint8_t pad)
147 /* voltage measurement is always valid */
148 if (pad == AO_LCO_PAD_VOLTAGE)
150 if (!ao_lco_channels[box])
152 if (pad > AO_PAD_MAX_CHANNELS)
154 return (ao_lco_channels[box] >> (pad - 1)) & 1;
158 ao_lco_pad_first(uint16_t box)
162 for (pad = 1; pad <= AO_PAD_MAX_CHANNELS; pad++)
163 if (ao_lco_pad_present(box, pad))
169 ao_lco_get_channels(uint16_t box, struct ao_pad_query *query)
173 r = ao_lco_query(box, query, &ao_lco_tick_offset[box]);
174 if (r == AO_RADIO_CMAC_OK) {
175 ao_lco_channels[box] = query->channels;
176 ao_lco_valid[box] = AO_LCO_VALID_LAST | AO_LCO_VALID_EVER;
178 ao_lco_valid[box] &= (uint8_t) ~AO_LCO_VALID_LAST;
179 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]);
180 ao_wakeup(&ao_pad_query);
181 return ao_lco_valid[box];
187 if (ao_lco_box_pseudo(ao_lco_box)) {
192 uint8_t previous_valid = ao_lco_valid[ao_lco_box];
194 if (ao_lco_get_channels(ao_lco_box, &ao_pad_query) & AO_LCO_VALID_LAST) {
195 if (!(previous_valid & AO_LCO_VALID_EVER)) {
196 if (ao_lco_pad != AO_LCO_PAD_VOLTAGE)
197 ao_lco_set_pad(ao_lco_pad_first(ao_lco_box));
199 if (ao_lco_pad == AO_LCO_PAD_VOLTAGE)
204 uint8_t ao_lco_box_mask[AO_LCO_MASK_SIZE(AO_PAD_MAX_BOXES)];
207 ao_lco_box_reset_present(void)
209 ao_lco_min_box = 0xff;
210 ao_lco_max_box = 0x00;
211 ao_lco_pretending = 0;
212 memset(ao_lco_box_mask, 0, sizeof (ao_lco_box_mask));
216 ao_lco_box_set_present(uint16_t box)
218 if (box < ao_lco_min_box)
219 ao_lco_min_box = box;
220 if (box > ao_lco_max_box)
221 ao_lco_max_box = box;
222 if (box >= AO_PAD_MAX_BOXES)
224 ao_lco_box_mask[AO_LCO_MASK_ID(box)] |= (uint8_t) (1 << AO_LCO_MASK_SHIFT(box));
228 ao_lco_set_pad(uint8_t new_pad)
230 ao_lco_pad = new_pad;
235 ao_lco_set_box(uint16_t new_box)
237 ao_lco_box = new_box;
238 if (!ao_lco_box_pseudo(ao_lco_box)) {
239 if (ao_lco_box < AO_PAD_MAX_BOXES) {
240 if (ao_lco_pretending)
241 ao_lco_channels[ao_lco_box] = 0xff;
243 ao_lco_channels[ao_lco_box] = 0;
251 ao_lco_step_pad(int8_t dir)
255 new_pad = (int16_t) ao_lco_pad;
258 if (new_pad > AO_PAD_MAX_CHANNELS)
259 new_pad = AO_LCO_PAD_VOLTAGE;
261 new_pad = AO_PAD_MAX_CHANNELS;
262 if (new_pad == ao_lco_pad)
264 } while (!ao_lco_pad_present(ao_lco_box, (uint8_t) new_pad));
265 PRINTD("New pad %d\n", new_pad);
266 ao_lco_set_pad((uint8_t) new_pad);
270 ao_lco_box_present(uint16_t box)
272 if (ao_lco_box_pseudo(box))
274 if (box >= AO_PAD_MAX_BOXES)
276 return (ao_lco_box_mask[AO_LCO_MASK_ID(box)] >> AO_LCO_MASK_SHIFT(box)) & 1;
280 ao_lco_step_box(int8_t dir)
282 int32_t new_box = (int32_t) ao_lco_box;
286 if (new_box > ao_lco_max_box)
287 new_box = AO_LCO_BOX_FIRST;
288 else if (new_box < 0)
289 new_box = ao_lco_max_box;
290 if (new_box == ao_lco_box)
292 } while (!ao_lco_box_present((uint16_t) new_box));
293 PRINTD("New box %ld\n", new_box);
294 ao_lco_set_box((uint16_t) new_box);
298 ao_lco_set_armed(uint8_t armed)
300 if (ao_lco_box_pseudo(ao_lco_box))
303 ao_lco_armed = armed;
304 PRINTD("Armed %d\n", ao_lco_armed);
307 if (ao_lco_drag_race) {
310 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++)
311 if (ao_lco_selected[box])
313 if (box > ao_lco_max_box)
318 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
320 ao_lco_selected[ao_lco_box] = (1 << (ao_lco_pad - 1));
325 ao_wakeup(&ao_lco_armed);
329 ao_lco_set_firing(uint8_t firing)
331 ao_lco_firing = firing;
332 PRINTD("Firing %d\n", ao_lco_firing);
333 ao_wakeup(&ao_lco_armed);
344 ao_lco_box_reset_present();
345 #ifdef AO_LCO_SEARCH_API
346 ao_lco_search_start();
351 for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
352 #ifdef AO_LCO_SEARCH_API
353 ao_lco_search_box_check(box);
356 ao_lco_show_box(box);
358 for (try = 0; try < 3; try++) {
359 ao_lco_tick_offset[box] = 0;
360 r = ao_lco_query(box, &ao_pad_query, &ao_lco_tick_offset[box]);
361 PRINTD("box %d result %d offset %d\n", box, r, ao_lco_tick_offset[box]);
362 if (r == AO_RADIO_CMAC_OK) {
364 ao_lco_box_set_present(box);
365 #ifdef AO_LCO_SEARCH_API
366 ao_lco_search_box_present(box);
368 ao_lco_show_pad((uint8_t) (boxes % 10));
370 ao_delay(AO_MS_TO_TICKS(30));
375 if (ao_lco_min_box <= ao_lco_max_box)
376 ao_lco_box = ao_lco_min_box;
378 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
379 memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
380 memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
381 #ifdef AO_LCO_SEARCH_API
382 ao_lco_search_done();
384 ao_lco_set_box(ao_lco_min_box);
392 ao_lco_pretending = 1;
394 ao_lco_max_box = AO_PAD_MAX_BOXES - 1;
395 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++)
396 ao_lco_box_set_present(box);
397 ao_lco_box = ao_lco_min_box;
398 memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
399 memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
400 ao_lco_set_box(ao_lco_min_box);
410 PRINTD("monitor armed %d firing %d\n",
411 ao_lco_armed, ao_lco_firing);
413 if (ao_lco_armed && ao_lco_firing) {
414 ao_lco_ignite(AO_PAD_FIRE);
418 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
419 if (ao_lco_selected[box]) {
420 PRINTD("Arming box %d pads %x\n",
421 box, ao_lco_selected[box]);
422 if (ao_lco_valid[box] & AO_LCO_VALID_EVER) {
423 ao_lco_arm(box, ao_lco_selected[box], ao_lco_tick_offset[box]);
424 ao_delay(AO_MS_TO_TICKS(10));
430 if (ao_lco_armed && ao_lco_firing)
431 delay = AO_MS_TO_TICKS(100);
433 delay = AO_SEC_TO_TICKS(1);
434 ao_sleep_for(&ao_lco_armed, delay);
440 uint8_t ao_lco_drag_beep_count;
441 static uint8_t ao_lco_drag_beep_on;
442 static AO_TICK_TYPE ao_lco_drag_beep_time;
443 static AO_TICK_TYPE ao_lco_drag_warn_time;
445 #define AO_LCO_DRAG_BEEP_TIME AO_MS_TO_TICKS(50)
446 #define AO_LCO_DRAG_WARN_TIME AO_SEC_TO_TICKS(5)
448 /* Request 'beeps' additional drag race beeps */
450 ao_lco_drag_add_beeps(uint8_t beeps)
452 PRINTD("beep %d\n", beeps);
453 if (ao_lco_drag_beep_count == 0)
454 ao_lco_drag_beep_time = ao_time();
455 ao_lco_drag_beep_count += beeps;
456 ao_wakeup(&ao_lco_drag_beep_count);
459 /* Toggle current pad in drag set */
461 ao_lco_toggle_drag(void)
463 if (ao_lco_drag_race && ao_lco_pad != AO_LCO_PAD_VOLTAGE && !ao_lco_box_pseudo(ao_lco_box)) {
464 ao_lco_selected[ao_lco_box] ^= (uint8_t) (1 << (ao_lco_pad - 1));
465 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
466 ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
467 ao_lco_drag_add_beeps(ao_lco_pad);
471 /* Check whether it's time to change the beeper status, then either
472 * turn it on or off as necessary and bump the remaining beep counts
476 ao_lco_drag_beep_check(AO_TICK_TYPE now, AO_TICK_TYPE delay)
478 PRINTD("beep check count %d delta %ld\n",
479 ao_lco_drag_beep_count,
480 (long) (AO_TICK_SIGNED) (now - ao_lco_drag_beep_time));
481 if (ao_lco_drag_beep_count) {
482 if ((AO_TICK_SIGNED) (now - ao_lco_drag_beep_time) >= 0) {
483 if (ao_lco_drag_beep_on) {
485 PRINTD("beep stop\n");
486 ao_lco_drag_beep_on = 0;
487 if (ao_lco_drag_beep_count) {
488 --ao_lco_drag_beep_count;
489 if (ao_lco_drag_beep_count)
490 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
493 ao_beep(AO_BEEP_HIGH);
494 PRINTD("beep start\n");
495 ao_lco_drag_beep_on = 1;
496 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
501 if (ao_lco_drag_beep_count) {
502 AO_TICK_TYPE beep_delay = 0;
504 if (ao_lco_drag_beep_time > now)
505 beep_delay = ao_lco_drag_beep_time - now;
507 if (delay > beep_delay)
514 ao_lco_drag_enable(void)
516 if (!ao_lco_drag_race) {
517 PRINTD("Drag enable\n");
518 ao_lco_drag_race = 1;
519 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
521 ao_led_on(AO_LED_DRAG);
523 ao_lco_drag_add_beeps(5);
529 ao_lco_drag_disable(void)
531 if (ao_lco_drag_race) {
532 PRINTD("Drag disable\n");
533 ao_lco_drag_race = 0;
535 ao_led_off(AO_LED_DRAG);
537 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
538 ao_lco_drag_add_beeps(2);
543 /* add a beep if it's time to warn the user that drag race mode is
548 ao_lco_drag_warn_check(AO_TICK_TYPE now, AO_TICK_TYPE delay)
550 if (ao_lco_drag_race) {
551 AO_TICK_TYPE warn_delay;
553 if ((AO_TICK_SIGNED) (now - ao_lco_drag_warn_time) >= 0) {
554 ao_lco_drag_add_beeps(1);
555 ao_lco_drag_warn_time = now + AO_LCO_DRAG_WARN_TIME;
557 warn_delay = ao_lco_drag_warn_time - now;
558 if (delay > warn_delay)
563 #endif /* AO_LCO_DRAG */
565 /* task function for beeping while arm is active */
567 ao_lco_arm_warn(void)
570 while (!ao_lco_armed) {
572 ao_led_off(AO_LED_FIRE);
574 ao_sleep(&ao_lco_armed);
577 ao_led_on(AO_LED_FIRE);
579 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
580 ao_delay(AO_MS_TO_TICKS(200));