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 uint8_t ao_lco_min_box, ao_lco_max_box;
30 uint8_t ao_lco_drag_race;
33 struct ao_pad_query ao_pad_query; /* latest query response */
35 static uint8_t ao_lco_channels[AO_PAD_MAX_BOXES]; /* pad channels available on each box */
36 static uint16_t ao_lco_tick_offset[AO_PAD_MAX_BOXES]; /* offset from local to remote tick count */
37 static uint8_t ao_lco_selected[AO_PAD_MAX_BOXES]; /* pads selected to fire */
39 #define AO_LCO_VALID_LAST 1
40 #define AO_LCO_VALID_EVER 2
42 static uint8_t ao_lco_valid[AO_PAD_MAX_BOXES]; /* AO_LCO_VALID bits per box */
44 static const AO_LED_TYPE continuity_led[AO_LED_CONTINUITY_NUM] = {
45 #ifdef AO_LED_CONTINUITY_0
48 #ifdef AO_LED_CONTINUITY_1
51 #ifdef AO_LED_CONTINUITY_2
54 #ifdef AO_LED_CONTINUITY_3
57 #ifdef AO_LED_CONTINUITY_4
60 #ifdef AO_LED_CONTINUITY_5
63 #ifdef AO_LED_CONTINUITY_6
66 #ifdef AO_LED_CONTINUITY_7
71 /* Set LEDs to match remote box status */
73 ao_lco_igniter_status(void)
81 ao_sleep_for(&ao_pad_query, AO_MS_TO_TICKS(50));
84 ao_sleep(&ao_pad_query);
85 PRINTD("RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_valid[ao_lco_box]);
86 if (!(ao_lco_valid[ao_lco_box] & AO_LCO_VALID_LAST)) {
87 ao_led_on(AO_LED_RED);
88 ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
91 if (ao_radio_cmac_rssi < -90) {
92 ao_led_on(AO_LED_AMBER);
93 ao_led_off(AO_LED_RED|AO_LED_GREEN);
95 ao_led_on(AO_LED_GREEN);
96 ao_led_off(AO_LED_RED|AO_LED_AMBER);
98 if (ao_pad_query.arm_status)
99 ao_led_on(AO_LED_REMOTE_ARM);
101 ao_led_off(AO_LED_REMOTE_ARM);
103 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
106 if (ao_pad_query.channels & (1 << c))
107 status = ao_pad_query.igniter_status[c];
109 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
112 if (ao_lco_drag_race && (ao_lco_selected[ao_lco_box] & (1 << c))) {
114 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN) {
122 ao_led_on(continuity_led[c]);
124 ao_led_off(continuity_led[c]);
128 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
129 ao_led_on(continuity_led[c]);
131 ao_led_off(continuity_led[c]);
139 ao_lco_pad_present(uint8_t box, uint8_t pad)
141 /* voltage measurement is always valid */
142 if (pad == AO_LCO_PAD_VOLTAGE)
144 if (!ao_lco_channels[box])
146 if (pad > AO_PAD_MAX_CHANNELS)
148 return (ao_lco_channels[box] >> (pad - 1)) & 1;
152 ao_lco_pad_first(uint8_t box)
156 for (pad = 1; pad <= AO_PAD_MAX_CHANNELS; pad++)
157 if (ao_lco_pad_present(box, pad))
163 ao_lco_get_channels(uint8_t box, struct ao_pad_query *query)
167 r = ao_lco_query(box, query, &ao_lco_tick_offset[box]);
168 if (r == AO_RADIO_CMAC_OK) {
169 ao_lco_channels[box] = query->channels;
170 ao_lco_valid[box] = AO_LCO_VALID_LAST | AO_LCO_VALID_EVER;
172 ao_lco_valid[box] &= ~AO_LCO_VALID_LAST;
173 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]);
174 ao_wakeup(&ao_pad_query);
175 return ao_lco_valid[box];
181 uint8_t previous_valid = ao_lco_valid[ao_lco_box];
183 if (ao_lco_get_channels(ao_lco_box, &ao_pad_query) & AO_LCO_VALID_LAST) {
184 if (!(previous_valid & AO_LCO_VALID_EVER)) {
185 if (ao_lco_pad != AO_LCO_PAD_VOLTAGE)
186 ao_lco_set_pad(ao_lco_pad_first(ao_lco_box));
188 if (ao_lco_pad == AO_LCO_PAD_VOLTAGE)
193 uint8_t ao_lco_box_mask[AO_LCO_MASK_SIZE(AO_PAD_MAX_BOXES)];
196 ao_lco_box_reset_present(void)
198 ao_lco_min_box = 0xff;
199 ao_lco_max_box = 0x00;
200 memset(ao_lco_box_mask, 0, sizeof (ao_lco_box_mask));
204 ao_lco_box_set_present(uint8_t box)
206 if (box < ao_lco_min_box)
207 ao_lco_min_box = box;
208 if (box > ao_lco_max_box)
209 ao_lco_max_box = box;
210 if (box >= AO_PAD_MAX_BOXES)
212 ao_lco_box_mask[AO_LCO_MASK_ID(box)] |= 1 << AO_LCO_MASK_SHIFT(box);
216 ao_lco_set_pad(uint8_t new_pad)
218 ao_lco_pad = new_pad;
223 ao_lco_set_box(uint16_t new_box)
225 ao_lco_box = new_box;
226 if (ao_lco_box < AO_PAD_MAX_BOXES)
227 ao_lco_channels[ao_lco_box] = 0;
233 ao_lco_step_pad(int8_t dir)
237 new_pad = ao_lco_pad;
240 if (new_pad > AO_PAD_MAX_CHANNELS)
241 new_pad = AO_LCO_PAD_VOLTAGE;
243 new_pad = AO_PAD_MAX_CHANNELS;
244 if (new_pad == ao_lco_pad)
246 } while (!ao_lco_pad_present(ao_lco_box, new_pad));
247 ao_lco_set_pad(new_pad);
251 ao_lco_set_armed(uint8_t armed)
253 ao_lco_armed = armed;
254 PRINTD("Armed %d\n", ao_lco_armed);
257 if (ao_lco_drag_race) {
260 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++)
261 if (ao_lco_selected[box])
263 if (box > ao_lco_max_box)
268 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
270 ao_lco_selected[ao_lco_box] = (1 << (ao_lco_pad - 1));
275 ao_wakeup(&ao_lco_armed);
279 ao_lco_set_firing(uint8_t firing)
281 ao_lco_firing = firing;
282 PRINTD("Firing %d\n", ao_lco_firing);
283 ao_wakeup(&ao_lco_armed);
294 ao_lco_box_reset_present();
297 for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
299 ao_lco_show_box(box);
300 for (try = 0; try < 3; try++) {
301 ao_lco_tick_offset[box] = 0;
302 r = ao_lco_query(box, &ao_pad_query, &ao_lco_tick_offset[box]);
303 PRINTD("box %d result %d offset %d\n", box, r, ao_lco_tick_offset[box]);
304 if (r == AO_RADIO_CMAC_OK) {
306 ao_lco_box_set_present(box);
307 ao_lco_show_pad(boxes % 10);
308 ao_delay(AO_MS_TO_TICKS(30));
313 if (ao_lco_min_box <= ao_lco_max_box)
314 ao_lco_box = ao_lco_min_box;
316 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
317 memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
318 memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
319 ao_lco_set_box(ao_lco_min_box);
329 PRINTD("monitor armed %d firing %d\n",
330 ao_lco_armed, ao_lco_firing);
332 if (ao_lco_armed && ao_lco_firing) {
333 ao_lco_ignite(AO_PAD_FIRE);
337 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
338 if (ao_lco_selected[box]) {
339 PRINTD("Arming box %d pads %x\n",
340 box, ao_lco_selected[box]);
341 if (ao_lco_valid[box] & AO_LCO_VALID_EVER) {
342 ao_lco_arm(box, ao_lco_selected[box], ao_lco_tick_offset[box]);
343 ao_delay(AO_MS_TO_TICKS(10));
349 if (ao_lco_armed && ao_lco_firing)
350 delay = AO_MS_TO_TICKS(100);
352 delay = AO_SEC_TO_TICKS(1);
353 ao_sleep_for(&ao_lco_armed, delay);
359 uint8_t ao_lco_drag_beep_count;
360 static uint8_t ao_lco_drag_beep_on;
361 static uint16_t ao_lco_drag_beep_time;
362 static uint16_t ao_lco_drag_warn_time;
364 #define AO_LCO_DRAG_BEEP_TIME AO_MS_TO_TICKS(50)
365 #define AO_LCO_DRAG_WARN_TIME AO_SEC_TO_TICKS(5)
367 /* Request 'beeps' additional drag race beeps */
369 ao_lco_drag_add_beeps(uint8_t beeps)
371 PRINTD("beep %d\n", beeps);
372 if (ao_lco_drag_beep_count == 0)
373 ao_lco_drag_beep_time = ao_time();
374 ao_lco_drag_beep_count += beeps;
375 ao_wakeup(&ao_lco_drag_beep_count);
378 /* Toggle current pad in drag set */
380 ao_lco_toggle_drag(void)
382 if (ao_lco_drag_race && ao_lco_pad != AO_LCO_PAD_VOLTAGE) {
383 ao_lco_selected[ao_lco_box] ^= (1 << (ao_lco_pad - 1));
384 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
385 ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
386 ao_lco_drag_add_beeps(ao_lco_pad);
390 /* Check whether it's time to change the beeper status, then either
391 * turn it on or off as necessary and bump the remaining beep counts
395 ao_lco_drag_beep_check(uint16_t now, uint16_t delay)
397 PRINTD("beep check count %d delta %d\n",
398 ao_lco_drag_beep_count,
399 (int16_t) (now - ao_lco_drag_beep_time));
400 if (ao_lco_drag_beep_count) {
401 if ((int16_t) (now - ao_lco_drag_beep_time) >= 0) {
402 if (ao_lco_drag_beep_on) {
404 PRINTD("beep stop\n");
405 ao_lco_drag_beep_on = 0;
406 if (ao_lco_drag_beep_count) {
407 --ao_lco_drag_beep_count;
408 if (ao_lco_drag_beep_count)
409 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
412 ao_beep(AO_BEEP_HIGH);
413 PRINTD("beep start\n");
414 ao_lco_drag_beep_on = 1;
415 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
420 if (ao_lco_drag_beep_count) {
421 uint16_t beep_delay = 0;
423 if (ao_lco_drag_beep_time > now)
424 beep_delay = ao_lco_drag_beep_time - now;
426 if (delay > beep_delay)
433 ao_lco_drag_enable(void)
435 if (!ao_lco_drag_race) {
436 PRINTD("Drag enable\n");
437 ao_lco_drag_race = 1;
438 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
440 ao_led_on(AO_LED_DRAG);
442 ao_lco_drag_add_beeps(5);
448 ao_lco_drag_disable(void)
450 if (ao_lco_drag_race) {
451 PRINTD("Drag disable\n");
452 ao_lco_drag_race = 0;
454 ao_led_off(AO_LED_DRAG);
456 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
457 ao_lco_drag_add_beeps(2);
462 /* add a beep if it's time to warn the user that drag race mode is
467 ao_lco_drag_warn_check(uint16_t now, uint16_t delay)
469 if (ao_lco_drag_race) {
472 if ((int16_t) (now - ao_lco_drag_warn_time) >= 0) {
473 ao_lco_drag_add_beeps(1);
474 ao_lco_drag_warn_time = now + AO_LCO_DRAG_WARN_TIME;
476 warn_delay = ao_lco_drag_warn_time - now;
477 if (delay > warn_delay)
482 #endif /* AO_LCO_DRAG */
484 /* task function for beeping while arm is active */
486 ao_lco_arm_warn(void)
489 while (!ao_lco_armed) {
491 ao_led_off(AO_LED_FIRE);
493 ao_sleep(&ao_lco_armed);
496 ao_led_on(AO_LED_FIRE);
498 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
499 ao_delay(AO_MS_TO_TICKS(200));