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>
30 #define AO_LCO_PAD_DIGIT 0
31 #define AO_LCO_BOX_DIGIT_1 1
32 #define AO_LCO_BOX_DIGIT_10 2
34 #define AO_LCO_DRAG_RACE_START_TIME AO_SEC_TO_TICKS(5)
35 #define AO_LCO_DRAG_RACE_STOP_TIME AO_SEC_TO_TICKS(2)
37 #define AO_LCO_VALID_LAST 1
38 #define AO_LCO_VALID_EVER 2
41 static uint16_t ao_lco_fire_tick;
42 static uint8_t ao_lco_fire_down;
44 static uint8_t ao_lco_display_mutex;
47 ao_lco_show_pad(uint8_t pad)
49 ao_mutex_get(&ao_lco_display_mutex);
50 ao_seven_segment_set(AO_LCO_PAD_DIGIT, pad | (ao_lco_drag_race << 4));
51 ao_mutex_put(&ao_lco_display_mutex);
54 #define SEVEN_SEGMENT_d ((0 << 0) | \
63 #define SEVEN_SEGMENT_r ((0 << 0) | \
72 ao_lco_show_box(uint16_t box)
74 ao_mutex_get(&ao_lco_display_mutex);
75 if (box == AO_LCO_BOX_DRAG) {
76 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_10, SEVEN_SEGMENT_d | (ao_lco_drag_race << 7));
77 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_1, SEVEN_SEGMENT_r | (ao_lco_drag_race << 7));
79 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, box % 10 | (ao_lco_drag_race << 4));
80 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, box / 10 | (ao_lco_drag_race << 4));
82 ao_mutex_put(&ao_lco_display_mutex);
86 ao_lco_show_voltage(uint16_t decivolts)
88 uint8_t tens, ones, tenths;
90 tenths = decivolts % 10;
91 ones = (decivolts / 10) % 10;
92 tens = (decivolts / 100) % 10;
93 ao_mutex_get(&ao_lco_display_mutex);
94 ao_seven_segment_set(AO_LCO_PAD_DIGIT, tenths);
95 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ones | 0x10);
96 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, tens);
97 ao_mutex_put(&ao_lco_display_mutex);
101 ao_lco_show_display(void)
103 if (ao_lco_pad == AO_LCO_PAD_VOLTAGE && ao_lco_box != AO_LCO_BOX_DRAG) {
104 ao_lco_show_voltage(ao_pad_query.battery);
106 if (ao_lco_box == AO_LCO_BOX_DRAG)
107 ao_lco_show_pad(ao_lco_drag_race);
109 ao_lco_show_pad(ao_lco_pad);
110 ao_lco_show_box(ao_lco_box);
115 ao_lco_box_present(uint16_t box)
117 if (box == AO_LCO_BOX_DRAG)
120 if (box >= AO_PAD_MAX_BOXES)
122 return (ao_lco_box_mask[AO_LCO_MASK_ID(box)] >> AO_LCO_MASK_SHIFT(box)) & 1;
125 static struct ao_task ao_lco_drag_task;
126 static uint8_t ao_lco_drag_active;
129 ao_lco_drag_button_check(uint16_t now, uint16_t delay)
131 uint16_t button_delay = ~0;
134 * Check to see if the button has been held down long enough
135 * to switch in/out of drag race mode
137 if (ao_lco_fire_down) {
138 if (ao_lco_drag_race) {
139 if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_STOP_TIME) {
140 ao_lco_drag_disable();
141 ao_lco_fire_down = 0;
144 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_STOP_TIME - now;
146 if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_START_TIME) {
147 ao_lco_drag_enable();
148 ao_lco_fire_down = 0;
151 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_START_TIME - now;
153 if (delay > button_delay)
154 delay = button_delay;
160 ao_lco_drag_monitor(void)
165 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
167 PRINTD("Drag monitor count %d active %d delay %d\n",
168 ao_lco_drag_beep_count, ao_lco_drag_active, delay);
169 if (delay == (uint16_t) ~0)
170 ao_sleep(&ao_lco_drag_beep_count);
172 ao_sleep_for(&ao_lco_drag_beep_count, delay);
175 if (!ao_lco_drag_active)
179 delay = ao_lco_drag_button_check(now, delay);
180 delay = ao_lco_drag_warn_check(now, delay);
181 delay = ao_lco_drag_beep_check(now, delay);
183 /* check to see if there's anything left to do here */
184 if (!ao_lco_fire_down && !ao_lco_drag_race && !ao_lco_drag_beep_count) {
186 ao_lco_drag_active = 0;
192 ao_lco_step_box(int8_t dir)
194 int16_t new_box = ao_lco_box;
196 if (new_box == AO_LCO_BOX_DRAG) {
198 new_box = ao_lco_max_box;
200 new_box = ao_lco_min_box;
203 if (new_box > ao_lco_max_box)
204 new_box = AO_LCO_BOX_DRAG;
205 else if (new_box < ao_lco_min_box)
206 new_box = AO_LCO_BOX_DRAG;
208 if (new_box == ao_lco_box)
210 } while (!ao_lco_box_present(new_box));
211 ao_lco_set_box(new_box);
217 static struct ao_event event;
220 ao_event_get(&event);
221 PRINTD("event type %d unit %d value %d\n",
222 event.type, event.unit, event.value);
223 switch (event.type) {
224 case AO_EVENT_QUADRATURE:
225 switch (event.unit) {
226 case AO_QUADRATURE_PAD:
228 ao_lco_step_pad((int8_t) event.value);
230 case AO_QUADRATURE_BOX:
232 ao_lco_step_box((int8_t) event.value);
236 case AO_EVENT_BUTTON:
237 switch (event.unit) {
239 ao_lco_set_armed(event.value);
243 ao_lco_fire_down = 0;
244 ao_lco_set_firing(event.value);
247 if (ao_lco_box == AO_LCO_BOX_DRAG) {
248 ao_lco_fire_down = 1;
249 ao_lco_fire_tick = ao_time();
250 ao_lco_drag_active = 1;
251 ao_wakeup(&ao_lco_drag_beep_count);
253 ao_lco_toggle_drag();
256 ao_lco_fire_down = 0;
257 if (ao_lco_drag_active)
258 ao_wakeup(&ao_lco_drag_beep_count);
269 * Light up everything for a second at power on to let the user
270 * visually inspect the system for correct operation
273 ao_lco_display_test()
275 ao_mutex_get(&ao_lco_display_mutex);
276 ao_seven_segment_set(AO_LCO_PAD_DIGIT, 8 | 0x10);
277 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, 8 | 0x10);
278 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, 8 | 0x10);
279 ao_mutex_put(&ao_lco_display_mutex);
280 ao_led_on(LEDS_AVAILABLE);
281 ao_delay(AO_MS_TO_TICKS(1000));
282 ao_led_off(LEDS_AVAILABLE);
287 ao_lco_batt_voltage(void)
289 struct ao_adc packet;
292 ao_adc_single_get(&packet);
293 decivolt = ao_battery_decivolt(packet.v_batt);
294 ao_lco_show_voltage(decivolt);
295 ao_delay(AO_MS_TO_TICKS(1000));
299 static struct ao_task ao_lco_input_task;
300 static struct ao_task ao_lco_monitor_task;
301 static struct ao_task ao_lco_arm_warn_task;
302 static struct ao_task ao_lco_igniter_status_task;
307 ao_lco_display_test();
309 ao_lco_batt_voltage();
312 ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
313 ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
314 ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
315 ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
321 ao_lco_set_debug(void)
324 if (ao_cmd_status == ao_cmd_success)
325 ao_lco_debug = ao_cmd_lex_i != 0;
328 __code struct ao_cmds ao_lco_cmds[] = {
329 { ao_lco_set_debug, "D <0 off, 1 on>\0Debug" },
330 { ao_lco_search, "s\0Search for pad boxes" },
338 ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
340 ao_cmd_register(&ao_lco_cmds[0]);