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 PRINTD("RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_valid[ao_lco_box]);
88 if (!(ao_lco_valid[ao_lco_box] & AO_LCO_VALID_LAST)) {
89 ao_led_on(AO_LED_RED);
90 ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
93 if (ao_radio_cmac_rssi < -90) {
94 ao_led_on(AO_LED_AMBER);
95 ao_led_off(AO_LED_RED|AO_LED_GREEN);
97 ao_led_on(AO_LED_GREEN);
98 ao_led_off(AO_LED_RED|AO_LED_AMBER);
100 if (ao_pad_query.arm_status)
101 ao_led_on(AO_LED_REMOTE_ARM);
103 ao_led_off(AO_LED_REMOTE_ARM);
105 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
108 if (ao_pad_query.channels & (1 << c))
109 status = ao_pad_query.igniter_status[c];
111 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
114 if (ao_lco_drag_race && (ao_lco_selected[ao_lco_box] & (1 << c))) {
116 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN) {
124 ao_led_on(continuity_led[c]);
126 ao_led_off(continuity_led[c]);
130 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
131 ao_led_on(continuity_led[c]);
133 ao_led_off(continuity_led[c]);
141 ao_lco_pad_present(uint16_t box, uint8_t pad)
143 /* voltage measurement is always valid */
144 if (pad == AO_LCO_PAD_VOLTAGE)
146 if (!ao_lco_channels[box])
148 if (pad > AO_PAD_MAX_CHANNELS)
150 return (ao_lco_channels[box] >> (pad - 1)) & 1;
154 ao_lco_pad_first(uint16_t box)
158 for (pad = 1; pad <= AO_PAD_MAX_CHANNELS; pad++)
159 if (ao_lco_pad_present(box, pad))
165 ao_lco_get_channels(uint16_t box, struct ao_pad_query *query)
169 r = ao_lco_query(box, query, &ao_lco_tick_offset[box]);
170 if (r == AO_RADIO_CMAC_OK) {
171 ao_lco_channels[box] = query->channels;
172 ao_lco_valid[box] = AO_LCO_VALID_LAST | AO_LCO_VALID_EVER;
174 ao_lco_valid[box] &= (uint8_t) ~AO_LCO_VALID_LAST;
175 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]);
176 ao_wakeup(&ao_pad_query);
177 return ao_lco_valid[box];
183 uint8_t previous_valid = ao_lco_valid[ao_lco_box];
185 if (ao_lco_get_channels(ao_lco_box, &ao_pad_query) & AO_LCO_VALID_LAST) {
186 if (!(previous_valid & AO_LCO_VALID_EVER)) {
187 if (ao_lco_pad != AO_LCO_PAD_VOLTAGE)
188 ao_lco_set_pad(ao_lco_pad_first(ao_lco_box));
190 if (ao_lco_pad == AO_LCO_PAD_VOLTAGE)
195 uint8_t ao_lco_box_mask[AO_LCO_MASK_SIZE(AO_PAD_MAX_BOXES)];
198 ao_lco_box_reset_present(void)
200 ao_lco_min_box = 0xff;
201 ao_lco_max_box = 0x00;
202 ao_lco_pretending = 0;
203 memset(ao_lco_box_mask, 0, sizeof (ao_lco_box_mask));
207 ao_lco_box_set_present(uint16_t box)
209 if (box < ao_lco_min_box)
210 ao_lco_min_box = box;
211 if (box > ao_lco_max_box)
212 ao_lco_max_box = box;
213 if (box >= AO_PAD_MAX_BOXES)
215 ao_lco_box_mask[AO_LCO_MASK_ID(box)] |= (uint8_t) (1 << AO_LCO_MASK_SHIFT(box));
219 ao_lco_set_pad(uint8_t new_pad)
221 ao_lco_pad = new_pad;
226 ao_lco_set_box(uint16_t new_box)
228 ao_lco_box = new_box;
229 if (ao_lco_box < AO_PAD_MAX_BOXES) {
230 if (ao_lco_pretending)
231 ao_lco_channels[ao_lco_box] = 0xff;
233 ao_lco_channels[ao_lco_box] = 0;
240 ao_lco_step_pad(int8_t dir)
244 new_pad = (int16_t) ao_lco_pad;
247 if (new_pad > AO_PAD_MAX_CHANNELS)
248 new_pad = AO_LCO_PAD_VOLTAGE;
250 new_pad = AO_PAD_MAX_CHANNELS;
251 if (new_pad == ao_lco_pad)
253 } while (!ao_lco_pad_present(ao_lco_box, (uint8_t) new_pad));
254 ao_lco_set_pad((uint8_t) new_pad);
258 ao_lco_set_armed(uint8_t armed)
260 ao_lco_armed = armed;
261 PRINTD("Armed %d\n", ao_lco_armed);
264 if (ao_lco_drag_race) {
267 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++)
268 if (ao_lco_selected[box])
270 if (box > ao_lco_max_box)
275 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
277 ao_lco_selected[ao_lco_box] = (1 << (ao_lco_pad - 1));
282 ao_wakeup(&ao_lco_armed);
286 ao_lco_set_firing(uint8_t firing)
288 ao_lco_firing = firing;
289 PRINTD("Firing %d\n", ao_lco_firing);
290 ao_wakeup(&ao_lco_armed);
301 ao_lco_box_reset_present();
304 for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
306 ao_lco_show_box(box);
307 for (try = 0; try < 3; try++) {
308 ao_lco_tick_offset[box] = 0;
309 r = ao_lco_query(box, &ao_pad_query, &ao_lco_tick_offset[box]);
310 PRINTD("box %d result %d offset %d\n", box, r, ao_lco_tick_offset[box]);
311 if (r == AO_RADIO_CMAC_OK) {
313 ao_lco_box_set_present(box);
314 ao_lco_show_pad((uint8_t) (boxes % 10));
315 ao_delay(AO_MS_TO_TICKS(30));
320 if (ao_lco_min_box <= ao_lco_max_box)
321 ao_lco_box = ao_lco_min_box;
323 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
324 memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
325 memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
326 ao_lco_set_box(ao_lco_min_box);
334 ao_lco_pretending = 1;
336 ao_lco_max_box = AO_PAD_MAX_BOXES - 1;
337 for (box = ao_lco_min_box; box < ao_lco_max_box; box++)
338 ao_lco_box_set_present(box);
339 ao_lco_box = ao_lco_min_box;
340 memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
341 memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
342 ao_lco_set_box(ao_lco_min_box);
352 PRINTD("monitor armed %d firing %d\n",
353 ao_lco_armed, ao_lco_firing);
355 if (ao_lco_armed && ao_lco_firing) {
356 ao_lco_ignite(AO_PAD_FIRE);
360 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
361 if (ao_lco_selected[box]) {
362 PRINTD("Arming box %d pads %x\n",
363 box, ao_lco_selected[box]);
364 if (ao_lco_valid[box] & AO_LCO_VALID_EVER) {
365 ao_lco_arm(box, ao_lco_selected[box], ao_lco_tick_offset[box]);
366 ao_delay(AO_MS_TO_TICKS(10));
372 if (ao_lco_armed && ao_lco_firing)
373 delay = AO_MS_TO_TICKS(100);
375 delay = AO_SEC_TO_TICKS(1);
376 ao_sleep_for(&ao_lco_armed, delay);
382 uint8_t ao_lco_drag_beep_count;
383 static uint8_t ao_lco_drag_beep_on;
384 static AO_TICK_TYPE ao_lco_drag_beep_time;
385 static AO_TICK_TYPE ao_lco_drag_warn_time;
387 #define AO_LCO_DRAG_BEEP_TIME AO_MS_TO_TICKS(50)
388 #define AO_LCO_DRAG_WARN_TIME AO_SEC_TO_TICKS(5)
390 /* Request 'beeps' additional drag race beeps */
392 ao_lco_drag_add_beeps(uint8_t beeps)
394 PRINTD("beep %d\n", beeps);
395 if (ao_lco_drag_beep_count == 0)
396 ao_lco_drag_beep_time = ao_time();
397 ao_lco_drag_beep_count += beeps;
398 ao_wakeup(&ao_lco_drag_beep_count);
401 /* Toggle current pad in drag set */
403 ao_lco_toggle_drag(void)
405 if (ao_lco_drag_race && ao_lco_pad != AO_LCO_PAD_VOLTAGE) {
406 ao_lco_selected[ao_lco_box] ^= (uint8_t) (1 << (ao_lco_pad - 1));
407 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
408 ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
409 ao_lco_drag_add_beeps(ao_lco_pad);
413 /* Check whether it's time to change the beeper status, then either
414 * turn it on or off as necessary and bump the remaining beep counts
418 ao_lco_drag_beep_check(AO_TICK_TYPE now, AO_TICK_TYPE delay)
420 PRINTD("beep check count %d delta %ld\n",
421 ao_lco_drag_beep_count,
422 (long) (AO_TICK_SIGNED) (now - ao_lco_drag_beep_time));
423 if (ao_lco_drag_beep_count) {
424 if ((AO_TICK_SIGNED) (now - ao_lco_drag_beep_time) >= 0) {
425 if (ao_lco_drag_beep_on) {
427 PRINTD("beep stop\n");
428 ao_lco_drag_beep_on = 0;
429 if (ao_lco_drag_beep_count) {
430 --ao_lco_drag_beep_count;
431 if (ao_lco_drag_beep_count)
432 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
435 ao_beep(AO_BEEP_HIGH);
436 PRINTD("beep start\n");
437 ao_lco_drag_beep_on = 1;
438 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
443 if (ao_lco_drag_beep_count) {
444 AO_TICK_TYPE beep_delay = 0;
446 if (ao_lco_drag_beep_time > now)
447 beep_delay = ao_lco_drag_beep_time - now;
449 if (delay > beep_delay)
456 ao_lco_drag_enable(void)
458 if (!ao_lco_drag_race) {
459 PRINTD("Drag enable\n");
460 ao_lco_drag_race = 1;
461 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
463 ao_led_on(AO_LED_DRAG);
465 ao_lco_drag_add_beeps(5);
471 ao_lco_drag_disable(void)
473 if (ao_lco_drag_race) {
474 PRINTD("Drag disable\n");
475 ao_lco_drag_race = 0;
477 ao_led_off(AO_LED_DRAG);
479 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
480 ao_lco_drag_add_beeps(2);
485 /* add a beep if it's time to warn the user that drag race mode is
490 ao_lco_drag_warn_check(AO_TICK_TYPE now, AO_TICK_TYPE delay)
492 if (ao_lco_drag_race) {
493 AO_TICK_TYPE warn_delay;
495 if ((AO_TICK_SIGNED) (now - ao_lco_drag_warn_time) >= 0) {
496 ao_lco_drag_add_beeps(1);
497 ao_lco_drag_warn_time = now + AO_LCO_DRAG_WARN_TIME;
499 warn_delay = ao_lco_drag_warn_time - now;
500 if (delay > warn_delay)
505 #endif /* AO_LCO_DRAG */
507 /* task function for beeping while arm is active */
509 ao_lco_arm_warn(void)
512 while (!ao_lco_armed) {
514 ao_led_off(AO_LED_FIRE);
516 ao_sleep(&ao_lco_armed);
519 ao_led_on(AO_LED_FIRE);
521 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
522 ao_delay(AO_MS_TO_TICKS(200));