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)
176 ao_lco_show_display();
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 if (new_pad != ao_lco_pad) {
206 ao_lco_pad = new_pad;
207 ao_lco_show_display();
212 ao_lco_set_box(uint16_t new_box)
214 if (ao_lco_box != new_box) {
215 ao_lco_box = new_box;
217 if (ao_lco_box != AO_LCO_BOX_DRAG)
219 ao_lco_channels[ao_lco_box] = 0;
225 ao_lco_step_pad(int8_t dir)
229 new_pad = ao_lco_pad;
232 if (new_pad > AO_PAD_MAX_CHANNELS)
233 new_pad = AO_LCO_PAD_VOLTAGE;
235 new_pad = AO_PAD_MAX_CHANNELS;
236 if (new_pad == ao_lco_pad)
238 } while (!ao_lco_pad_present(ao_lco_box, new_pad));
239 ao_lco_set_pad(new_pad);
243 ao_lco_set_armed(uint8_t armed)
245 ao_lco_armed = armed;
246 PRINTD("Armed %d\n", ao_lco_armed);
248 if (ao_lco_drag_race) {
251 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++)
252 if (ao_lco_selected[box])
254 if (box > ao_lco_max_box)
257 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
259 ao_lco_selected[ao_lco_box] = (1 << (ao_lco_pad - 1));
264 ao_wakeup(&ao_lco_armed);
268 ao_lco_set_firing(uint8_t firing)
270 ao_lco_firing = firing;
271 PRINTD("Firing %d\n", ao_lco_firing);
272 ao_wakeup(&ao_lco_armed);
283 ao_lco_box_reset_present();
285 for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
288 for (try = 0; try < 3; try++) {
289 ao_lco_tick_offset[box] = 0;
290 r = ao_lco_query(box, &ao_pad_query, &ao_lco_tick_offset[box]);
291 PRINTD("box %d result %d offset %d\n", box, r, ao_lco_tick_offset[box]);
292 if (r == AO_RADIO_CMAC_OK) {
294 ao_lco_box_set_present(box);
295 ao_lco_set_pad(boxes % 10);
296 ao_delay(AO_MS_TO_TICKS(30));
301 if (ao_lco_min_box <= ao_lco_max_box)
302 ao_lco_box = ao_lco_min_box;
304 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
305 memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
306 memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
317 PRINTD("monitor armed %d firing %d\n",
318 ao_lco_armed, ao_lco_firing);
320 if (ao_lco_armed && ao_lco_firing) {
321 ao_lco_ignite(AO_PAD_FIRE);
325 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
326 if (ao_lco_selected[box]) {
327 PRINTD("Arming box %d pads %x\n",
328 box, ao_lco_selected[box]);
329 if (ao_lco_valid[box] & AO_LCO_VALID_EVER) {
330 ao_lco_arm(box, ao_lco_selected[box], ao_lco_tick_offset[box]);
331 ao_delay(AO_MS_TO_TICKS(10));
337 if (ao_lco_armed && ao_lco_firing)
338 delay = AO_MS_TO_TICKS(100);
340 delay = AO_SEC_TO_TICKS(1);
341 ao_sleep_for(&ao_lco_armed, delay);
347 uint8_t ao_lco_drag_beep_count;
348 static uint8_t ao_lco_drag_beep_on;
349 static uint16_t ao_lco_drag_beep_time;
350 static uint16_t ao_lco_drag_warn_time;
352 #define AO_LCO_DRAG_BEEP_TIME AO_MS_TO_TICKS(50)
353 #define AO_LCO_DRAG_WARN_TIME AO_SEC_TO_TICKS(5)
355 /* Request 'beeps' additional drag race beeps */
357 ao_lco_drag_add_beeps(uint8_t beeps)
359 PRINTD("beep %d\n", beeps);
360 if (ao_lco_drag_beep_count == 0)
361 ao_lco_drag_beep_time = ao_time();
362 ao_lco_drag_beep_count += beeps;
363 ao_wakeup(&ao_lco_drag_beep_count);
366 /* Toggle current pad in drag set */
368 ao_lco_toggle_drag(void)
370 if (ao_lco_drag_race && ao_lco_pad != AO_LCO_PAD_VOLTAGE) {
371 ao_lco_selected[ao_lco_box] ^= (1 << (ao_lco_pad - 1));
372 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
373 ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
374 ao_lco_drag_add_beeps(ao_lco_pad);
378 /* Check whether it's time to change the beeper status, then either
379 * turn it on or off as necessary and bump the remaining beep counts
383 ao_lco_drag_beep_check(uint16_t now, uint16_t delay)
385 PRINTD("beep check count %d delta %d\n",
386 ao_lco_drag_beep_count,
387 (int16_t) (now - ao_lco_drag_beep_time));
388 if (ao_lco_drag_beep_count) {
389 if ((int16_t) (now - ao_lco_drag_beep_time) >= 0) {
390 if (ao_lco_drag_beep_on) {
392 PRINTD("beep stop\n");
393 ao_lco_drag_beep_on = 0;
394 if (ao_lco_drag_beep_count) {
395 --ao_lco_drag_beep_count;
396 if (ao_lco_drag_beep_count)
397 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
400 ao_beep(AO_BEEP_HIGH);
401 PRINTD("beep start\n");
402 ao_lco_drag_beep_on = 1;
403 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
408 if (ao_lco_drag_beep_count) {
409 uint16_t beep_delay = 0;
411 if (ao_lco_drag_beep_time > now)
412 beep_delay = ao_lco_drag_beep_time - now;
414 if (delay > beep_delay)
421 ao_lco_drag_enable(void)
423 if (!ao_lco_drag_race) {
424 PRINTD("Drag enable\n");
425 ao_lco_drag_race = 1;
426 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
428 ao_led_on(AO_LED_DRAG);
430 ao_lco_drag_add_beeps(5);
431 ao_lco_show_display();
436 ao_lco_drag_disable(void)
438 if (ao_lco_drag_race) {
439 PRINTD("Drag disable\n");
440 ao_lco_drag_race = 0;
442 ao_led_off(AO_LED_DRAG);
444 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
445 ao_lco_drag_add_beeps(2);
446 ao_lco_show_display();
450 /* add a beep if it's time to warn the user that drag race mode is
455 ao_lco_drag_warn_check(uint16_t now, uint16_t delay)
457 if (ao_lco_drag_race) {
460 if ((int16_t) (now - ao_lco_drag_warn_time) >= 0) {
461 ao_lco_drag_add_beeps(1);
462 ao_lco_drag_warn_time = now + AO_LCO_DRAG_WARN_TIME;
464 warn_delay = ao_lco_drag_warn_time - now;
465 if (delay > warn_delay)
470 #endif /* AO_LCO_DRAG */
472 /* task function for beeping while arm is active */
474 ao_lco_arm_warn(void)
477 while (!ao_lco_armed) {
479 ao_led_off(AO_LED_FIRE);
481 ao_sleep(&ao_lco_armed);
484 ao_led_on(AO_LED_FIRE);
486 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
487 ao_delay(AO_MS_TO_TICKS(200));