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_BOX_DRAG 0x1000
40 static AO_TICK_TYPE ao_lco_fire_tick;
41 static uint8_t ao_lco_fire_down;
43 static uint8_t ao_lco_display_mutex;
46 ao_lco_show_pad(uint8_t pad)
48 ao_mutex_get(&ao_lco_display_mutex);
49 ao_seven_segment_set(AO_LCO_PAD_DIGIT, pad | (ao_lco_drag_race << 4));
50 ao_mutex_put(&ao_lco_display_mutex);
53 #define SEVEN_SEGMENT_d ((0 << 0) | \
62 #define SEVEN_SEGMENT_r ((0 << 0) | \
71 ao_lco_show_box(uint16_t box)
73 ao_mutex_get(&ao_lco_display_mutex);
74 if (box == AO_LCO_BOX_DRAG) {
75 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_10, SEVEN_SEGMENT_d | (ao_lco_drag_race << 7));
76 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_1, SEVEN_SEGMENT_r | (ao_lco_drag_race << 7));
78 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, box % 10 | (ao_lco_drag_race << 4));
79 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, box / 10 | (ao_lco_drag_race << 4));
81 ao_mutex_put(&ao_lco_display_mutex);
85 ao_lco_show_voltage(uint16_t decivolts)
87 uint8_t tens, ones, tenths;
89 tenths = decivolts % 10;
90 ones = (decivolts / 10) % 10;
91 tens = (decivolts / 100) % 10;
92 ao_mutex_get(&ao_lco_display_mutex);
93 ao_seven_segment_set(AO_LCO_PAD_DIGIT, tenths);
94 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ones | 0x10);
95 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, tens);
96 ao_mutex_put(&ao_lco_display_mutex);
102 if (ao_lco_pad == AO_LCO_PAD_VOLTAGE && ao_lco_box != AO_LCO_BOX_DRAG) {
103 ao_lco_show_voltage(ao_pad_query.battery);
105 if (ao_lco_box == AO_LCO_BOX_DRAG)
106 ao_lco_show_pad(ao_lco_drag_race);
108 ao_lco_show_pad(ao_lco_pad);
109 ao_lco_show_box(ao_lco_box);
114 ao_lco_box_present(uint16_t box)
116 if (box == AO_LCO_BOX_DRAG)
119 if (box >= AO_PAD_MAX_BOXES)
121 return (ao_lco_box_mask[AO_LCO_MASK_ID(box)] >> AO_LCO_MASK_SHIFT(box)) & 1;
124 static struct ao_task ao_lco_drag_task;
125 static uint8_t ao_lco_drag_active;
128 ao_lco_drag_button_check(AO_TICK_TYPE now, AO_TICK_TYPE delay)
130 AO_TICK_TYPE button_delay = ~0;
133 * Check to see if the button has been held down long enough
134 * to switch in/out of drag race mode
136 if (ao_lco_fire_down) {
137 if (ao_lco_drag_race) {
138 if ((AO_TICK_SIGNED) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_STOP_TIME) {
139 ao_lco_drag_disable();
140 ao_lco_fire_down = 0;
143 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_STOP_TIME - now;
145 if ((AO_TICK_SIGNED) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_START_TIME) {
146 ao_lco_drag_enable();
147 ao_lco_fire_down = 0;
150 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_START_TIME - now;
152 if (delay > button_delay)
153 delay = button_delay;
159 ao_lco_drag_monitor(void)
161 AO_TICK_TYPE delay = ~0;
164 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
166 PRINTD("Drag monitor count %d active %d delay %d\n",
167 ao_lco_drag_beep_count, ao_lco_drag_active, delay);
168 if (delay == (AO_TICK_TYPE) ~0)
169 ao_sleep(&ao_lco_drag_beep_count);
171 ao_sleep_for(&ao_lco_drag_beep_count, delay);
174 if (!ao_lco_drag_active)
178 delay = ao_lco_drag_button_check(now, delay);
179 delay = ao_lco_drag_warn_check(now, delay);
180 delay = ao_lco_drag_beep_check(now, delay);
182 /* check to see if there's anything left to do here */
183 if (!ao_lco_fire_down && !ao_lco_drag_race && !ao_lco_drag_beep_count) {
185 ao_lco_drag_active = 0;
191 ao_lco_step_box(int8_t dir)
193 int16_t new_box = ao_lco_box;
195 if (new_box == AO_LCO_BOX_DRAG) {
197 new_box = ao_lco_max_box;
199 new_box = ao_lco_min_box;
202 if (new_box > ao_lco_max_box)
203 new_box = AO_LCO_BOX_DRAG;
204 else if (new_box < ao_lco_min_box)
205 new_box = AO_LCO_BOX_DRAG;
207 if (new_box == ao_lco_box)
209 } while (!ao_lco_box_present(new_box));
210 ao_lco_set_box(new_box);
216 static struct ao_event event;
219 ao_event_get(&event);
220 PRINTD("event type %d unit %d value %d\n",
221 event.type, event.unit, event.value);
222 switch (event.type) {
223 case AO_EVENT_QUADRATURE:
224 switch (event.unit) {
225 case AO_QUADRATURE_PAD:
227 ao_lco_step_pad((int8_t) event.value);
229 case AO_QUADRATURE_BOX:
231 ao_lco_step_box((int8_t) event.value);
235 case AO_EVENT_BUTTON:
236 switch (event.unit) {
238 ao_lco_set_armed(event.value);
242 ao_lco_fire_down = 0;
243 ao_lco_set_firing(event.value);
246 if (ao_lco_box == AO_LCO_BOX_DRAG) {
247 ao_lco_fire_down = 1;
248 ao_lco_fire_tick = ao_time();
249 ao_lco_drag_active = 1;
250 ao_wakeup(&ao_lco_drag_beep_count);
252 ao_lco_toggle_drag();
255 ao_lco_fire_down = 0;
256 if (ao_lco_drag_active)
257 ao_wakeup(&ao_lco_drag_beep_count);
268 * Light up everything for a second at power on to let the user
269 * visually inspect the system for correct operation
272 ao_lco_display_test(void)
274 ao_mutex_get(&ao_lco_display_mutex);
275 ao_seven_segment_set(AO_LCO_PAD_DIGIT, 8 | 0x10);
276 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, 8 | 0x10);
277 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, 8 | 0x10);
278 ao_mutex_put(&ao_lco_display_mutex);
279 ao_led_on(LEDS_AVAILABLE);
280 ao_delay(AO_MS_TO_TICKS(1000));
281 ao_led_off(LEDS_AVAILABLE);
286 ao_lco_batt_voltage(void)
288 struct ao_adc packet;
291 ao_adc_single_get(&packet);
292 decivolt = ao_battery_decivolt(packet.v_batt);
293 ao_lco_show_voltage(decivolt);
294 ao_delay(AO_MS_TO_TICKS(1000));
298 static struct ao_task ao_lco_input_task;
299 static struct ao_task ao_lco_monitor_task;
300 static struct ao_task ao_lco_arm_warn_task;
301 static struct ao_task ao_lco_igniter_status_task;
306 ao_lco_display_test();
308 ao_lco_batt_voltage();
311 ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
312 ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
313 ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
314 ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
320 ao_lco_set_debug(void)
322 uint16_t r = ao_cmd_decimal();
323 if (ao_cmd_status == ao_cmd_success)
324 ao_lco_debug = r != 0;
327 const struct ao_cmds ao_lco_cmds[] = {
328 { ao_lco_set_debug, "D <0 off, 1 on>\0Debug" },
329 { ao_lco_search, "s\0Search for pad boxes" },
337 ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
339 ao_cmd_register(&ao_lco_cmds[0]);