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>
26 #include <ao_adc_single.h>
31 static uint8_t ao_lco_debug;
32 #define PRINTD(...) do { if (!ao_lco_debug) break; printf ("\r%5u %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } while(0)
37 #define AO_LCO_PAD_DIGIT 0
38 #define AO_LCO_BOX_DIGIT_1 1
39 #define AO_LCO_BOX_DIGIT_10 2
41 #define AO_LCO_DRAG_RACE_START_TIME AO_SEC_TO_TICKS(5)
42 #define AO_LCO_DRAG_RACE_STOP_TIME AO_SEC_TO_TICKS(2)
44 #define AO_LCO_VALID_LAST 1
45 #define AO_LCO_VALID_EVER 2
47 static uint8_t ao_lco_min_box, ao_lco_max_box;
48 static uint8_t ao_lco_selected[AO_PAD_MAX_BOXES];
49 static uint8_t ao_lco_valid[AO_PAD_MAX_BOXES];
50 static uint8_t ao_lco_channels[AO_PAD_MAX_BOXES];
51 static uint16_t ao_lco_tick_offset[AO_PAD_MAX_BOXES];
54 static uint8_t ao_lco_armed;
55 static uint8_t ao_lco_firing;
56 static uint8_t ao_lco_drag_race;
57 static uint8_t ao_lco_pad;
58 static int16_t ao_lco_box;
59 static uint8_t ao_lco_select_mode;
60 #define AO_LCO_SELECT_PAD 0
61 #define AO_LCO_SELECT_BOX 1
63 static struct ao_pad_query ao_pad_query;
65 static uint8_t ao_lco_display_mutex;
68 ao_lco_set_pad(uint8_t pad)
70 ao_mutex_get(&ao_lco_display_mutex);
71 ao_seven_segment_set(AO_LCO_PAD_DIGIT, pad | (ao_lco_drag_race << 4));
72 ao_mutex_put(&ao_lco_display_mutex);
75 #define SEVEN_SEGMENT_d ((0 << 0) | \
84 #define SEVEN_SEGMENT_r ((0 << 0) | \
93 ao_lco_set_box(uint16_t box)
95 ao_mutex_get(&ao_lco_display_mutex);
96 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, box % 10 | (ao_lco_drag_race << 4));
97 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, box / 10 | (ao_lco_drag_race << 4));
98 ao_mutex_put(&ao_lco_display_mutex);
102 ao_lco_set_voltage(uint16_t decivolts)
104 uint8_t tens, ones, tenths;
106 PRINTD("voltage %d\n", decivolts);
107 tenths = decivolts % 10;
108 ones = (decivolts / 10) % 10;
109 tens = (decivolts / 100) % 10;
110 ao_mutex_get(&ao_lco_display_mutex);
111 ao_seven_segment_set(AO_LCO_PAD_DIGIT, tenths);
112 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ones | 0x10);
113 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, tens);
114 ao_mutex_put(&ao_lco_display_mutex);
118 ao_lco_set_display(void)
120 if (ao_lco_pad == 0) {
121 ao_lco_set_voltage(ao_pad_query.battery);
123 ao_lco_set_pad(ao_lco_pad);
124 ao_lco_set_box(ao_lco_box);
128 #define MASK_SIZE(n) (((n) + 7) >> 3)
129 #define MASK_ID(n) ((n) >> 3)
130 #define MASK_SHIFT(n) ((n) & 7)
132 static uint8_t ao_lco_box_mask[MASK_SIZE(AO_PAD_MAX_BOXES)];
135 ao_lco_box_present(uint16_t box)
137 if (box >= AO_PAD_MAX_BOXES)
139 return (ao_lco_box_mask[MASK_ID(box)] >> MASK_SHIFT(box)) & 1;
143 ao_lco_pad_present(uint8_t box, uint8_t pad)
145 /* voltage measurement is always valid */
148 if (!ao_lco_channels[box])
150 if (pad > AO_PAD_MAX_CHANNELS)
152 return (ao_lco_channels[box] >> (pad - 1)) & 1;
156 ao_lco_pad_first(uint8_t box)
160 for (pad = 1; pad <= AO_PAD_MAX_CHANNELS; pad++)
161 if (ao_lco_pad_present(box, pad))
167 ao_lco_set_select(void)
170 ao_led_off(AO_LED_PAD);
171 ao_led_off(AO_LED_BOX);
173 switch (ao_lco_select_mode) {
174 case AO_LCO_SELECT_PAD:
175 ao_led_off(AO_LED_BOX);
176 ao_led_on(AO_LED_PAD);
178 case AO_LCO_SELECT_BOX:
179 ao_led_off(AO_LED_PAD);
180 ao_led_on(AO_LED_BOX);
188 static struct ao_task ao_lco_drag_task;
189 static uint8_t ao_lco_drag_active;
190 static uint8_t ao_lco_drag_beep_count;
191 static uint8_t ao_lco_drag_beep_on;
192 static uint16_t ao_lco_drag_beep_time;
193 static uint16_t ao_lco_drag_warn_time;
195 #define AO_LCO_DRAG_BEEP_TIME AO_MS_TO_TICKS(50)
196 #define AO_LCO_DRAG_WARN_TIME AO_SEC_TO_TICKS(5)
199 ao_lco_drag_beep_start(void)
201 ao_beep(AO_BEEP_HIGH);
202 PRINTD("beep start\n");
203 ao_lco_drag_beep_on = 1;
204 ao_lco_drag_beep_time = ao_time() + AO_LCO_DRAG_BEEP_TIME;
208 ao_lco_drag_beep_stop(void)
211 PRINTD("beep stop\n");
212 ao_lco_drag_beep_on = 0;
213 if (ao_lco_drag_beep_count) {
214 --ao_lco_drag_beep_count;
215 if (ao_lco_drag_beep_count)
216 ao_lco_drag_beep_time = ao_time() + AO_LCO_DRAG_BEEP_TIME;
221 ao_lco_drag_beep(uint8_t beeps)
223 PRINTD("beep %d\n", beeps);
224 if (!ao_lco_drag_beep_count)
225 ao_lco_drag_beep_start();
226 ao_lco_drag_beep_count += beeps;
230 ao_lco_drag_beep_check(uint16_t now, uint16_t delay)
232 PRINTD("beep check count %d delta %d\n",
233 ao_lco_drag_beep_count,
234 (int16_t) (now - ao_lco_drag_beep_time));
235 if (ao_lco_drag_beep_count) {
236 if ((int16_t) (now - ao_lco_drag_beep_time) >= 0) {
237 if (ao_lco_drag_beep_on)
238 ao_lco_drag_beep_stop();
240 ao_lco_drag_beep_start();
244 if (ao_lco_drag_beep_count) {
245 if (delay > AO_LCO_DRAG_BEEP_TIME)
246 delay = AO_LCO_DRAG_BEEP_TIME;
252 ao_lco_drag_enable(void)
254 PRINTD("Drag enable\n");
255 ao_lco_drag_race = 1;
256 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
257 ao_led_on(AO_LED_DRAG);
259 ao_lco_set_display();
263 ao_lco_drag_disable(void)
265 PRINTD("Drag disable\n");
266 ao_lco_drag_race = 0;
267 ao_led_off(AO_LED_DRAG);
268 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
270 ao_lco_set_display();
274 ao_lco_drag_warn_check(uint16_t now, uint16_t delay)
276 uint16_t warn_delay = ~0;
278 if (ao_lco_drag_race) {
279 if ((int16_t) (now - ao_lco_drag_warn_time) >= 0) {
281 ao_lco_drag_warn_time = now + AO_LCO_DRAG_WARN_TIME;
283 warn_delay = ao_lco_drag_warn_time - now;
285 if (delay > warn_delay)
291 ao_lco_drag_monitor(void)
297 PRINTD("Drag monitor active %d delay %d\n", ao_lco_drag_active, delay);
298 if (delay == (uint16_t) ~0)
299 ao_sleep(&ao_lco_drag_active);
301 ao_sleep_for(&ao_lco_drag_active, delay);
304 if (!ao_lco_drag_active)
308 delay = ao_lco_drag_warn_check(now, delay);
309 delay = ao_lco_drag_beep_check(now, delay);
311 /* check to see if there's anything left to do here */
312 if (!ao_lco_drag_race && !ao_lco_drag_beep_count) {
314 ao_lco_drag_active = 0;
322 static struct ao_event event;
326 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
328 ao_event_get(&event);
329 PRINTD("event type %d unit %d value %d\n",
330 event.type, event.unit, event.value);
331 switch (event.type) {
332 case AO_EVENT_QUADRATURE:
333 switch (event.unit) {
334 case AO_QUADRATURE_SELECT:
336 switch (ao_lco_select_mode) {
337 case AO_LCO_SELECT_PAD:
338 dir = (int8_t) event.value;
339 new_pad = ao_lco_pad;
342 if (new_pad > AO_PAD_MAX_CHANNELS)
345 new_pad = AO_PAD_MAX_CHANNELS;
346 if (new_pad == ao_lco_pad)
348 } while (!ao_lco_pad_present(ao_lco_box, new_pad));
349 if (new_pad != ao_lco_pad) {
350 ao_lco_pad = new_pad;
351 ao_lco_set_display();
354 case AO_LCO_SELECT_BOX:
355 dir = (int8_t) event.value;
356 new_box = ao_lco_box;
359 if (new_box > ao_lco_max_box)
360 new_box = ao_lco_min_box;
361 else if (new_box < ao_lco_min_box)
362 new_box = ao_lco_max_box;
363 if (new_box == ao_lco_box)
365 } while (!ao_lco_box_present(new_box));
366 if (ao_lco_box != new_box) {
367 ao_lco_box = new_box;
369 ao_lco_channels[ao_lco_box] = 0;
370 ao_lco_set_display();
380 case AO_EVENT_BUTTON:
381 switch (event.unit) {
383 ao_lco_armed = event.value;
384 PRINTD("Armed %d\n", ao_lco_armed);
386 if (ao_lco_drag_race) {
389 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++)
390 if (ao_lco_selected[box])
392 if (box > ao_lco_max_box)
395 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
397 ao_lco_selected[ao_lco_box] = (1 << (ao_lco_pad - 1));
403 ao_wakeup(&ao_lco_armed);
407 ao_lco_firing = event.value;
408 PRINTD("Firing %d\n", ao_lco_firing);
409 ao_wakeup(&ao_lco_armed);
412 case AO_BUTTON_DRAG_SELECT:
413 if (event.value && ao_lco_drag_race) {
414 if (ao_lco_pad != 0) {
415 ao_lco_selected[ao_lco_box] ^= (1 << (ao_lco_pad - 1));
416 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
417 ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
418 ao_lco_drag_beep(ao_lco_pad);
422 case AO_BUTTON_DRAG_MODE:
424 ao_lco_drag_enable();
426 ao_lco_drag_disable();
428 case AO_BUTTON_ENCODER_SELECT:
431 ao_lco_select_mode = 1 - ao_lco_select_mode;
442 static AO_LED_TYPE continuity_led[AO_LED_CONTINUITY_NUM] = {
443 #ifdef AO_LED_CONTINUITY_0
446 #ifdef AO_LED_CONTINUITY_1
449 #ifdef AO_LED_CONTINUITY_2
452 #ifdef AO_LED_CONTINUITY_3
455 #ifdef AO_LED_CONTINUITY_4
458 #ifdef AO_LED_CONTINUITY_5
461 #ifdef AO_LED_CONTINUITY_6
464 #ifdef AO_LED_CONTINUITY_7
470 ao_lco_get_channels(uint8_t box, struct ao_pad_query *query)
474 r = ao_lco_query(box, query, &ao_lco_tick_offset[box]);
475 if (r == AO_RADIO_CMAC_OK) {
476 ao_lco_channels[box] = query->channels;
477 ao_lco_valid[box] = AO_LCO_VALID_LAST | AO_LCO_VALID_EVER;
479 ao_lco_valid[box] &= ~AO_LCO_VALID_LAST;
480 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]);
481 ao_wakeup(&ao_pad_query);
482 return ao_lco_valid[box];
488 uint8_t previous_valid = ao_lco_valid[ao_lco_box];
490 if (ao_lco_get_channels(ao_lco_box, &ao_pad_query) & AO_LCO_VALID_LAST) {
491 if (!(previous_valid & AO_LCO_VALID_EVER)) {
493 ao_lco_pad = ao_lco_pad_first(ao_lco_box);
494 ao_lco_set_display();
497 ao_lco_set_display();
502 ao_lco_box_reset_present(void)
504 ao_lco_min_box = 0xff;
505 ao_lco_max_box = 0x00;
506 memset(ao_lco_box_mask, 0, sizeof (ao_lco_box_mask));
510 ao_lco_box_set_present(uint8_t box)
512 if (box < ao_lco_min_box)
513 ao_lco_min_box = box;
514 if (box > ao_lco_max_box)
515 ao_lco_max_box = box;
516 if (box >= AO_PAD_MAX_BOXES)
518 ao_lco_box_mask[MASK_ID(box)] |= 1 << MASK_SHIFT(box);
529 ao_lco_box_reset_present();
531 for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
534 for (try = 0; try < 3; try++) {
535 ao_lco_tick_offset[box] = 0;
536 r = ao_lco_query(box, &ao_pad_query, &ao_lco_tick_offset[box]);
537 PRINTD("box %d result %d offset %d\n", box, r, ao_lco_tick_offset[box]);
538 if (r == AO_RADIO_CMAC_OK) {
540 ao_lco_box_set_present(box);
541 ao_lco_set_pad(boxes % 10);
542 ao_delay(AO_MS_TO_TICKS(30));
547 if (ao_lco_min_box <= ao_lco_max_box)
548 ao_lco_box = ao_lco_min_box;
550 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
551 memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
552 memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
554 ao_lco_set_display();
558 ao_lco_igniter_status(void)
564 ao_sleep(&ao_pad_query);
565 PRINTD("RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_valid[ao_lco_box]);
566 if (!(ao_lco_valid[ao_lco_box] & AO_LCO_VALID_LAST)) {
567 ao_led_on(AO_LED_RED);
568 ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
571 if (ao_radio_cmac_rssi < -90) {
572 ao_led_on(AO_LED_AMBER);
573 ao_led_off(AO_LED_RED|AO_LED_GREEN);
575 ao_led_on(AO_LED_GREEN);
576 ao_led_off(AO_LED_RED|AO_LED_AMBER);
578 if (ao_pad_query.arm_status)
579 ao_led_on(AO_LED_REMOTE_ARM);
581 ao_led_off(AO_LED_REMOTE_ARM);
583 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
586 if (ao_pad_query.channels & (1 << c))
587 status = ao_pad_query.igniter_status[c];
589 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
591 if (ao_lco_drag_race && (ao_lco_selected[ao_lco_box] & (1 << c))) {
593 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN) {
601 ao_led_on(continuity_led[c]);
603 ao_led_off(continuity_led[c]);
605 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
606 ao_led_on(continuity_led[c]);
608 ao_led_off(continuity_led[c]);
616 ao_lco_arm_warn(void)
619 while (!ao_lco_armed) {
620 ao_led_off(AO_LED_FIRE);
621 ao_sleep(&ao_lco_armed);
623 ao_led_on(AO_LED_FIRE);
624 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
625 ao_delay(AO_MS_TO_TICKS(200));
630 * Light up everything for a second at power on to let the user
631 * visually inspect the system for correct operation
634 ao_lco_display_test()
636 ao_mutex_get(&ao_lco_display_mutex);
637 ao_seven_segment_set(AO_LCO_PAD_DIGIT, 8 | 0x10);
638 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, 8 | 0x10);
639 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, 8 | 0x10);
640 ao_mutex_put(&ao_lco_display_mutex);
641 ao_led_on(LEDS_AVAILABLE);
642 ao_delay(AO_MS_TO_TICKS(1000));
643 ao_led_off(LEDS_AVAILABLE);
647 ao_lco_batt_voltage(void)
649 struct ao_adc packet;
652 ao_adc_single_get(&packet);
653 decivolt = ao_battery_decivolt(packet.v_batt);
654 ao_lco_set_voltage(decivolt);
655 ao_delay(AO_MS_TO_TICKS(1000));
658 static struct ao_task ao_lco_input_task;
659 static struct ao_task ao_lco_monitor_task;
660 static struct ao_task ao_lco_arm_warn_task;
661 static struct ao_task ao_lco_igniter_status_task;
669 ao_lco_display_test();
670 ao_lco_batt_voltage();
672 ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
673 ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
674 ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
675 ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
677 PRINTD("monitor armed %d firing %d\n",
678 ao_lco_armed, ao_lco_firing);
680 if (ao_lco_armed && ao_lco_firing) {
681 ao_lco_ignite(AO_PAD_FIRE);
685 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
686 if (ao_lco_selected[box]) {
687 PRINTD("Arming box %d pads %x\n",
688 box, ao_lco_selected[box]);
689 if (ao_lco_valid[box] & AO_LCO_VALID_EVER) {
690 ao_lco_arm(box, ao_lco_selected[box], ao_lco_tick_offset[box]);
691 ao_delay(AO_MS_TO_TICKS(10));
697 if (ao_lco_armed && ao_lco_firing)
698 delay = AO_MS_TO_TICKS(100);
700 delay = AO_SEC_TO_TICKS(1);
701 ao_sleep_for(&ao_lco_armed, delay);
707 ao_lco_set_debug(void)
710 if (ao_cmd_status == ao_cmd_success)
711 ao_lco_debug = ao_cmd_lex_i != 0;
714 __code struct ao_cmds ao_lco_cmds[] = {
715 { ao_lco_set_debug, "D <0 off, 1 on>\0Debug" },
716 { ao_lco_search, "s\0Search for pad boxes" },
724 ao_add_task(&ao_lco_monitor_task, ao_lco_monitor, "lco monitor");
726 ao_cmd_register(&ao_lco_cmds[0]);