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)
38 static AO_TICK_TYPE ao_lco_fire_tick;
39 static uint8_t ao_lco_fire_down;
41 static uint8_t ao_lco_display_mutex;
44 ao_lco_show_pad(uint8_t pad)
46 ao_mutex_get(&ao_lco_display_mutex);
47 ao_seven_segment_set(AO_LCO_PAD_DIGIT, (uint8_t) (pad | (ao_lco_drag_race << 4)));
48 ao_mutex_put(&ao_lco_display_mutex);
51 #define SEVEN_SEGMENT_d ((0 << 0) | \
60 #define SEVEN_SEGMENT_r ((0 << 0) | \
69 ao_lco_show_box(int16_t box)
71 ao_mutex_get(&ao_lco_display_mutex);
72 if (box == AO_LCO_BOX_DRAG) {
73 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_10, (uint8_t) (SEVEN_SEGMENT_d | (ao_lco_drag_race << 7)));
74 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_1, (uint8_t) (SEVEN_SEGMENT_r | (ao_lco_drag_race << 7)));
76 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, (uint8_t) (box % 10 | (ao_lco_drag_race << 4)));
77 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, (uint8_t) (box / 10 | (ao_lco_drag_race << 4)));
79 ao_mutex_put(&ao_lco_display_mutex);
83 ao_lco_show_voltage(uint16_t decivolts)
85 uint8_t tens, ones, tenths;
87 tenths = (uint8_t) (decivolts % 10);
88 ones = (uint8_t) ((decivolts / 10) % 10);
89 tens = (uint8_t) ((decivolts / 100) % 10);
90 ao_mutex_get(&ao_lco_display_mutex);
91 ao_seven_segment_set(AO_LCO_PAD_DIGIT, tenths);
92 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ones | 0x10);
93 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, tens);
94 ao_mutex_put(&ao_lco_display_mutex);
100 if (ao_lco_pad == AO_LCO_PAD_VOLTAGE && ao_lco_box != AO_LCO_BOX_DRAG) {
101 ao_lco_show_voltage(ao_pad_query.battery);
103 if (ao_lco_box == AO_LCO_BOX_DRAG)
104 ao_lco_show_pad(ao_lco_drag_race);
106 ao_lco_show_pad(ao_lco_pad);
107 ao_lco_show_box(ao_lco_box);
111 static struct ao_task ao_lco_drag_task;
112 static uint8_t ao_lco_drag_active;
115 ao_lco_drag_button_check(AO_TICK_TYPE now, AO_TICK_TYPE delay)
117 AO_TICK_TYPE button_delay = ~0UL;
120 * Check to see if the button has been held down long enough
121 * to switch in/out of drag race mode
123 if (ao_lco_fire_down) {
124 if (ao_lco_drag_race) {
125 if ((AO_TICK_SIGNED) (now - ao_lco_fire_tick) >= (AO_TICK_SIGNED) AO_LCO_DRAG_RACE_STOP_TIME) {
126 ao_lco_drag_disable();
127 ao_lco_fire_down = 0;
130 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_STOP_TIME - now;
132 if ((AO_TICK_SIGNED) (now - ao_lco_fire_tick) >= (AO_TICK_SIGNED) AO_LCO_DRAG_RACE_START_TIME) {
133 ao_lco_drag_enable();
134 ao_lco_fire_down = 0;
137 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_START_TIME - now;
139 if (delay > button_delay)
140 delay = button_delay;
146 ao_lco_drag_monitor(void)
148 AO_TICK_TYPE delay = ~0UL;
151 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
153 PRINTD("Drag monitor count %d active %d delay %lu\n",
154 ao_lco_drag_beep_count, ao_lco_drag_active, (unsigned long) delay);
155 if (delay == (AO_TICK_TYPE) ~0)
156 ao_sleep(&ao_lco_drag_beep_count);
158 ao_sleep_for(&ao_lco_drag_beep_count, delay);
161 if (!ao_lco_drag_active)
165 delay = ao_lco_drag_button_check(now, delay);
166 delay = ao_lco_drag_warn_check(now, delay);
167 delay = ao_lco_drag_beep_check(now, delay);
169 /* check to see if there's anything left to do here */
170 if (!ao_lco_fire_down && !ao_lco_drag_race && !ao_lco_drag_beep_count) {
172 ao_lco_drag_active = 0;
180 static struct ao_event event;
183 ao_event_get(&event);
184 PRINTD("event type %d unit %d value %ld\n",
185 event.type, event.unit, (long) event.value);
186 switch (event.type) {
187 case AO_EVENT_QUADRATURE:
188 switch (event.unit) {
189 case AO_QUADRATURE_PAD:
191 ao_lco_step_pad((int8_t) event.value);
193 case AO_QUADRATURE_BOX:
195 ao_lco_step_box((int8_t) event.value);
199 case AO_EVENT_BUTTON:
200 switch (event.unit) {
202 ao_lco_set_armed((uint8_t) event.value);
206 ao_lco_fire_down = 0;
207 ao_lco_set_firing((uint8_t) event.value);
210 if (ao_lco_box == AO_LCO_BOX_DRAG) {
211 ao_lco_fire_down = 1;
212 ao_lco_fire_tick = ao_time();
213 ao_lco_drag_active = 1;
214 ao_wakeup(&ao_lco_drag_beep_count);
216 ao_lco_toggle_drag();
219 ao_lco_fire_down = 0;
220 if (ao_lco_drag_active)
221 ao_wakeup(&ao_lco_drag_beep_count);
232 * Light up everything for a second at power on to let the user
233 * visually inspect the system for correct operation
236 ao_lco_display_test(void)
238 ao_mutex_get(&ao_lco_display_mutex);
239 ao_seven_segment_set(AO_LCO_PAD_DIGIT, 8 | 0x10);
240 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, 8 | 0x10);
241 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, 8 | 0x10);
242 ao_mutex_put(&ao_lco_display_mutex);
243 ao_led_on(LEDS_AVAILABLE);
244 ao_delay(AO_MS_TO_TICKS(1000));
245 ao_led_off(LEDS_AVAILABLE);
250 ao_lco_batt_voltage(void)
252 struct ao_adc packet;
255 ao_adc_single_get(&packet);
256 decivolt = ao_battery_decivolt(packet.v_batt);
257 ao_lco_show_voltage((uint16_t) decivolt);
258 ao_delay(AO_MS_TO_TICKS(1000));
262 static struct ao_task ao_lco_input_task;
263 static struct ao_task ao_lco_monitor_task;
264 static struct ao_task ao_lco_arm_warn_task;
265 static struct ao_task ao_lco_igniter_status_task;
270 ao_lco_display_test();
272 ao_lco_batt_voltage();
275 ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
276 ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
277 ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
278 ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
284 ao_lco_set_debug(void)
286 uint32_t r = ao_cmd_decimal();
287 if (ao_cmd_status == ao_cmd_success)
288 ao_lco_debug = r != 0;
291 const struct ao_cmds ao_lco_cmds[] = {
292 { ao_lco_set_debug, "D <0 off, 1 on>\0Debug" },
293 { ao_lco_search, "s\0Search for pad boxes" },
301 ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
303 ao_cmd_register(&ao_lco_cmds[0]);