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 int16_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 uint8_t ao_lco_valid[AO_PAD_MAX_BOXES]; /* AO_LCO_VALID bits per box */
43 static const AO_LED_TYPE continuity_led[AO_LED_CONTINUITY_NUM] = {
44 #ifdef AO_LED_CONTINUITY_0
47 #ifdef AO_LED_CONTINUITY_1
50 #ifdef AO_LED_CONTINUITY_2
53 #ifdef AO_LED_CONTINUITY_3
56 #ifdef AO_LED_CONTINUITY_4
59 #ifdef AO_LED_CONTINUITY_5
62 #ifdef AO_LED_CONTINUITY_6
65 #ifdef AO_LED_CONTINUITY_7
70 /* Set LEDs to match remote box status */
72 ao_lco_igniter_status(void)
80 ao_sleep_for(&ao_pad_query, AO_MS_TO_TICKS(50));
83 ao_sleep(&ao_pad_query);
84 if (ao_lco_box_pseudo(ao_lco_box)) {
85 ao_led_off(AO_LED_GREEN|AO_LED_AMBER|AO_LED_RED);
88 PRINTD("RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_valid[ao_lco_box]);
89 if (!(ao_lco_valid[ao_lco_box] & AO_LCO_VALID_LAST)) {
90 ao_led_on(AO_LED_RED);
91 ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
94 if (ao_radio_cmac_rssi < -90) {
95 ao_led_on(AO_LED_AMBER);
96 ao_led_off(AO_LED_RED|AO_LED_GREEN);
98 ao_led_on(AO_LED_GREEN);
99 ao_led_off(AO_LED_RED|AO_LED_AMBER);
101 if (ao_pad_query.arm_status)
102 ao_led_on(AO_LED_REMOTE_ARM);
104 ao_led_off(AO_LED_REMOTE_ARM);
106 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
109 if (ao_pad_query.channels & (1 << c))
110 status = ao_pad_query.igniter_status[c];
112 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
115 if (ao_lco_drag_race && (ao_lco_selected[ao_lco_box] & (1 << c))) {
117 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN) {
125 ao_led_on(continuity_led[c]);
127 ao_led_off(continuity_led[c]);
131 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
132 ao_led_on(continuity_led[c]);
134 ao_led_off(continuity_led[c]);
142 ao_lco_pad_present(int16_t box, int8_t pad)
144 /* voltage measurement is always valid */
145 if (ao_lco_pad_pseudo(pad))
147 if (!ao_lco_channels[box])
149 if (pad > AO_PAD_MAX_CHANNELS)
151 return (ao_lco_channels[box] >> (pad - 1)) & 1;
155 ao_lco_pad_first(int16_t box)
159 for (pad = 1; pad <= AO_PAD_MAX_CHANNELS; pad++)
160 if (ao_lco_pad_present(box, pad))
166 ao_lco_get_channels(int16_t box, struct ao_pad_query *query)
170 r = ao_lco_query((uint16_t) box, query, &ao_lco_tick_offset[box]);
171 if (r == AO_RADIO_CMAC_OK) {
172 ao_lco_channels[box] = query->channels;
173 ao_lco_valid[box] = AO_LCO_VALID_LAST | AO_LCO_VALID_EVER;
175 ao_lco_valid[box] &= (uint8_t) ~AO_LCO_VALID_LAST;
176 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]);
177 ao_wakeup(&ao_pad_query);
178 return ao_lco_valid[box];
184 if (ao_lco_box_pseudo(ao_lco_box)) {
189 uint8_t previous_valid = ao_lco_valid[ao_lco_box];
191 if (ao_lco_get_channels(ao_lco_box, &ao_pad_query) & AO_LCO_VALID_LAST) {
192 if (!(previous_valid & AO_LCO_VALID_EVER)) {
193 if (!ao_lco_pad_pseudo(ao_lco_pad))
194 ao_lco_set_pad(ao_lco_pad_first(ao_lco_box));
196 if (ao_lco_pad_pseudo(ao_lco_pad))
201 uint8_t ao_lco_box_mask[AO_LCO_MASK_SIZE(AO_PAD_MAX_BOXES)];
204 ao_lco_box_reset_present(void)
206 ao_lco_min_box = 0xff;
207 ao_lco_max_box = 0x00;
208 ao_lco_pretending = 0;
209 memset(ao_lco_box_mask, 0, sizeof (ao_lco_box_mask));
213 ao_lco_box_set_present(int16_t box)
215 if (box < ao_lco_min_box)
216 ao_lco_min_box = box;
217 if (box > ao_lco_max_box)
218 ao_lco_max_box = box;
219 if (box >= AO_PAD_MAX_BOXES)
221 ao_lco_box_mask[AO_LCO_MASK_ID(box)] |= (uint8_t) (1 << AO_LCO_MASK_SHIFT(box));
225 ao_lco_set_pad(int8_t new_pad)
227 ao_lco_pad = new_pad;
232 ao_lco_set_box(int16_t new_box)
234 ao_lco_box = new_box;
235 if (!ao_lco_box_pseudo(ao_lco_box)) {
236 if (ao_lco_box < AO_PAD_MAX_BOXES) {
237 if (ao_lco_pretending)
238 ao_lco_channels[ao_lco_box] = 0xff;
240 ao_lco_channels[ao_lco_box] = 0;
248 ao_lco_step_pad(int8_t dir)
252 switch (ao_lco_box) {
253 #ifdef AO_LCO_HAS_CONTRAST
254 case AO_LCO_CONTRAST: {
255 int32_t contrast = ao_lco_get_contrast();
257 contrast = (contrast + AO_LCO_CONTRAST_STEP - 1) / AO_LCO_CONTRAST_STEP;
259 contrast *= AO_LCO_CONTRAST_STEP;
260 if (contrast < AO_LCO_MIN_CONTRAST)
261 contrast = AO_LCO_MIN_CONTRAST;
262 if (contrast > AO_LCO_MAX_CONTRAST)
263 contrast = AO_LCO_MAX_CONTRAST;
264 ao_lco_set_contrast(contrast);
269 #ifdef AO_LCO_HAS_BACKLIGHT
270 case AO_LCO_BACKLIGHT: {
271 int32_t backlight = ao_lco_get_backlight();
273 backlight = (backlight + AO_LCO_BACKLIGHT_STEP - 1) / AO_LCO_BACKLIGHT_STEP;
275 backlight *= AO_LCO_BACKLIGHT_STEP;
276 if (backlight < AO_LCO_MIN_BACKLIGHT)
277 backlight = AO_LCO_MIN_BACKLIGHT;
278 if (backlight > AO_LCO_MAX_BACKLIGHT)
279 backlight = AO_LCO_MAX_BACKLIGHT;
280 ao_lco_set_backlight(backlight);
285 #ifdef AO_LCO_HAS_INFO
287 #if AO_LCO_MIN_INFO_PAGE < AO_LCO_MAX_INFO_PAGE
288 int32_t info_page = ao_lco_get_info_page();
291 if (info_page < AO_LCO_MIN_INFO_PAGE)
292 info_page = AO_LCO_MIN_INFO_PAGE;
293 if (info_page > AO_LCO_MAX_INFO_PAGE)
294 info_page = AO_LCO_MAX_INFO_PAGE;
295 ao_lco_set_info_page();
301 new_pad = (int16_t) ao_lco_pad;
304 if (new_pad > AO_PAD_MAX_CHANNELS)
305 new_pad = AO_LCO_PAD_FIRST;
306 if (new_pad < AO_LCO_PAD_FIRST)
307 new_pad = AO_PAD_MAX_CHANNELS;
308 if (new_pad == ao_lco_pad)
310 } while (!ao_lco_pad_present(ao_lco_box, (int8_t) new_pad));
311 PRINTD("New pad %d\n", new_pad);
312 ao_lco_set_pad((int8_t) new_pad);
318 ao_lco_box_present(int16_t box)
320 if (ao_lco_box_pseudo(box))
322 if (box >= AO_PAD_MAX_BOXES)
324 return (ao_lco_box_mask[AO_LCO_MASK_ID(box)] >> AO_LCO_MASK_SHIFT(box)) & 1;
328 ao_lco_step_box(int8_t dir)
330 int16_t new_box = ao_lco_box;
334 if (new_box > ao_lco_max_box)
335 new_box = AO_LCO_BOX_FIRST;
336 else if (new_box < AO_LCO_BOX_FIRST)
337 new_box = ao_lco_max_box;
338 if (new_box == ao_lco_box)
340 } while (!ao_lco_box_present(new_box));
341 PRINTD("New box %d\n", new_box);
342 ao_lco_set_box(new_box);
346 ao_lco_set_armed(uint8_t armed)
348 if (ao_lco_box_pseudo(ao_lco_box))
351 ao_lco_armed = armed;
352 PRINTD("Armed %d\n", ao_lco_armed);
355 if (ao_lco_drag_race) {
358 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++)
359 if (ao_lco_selected[box])
361 if (box > ao_lco_max_box)
366 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
368 ao_lco_selected[ao_lco_box] = (1 << (ao_lco_pad - 1));
373 ao_wakeup(&ao_lco_armed);
377 ao_lco_set_firing(uint8_t firing)
379 ao_lco_firing = firing;
380 PRINTD("Firing %d\n", ao_lco_firing);
381 ao_wakeup(&ao_lco_armed);
385 static int16_t fake_boxes[] = {
386 1, 2, 3, 5, 8, 11, 13, 17, 19, 23, 29, 31, 37, 62, 97
388 #define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
389 #define NFAKE ARRAYSIZE(fake_boxes)
395 for (i = 0; i < NFAKE; i++)
396 if (fake_boxes[i] == box)
401 #define is_fake(b) false
412 ao_lco_box_reset_present();
413 #ifdef AO_LCO_SEARCH_API
414 ao_lco_search_start();
419 for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
420 #ifdef AO_LCO_SEARCH_API
421 ao_lco_search_box_check(box);
424 ao_lco_show_box(box);
426 for (try = 0; try < 3; try++) {
427 ao_lco_tick_offset[box] = 0;
428 r = ao_lco_query((uint16_t) box, &ao_pad_query, &ao_lco_tick_offset[box]);
429 PRINTD("box %d result %d offset %d\n", box, r, ao_lco_tick_offset[box]);
430 if (r == AO_RADIO_CMAC_OK || is_fake(box)) {
432 ao_lco_box_set_present(box);
433 #ifdef AO_LCO_SEARCH_API
434 ao_lco_search_box_present(box);
436 ao_lco_show_pad((int8_t) (boxes % 10));
438 ao_delay(AO_MS_TO_TICKS(30));
443 if (ao_lco_min_box <= ao_lco_max_box)
444 ao_lco_box = ao_lco_min_box;
446 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
447 memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
448 memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
449 #ifdef AO_LCO_SEARCH_API
450 ao_lco_search_done();
452 ao_lco_set_box(ao_lco_min_box);
460 ao_lco_pretending = 1;
462 ao_lco_max_box = AO_PAD_MAX_BOXES - 1;
463 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++)
464 ao_lco_box_set_present(box);
465 ao_lco_box = ao_lco_min_box;
466 memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
467 memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
468 ao_lco_set_box(ao_lco_min_box);
478 PRINTD("monitor armed %d firing %d\n",
479 ao_lco_armed, ao_lco_firing);
481 if (ao_lco_armed && ao_lco_firing) {
482 ao_lco_ignite(AO_PAD_FIRE);
486 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
487 if (ao_lco_selected[box]) {
488 PRINTD("Arming box %d pads %x\n",
489 box, ao_lco_selected[box]);
490 if (ao_lco_valid[box] & AO_LCO_VALID_EVER) {
491 ao_lco_arm((uint16_t) box, ao_lco_selected[box], ao_lco_tick_offset[box]);
492 ao_delay(AO_MS_TO_TICKS(10));
498 if (ao_lco_armed && ao_lco_firing)
499 delay = AO_MS_TO_TICKS(100);
501 delay = AO_SEC_TO_TICKS(1);
502 ao_sleep_for(&ao_lco_armed, delay);
508 int8_t ao_lco_drag_beep_count;
509 static uint8_t ao_lco_drag_beep_on;
510 static AO_TICK_TYPE ao_lco_drag_beep_time;
511 static AO_TICK_TYPE ao_lco_drag_warn_time;
513 #define AO_LCO_DRAG_BEEP_TIME AO_MS_TO_TICKS(50)
514 #define AO_LCO_DRAG_WARN_TIME AO_SEC_TO_TICKS(5)
516 /* Request 'beeps' additional drag race beeps */
518 ao_lco_drag_add_beeps(int8_t beeps)
520 PRINTD("beep %d\n", beeps);
521 if (ao_lco_drag_beep_count == 0)
522 ao_lco_drag_beep_time = ao_time();
523 ao_lco_drag_beep_count += beeps;
524 ao_wakeup(&ao_lco_drag_beep_count);
527 /* Toggle current pad in drag set */
529 ao_lco_toggle_drag(void)
531 if (ao_lco_drag_race && !ao_lco_pad_pseudo(ao_lco_pad) && !ao_lco_box_pseudo(ao_lco_box)) {
532 ao_lco_selected[ao_lco_box] ^= (uint8_t) (1 << (ao_lco_pad - 1));
533 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
534 ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
535 ao_lco_drag_add_beeps(ao_lco_pad);
539 /* Check whether it's time to change the beeper status, then either
540 * turn it on or off as necessary and bump the remaining beep counts
544 ao_lco_drag_beep_check(AO_TICK_TYPE now, AO_TICK_TYPE delay)
546 PRINTD("beep check count %d delta %ld\n",
547 ao_lco_drag_beep_count,
548 (long) (AO_TICK_SIGNED) (now - ao_lco_drag_beep_time));
549 if (ao_lco_drag_beep_count) {
550 if ((AO_TICK_SIGNED) (now - ao_lco_drag_beep_time) >= 0) {
551 if (ao_lco_drag_beep_on) {
553 PRINTD("beep stop\n");
554 ao_lco_drag_beep_on = 0;
555 if (ao_lco_drag_beep_count) {
556 --ao_lco_drag_beep_count;
557 if (ao_lco_drag_beep_count)
558 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
561 ao_beep(AO_BEEP_HIGH);
562 PRINTD("beep start\n");
563 ao_lco_drag_beep_on = 1;
564 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
569 if (ao_lco_drag_beep_count) {
570 AO_TICK_TYPE beep_delay = 0;
572 if (ao_lco_drag_beep_time > now)
573 beep_delay = ao_lco_drag_beep_time - now;
575 if (delay > beep_delay)
582 ao_lco_drag_enable(void)
584 if (!ao_lco_drag_race) {
585 PRINTD("Drag enable\n");
586 ao_lco_drag_race = 1;
587 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
589 ao_led_on(AO_LED_DRAG);
591 ao_lco_drag_add_beeps(5);
597 ao_lco_drag_disable(void)
599 if (ao_lco_drag_race) {
600 PRINTD("Drag disable\n");
601 ao_lco_drag_race = 0;
603 ao_led_off(AO_LED_DRAG);
605 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
606 ao_lco_drag_add_beeps(2);
611 /* add a beep if it's time to warn the user that drag race mode is
616 ao_lco_drag_warn_check(AO_TICK_TYPE now, AO_TICK_TYPE delay)
618 if (ao_lco_drag_race) {
619 AO_TICK_TYPE warn_delay;
621 if ((AO_TICK_SIGNED) (now - ao_lco_drag_warn_time) >= 0) {
622 ao_lco_drag_add_beeps(1);
623 ao_lco_drag_warn_time = now + AO_LCO_DRAG_WARN_TIME;
625 warn_delay = ao_lco_drag_warn_time - now;
626 if (delay > warn_delay)
631 #endif /* AO_LCO_DRAG */
633 /* task function for beeping while arm is active */
635 ao_lco_arm_warn(void)
638 while (!ao_lco_armed) {
640 ao_led_off(AO_LED_FIRE);
642 ao_sleep(&ao_lco_armed);
645 ao_led_on(AO_LED_FIRE);
647 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
648 ao_delay(AO_MS_TO_TICKS(200));