2 * Copyright © 2012 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.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 #include <ao_seven_segment.h>
23 #include <ao_quadrature.h>
24 #include <ao_lco_func.h>
25 #include <ao_radio_cmac.h>
27 #include <ao_adc_single.h>
36 static uint8_t ao_lco_debug;
37 #define PRINTD(...) do { if (!ao_lco_debug) break; printf ("\r%5u %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } while(0)
42 #define AO_LCO_PAD_DIGIT 0
43 #define AO_LCO_BOX_DIGIT_1 1
44 #define AO_LCO_BOX_DIGIT_10 2
46 #define AO_LCO_DRAG_RACE_START_TIME AO_SEC_TO_TICKS(5)
47 #define AO_LCO_DRAG_RACE_STOP_TIME AO_SEC_TO_TICKS(2)
49 #define AO_LCO_VALID_LAST 1
50 #define AO_LCO_VALID_EVER 2
52 static uint8_t ao_lco_min_box, ao_lco_max_box;
53 static uint8_t ao_lco_selected[AO_PAD_MAX_BOXES];
54 static uint8_t ao_lco_valid[AO_PAD_MAX_BOXES];
55 static uint8_t ao_lco_channels[AO_PAD_MAX_BOXES];
56 static uint16_t ao_lco_tick_offset[AO_PAD_MAX_BOXES];
59 static uint8_t ao_lco_armed;
60 static uint8_t ao_lco_firing;
61 static uint16_t ao_lco_fire_tick;
62 static uint8_t ao_lco_fire_down;
63 static uint8_t ao_lco_drag_race;
64 static uint8_t ao_lco_pad;
65 static int16_t ao_lco_box;
67 #define AO_LCO_BOX_DRAG 0x1000
69 static struct ao_pad_query ao_pad_query;
71 static uint8_t ao_lco_display_mutex;
74 ao_lco_set_pad(uint8_t pad)
76 ao_mutex_get(&ao_lco_display_mutex);
77 ao_seven_segment_set(AO_LCO_PAD_DIGIT, pad | (ao_lco_drag_race << 4));
78 ao_mutex_put(&ao_lco_display_mutex);
81 #define SEVEN_SEGMENT_d ((0 << 0) | \
90 #define SEVEN_SEGMENT_r ((0 << 0) | \
99 ao_lco_set_box(uint16_t box)
101 ao_mutex_get(&ao_lco_display_mutex);
102 if (box == AO_LCO_BOX_DRAG) {
103 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_10, SEVEN_SEGMENT_d | (ao_lco_drag_race << 7));
104 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_1, SEVEN_SEGMENT_r | (ao_lco_drag_race << 7));
106 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, box % 10 | (ao_lco_drag_race << 4));
107 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, box / 10 | (ao_lco_drag_race << 4));
109 ao_mutex_put(&ao_lco_display_mutex);
113 ao_lco_set_voltage(uint16_t decivolts)
115 uint8_t tens, ones, tenths;
117 tenths = decivolts % 10;
118 ones = (decivolts / 10) % 10;
119 tens = (decivolts / 100) % 10;
120 ao_mutex_get(&ao_lco_display_mutex);
121 ao_seven_segment_set(AO_LCO_PAD_DIGIT, tenths);
122 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ones | 0x10);
123 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, tens);
124 ao_mutex_put(&ao_lco_display_mutex);
128 ao_lco_set_display(void)
130 if (ao_lco_pad == 0 && ao_lco_box != AO_LCO_BOX_DRAG) {
131 ao_lco_set_voltage(ao_pad_query.battery);
133 if (ao_lco_box == AO_LCO_BOX_DRAG)
134 ao_lco_set_pad(ao_lco_drag_race);
136 ao_lco_set_pad(ao_lco_pad);
137 ao_lco_set_box(ao_lco_box);
141 #define MASK_SIZE(n) (((n) + 7) >> 3)
142 #define MASK_ID(n) ((n) >> 3)
143 #define MASK_SHIFT(n) ((n) & 7)
145 static uint8_t ao_lco_box_mask[MASK_SIZE(AO_PAD_MAX_BOXES)];
148 ao_lco_box_present(uint16_t box)
150 if (box == AO_LCO_BOX_DRAG)
153 if (box >= AO_PAD_MAX_BOXES)
155 return (ao_lco_box_mask[MASK_ID(box)] >> MASK_SHIFT(box)) & 1;
159 ao_lco_pad_present(uint8_t box, uint8_t pad)
161 /* voltage measurement is always valid */
164 if (!ao_lco_channels[box])
166 if (pad > AO_PAD_MAX_CHANNELS)
168 return (ao_lco_channels[box] >> (pad - 1)) & 1;
172 ao_lco_pad_first(uint8_t box)
176 for (pad = 1; pad <= AO_PAD_MAX_CHANNELS; pad++)
177 if (ao_lco_pad_present(box, pad))
182 static struct ao_task ao_lco_drag_task;
183 static uint8_t ao_lco_drag_active;
184 static uint8_t ao_lco_drag_beep_count;
185 static uint8_t ao_lco_drag_beep_on;
186 static uint16_t ao_lco_drag_beep_time;
187 static uint16_t ao_lco_drag_warn_time;
189 #define AO_LCO_DRAG_BEEP_TIME AO_MS_TO_TICKS(50)
190 #define AO_LCO_DRAG_WARN_TIME AO_SEC_TO_TICKS(5)
193 ao_lco_drag_beep_start(void)
195 ao_beep(AO_BEEP_HIGH);
196 PRINTD("beep start\n");
197 ao_lco_drag_beep_on = 1;
198 ao_lco_drag_beep_time = ao_time() + AO_LCO_DRAG_BEEP_TIME;
202 ao_lco_drag_beep_stop(void)
205 PRINTD("beep stop\n");
206 ao_lco_drag_beep_on = 0;
207 if (ao_lco_drag_beep_count) {
208 --ao_lco_drag_beep_count;
209 if (ao_lco_drag_beep_count)
210 ao_lco_drag_beep_time = ao_time() + AO_LCO_DRAG_BEEP_TIME;
215 ao_lco_drag_beep(uint8_t beeps)
217 PRINTD("beep %d\n", beeps);
218 if (!ao_lco_drag_beep_count)
219 ao_lco_drag_beep_start();
220 ao_lco_drag_beep_count += beeps;
224 ao_lco_drag_beep_check(uint16_t now, uint16_t delay)
226 PRINTD("beep check count %d delta %d\n",
227 ao_lco_drag_beep_count,
228 (int16_t) (now - ao_lco_drag_beep_time));
229 if (ao_lco_drag_beep_count) {
230 if ((int16_t) (now - ao_lco_drag_beep_time) >= 0) {
231 if (ao_lco_drag_beep_on)
232 ao_lco_drag_beep_stop();
234 ao_lco_drag_beep_start();
238 if (ao_lco_drag_beep_count) {
239 if (delay > AO_LCO_DRAG_BEEP_TIME)
240 delay = AO_LCO_DRAG_BEEP_TIME;
246 ao_lco_drag_enable(void)
248 PRINTD("Drag enable\n");
249 ao_lco_drag_race = 1;
250 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
252 ao_lco_set_display();
253 ao_lco_fire_down = 0;
257 ao_lco_drag_disable(void)
259 PRINTD("Drag disable\n");
260 ao_lco_drag_race = 0;
261 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
263 ao_lco_set_display();
264 ao_lco_fire_down = 0;
268 ao_lco_drag_button_check(uint16_t now, uint16_t delay)
270 uint16_t button_delay = ~0;
273 * Check to see if the button has been held down long enough
274 * to switch in/out of drag race mode
276 if (ao_lco_fire_down) {
277 if (ao_lco_drag_race) {
278 if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_STOP_TIME)
279 ao_lco_drag_disable();
281 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_STOP_TIME - now;
283 if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_START_TIME)
284 ao_lco_drag_enable();
286 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_START_TIME - now;
288 if (delay > button_delay)
289 delay = button_delay;
295 ao_lco_drag_warn_check(uint16_t now, uint16_t delay)
297 uint16_t warn_delay = ~0;
299 if (ao_lco_drag_race) {
300 if ((int16_t) (now - ao_lco_drag_warn_time) >= 0) {
302 ao_lco_drag_warn_time = now + AO_LCO_DRAG_WARN_TIME;
304 warn_delay = ao_lco_drag_warn_time - now;
306 if (delay > warn_delay)
312 ao_lco_drag_monitor(void)
318 PRINTD("Drag monitor active %d delay %d\n", ao_lco_drag_active, delay);
319 if (delay == (uint16_t) ~0)
320 ao_sleep(&ao_lco_drag_active);
322 ao_sleep_for(&ao_lco_drag_active, delay);
325 if (!ao_lco_drag_active)
329 delay = ao_lco_drag_button_check(now, delay);
330 delay = ao_lco_drag_warn_check(now, delay);
331 delay = ao_lco_drag_beep_check(now, delay);
333 /* check to see if there's anything left to do here */
334 if (!ao_lco_fire_down && !ao_lco_drag_race && !ao_lco_drag_beep_count) {
336 ao_lco_drag_active = 0;
344 static struct ao_event event;
348 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
350 ao_event_get(&event);
351 PRINTD("event type %d unit %d value %d\n",
352 event.type, event.unit, event.value);
353 switch (event.type) {
354 case AO_EVENT_QUADRATURE:
355 switch (event.unit) {
356 case AO_QUADRATURE_PAD:
358 dir = (int8_t) event.value;
359 new_pad = ao_lco_pad;
362 if (new_pad > AO_PAD_MAX_CHANNELS)
365 new_pad = AO_PAD_MAX_CHANNELS;
366 if (new_pad == ao_lco_pad)
368 } while (!ao_lco_pad_present(ao_lco_box, new_pad));
369 if (new_pad != ao_lco_pad) {
370 ao_lco_pad = new_pad;
371 ao_lco_set_display();
375 case AO_QUADRATURE_BOX:
377 dir = (int8_t) event.value;
378 new_box = ao_lco_box;
380 if (new_box == AO_LCO_BOX_DRAG) {
382 new_box = ao_lco_max_box;
384 new_box = ao_lco_min_box;
387 if (new_box > ao_lco_max_box)
388 new_box = AO_LCO_BOX_DRAG;
389 else if (new_box < ao_lco_min_box)
390 new_box = AO_LCO_BOX_DRAG;
392 if (new_box == ao_lco_box)
394 } while (!ao_lco_box_present(new_box));
395 if (ao_lco_box != new_box) {
396 ao_lco_box = new_box;
398 if (ao_lco_box != AO_LCO_BOX_DRAG)
399 ao_lco_channels[ao_lco_box] = 0;
400 ao_lco_set_display();
406 case AO_EVENT_BUTTON:
407 switch (event.unit) {
409 ao_lco_armed = event.value;
410 PRINTD("Armed %d\n", ao_lco_armed);
412 if (ao_lco_drag_race) {
415 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
416 if (ao_lco_selected[box]) {
417 ao_wakeup(&ao_lco_armed);
422 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
423 if (ao_lco_pad != 0 && ao_lco_box != AO_LCO_BOX_DRAG)
424 ao_lco_selected[ao_lco_box] = (1 << (ao_lco_pad - 1));
429 ao_wakeup(&ao_lco_armed);
433 ao_lco_fire_down = 0;
434 ao_lco_firing = event.value;
435 PRINTD("Firing %d\n", ao_lco_firing);
436 ao_wakeup(&ao_lco_armed);
439 if (ao_lco_box == AO_LCO_BOX_DRAG) {
440 ao_lco_fire_down = 1;
441 ao_lco_fire_tick = ao_time();
442 ao_lco_drag_active = 1;
444 if (ao_lco_drag_race) {
445 if (ao_lco_pad != 0 && ao_lco_box != AO_LCO_BOX_DRAG) {
446 ao_lco_selected[ao_lco_box] ^= (1 << (ao_lco_pad - 1));
447 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
448 ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
449 ao_lco_drag_beep(ao_lco_pad);
452 ao_wakeup(&ao_lco_drag_active);
454 ao_lco_fire_down = 0;
455 if (ao_lco_drag_active)
456 ao_wakeup(&ao_lco_drag_active);
466 static AO_LED_TYPE continuity_led[AO_LED_CONTINUITY_NUM] = {
467 #ifdef AO_LED_CONTINUITY_0
470 #ifdef AO_LED_CONTINUITY_1
473 #ifdef AO_LED_CONTINUITY_2
476 #ifdef AO_LED_CONTINUITY_3
479 #ifdef AO_LED_CONTINUITY_4
482 #ifdef AO_LED_CONTINUITY_5
485 #ifdef AO_LED_CONTINUITY_6
488 #ifdef AO_LED_CONTINUITY_7
494 ao_lco_get_channels(uint8_t box, struct ao_pad_query *query)
498 r = ao_lco_query(box, query, &ao_lco_tick_offset[box]);
499 if (r == AO_RADIO_CMAC_OK) {
500 ao_lco_channels[box] = query->channels;
501 ao_lco_valid[box] = AO_LCO_VALID_LAST | AO_LCO_VALID_EVER;
503 ao_lco_valid[box] &= ~AO_LCO_VALID_LAST;
504 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]);
505 ao_wakeup(&ao_pad_query);
506 return ao_lco_valid[box];
512 if (ao_lco_box != AO_LCO_BOX_DRAG) {
513 uint8_t previous_valid = ao_lco_valid[ao_lco_box];
515 if (ao_lco_get_channels(ao_lco_box, &ao_pad_query) & AO_LCO_VALID_LAST) {
516 if (!(previous_valid & AO_LCO_VALID_EVER)) {
518 ao_lco_pad = ao_lco_pad_first(ao_lco_box);
519 ao_lco_set_display();
522 ao_lco_set_display();
528 ao_lco_box_reset_present(void)
530 ao_lco_min_box = 0xff;
531 ao_lco_max_box = 0x00;
532 memset(ao_lco_box_mask, 0, sizeof (ao_lco_box_mask));
536 ao_lco_box_set_present(uint8_t box)
538 if (box < ao_lco_min_box)
539 ao_lco_min_box = box;
540 if (box > ao_lco_max_box)
541 ao_lco_max_box = box;
542 if (box >= AO_PAD_MAX_BOXES)
544 ao_lco_box_mask[MASK_ID(box)] |= 1 << MASK_SHIFT(box);
555 ao_lco_box_reset_present();
557 for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
560 for (try = 0; try < 3; try++) {
561 ao_lco_tick_offset[box] = 0;
562 r = ao_lco_query(box, &ao_pad_query, &ao_lco_tick_offset[box]);
563 PRINTD("box %d result %d offset %d\n", box, r, ao_lco_tick_offset[box]);
564 if (r == AO_RADIO_CMAC_OK) {
566 ao_lco_box_set_present(box);
567 ao_lco_set_pad(boxes % 10);
568 ao_delay(AO_MS_TO_TICKS(30));
573 if (ao_lco_min_box <= ao_lco_max_box)
574 ao_lco_box = ao_lco_min_box;
576 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
577 memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
578 memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
580 ao_lco_set_display();
584 ao_lco_igniter_status(void)
590 ao_sleep(&ao_pad_query);
591 PRINTD("RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_box == AO_LCO_BOX_DRAG ? -1 : ao_lco_valid[ao_lco_box]);
592 if (ao_lco_box == AO_LCO_BOX_DRAG) {
593 ao_led_off(AO_LED_RED|AO_LED_GREEN|AO_LED_AMBER);
594 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++)
595 ao_led_off(continuity_led[c]);
597 if (!(ao_lco_valid[ao_lco_box] & AO_LCO_VALID_LAST)) {
598 ao_led_on(AO_LED_RED);
599 ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
602 if (ao_radio_cmac_rssi < -90) {
603 ao_led_on(AO_LED_AMBER);
604 ao_led_off(AO_LED_RED|AO_LED_GREEN);
606 ao_led_on(AO_LED_GREEN);
607 ao_led_off(AO_LED_RED|AO_LED_AMBER);
609 if (ao_pad_query.arm_status)
610 ao_led_on(AO_LED_REMOTE_ARM);
612 ao_led_off(AO_LED_REMOTE_ARM);
614 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
617 if (ao_lco_drag_race) {
618 if (ao_lco_selected[ao_lco_box] & (1 << c) && t)
619 ao_led_on(continuity_led[c]);
621 ao_led_off(continuity_led[c]);
623 if (ao_pad_query.channels & (1 << c))
624 status = ao_pad_query.igniter_status[c];
626 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
627 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
628 ao_led_on(continuity_led[c]);
630 ao_led_off(continuity_led[c]);
639 ao_lco_arm_warn(void)
642 while (!ao_lco_armed)
643 ao_sleep(&ao_lco_armed);
644 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
645 ao_delay(AO_MS_TO_TICKS(200));
650 * Light up everything for a second at power on to let the user
651 * visually inspect the system for correct operation
654 ao_lco_display_test()
656 ao_mutex_get(&ao_lco_display_mutex);
657 ao_seven_segment_set(AO_LCO_PAD_DIGIT, 8 | 0x10);
658 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, 8 | 0x10);
659 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, 8 | 0x10);
660 ao_mutex_put(&ao_lco_display_mutex);
661 ao_led_on(LEDS_AVAILABLE);
662 ao_delay(AO_MS_TO_TICKS(1000));
663 ao_led_off(LEDS_AVAILABLE);
667 ao_lco_batt_voltage(void)
669 struct ao_adc packet;
672 ao_adc_single_get(&packet);
673 decivolt = ao_battery_decivolt(packet.v_batt);
674 ao_lco_set_voltage(decivolt);
675 ao_delay(AO_MS_TO_TICKS(1000));
678 static struct ao_task ao_lco_input_task;
679 static struct ao_task ao_lco_monitor_task;
680 static struct ao_task ao_lco_arm_warn_task;
681 static struct ao_task ao_lco_igniter_status_task;
689 ao_lco_display_test();
690 ao_lco_batt_voltage();
692 ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
693 ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
694 ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
695 ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
697 PRINTD("monitor armed %d firing %d\n",
698 ao_lco_armed, ao_lco_firing);
700 if (ao_lco_armed && ao_lco_firing) {
701 ao_lco_ignite(AO_PAD_FIRE);
705 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
706 if (ao_lco_selected[box]) {
707 PRINTD("Arming box %d pads %x\n",
708 box, ao_lco_selected[box]);
709 if (ao_lco_valid[box] & AO_LCO_VALID_EVER) {
710 ao_lco_arm(box, ao_lco_selected[box], ao_lco_tick_offset[box]);
711 ao_delay(AO_MS_TO_TICKS(10));
717 if (ao_lco_armed && ao_lco_firing)
718 delay = AO_MS_TO_TICKS(100);
720 delay = AO_SEC_TO_TICKS(1);
721 ao_sleep_for(&ao_lco_armed, delay);
727 ao_lco_set_debug(void)
730 if (ao_cmd_status == ao_cmd_success)
731 ao_lco_debug = ao_cmd_lex_i != 0;
734 __code struct ao_cmds ao_lco_cmds[] = {
735 { ao_lco_set_debug, "D <0 off, 1 on>\0Debug" },
736 { ao_lco_search, "s\0Search for pad boxes" },
744 ao_add_task(&ao_lco_monitor_task, ao_lco_monitor, "lco monitor");
746 ao_cmd_register(&ao_lco_cmds[0]);