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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 #include <ao_seven_segment.h>
22 #include <ao_quadrature.h>
23 #include <ao_lco_func.h>
24 #include <ao_radio_cmac.h>
29 static uint8_t ao_lco_debug;
30 #define PRINTD(...) do { if (!ao_lco_debug) break; printf ("\r%5u %s: ", ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } while(0)
35 #define AO_LCO_PAD_DIGIT 0
36 #define AO_LCO_BOX_DIGIT_1 1
37 #define AO_LCO_BOX_DIGIT_10 2
39 #define AO_LCO_DRAG_RACE_START_TIME AO_SEC_TO_TICKS(5)
40 #define AO_LCO_DRAG_RACE_STOP_TIME AO_SEC_TO_TICKS(2)
42 static uint8_t ao_lco_min_box, ao_lco_max_box;
43 static uint8_t ao_lco_selected[AO_PAD_MAX_BOXES];
44 static uint8_t ao_lco_valid[AO_PAD_MAX_BOXES];
45 static uint8_t ao_lco_channels[AO_PAD_MAX_BOXES];
46 static uint16_t ao_lco_tick_offset[AO_PAD_MAX_BOXES];
49 static uint8_t ao_lco_armed;
50 static uint8_t ao_lco_firing;
51 static uint16_t ao_lco_fire_tick;
52 static uint8_t ao_lco_fire_down;
53 static uint8_t ao_lco_drag_race;
54 static uint8_t ao_lco_pad;
55 static int16_t ao_lco_box;
57 #define AO_LCO_BOX_DRAG 0x1000
59 static struct ao_pad_query ao_pad_query;
61 static uint8_t ao_lco_display_mutex;
64 ao_lco_set_pad(uint8_t pad)
66 ao_mutex_get(&ao_lco_display_mutex);
67 ao_seven_segment_set(AO_LCO_PAD_DIGIT, pad | (ao_lco_drag_race << 4));
68 ao_mutex_put(&ao_lco_display_mutex);
71 #define SEVEN_SEGMENT_d ((0 << 0) | \
80 #define SEVEN_SEGMENT_r ((0 << 0) | \
89 ao_lco_set_box(uint16_t box)
91 ao_mutex_get(&ao_lco_display_mutex);
92 if (box == AO_LCO_BOX_DRAG) {
93 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_10, SEVEN_SEGMENT_d | (ao_lco_drag_race << 7));
94 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_1, SEVEN_SEGMENT_r | (ao_lco_drag_race << 7));
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));
99 ao_mutex_put(&ao_lco_display_mutex);
103 ao_lco_set_voltage(uint16_t decivolts)
105 uint8_t tens, ones, tenths;
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 && ao_lco_box != AO_LCO_BOX_DRAG) {
121 ao_lco_set_voltage(ao_pad_query.battery);
123 if (ao_lco_box == AO_LCO_BOX_DRAG)
124 ao_lco_set_pad(ao_lco_drag_race);
126 ao_lco_set_pad(ao_lco_pad);
127 ao_lco_set_box(ao_lco_box);
131 #define MASK_SIZE(n) (((n) + 7) >> 3)
132 #define MASK_ID(n) ((n) >> 3)
133 #define MASK_SHIFT(n) ((n) & 7)
135 static uint8_t ao_lco_box_mask[MASK_SIZE(AO_PAD_MAX_BOXES)];
138 ao_lco_box_present(uint16_t box)
140 if (box == AO_LCO_BOX_DRAG)
143 if (box >= AO_PAD_MAX_BOXES)
145 return (ao_lco_box_mask[MASK_ID(box)] >> MASK_SHIFT(box)) & 1;
149 ao_lco_pad_present(uint8_t box, uint8_t pad)
151 /* voltage measurement is always valid */
154 if (!ao_lco_channels[box])
156 if (pad > AO_PAD_MAX_CHANNELS)
158 return (ao_lco_channels[box] >> (pad - 1)) & 1;
162 ao_lco_pad_first(uint8_t box)
166 for (pad = 1; pad <= AO_PAD_MAX_CHANNELS; pad++)
167 if (ao_lco_pad_present(box, pad))
172 static struct ao_task ao_lco_drag_task;
173 static uint8_t ao_lco_drag_active;
174 static uint8_t ao_lco_drag_beep_count;
175 static uint8_t ao_lco_drag_beep_on;
176 static uint16_t ao_lco_drag_beep_time;
177 static uint16_t ao_lco_drag_warn_time;
179 #define AO_LCO_DRAG_BEEP_TIME AO_MS_TO_TICKS(50)
180 #define AO_LCO_DRAG_WARN_TIME AO_SEC_TO_TICKS(5)
183 ao_lco_drag_beep_start(void)
185 ao_beep(AO_BEEP_HIGH);
186 PRINTD("beep start\n");
187 ao_lco_drag_beep_on = 1;
188 ao_lco_drag_beep_time = ao_time() + AO_LCO_DRAG_BEEP_TIME;
192 ao_lco_drag_beep_stop(void)
195 PRINTD("beep stop\n");
196 ao_lco_drag_beep_on = 0;
197 if (ao_lco_drag_beep_count) {
198 --ao_lco_drag_beep_count;
199 if (ao_lco_drag_beep_count)
200 ao_lco_drag_beep_time = ao_time() + AO_LCO_DRAG_BEEP_TIME;
205 ao_lco_drag_beep(uint8_t beeps)
207 PRINTD("beep %d\n", beeps);
208 if (!ao_lco_drag_beep_count)
209 ao_lco_drag_beep_start();
210 ao_lco_drag_beep_count += beeps;
214 ao_lco_drag_beep_check(uint16_t now, uint16_t delay)
216 PRINTD("beep check count %d delta %d\n",
217 ao_lco_drag_beep_count,
218 (int16_t) (now - ao_lco_drag_beep_time));
219 if (ao_lco_drag_beep_count) {
220 if ((int16_t) (now - ao_lco_drag_beep_time) >= 0) {
221 if (ao_lco_drag_beep_on)
222 ao_lco_drag_beep_stop();
224 ao_lco_drag_beep_start();
228 if (ao_lco_drag_beep_count) {
229 if (delay > AO_LCO_DRAG_BEEP_TIME)
230 delay = AO_LCO_DRAG_BEEP_TIME;
236 ao_lco_drag_enable(void)
238 PRINTD("Drag enable\n");
239 ao_lco_drag_race = 1;
240 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
242 ao_lco_set_display();
243 ao_lco_fire_down = 0;
247 ao_lco_drag_disable(void)
249 PRINTD("Drag disable\n");
250 ao_lco_drag_race = 0;
251 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
253 ao_lco_set_display();
254 ao_lco_fire_down = 0;
258 ao_lco_drag_button_check(uint16_t now, uint16_t delay)
260 uint16_t button_delay = ~0;
263 * Check to see if the button has been held down long enough
264 * to switch in/out of drag race mode
266 if (ao_lco_fire_down) {
267 if (ao_lco_drag_race) {
268 if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_STOP_TIME)
269 ao_lco_drag_disable();
271 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_STOP_TIME - now;
273 if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_START_TIME)
274 ao_lco_drag_enable();
276 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_START_TIME - now;
278 if (delay > button_delay)
279 delay = button_delay;
285 ao_lco_drag_warn_check(uint16_t now, uint16_t delay)
287 uint16_t warn_delay = ~0;
289 if (ao_lco_drag_race) {
290 if ((int16_t) (now - ao_lco_drag_warn_time) >= 0) {
292 ao_lco_drag_warn_time = now + AO_LCO_DRAG_WARN_TIME;
294 warn_delay = ao_lco_drag_warn_time - now;
296 if (delay > warn_delay)
302 ao_lco_drag_monitor(void)
308 PRINTD("Drag monitor active %d delay %d\n", ao_lco_drag_active, delay);
309 if (delay == (uint16_t) ~0)
310 ao_sleep(&ao_lco_drag_active);
312 ao_sleep_for(&ao_lco_drag_active, delay);
315 if (!ao_lco_drag_active)
319 delay = ao_lco_drag_button_check(now, delay);
320 delay = ao_lco_drag_warn_check(now, delay);
321 delay = ao_lco_drag_beep_check(now, delay);
323 /* check to see if there's anything left to do here */
324 if (!ao_lco_fire_down && !ao_lco_drag_race && !ao_lco_drag_beep_count) {
326 ao_lco_drag_active = 0;
334 static struct ao_event event;
338 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
340 ao_event_get(&event);
341 PRINTD("event type %d unit %d value %d\n",
342 event.type, event.unit, event.value);
343 switch (event.type) {
344 case AO_EVENT_QUADRATURE:
345 switch (event.unit) {
346 case AO_QUADRATURE_PAD:
348 dir = (int8_t) event.value;
349 new_pad = ao_lco_pad;
352 if (new_pad > AO_PAD_MAX_CHANNELS)
355 new_pad = AO_PAD_MAX_CHANNELS;
356 if (new_pad == ao_lco_pad)
358 } while (!ao_lco_pad_present(ao_lco_box, new_pad));
359 if (new_pad != ao_lco_pad) {
360 ao_lco_pad = new_pad;
361 ao_lco_set_display();
365 case AO_QUADRATURE_BOX:
367 dir = (int8_t) event.value;
368 new_box = ao_lco_box;
370 if (new_box == AO_LCO_BOX_DRAG) {
372 new_box = ao_lco_max_box;
374 new_box = ao_lco_min_box;
377 if (new_box > ao_lco_max_box)
378 new_box = AO_LCO_BOX_DRAG;
379 else if (new_box < ao_lco_min_box)
380 new_box = AO_LCO_BOX_DRAG;
382 if (new_box == ao_lco_box)
384 } while (!ao_lco_box_present(new_box));
385 if (ao_lco_box != new_box) {
386 ao_lco_box = new_box;
388 if (ao_lco_box != AO_LCO_BOX_DRAG)
389 ao_lco_channels[ao_lco_box] = 0;
390 ao_lco_set_display();
396 case AO_EVENT_BUTTON:
397 switch (event.unit) {
399 ao_lco_armed = event.value;
400 PRINTD("Armed %d\n", ao_lco_armed);
402 if (ao_lco_drag_race) {
405 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
406 if (ao_lco_selected[box]) {
407 ao_wakeup(&ao_lco_armed);
412 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
413 if (ao_lco_pad != 0 && ao_lco_box != AO_LCO_BOX_DRAG)
414 ao_lco_selected[ao_lco_box] = (1 << (ao_lco_pad - 1));
419 ao_wakeup(&ao_lco_armed);
423 ao_lco_fire_down = 0;
424 ao_lco_firing = event.value;
425 PRINTD("Firing %d\n", ao_lco_firing);
426 ao_wakeup(&ao_lco_armed);
429 if (ao_lco_box == AO_LCO_BOX_DRAG) {
430 ao_lco_fire_down = 1;
431 ao_lco_fire_tick = ao_time();
432 ao_lco_drag_active = 1;
434 if (ao_lco_drag_race) {
435 if (ao_lco_pad != 0 && ao_lco_box != AO_LCO_BOX_DRAG) {
436 ao_lco_selected[ao_lco_box] ^= (1 << (ao_lco_pad - 1));
437 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
438 ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
439 ao_lco_drag_beep(ao_lco_pad);
442 ao_wakeup(&ao_lco_drag_active);
444 ao_lco_fire_down = 0;
445 if (ao_lco_drag_active)
446 ao_wakeup(&ao_lco_drag_active);
456 static AO_LED_TYPE continuity_led[AO_LED_CONTINUITY_NUM] = {
457 #ifdef AO_LED_CONTINUITY_0
460 #ifdef AO_LED_CONTINUITY_1
463 #ifdef AO_LED_CONTINUITY_2
466 #ifdef AO_LED_CONTINUITY_3
469 #ifdef AO_LED_CONTINUITY_4
472 #ifdef AO_LED_CONTINUITY_5
475 #ifdef AO_LED_CONTINUITY_6
478 #ifdef AO_LED_CONTINUITY_7
484 ao_lco_get_channels(uint8_t box, struct ao_pad_query *query)
488 r = ao_lco_query(box, query, &ao_lco_tick_offset[box]);
489 if (r == AO_RADIO_CMAC_OK) {
490 ao_lco_channels[box] = query->channels;
491 ao_lco_valid[box] = 1;
493 ao_lco_valid[box] = 0;
494 PRINTD("ao_lco_get_channels(%d) rssi %d valid %d ret %d\n", box, ao_radio_cmac_rssi, ao_lco_valid[box], r);
495 ao_wakeup(&ao_pad_query);
496 return ao_lco_valid[box];
502 if (ao_lco_box != AO_LCO_BOX_DRAG) {
503 uint8_t previous_valid = ao_lco_valid[ao_lco_box];
505 if (ao_lco_get_channels(ao_lco_box, &ao_pad_query)) {
506 if (!previous_valid) {
508 ao_lco_pad = ao_lco_pad_first(ao_lco_box);
509 ao_lco_set_display();
512 ao_lco_set_display();
518 ao_lco_box_reset_present(void)
520 ao_lco_min_box = 0xff;
521 ao_lco_max_box = 0x00;
522 memset(ao_lco_box_mask, 0, sizeof (ao_lco_box_mask));
526 ao_lco_box_set_present(uint8_t box)
528 if (box < ao_lco_min_box)
529 ao_lco_min_box = box;
530 if (box > ao_lco_max_box)
531 ao_lco_max_box = box;
532 if (box >= AO_PAD_MAX_BOXES)
534 ao_lco_box_mask[MASK_ID(box)] |= 1 << MASK_SHIFT(box);
540 uint16_t tick_offset;
546 ao_lco_box_reset_present();
548 for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
551 for (try = 0; try < 3; try++) {
553 r = ao_lco_query(box, &ao_pad_query, &tick_offset);
554 PRINTD("box %d result %d\n", box, r);
555 if (r == AO_RADIO_CMAC_OK) {
557 ao_lco_box_set_present(box);
558 ao_lco_set_pad(boxes % 10);
559 ao_delay(AO_MS_TO_TICKS(30));
564 if (ao_lco_min_box <= ao_lco_max_box)
565 ao_lco_box = ao_lco_min_box;
567 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
568 memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
569 memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
571 ao_lco_set_display();
575 ao_lco_igniter_status(void)
581 ao_sleep(&ao_pad_query);
582 PRINTD("RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_box == AO_LCO_BOX_DRAG ? -1 : ao_lco_valid[ao_lco_box]);
583 if (ao_lco_box == AO_LCO_BOX_DRAG) {
584 ao_led_off(AO_LED_RED|AO_LED_GREEN|AO_LED_AMBER);
585 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++)
586 ao_led_off(continuity_led[c]);
588 if (!ao_lco_valid[ao_lco_box]) {
589 ao_led_on(AO_LED_RED);
590 ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
593 if (ao_radio_cmac_rssi < -90) {
594 ao_led_on(AO_LED_AMBER);
595 ao_led_off(AO_LED_RED|AO_LED_GREEN);
597 ao_led_on(AO_LED_GREEN);
598 ao_led_off(AO_LED_RED|AO_LED_AMBER);
600 if (ao_pad_query.arm_status)
601 ao_led_on(AO_LED_REMOTE_ARM);
603 ao_led_off(AO_LED_REMOTE_ARM);
605 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
608 if (ao_lco_drag_race) {
609 if (ao_lco_selected[ao_lco_box] & (1 << c) && t)
610 ao_led_on(continuity_led[c]);
612 ao_led_off(continuity_led[c]);
614 if (ao_pad_query.channels & (1 << c))
615 status = ao_pad_query.igniter_status[c];
617 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
618 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
619 ao_led_on(continuity_led[c]);
621 ao_led_off(continuity_led[c]);
630 ao_lco_arm_warn(void)
633 while (!ao_lco_armed)
634 ao_sleep(&ao_lco_armed);
635 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
636 ao_delay(AO_MS_TO_TICKS(200));
640 static struct ao_task ao_lco_input_task;
641 static struct ao_task ao_lco_monitor_task;
642 static struct ao_task ao_lco_arm_warn_task;
643 static struct ao_task ao_lco_igniter_status_task;
652 ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
653 ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
654 ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
655 ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
657 PRINTD("monitor armed %d firing %d\n",
658 ao_lco_armed, ao_lco_firing);
660 if (ao_lco_armed && ao_lco_firing) {
665 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
666 if (ao_lco_selected[box]) {
667 PRINTD("Arming box %d pads %x\n",
668 box, ao_lco_selected[box]);
669 if (ao_lco_valid[box])
670 ao_lco_arm(box, ao_lco_selected[box], ao_lco_tick_offset[box]);
675 if (ao_lco_armed && ao_lco_firing)
676 delay = AO_MS_TO_TICKS(100);
678 delay = AO_SEC_TO_TICKS(1);
679 ao_sleep_for(&ao_lco_armed, delay);
685 ao_lco_set_debug(void)
688 if (ao_cmd_status == ao_cmd_success)
689 ao_lco_debug = ao_cmd_lex_i != 0;
692 __code struct ao_cmds ao_lco_cmds[] = {
693 { ao_lco_set_debug, "D <0 off, 1 on>\0Debug" },
694 { ao_lco_search, "s\0Search for pad boxes" },
702 ao_add_task(&ao_lco_monitor_task, ao_lco_monitor, "lco monitor");
704 ao_cmd_register(&ao_lco_cmds[0]);