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>
23 uint8_t ao_lco_drag_race;
25 uint8_t ao_lco_armed; /* arm active */
26 uint8_t ao_lco_firing; /* fire active */
28 uint8_t ao_lco_min_box, ao_lco_max_box;
29 uint8_t ao_lco_selected[AO_PAD_MAX_BOXES];
30 uint8_t ao_lco_valid[AO_PAD_MAX_BOXES];
31 uint8_t ao_lco_channels[AO_PAD_MAX_BOXES];
32 uint16_t ao_lco_tick_offset[AO_PAD_MAX_BOXES];
34 struct ao_pad_query ao_pad_query;
36 static AO_LED_TYPE continuity_led[AO_LED_CONTINUITY_NUM] = {
37 #ifdef AO_LED_CONTINUITY_0
40 #ifdef AO_LED_CONTINUITY_1
43 #ifdef AO_LED_CONTINUITY_2
46 #ifdef AO_LED_CONTINUITY_3
49 #ifdef AO_LED_CONTINUITY_4
52 #ifdef AO_LED_CONTINUITY_5
55 #ifdef AO_LED_CONTINUITY_6
58 #ifdef AO_LED_CONTINUITY_7
63 /* Set LEDs to match remote box status */
65 ao_lco_igniter_status(void)
72 ao_sleep_for(&ao_pad_query, AO_MS_TO_TICKS(50));
74 ao_sleep(&ao_pad_query);
75 PRINTD("RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_valid[ao_lco_box]);
76 if (!(ao_lco_valid[ao_lco_box] & AO_LCO_VALID_LAST)) {
77 ao_led_on(AO_LED_RED);
78 ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
81 if (ao_radio_cmac_rssi < -90) {
82 ao_led_on(AO_LED_AMBER);
83 ao_led_off(AO_LED_RED|AO_LED_GREEN);
85 ao_led_on(AO_LED_GREEN);
86 ao_led_off(AO_LED_RED|AO_LED_AMBER);
88 if (ao_pad_query.arm_status)
89 ao_led_on(AO_LED_REMOTE_ARM);
91 ao_led_off(AO_LED_REMOTE_ARM);
93 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
96 if (ao_pad_query.channels & (1 << c))
97 status = ao_pad_query.igniter_status[c];
99 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
101 if (ao_lco_drag_race && (ao_lco_selected[ao_lco_box] & (1 << c))) {
103 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN) {
111 ao_led_on(continuity_led[c]);
113 ao_led_off(continuity_led[c]);
115 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
116 ao_led_on(continuity_led[c]);
118 ao_led_off(continuity_led[c]);
126 ao_lco_pad_present(uint8_t box, uint8_t pad)
128 /* voltage measurement is always valid */
129 if (pad == AO_LCO_PAD_VOLTAGE)
131 if (!ao_lco_channels[box])
133 if (pad > AO_PAD_MAX_CHANNELS)
135 return (ao_lco_channels[box] >> (pad - 1)) & 1;
139 ao_lco_pad_first(uint8_t box)
143 for (pad = 1; pad <= AO_PAD_MAX_CHANNELS; pad++)
144 if (ao_lco_pad_present(box, pad))
150 ao_lco_get_channels(uint8_t box, struct ao_pad_query *query)
154 r = ao_lco_query(box, query, &ao_lco_tick_offset[box]);
155 if (r == AO_RADIO_CMAC_OK) {
156 ao_lco_channels[box] = query->channels;
157 ao_lco_valid[box] = AO_LCO_VALID_LAST | AO_LCO_VALID_EVER;
159 ao_lco_valid[box] &= ~AO_LCO_VALID_LAST;
160 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]);
161 ao_wakeup(&ao_pad_query);
162 return ao_lco_valid[box];
168 uint8_t previous_valid = ao_lco_valid[ao_lco_box];
170 if (ao_lco_get_channels(ao_lco_box, &ao_pad_query) & AO_LCO_VALID_LAST) {
171 if (!(previous_valid & AO_LCO_VALID_EVER)) {
172 if (ao_lco_pad != AO_LCO_PAD_VOLTAGE)
173 ao_lco_set_pad(ao_lco_pad_first(ao_lco_box));
175 if (ao_lco_pad == AO_LCO_PAD_VOLTAGE)
180 uint8_t ao_lco_box_mask[AO_LCO_MASK_SIZE(AO_PAD_MAX_BOXES)];
183 ao_lco_box_reset_present(void)
185 ao_lco_min_box = 0xff;
186 ao_lco_max_box = 0x00;
187 memset(ao_lco_box_mask, 0, sizeof (ao_lco_box_mask));
191 ao_lco_box_set_present(uint8_t box)
193 if (box < ao_lco_min_box)
194 ao_lco_min_box = box;
195 if (box > ao_lco_max_box)
196 ao_lco_max_box = box;
197 if (box >= AO_PAD_MAX_BOXES)
199 ao_lco_box_mask[AO_LCO_MASK_ID(box)] |= 1 << AO_LCO_MASK_SHIFT(box);
203 ao_lco_set_pad(uint8_t new_pad)
205 ao_lco_pad = new_pad;
210 ao_lco_set_box(uint16_t new_box)
212 ao_lco_box = new_box;
214 if (ao_lco_box != AO_LCO_BOX_DRAG)
216 ao_lco_channels[ao_lco_box] = 0;
222 ao_lco_step_pad(int8_t dir)
226 new_pad = ao_lco_pad;
229 if (new_pad > AO_PAD_MAX_CHANNELS)
230 new_pad = AO_LCO_PAD_VOLTAGE;
232 new_pad = AO_PAD_MAX_CHANNELS;
233 if (new_pad == ao_lco_pad)
235 } while (!ao_lco_pad_present(ao_lco_box, new_pad));
236 ao_lco_set_pad(new_pad);
240 ao_lco_set_armed(uint8_t armed)
242 ao_lco_armed = armed;
243 PRINTD("Armed %d\n", ao_lco_armed);
245 if (ao_lco_drag_race) {
248 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++)
249 if (ao_lco_selected[box])
251 if (box > ao_lco_max_box)
254 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
256 ao_lco_selected[ao_lco_box] = (1 << (ao_lco_pad - 1));
261 ao_wakeup(&ao_lco_armed);
265 ao_lco_set_firing(uint8_t firing)
267 ao_lco_firing = firing;
268 PRINTD("Firing %d\n", ao_lco_firing);
269 ao_wakeup(&ao_lco_armed);
280 ao_lco_box_reset_present();
283 for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
285 ao_lco_show_box(box);
286 for (try = 0; try < 3; try++) {
287 ao_lco_tick_offset[box] = 0;
288 r = ao_lco_query(box, &ao_pad_query, &ao_lco_tick_offset[box]);
289 PRINTD("box %d result %d offset %d\n", box, r, ao_lco_tick_offset[box]);
290 if (r == AO_RADIO_CMAC_OK) {
292 ao_lco_box_set_present(box);
293 ao_lco_show_pad(boxes % 10);
294 ao_delay(AO_MS_TO_TICKS(30));
299 if (ao_lco_min_box <= ao_lco_max_box)
300 ao_lco_box = ao_lco_min_box;
302 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
303 memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
304 memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
305 ao_lco_set_box(ao_lco_min_box);
315 PRINTD("monitor armed %d firing %d\n",
316 ao_lco_armed, ao_lco_firing);
318 if (ao_lco_armed && ao_lco_firing) {
319 ao_lco_ignite(AO_PAD_FIRE);
323 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
324 if (ao_lco_selected[box]) {
325 PRINTD("Arming box %d pads %x\n",
326 box, ao_lco_selected[box]);
327 if (ao_lco_valid[box] & AO_LCO_VALID_EVER) {
328 ao_lco_arm(box, ao_lco_selected[box], ao_lco_tick_offset[box]);
329 ao_delay(AO_MS_TO_TICKS(10));
335 if (ao_lco_armed && ao_lco_firing)
336 delay = AO_MS_TO_TICKS(100);
338 delay = AO_SEC_TO_TICKS(1);
339 ao_sleep_for(&ao_lco_armed, delay);
345 uint8_t ao_lco_drag_beep_count;
346 static uint8_t ao_lco_drag_beep_on;
347 static uint16_t ao_lco_drag_beep_time;
348 static uint16_t ao_lco_drag_warn_time;
350 #define AO_LCO_DRAG_BEEP_TIME AO_MS_TO_TICKS(50)
351 #define AO_LCO_DRAG_WARN_TIME AO_SEC_TO_TICKS(5)
353 /* Request 'beeps' additional drag race beeps */
355 ao_lco_drag_add_beeps(uint8_t beeps)
357 PRINTD("beep %d\n", beeps);
358 if (ao_lco_drag_beep_count == 0)
359 ao_lco_drag_beep_time = ao_time();
360 ao_lco_drag_beep_count += beeps;
361 ao_wakeup(&ao_lco_drag_beep_count);
364 /* Toggle current pad in drag set */
366 ao_lco_toggle_drag(void)
368 if (ao_lco_drag_race && ao_lco_pad != AO_LCO_PAD_VOLTAGE) {
369 ao_lco_selected[ao_lco_box] ^= (1 << (ao_lco_pad - 1));
370 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
371 ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
372 ao_lco_drag_add_beeps(ao_lco_pad);
376 /* Check whether it's time to change the beeper status, then either
377 * turn it on or off as necessary and bump the remaining beep counts
381 ao_lco_drag_beep_check(uint16_t now, uint16_t delay)
383 PRINTD("beep check count %d delta %d\n",
384 ao_lco_drag_beep_count,
385 (int16_t) (now - ao_lco_drag_beep_time));
386 if (ao_lco_drag_beep_count) {
387 if ((int16_t) (now - ao_lco_drag_beep_time) >= 0) {
388 if (ao_lco_drag_beep_on) {
390 PRINTD("beep stop\n");
391 ao_lco_drag_beep_on = 0;
392 if (ao_lco_drag_beep_count) {
393 --ao_lco_drag_beep_count;
394 if (ao_lco_drag_beep_count)
395 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
398 ao_beep(AO_BEEP_HIGH);
399 PRINTD("beep start\n");
400 ao_lco_drag_beep_on = 1;
401 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
406 if (ao_lco_drag_beep_count) {
407 uint16_t beep_delay = 0;
409 if (ao_lco_drag_beep_time > now)
410 beep_delay = ao_lco_drag_beep_time - now;
412 if (delay > beep_delay)
419 ao_lco_drag_enable(void)
421 if (!ao_lco_drag_race) {
422 PRINTD("Drag enable\n");
423 ao_lco_drag_race = 1;
424 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
426 ao_led_on(AO_LED_DRAG);
428 ao_lco_drag_add_beeps(5);
434 ao_lco_drag_disable(void)
436 if (ao_lco_drag_race) {
437 PRINTD("Drag disable\n");
438 ao_lco_drag_race = 0;
440 ao_led_off(AO_LED_DRAG);
442 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
443 ao_lco_drag_add_beeps(2);
448 /* add a beep if it's time to warn the user that drag race mode is
453 ao_lco_drag_warn_check(uint16_t now, uint16_t delay)
455 if (ao_lco_drag_race) {
458 if ((int16_t) (now - ao_lco_drag_warn_time) >= 0) {
459 ao_lco_drag_add_beeps(1);
460 ao_lco_drag_warn_time = now + AO_LCO_DRAG_WARN_TIME;
462 warn_delay = ao_lco_drag_warn_time - now;
463 if (delay > warn_delay)
468 #endif /* AO_LCO_DRAG */
470 /* task function for beeping while arm is active */
472 ao_lco_arm_warn(void)
475 while (!ao_lco_armed) {
477 ao_led_off(AO_LED_FIRE);
479 ao_sleep(&ao_lco_armed);
482 ao_led_on(AO_LED_FIRE);
484 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
485 ao_delay(AO_MS_TO_TICKS(200));