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 uint8_t ao_lco_box;
56 static struct ao_pad_query ao_pad_query;
58 static uint8_t ao_lco_display_mutex;
61 ao_lco_set_pad(uint8_t pad)
63 ao_mutex_get(&ao_lco_display_mutex);
64 ao_seven_segment_set(AO_LCO_PAD_DIGIT, pad | (ao_lco_drag_race << 4));
65 ao_mutex_put(&ao_lco_display_mutex);
69 ao_lco_set_box(uint8_t box)
71 ao_mutex_get(&ao_lco_display_mutex);
72 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, box % 10 | (ao_lco_drag_race << 4));
73 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, box / 10 | (ao_lco_drag_race << 4));
74 ao_mutex_put(&ao_lco_display_mutex);
78 ao_lco_set_voltage(uint16_t decivolts)
80 uint8_t tens, ones, tenths;
82 tenths = decivolts % 10;
83 ones = (decivolts / 10) % 10;
84 tens = (decivolts / 100) % 10;
85 ao_mutex_get(&ao_lco_display_mutex);
86 ao_seven_segment_set(AO_LCO_PAD_DIGIT, tenths);
87 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ones | 0x10);
88 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, tens);
89 ao_mutex_put(&ao_lco_display_mutex);
93 ao_lco_set_display(void)
95 if (ao_lco_pad == 0) {
96 ao_lco_set_voltage(ao_pad_query.battery);
98 ao_lco_set_pad(ao_lco_pad);
99 ao_lco_set_box(ao_lco_box);
103 #define SEVEN_SEGMENT_d ((0 << 0) | \
112 #define SEVEN_SEGMENT_r ((0 << 0) | \
121 ao_lco_set_display_drag(void)
123 ao_mutex_get(&ao_lco_display_mutex);
124 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_10, SEVEN_SEGMENT_d | 0x80);
125 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_1, SEVEN_SEGMENT_r | 0x80);
126 ao_mutex_put(&ao_lco_display_mutex);
127 ao_lco_set_pad(ao_lco_pad);
130 #define MASK_SIZE(n) (((n) + 7) >> 3)
131 #define MASK_ID(n) ((n) >> 3)
132 #define MASK_SHIFT(n) ((n) & 7)
134 static uint8_t ao_lco_box_mask[MASK_SIZE(AO_PAD_MAX_BOXES)];
137 ao_lco_box_present(uint8_t box)
139 if (box >= AO_PAD_MAX_BOXES)
141 return (ao_lco_box_mask[MASK_ID(box)] >> MASK_SHIFT(box)) & 1;
145 ao_lco_pad_present(uint8_t box, uint8_t pad)
147 /* voltage measurement is always valid */
150 if (!ao_lco_channels[box])
152 if (pad > AO_PAD_MAX_CHANNELS)
154 return (ao_lco_channels[box] >> (pad - 1)) & 1;
158 ao_lco_pad_first(uint8_t box)
162 for (pad = 1; pad <= AO_PAD_MAX_CHANNELS; pad++)
163 if (ao_lco_pad_present(box, pad))
168 static struct ao_task ao_lco_drag_task;
169 static uint8_t ao_lco_drag_active;
170 static uint8_t ao_lco_drag_beep_count;
171 static uint8_t ao_lco_drag_beep_on;
172 static uint16_t ao_lco_drag_beep_time;
173 static uint16_t ao_lco_drag_warn_time;
174 static uint16_t ao_lco_drag_display_time;
175 static uint8_t ao_lco_drag_display_on;
177 #define AO_LCO_DRAG_BEEP_TIME AO_MS_TO_TICKS(50)
178 #define AO_LCO_DRAG_WARN_TIME AO_SEC_TO_TICKS(5)
181 ao_lco_drag_beep_start(void)
183 ao_beep(AO_BEEP_HIGH);
184 PRINTD("beep start\n");
185 ao_lco_drag_beep_on = 1;
186 ao_lco_drag_beep_time = ao_time() + AO_LCO_DRAG_BEEP_TIME;
190 ao_lco_drag_beep_stop(void)
193 PRINTD("beep stop\n");
194 ao_lco_drag_beep_on = 0;
195 if (ao_lco_drag_beep_count) {
196 --ao_lco_drag_beep_count;
197 if (ao_lco_drag_beep_count)
198 ao_lco_drag_beep_time = ao_time() + AO_LCO_DRAG_BEEP_TIME;
203 ao_lco_drag_beep(uint8_t beeps)
205 PRINTD("beep %d\n", beeps);
206 if (!ao_lco_drag_beep_count)
207 ao_lco_drag_beep_start();
208 ao_lco_drag_beep_count += beeps;
212 ao_lco_drag_beep_check(uint16_t now, uint16_t delay)
214 PRINTD("beep check count %d delta %d\n",
215 ao_lco_drag_beep_count,
216 (int16_t) (now - ao_lco_drag_beep_time));
217 if (ao_lco_drag_beep_count) {
218 if ((int16_t) (now - ao_lco_drag_beep_time) >= 0) {
219 if (ao_lco_drag_beep_on)
220 ao_lco_drag_beep_stop();
222 ao_lco_drag_beep_start();
226 if (ao_lco_drag_beep_count) {
227 if (delay > AO_LCO_DRAG_BEEP_TIME)
228 delay = AO_LCO_DRAG_BEEP_TIME;
234 ao_lco_drag_enable(void)
236 PRINTD("Drag enable\n");
237 ao_lco_drag_race = 1;
238 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
240 ao_lco_set_display_drag();
241 ao_lco_fire_down = 0;
242 ao_lco_drag_display_on = 1;
243 ao_lco_drag_display_time = ao_time() + AO_SEC_TO_TICKS(5);
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_display_check(uint16_t now, uint16_t delay)
304 uint16_t display_delay;
306 if (ao_lco_drag_display_on) {
307 if ((int16_t) (now - ao_lco_drag_display_time) >= 0) {
308 ao_lco_drag_display_on = 0;
309 ao_lco_set_display();
311 display_delay = ao_lco_drag_display_time - now;
312 if (delay > display_delay)
313 delay = display_delay;
320 ao_lco_drag_monitor(void)
326 PRINTD("Drag monitor active %d delay %d\n", ao_lco_drag_active, delay);
327 if (delay == (uint16_t) ~0)
328 ao_sleep(&ao_lco_drag_active);
330 ao_sleep_for(&ao_lco_drag_active, delay);
333 if (!ao_lco_drag_active)
337 delay = ao_lco_drag_button_check(now, delay);
338 delay = ao_lco_drag_warn_check(now, delay);
339 delay = ao_lco_drag_beep_check(now, delay);
340 delay = ao_lco_drag_display_check(now, delay);
342 /* check to see if there's anything left to do here */
343 if (!ao_lco_fire_down && !ao_lco_drag_race && !ao_lco_drag_beep_count) {
345 ao_lco_drag_active = 0;
353 static struct ao_event event;
354 int8_t dir, new_box, new_pad;
356 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
358 ao_event_get(&event);
359 PRINTD("event type %d unit %d value %d\n",
360 event.type, event.unit, event.value);
361 switch (event.type) {
362 case AO_EVENT_QUADRATURE:
363 switch (event.unit) {
364 case AO_QUADRATURE_PAD:
366 dir = (int8_t) event.value;
367 new_pad = ao_lco_pad;
370 if (new_pad > AO_PAD_MAX_CHANNELS)
373 new_pad = AO_PAD_MAX_CHANNELS;
374 if (new_pad == ao_lco_pad)
376 } while (!ao_lco_pad_present(ao_lco_box, new_pad));
377 if (new_pad != ao_lco_pad) {
378 ao_lco_pad = new_pad;
379 ao_lco_set_display();
383 case AO_QUADRATURE_BOX:
385 dir = (int8_t) event.value;
386 new_box = ao_lco_box;
389 if (new_box > ao_lco_max_box)
390 new_box = ao_lco_min_box;
391 else if (new_box < ao_lco_min_box)
392 new_box = ao_lco_max_box;
393 if (new_box == ao_lco_box)
395 } while (!ao_lco_box_present(new_box));
396 if (ao_lco_box != new_box) {
397 ao_lco_box = new_box;
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);
421 } else if (ao_lco_pad != 0) {
422 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
423 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 ao_lco_fire_down = 1;
437 ao_lco_fire_tick = ao_time();
438 ao_lco_drag_active = 1;
439 /* ao_lco_fire_down will already be off if we just enabled drag mode */
440 if (ao_lco_fire_down && ao_lco_drag_race) {
441 if (ao_lco_pad != 0) {
442 ao_lco_selected[ao_lco_box] ^= (1 << (ao_lco_pad - 1));
443 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
444 ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
445 ao_lco_drag_beep(ao_lco_pad);
448 ao_wakeup(&ao_lco_drag_active);
450 ao_lco_fire_down = 0;
451 if (ao_lco_drag_active)
452 ao_wakeup(&ao_lco_drag_active);
462 static AO_LED_TYPE continuity_led[AO_LED_CONTINUITY_NUM] = {
463 #ifdef AO_LED_CONTINUITY_0
466 #ifdef AO_LED_CONTINUITY_1
469 #ifdef AO_LED_CONTINUITY_2
472 #ifdef AO_LED_CONTINUITY_3
475 #ifdef AO_LED_CONTINUITY_4
478 #ifdef AO_LED_CONTINUITY_5
481 #ifdef AO_LED_CONTINUITY_6
484 #ifdef AO_LED_CONTINUITY_7
490 ao_lco_get_channels(uint8_t box, struct ao_pad_query *query)
494 r = ao_lco_query(box, query, &ao_lco_tick_offset[box]);
495 if (r == AO_RADIO_CMAC_OK) {
496 ao_lco_channels[box] = query->channels;
497 ao_lco_valid[box] = 1;
499 ao_lco_valid[box] = 0;
500 PRINTD("ao_lco_get_channels(%d) rssi %d valid %d ret %d\n", box, ao_radio_cmac_rssi, ao_lco_valid[box], r);
501 ao_wakeup(&ao_pad_query);
502 return ao_lco_valid[box];
508 uint8_t previous_valid = ao_lco_valid[ao_lco_box];
510 if (ao_lco_get_channels(ao_lco_box, &ao_pad_query)) {
511 if (!previous_valid) {
513 ao_lco_pad = ao_lco_pad_first(ao_lco_box);
514 ao_lco_set_display();
517 ao_lco_set_display();
522 ao_lco_box_reset_present(void)
524 ao_lco_min_box = 0xff;
525 ao_lco_max_box = 0x00;
526 memset(ao_lco_box_mask, 0, sizeof (ao_lco_box_mask));
530 ao_lco_box_set_present(uint8_t box)
532 if (box < ao_lco_min_box)
533 ao_lco_min_box = box;
534 if (box > ao_lco_max_box)
535 ao_lco_max_box = box;
536 if (box >= AO_PAD_MAX_BOXES)
538 ao_lco_box_mask[MASK_ID(box)] |= 1 << MASK_SHIFT(box);
544 uint16_t tick_offset;
550 ao_lco_box_reset_present();
552 for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
555 for (try = 0; try < 3; try++) {
557 r = ao_lco_query(box, &ao_pad_query, &tick_offset);
558 PRINTD("box %d result %d\n", box, r);
559 if (r == AO_RADIO_CMAC_OK) {
561 ao_lco_box_set_present(box);
562 ao_lco_set_pad(boxes % 10);
563 ao_delay(AO_MS_TO_TICKS(30));
568 if (ao_lco_min_box <= ao_lco_max_box)
569 ao_lco_box = ao_lco_min_box;
571 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
572 memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
573 memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
575 ao_lco_set_display();
579 ao_lco_igniter_status(void)
585 ao_sleep(&ao_pad_query);
586 PRINTD("RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_valid[ao_lco_box]);
587 if (!ao_lco_valid[ao_lco_box]) {
588 ao_led_on(AO_LED_RED);
589 ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
592 if (ao_radio_cmac_rssi < -90) {
593 ao_led_on(AO_LED_AMBER);
594 ao_led_off(AO_LED_RED|AO_LED_GREEN);
596 ao_led_on(AO_LED_GREEN);
597 ao_led_off(AO_LED_RED|AO_LED_AMBER);
599 if (ao_pad_query.arm_status)
600 ao_led_on(AO_LED_REMOTE_ARM);
602 ao_led_off(AO_LED_REMOTE_ARM);
604 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
607 if (ao_lco_drag_race) {
608 if (ao_lco_selected[ao_lco_box] & (1 << c) && t)
609 ao_led_on(continuity_led[c]);
611 ao_led_off(continuity_led[c]);
613 if (ao_pad_query.channels & (1 << c))
614 status = ao_pad_query.igniter_status[c];
616 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
617 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
618 ao_led_on(continuity_led[c]);
620 ao_led_off(continuity_led[c]);
628 ao_lco_arm_warn(void)
631 while (!ao_lco_armed)
632 ao_sleep(&ao_lco_armed);
633 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
634 ao_delay(AO_MS_TO_TICKS(200));
638 static struct ao_task ao_lco_input_task;
639 static struct ao_task ao_lco_monitor_task;
640 static struct ao_task ao_lco_arm_warn_task;
641 static struct ao_task ao_lco_igniter_status_task;
650 ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
651 ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
652 ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
653 ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
655 PRINTD("monitor armed %d firing %d\n",
656 ao_lco_armed, ao_lco_firing);
658 if (ao_lco_armed && ao_lco_firing) {
663 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
664 if (ao_lco_selected[box]) {
665 PRINTD("Arming box %d pads %x\n",
666 box, ao_lco_selected[box]);
667 if (ao_lco_valid[box])
668 ao_lco_arm(box, ao_lco_selected[box], ao_lco_tick_offset[ao_lco_box]);
673 if (ao_lco_armed && ao_lco_firing)
674 delay = AO_MS_TO_TICKS(100);
676 delay = AO_SEC_TO_TICKS(1);
677 ao_sleep_for(&ao_lco_armed, delay);
683 ao_lco_set_debug(void)
686 if (ao_cmd_status == ao_cmd_success)
687 ao_lco_debug = ao_cmd_lex_i != 0;
690 __code struct ao_cmds ao_lco_cmds[] = {
691 { ao_lco_set_debug, "D <0 off, 1 on>\0Debug" },
692 { ao_lco_search, "s\0Search for pad boxes" },
700 ao_add_task(&ao_lco_monitor_task, ao_lco_monitor, "lco monitor");
702 ao_cmd_register(&ao_lco_cmds[0]);