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 #define AO_LCO_BOX_DRAG 0x1000
46 static uint8_t ao_lco_display_mutex;
49 ao_lco_set_pad(uint8_t pad)
51 ao_mutex_get(&ao_lco_display_mutex);
52 ao_seven_segment_set(AO_LCO_PAD_DIGIT, pad | (ao_lco_drag_race << 4));
53 ao_mutex_put(&ao_lco_display_mutex);
56 #define SEVEN_SEGMENT_d ((0 << 0) | \
65 #define SEVEN_SEGMENT_r ((0 << 0) | \
74 ao_lco_set_box(uint16_t box)
76 ao_mutex_get(&ao_lco_display_mutex);
77 if (box == AO_LCO_BOX_DRAG) {
78 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_10, SEVEN_SEGMENT_d | (ao_lco_drag_race << 7));
79 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_1, SEVEN_SEGMENT_r | (ao_lco_drag_race << 7));
81 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, box % 10 | (ao_lco_drag_race << 4));
82 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, box / 10 | (ao_lco_drag_race << 4));
84 ao_mutex_put(&ao_lco_display_mutex);
88 ao_lco_set_voltage(uint16_t decivolts)
90 uint8_t tens, ones, tenths;
92 tenths = decivolts % 10;
93 ones = (decivolts / 10) % 10;
94 tens = (decivolts / 100) % 10;
95 ao_mutex_get(&ao_lco_display_mutex);
96 ao_seven_segment_set(AO_LCO_PAD_DIGIT, tenths);
97 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ones | 0x10);
98 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, tens);
99 ao_mutex_put(&ao_lco_display_mutex);
103 ao_lco_set_display(void)
105 if (ao_lco_pad == AO_LCO_PAD_VOLTAGE && ao_lco_box != AO_LCO_BOX_DRAG) {
106 ao_lco_set_voltage(ao_pad_query.battery);
108 if (ao_lco_box == AO_LCO_BOX_DRAG)
109 ao_lco_set_pad(ao_lco_drag_race);
111 ao_lco_set_pad(ao_lco_pad);
112 ao_lco_set_box(ao_lco_box);
117 ao_lco_box_present(uint16_t box)
119 if (box == AO_LCO_BOX_DRAG)
122 if (box >= AO_PAD_MAX_BOXES)
124 return (ao_lco_box_mask[AO_LCO_MASK_ID(box)] >> AO_LCO_MASK_SHIFT(box)) & 1;
127 static struct ao_task ao_lco_drag_task;
128 static uint8_t ao_lco_drag_active;
131 ao_lco_drag_button_check(uint16_t now, uint16_t delay)
133 uint16_t button_delay = ~0;
136 * Check to see if the button has been held down long enough
137 * to switch in/out of drag race mode
139 if (ao_lco_fire_down) {
140 if (ao_lco_drag_race) {
141 if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_STOP_TIME) {
142 ao_lco_drag_disable();
143 ao_lco_fire_down = 0;
146 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_STOP_TIME - now;
148 if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_START_TIME) {
149 ao_lco_drag_enable();
150 ao_lco_fire_down = 0;
153 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_START_TIME - now;
155 if (delay > button_delay)
156 delay = button_delay;
162 ao_lco_drag_monitor(void)
167 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
169 PRINTD("Drag monitor count %d active %d delay %d\n",
170 ao_lco_drag_beep_count, ao_lco_drag_active, delay);
171 if (delay == (uint16_t) ~0)
172 ao_sleep(&ao_lco_drag_beep_count);
174 ao_sleep_for(&ao_lco_drag_beep_count, delay);
177 if (!ao_lco_drag_active)
181 delay = ao_lco_drag_button_check(now, delay);
182 delay = ao_lco_drag_warn_check(now, delay);
183 delay = ao_lco_drag_beep_check(now, delay);
185 /* check to see if there's anything left to do here */
186 if (!ao_lco_fire_down && !ao_lco_drag_race && !ao_lco_drag_beep_count) {
188 ao_lco_drag_active = 0;
194 ao_lco_step_box(int8_t dir)
196 int16_t new_box = ao_lco_box;
198 if (new_box == AO_LCO_BOX_DRAG) {
200 new_box = ao_lco_max_box;
202 new_box = ao_lco_min_box;
205 if (new_box > ao_lco_max_box)
206 new_box = AO_LCO_BOX_DRAG;
207 else if (new_box < ao_lco_min_box)
208 new_box = AO_LCO_BOX_DRAG;
210 if (new_box == ao_lco_box)
212 } while (!ao_lco_box_present(new_box));
213 if (ao_lco_box != new_box) {
214 ao_lco_box = new_box;
216 if (ao_lco_box != AO_LCO_BOX_DRAG)
217 ao_lco_channels[ao_lco_box] = 0;
218 ao_lco_set_display();
225 static struct ao_event event;
228 ao_event_get(&event);
229 PRINTD("event type %d unit %d value %d\n",
230 event.type, event.unit, event.value);
231 switch (event.type) {
232 case AO_EVENT_QUADRATURE:
233 switch (event.unit) {
234 case AO_QUADRATURE_PAD:
236 ao_lco_step_pad((int8_t) event.value);
238 case AO_QUADRATURE_BOX:
240 ao_lco_step_box((int8_t) event.value);
244 case AO_EVENT_BUTTON:
245 switch (event.unit) {
247 ao_lco_set_armed(event.value);
251 ao_lco_fire_down = 0;
252 ao_lco_set_firing(event.value);
255 if (ao_lco_box == AO_LCO_BOX_DRAG) {
256 ao_lco_fire_down = 1;
257 ao_lco_fire_tick = ao_time();
258 ao_lco_drag_active = 1;
259 ao_wakeup(&ao_lco_drag_beep_count);
261 ao_lco_toggle_drag();
264 ao_lco_fire_down = 0;
265 if (ao_lco_drag_active)
266 ao_wakeup(&ao_lco_drag_beep_count);
277 * Light up everything for a second at power on to let the user
278 * visually inspect the system for correct operation
281 ao_lco_display_test()
283 ao_mutex_get(&ao_lco_display_mutex);
284 ao_seven_segment_set(AO_LCO_PAD_DIGIT, 8 | 0x10);
285 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, 8 | 0x10);
286 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, 8 | 0x10);
287 ao_mutex_put(&ao_lco_display_mutex);
288 ao_led_on(LEDS_AVAILABLE);
289 ao_delay(AO_MS_TO_TICKS(1000));
290 ao_led_off(LEDS_AVAILABLE);
295 ao_lco_batt_voltage(void)
297 struct ao_adc packet;
300 ao_adc_single_get(&packet);
301 decivolt = ao_battery_decivolt(packet.v_batt);
302 ao_lco_set_voltage(decivolt);
303 ao_delay(AO_MS_TO_TICKS(1000));
307 static struct ao_task ao_lco_input_task;
308 static struct ao_task ao_lco_monitor_task;
309 static struct ao_task ao_lco_arm_warn_task;
310 static struct ao_task ao_lco_igniter_status_task;
315 ao_lco_display_test();
317 ao_lco_batt_voltage();
320 ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
321 ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
322 ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
323 ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
329 ao_lco_set_debug(void)
332 if (ao_cmd_status == ao_cmd_success)
333 ao_lco_debug = ao_cmd_lex_i != 0;
336 __code struct ao_cmds ao_lco_cmds[] = {
337 { ao_lco_set_debug, "D <0 off, 1 on>\0Debug" },
338 { ao_lco_search, "s\0Search for pad boxes" },
346 ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
348 ao_cmd_register(&ao_lco_cmds[0]);