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>
33 static uint8_t ao_lco_debug;
34 #define PRINTD(...) do { if (!ao_lco_debug) break; printf ("\r%5u %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } while(0)
39 #define AO_LCO_PAD_DIGIT 0
40 #define AO_LCO_BOX_DIGIT_1 1
41 #define AO_LCO_BOX_DIGIT_10 2
43 #define AO_LCO_DRAG_RACE_START_TIME AO_SEC_TO_TICKS(5)
44 #define AO_LCO_DRAG_RACE_STOP_TIME AO_SEC_TO_TICKS(2)
46 #define AO_LCO_VALID_LAST 1
47 #define AO_LCO_VALID_EVER 2
49 static uint8_t ao_lco_min_box, ao_lco_max_box;
50 static uint8_t ao_lco_selected[AO_PAD_MAX_BOXES];
51 static uint8_t ao_lco_valid[AO_PAD_MAX_BOXES];
52 static uint8_t ao_lco_channels[AO_PAD_MAX_BOXES];
53 static uint16_t ao_lco_tick_offset[AO_PAD_MAX_BOXES];
56 static uint8_t ao_lco_armed;
57 static uint8_t ao_lco_firing;
58 static uint16_t ao_lco_fire_tick;
59 static uint8_t ao_lco_fire_down;
60 static uint8_t ao_lco_drag_race;
61 static uint8_t ao_lco_pad;
62 static int16_t ao_lco_box;
64 #define AO_LCO_BOX_DRAG 0x1000
66 static struct ao_pad_query ao_pad_query;
68 static uint8_t ao_lco_display_mutex;
71 ao_lco_set_pad(uint8_t pad)
73 ao_mutex_get(&ao_lco_display_mutex);
74 ao_seven_segment_set(AO_LCO_PAD_DIGIT, pad | (ao_lco_drag_race << 4));
75 ao_mutex_put(&ao_lco_display_mutex);
78 #define SEVEN_SEGMENT_d ((0 << 0) | \
87 #define SEVEN_SEGMENT_r ((0 << 0) | \
96 ao_lco_set_box(uint16_t box)
98 ao_mutex_get(&ao_lco_display_mutex);
99 if (box == AO_LCO_BOX_DRAG) {
100 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_10, SEVEN_SEGMENT_d | (ao_lco_drag_race << 7));
101 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_1, SEVEN_SEGMENT_r | (ao_lco_drag_race << 7));
103 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, box % 10 | (ao_lco_drag_race << 4));
104 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, box / 10 | (ao_lco_drag_race << 4));
106 ao_mutex_put(&ao_lco_display_mutex);
110 ao_lco_set_voltage(uint16_t decivolts)
112 uint8_t tens, ones, tenths;
114 tenths = decivolts % 10;
115 ones = (decivolts / 10) % 10;
116 tens = (decivolts / 100) % 10;
117 ao_mutex_get(&ao_lco_display_mutex);
118 ao_seven_segment_set(AO_LCO_PAD_DIGIT, tenths);
119 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ones | 0x10);
120 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, tens);
121 ao_mutex_put(&ao_lco_display_mutex);
125 ao_lco_set_display(void)
127 if (ao_lco_pad == 0 && ao_lco_box != AO_LCO_BOX_DRAG) {
128 ao_lco_set_voltage(ao_pad_query.battery);
130 if (ao_lco_box == AO_LCO_BOX_DRAG)
131 ao_lco_set_pad(ao_lco_drag_race);
133 ao_lco_set_pad(ao_lco_pad);
134 ao_lco_set_box(ao_lco_box);
138 #define MASK_SIZE(n) (((n) + 7) >> 3)
139 #define MASK_ID(n) ((n) >> 3)
140 #define MASK_SHIFT(n) ((n) & 7)
142 static uint8_t ao_lco_box_mask[MASK_SIZE(AO_PAD_MAX_BOXES)];
145 ao_lco_box_present(uint16_t box)
147 if (box == AO_LCO_BOX_DRAG)
150 if (box >= AO_PAD_MAX_BOXES)
152 return (ao_lco_box_mask[MASK_ID(box)] >> MASK_SHIFT(box)) & 1;
156 ao_lco_pad_present(uint8_t box, uint8_t pad)
158 /* voltage measurement is always valid */
161 if (!ao_lco_channels[box])
163 if (pad > AO_PAD_MAX_CHANNELS)
165 return (ao_lco_channels[box] >> (pad - 1)) & 1;
169 ao_lco_pad_first(uint8_t box)
173 for (pad = 1; pad <= AO_PAD_MAX_CHANNELS; pad++)
174 if (ao_lco_pad_present(box, pad))
179 static struct ao_task ao_lco_drag_task;
180 static uint8_t ao_lco_drag_active;
181 static uint8_t ao_lco_drag_beep_count;
182 static uint8_t ao_lco_drag_beep_on;
183 static uint16_t ao_lco_drag_beep_time;
184 static uint16_t ao_lco_drag_warn_time;
186 #define AO_LCO_DRAG_BEEP_TIME AO_MS_TO_TICKS(50)
187 #define AO_LCO_DRAG_WARN_TIME AO_SEC_TO_TICKS(5)
190 ao_lco_drag_beep_start(void)
192 ao_beep(AO_BEEP_HIGH);
193 PRINTD("beep start\n");
194 ao_lco_drag_beep_on = 1;
195 ao_lco_drag_beep_time = ao_time() + AO_LCO_DRAG_BEEP_TIME;
199 ao_lco_drag_beep_stop(void)
202 PRINTD("beep stop\n");
203 ao_lco_drag_beep_on = 0;
204 if (ao_lco_drag_beep_count) {
205 --ao_lco_drag_beep_count;
206 if (ao_lco_drag_beep_count)
207 ao_lco_drag_beep_time = ao_time() + AO_LCO_DRAG_BEEP_TIME;
212 ao_lco_drag_beep(uint8_t beeps)
214 PRINTD("beep %d\n", beeps);
215 if (!ao_lco_drag_beep_count)
216 ao_lco_drag_beep_start();
217 ao_lco_drag_beep_count += beeps;
221 ao_lco_drag_beep_check(uint16_t now, uint16_t delay)
223 PRINTD("beep check count %d delta %d\n",
224 ao_lco_drag_beep_count,
225 (int16_t) (now - ao_lco_drag_beep_time));
226 if (ao_lco_drag_beep_count) {
227 if ((int16_t) (now - ao_lco_drag_beep_time) >= 0) {
228 if (ao_lco_drag_beep_on)
229 ao_lco_drag_beep_stop();
231 ao_lco_drag_beep_start();
235 if (ao_lco_drag_beep_count) {
236 if (delay > AO_LCO_DRAG_BEEP_TIME)
237 delay = AO_LCO_DRAG_BEEP_TIME;
243 ao_lco_drag_enable(void)
245 PRINTD("Drag enable\n");
246 ao_lco_drag_race = 1;
247 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
249 ao_lco_set_display();
250 ao_lco_fire_down = 0;
254 ao_lco_drag_disable(void)
256 PRINTD("Drag disable\n");
257 ao_lco_drag_race = 0;
258 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
260 ao_lco_set_display();
261 ao_lco_fire_down = 0;
265 ao_lco_drag_button_check(uint16_t now, uint16_t delay)
267 uint16_t button_delay = ~0;
270 * Check to see if the button has been held down long enough
271 * to switch in/out of drag race mode
273 if (ao_lco_fire_down) {
274 if (ao_lco_drag_race) {
275 if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_STOP_TIME)
276 ao_lco_drag_disable();
278 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_STOP_TIME - now;
280 if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_START_TIME)
281 ao_lco_drag_enable();
283 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_START_TIME - now;
285 if (delay > button_delay)
286 delay = button_delay;
292 ao_lco_drag_warn_check(uint16_t now, uint16_t delay)
294 uint16_t warn_delay = ~0;
296 if (ao_lco_drag_race) {
297 if ((int16_t) (now - ao_lco_drag_warn_time) >= 0) {
299 ao_lco_drag_warn_time = now + AO_LCO_DRAG_WARN_TIME;
301 warn_delay = ao_lco_drag_warn_time - now;
303 if (delay > warn_delay)
309 ao_lco_drag_monitor(void)
315 PRINTD("Drag monitor active %d delay %d\n", ao_lco_drag_active, delay);
316 if (delay == (uint16_t) ~0)
317 ao_sleep(&ao_lco_drag_active);
319 ao_sleep_for(&ao_lco_drag_active, delay);
322 if (!ao_lco_drag_active)
326 delay = ao_lco_drag_button_check(now, delay);
327 delay = ao_lco_drag_warn_check(now, delay);
328 delay = ao_lco_drag_beep_check(now, delay);
330 /* check to see if there's anything left to do here */
331 if (!ao_lco_fire_down && !ao_lco_drag_race && !ao_lco_drag_beep_count) {
333 ao_lco_drag_active = 0;
341 static struct ao_event event;
345 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
347 ao_event_get(&event);
348 PRINTD("event type %d unit %d value %d\n",
349 event.type, event.unit, event.value);
350 switch (event.type) {
351 case AO_EVENT_QUADRATURE:
352 switch (event.unit) {
353 case AO_QUADRATURE_PAD:
355 dir = (int8_t) event.value;
356 new_pad = ao_lco_pad;
359 if (new_pad > AO_PAD_MAX_CHANNELS)
362 new_pad = AO_PAD_MAX_CHANNELS;
363 if (new_pad == ao_lco_pad)
365 } while (!ao_lco_pad_present(ao_lco_box, new_pad));
366 if (new_pad != ao_lco_pad) {
367 ao_lco_pad = new_pad;
368 ao_lco_set_display();
372 case AO_QUADRATURE_BOX:
374 dir = (int8_t) event.value;
375 new_box = ao_lco_box;
377 if (new_box == AO_LCO_BOX_DRAG) {
379 new_box = ao_lco_max_box;
381 new_box = ao_lco_min_box;
384 if (new_box > ao_lco_max_box)
385 new_box = AO_LCO_BOX_DRAG;
386 else if (new_box < ao_lco_min_box)
387 new_box = AO_LCO_BOX_DRAG;
389 if (new_box == ao_lco_box)
391 } while (!ao_lco_box_present(new_box));
392 if (ao_lco_box != new_box) {
393 ao_lco_box = new_box;
395 if (ao_lco_box != AO_LCO_BOX_DRAG)
396 ao_lco_channels[ao_lco_box] = 0;
397 ao_lco_set_display();
403 case AO_EVENT_BUTTON:
404 switch (event.unit) {
406 ao_lco_armed = event.value;
407 PRINTD("Armed %d\n", ao_lco_armed);
409 if (ao_lco_drag_race) {
412 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
413 if (ao_lco_selected[box]) {
414 ao_wakeup(&ao_lco_armed);
419 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
420 if (ao_lco_pad != 0 && ao_lco_box != AO_LCO_BOX_DRAG)
421 ao_lco_selected[ao_lco_box] = (1 << (ao_lco_pad - 1));
426 ao_wakeup(&ao_lco_armed);
430 ao_lco_fire_down = 0;
431 ao_lco_firing = event.value;
432 PRINTD("Firing %d\n", ao_lco_firing);
433 ao_wakeup(&ao_lco_armed);
436 if (ao_lco_box == AO_LCO_BOX_DRAG) {
437 ao_lco_fire_down = 1;
438 ao_lco_fire_tick = ao_time();
439 ao_lco_drag_active = 1;
441 if (ao_lco_drag_race) {
442 if (ao_lco_pad != 0 && ao_lco_box != AO_LCO_BOX_DRAG) {
443 ao_lco_selected[ao_lco_box] ^= (1 << (ao_lco_pad - 1));
444 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
445 ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
446 ao_lco_drag_beep(ao_lco_pad);
449 ao_wakeup(&ao_lco_drag_active);
451 ao_lco_fire_down = 0;
452 if (ao_lco_drag_active)
453 ao_wakeup(&ao_lco_drag_active);
463 static AO_LED_TYPE continuity_led[AO_LED_CONTINUITY_NUM] = {
464 #ifdef AO_LED_CONTINUITY_0
467 #ifdef AO_LED_CONTINUITY_1
470 #ifdef AO_LED_CONTINUITY_2
473 #ifdef AO_LED_CONTINUITY_3
476 #ifdef AO_LED_CONTINUITY_4
479 #ifdef AO_LED_CONTINUITY_5
482 #ifdef AO_LED_CONTINUITY_6
485 #ifdef AO_LED_CONTINUITY_7
491 ao_lco_get_channels(uint8_t box, struct ao_pad_query *query)
495 r = ao_lco_query(box, query, &ao_lco_tick_offset[box]);
496 if (r == AO_RADIO_CMAC_OK) {
497 ao_lco_channels[box] = query->channels;
498 ao_lco_valid[box] = AO_LCO_VALID_LAST | AO_LCO_VALID_EVER;
500 ao_lco_valid[box] &= ~AO_LCO_VALID_LAST;
501 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]);
502 ao_wakeup(&ao_pad_query);
503 return ao_lco_valid[box];
509 if (ao_lco_box != AO_LCO_BOX_DRAG) {
510 uint8_t previous_valid = ao_lco_valid[ao_lco_box];
512 if (ao_lco_get_channels(ao_lco_box, &ao_pad_query) & AO_LCO_VALID_LAST) {
513 if (!(previous_valid & AO_LCO_VALID_EVER)) {
515 ao_lco_pad = ao_lco_pad_first(ao_lco_box);
516 ao_lco_set_display();
519 ao_lco_set_display();
525 ao_lco_box_reset_present(void)
527 ao_lco_min_box = 0xff;
528 ao_lco_max_box = 0x00;
529 memset(ao_lco_box_mask, 0, sizeof (ao_lco_box_mask));
533 ao_lco_box_set_present(uint8_t box)
535 if (box < ao_lco_min_box)
536 ao_lco_min_box = box;
537 if (box > ao_lco_max_box)
538 ao_lco_max_box = box;
539 if (box >= AO_PAD_MAX_BOXES)
541 ao_lco_box_mask[MASK_ID(box)] |= 1 << MASK_SHIFT(box);
552 ao_lco_box_reset_present();
554 for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
557 for (try = 0; try < 3; try++) {
558 ao_lco_tick_offset[box] = 0;
559 r = ao_lco_query(box, &ao_pad_query, &ao_lco_tick_offset[box]);
560 PRINTD("box %d result %d offset %d\n", box, r, ao_lco_tick_offset[box]);
561 if (r == AO_RADIO_CMAC_OK) {
563 ao_lco_box_set_present(box);
564 ao_lco_set_pad(boxes % 10);
565 ao_delay(AO_MS_TO_TICKS(30));
570 if (ao_lco_min_box <= ao_lco_max_box)
571 ao_lco_box = ao_lco_min_box;
573 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
574 memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
575 memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
577 ao_lco_set_display();
581 ao_lco_igniter_status(void)
587 ao_sleep(&ao_pad_query);
588 PRINTD("RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_box == AO_LCO_BOX_DRAG ? -1 : ao_lco_valid[ao_lco_box]);
589 if (ao_lco_box == AO_LCO_BOX_DRAG) {
590 ao_led_off(AO_LED_RED|AO_LED_GREEN|AO_LED_AMBER);
591 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++)
592 ao_led_off(continuity_led[c]);
594 if (!(ao_lco_valid[ao_lco_box] & AO_LCO_VALID_LAST)) {
595 ao_led_on(AO_LED_RED);
596 ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
599 if (ao_radio_cmac_rssi < -90) {
600 ao_led_on(AO_LED_AMBER);
601 ao_led_off(AO_LED_RED|AO_LED_GREEN);
603 ao_led_on(AO_LED_GREEN);
604 ao_led_off(AO_LED_RED|AO_LED_AMBER);
606 if (ao_pad_query.arm_status)
607 ao_led_on(AO_LED_REMOTE_ARM);
609 ao_led_off(AO_LED_REMOTE_ARM);
611 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
614 if (ao_lco_drag_race) {
615 if (ao_lco_selected[ao_lco_box] & (1 << c) && t)
616 ao_led_on(continuity_led[c]);
618 ao_led_off(continuity_led[c]);
620 if (ao_pad_query.channels & (1 << c))
621 status = ao_pad_query.igniter_status[c];
623 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
624 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
625 ao_led_on(continuity_led[c]);
627 ao_led_off(continuity_led[c]);
636 ao_lco_arm_warn(void)
639 while (!ao_lco_armed)
640 ao_sleep(&ao_lco_armed);
641 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
642 ao_delay(AO_MS_TO_TICKS(200));
647 * Light up everything for a second at power on to let the user
648 * visually inspect the system for correct operation
651 ao_lco_display_test()
653 ao_mutex_get(&ao_lco_display_mutex);
654 ao_seven_segment_set(AO_LCO_PAD_DIGIT, 8 | 0x10);
655 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, 8 | 0x10);
656 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, 8 | 0x10);
657 ao_mutex_put(&ao_lco_display_mutex);
658 ao_led_on(LEDS_AVAILABLE);
659 ao_delay(AO_MS_TO_TICKS(1000));
660 ao_led_off(LEDS_AVAILABLE);
665 ao_lco_batt_voltage(void)
667 struct ao_adc packet;
670 ao_adc_single_get(&packet);
671 decivolt = ao_battery_decivolt(packet.v_batt);
672 ao_lco_set_voltage(decivolt);
673 ao_delay(AO_MS_TO_TICKS(1000));
677 static struct ao_task ao_lco_input_task;
678 static struct ao_task ao_lco_monitor_task;
679 static struct ao_task ao_lco_arm_warn_task;
680 static struct ao_task ao_lco_igniter_status_task;
688 ao_lco_display_test();
690 ao_lco_batt_voltage();
693 ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
694 ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
695 ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
696 ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
698 PRINTD("monitor armed %d firing %d\n",
699 ao_lco_armed, ao_lco_firing);
701 if (ao_lco_armed && ao_lco_firing) {
702 ao_lco_ignite(AO_PAD_FIRE);
706 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
707 if (ao_lco_selected[box]) {
708 PRINTD("Arming box %d pads %x\n",
709 box, ao_lco_selected[box]);
710 if (ao_lco_valid[box] & AO_LCO_VALID_EVER) {
711 ao_lco_arm(box, ao_lco_selected[box], ao_lco_tick_offset[box]);
712 ao_delay(AO_MS_TO_TICKS(10));
718 if (ao_lco_armed && ao_lco_firing)
719 delay = AO_MS_TO_TICKS(100);
721 delay = AO_SEC_TO_TICKS(1);
722 ao_sleep_for(&ao_lco_armed, delay);
728 ao_lco_set_debug(void)
731 if (ao_cmd_status == ao_cmd_success)
732 ao_lco_debug = ao_cmd_lex_i != 0;
735 __code struct ao_cmds ao_lco_cmds[] = {
736 { ao_lco_set_debug, "D <0 off, 1 on>\0Debug" },
737 { ao_lco_search, "s\0Search for pad boxes" },
745 ao_add_task(&ao_lco_monitor_task, ao_lco_monitor, "lco monitor");
747 ao_cmd_register(&ao_lco_cmds[0]);