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_radio_cmac.h>
25 #include <ao_adc_single.h>
27 #define AO_LCO_PAD_DIGIT 0
28 #define AO_LCO_BOX_DIGIT_1 1
29 #define AO_LCO_BOX_DIGIT_10 2
31 #define AO_LCO_DRAG_RACE_START_TIME AO_SEC_TO_TICKS(5)
32 #define AO_LCO_DRAG_RACE_STOP_TIME AO_SEC_TO_TICKS(2)
35 static uint8_t ao_lco_select_mode;
36 static uint8_t ao_lco_event_debug;
38 #define PRINTE(...) do { if (!ao_lco_debug && !ao_lco_event_debug) break; printf ("\r%5lu %s: ", (unsigned long) ao_tick_count, __func__); printf(__VA_ARGS__); flush(); } while(0)
39 #define AO_LCO_SELECT_PAD 0
40 #define AO_LCO_SELECT_BOX 1
42 static uint8_t ao_lco_display_mutex;
45 ao_lco_show_pad(uint8_t pad)
47 ao_mutex_get(&ao_lco_display_mutex);
48 ao_seven_segment_set(AO_LCO_PAD_DIGIT, (uint8_t) (pad | (ao_lco_drag_race << 4)));
49 ao_mutex_put(&ao_lco_display_mutex);
52 #define SEVEN_SEGMENT_d ((0 << 0) | \
61 #define SEVEN_SEGMENT_r ((0 << 0) | \
70 ao_lco_show_box(uint16_t box)
72 ao_mutex_get(&ao_lco_display_mutex);
73 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, (uint8_t) (box % 10 | (ao_lco_drag_race << 4)));
74 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, (uint8_t) (box / 10 | (ao_lco_drag_race << 4)));
75 ao_mutex_put(&ao_lco_display_mutex);
79 ao_lco_show_voltage(uint16_t decivolts)
81 uint8_t tens, ones, tenths;
83 PRINTD("voltage %d\n", decivolts);
84 tenths = (uint8_t) (decivolts % 10);
85 ones = (uint8_t) ((decivolts / 10) % 10);
86 tens = (uint8_t) ((decivolts / 100) % 10);
87 ao_mutex_get(&ao_lco_display_mutex);
88 ao_seven_segment_set(AO_LCO_PAD_DIGIT, tenths);
89 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ones | 0x10);
90 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, tens);
91 ao_mutex_put(&ao_lco_display_mutex);
95 ao_lco_show_lco_voltage(void)
100 ao_adc_single_get(&packet);
101 decivolt = ao_battery_decivolt(packet.v_batt);
102 ao_lco_show_voltage((uint16_t) decivolt);
108 if (ao_lco_box == AO_LCO_LCO_VOLTAGE) {
109 ao_lco_show_lco_voltage();
110 } else if (ao_lco_pad == AO_LCO_PAD_VOLTAGE) {
111 ao_lco_show_voltage(ao_pad_query.battery);
113 ao_lco_show_pad(ao_lco_pad);
114 ao_lco_show_box(ao_lco_box);
119 ao_lco_set_select(void)
122 ao_led_off(AO_LED_PAD);
123 ao_led_off(AO_LED_BOX);
125 switch (ao_lco_select_mode) {
126 case AO_LCO_SELECT_PAD:
127 ao_led_off(AO_LED_BOX);
128 ao_led_on(AO_LED_PAD);
130 case AO_LCO_SELECT_BOX:
131 ao_led_off(AO_LED_PAD);
132 ao_led_on(AO_LED_BOX);
140 static struct ao_task ao_lco_drag_task;
143 ao_lco_drag_monitor(void)
145 AO_TICK_TYPE delay = ~0UL;
148 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
150 PRINTD("Drag monitor count %d delay %lu\n", ao_lco_drag_beep_count, (unsigned long) delay);
151 if (delay == (AO_TICK_TYPE) ~0)
152 ao_sleep(&ao_lco_drag_beep_count);
154 ao_sleep_for(&ao_lco_drag_beep_count, delay);
158 delay = ao_lco_drag_warn_check(now, delay);
159 delay = ao_lco_drag_beep_check(now, delay);
166 static struct ao_event event;
169 ao_event_get(&event);
170 PRINTE("event type %d unit %d value %ld\n",
171 event.type, event.unit, (long) event.value);
172 switch (event.type) {
173 case AO_EVENT_QUADRATURE:
174 switch (event.unit) {
175 case AO_QUADRATURE_SELECT:
177 switch (ao_lco_select_mode) {
178 case AO_LCO_SELECT_PAD:
179 ao_lco_step_pad((int8_t) event.value);
181 case AO_LCO_SELECT_BOX:
182 ao_lco_step_box((int8_t) event.value);
191 case AO_EVENT_BUTTON:
192 switch (event.unit) {
194 ao_lco_set_armed((uint8_t) event.value);
199 ao_lco_set_firing((uint8_t) event.value);
201 case AO_BUTTON_DRAG_SELECT:
203 ao_lco_toggle_drag();
205 case AO_BUTTON_DRAG_MODE:
207 ao_lco_drag_enable();
209 ao_lco_drag_disable();
211 case AO_BUTTON_ENCODER_SELECT:
214 ao_lco_select_mode = 1 - ao_lco_select_mode;
226 * Light up everything for a second at power on to let the user
227 * visually inspect the system for correct operation
230 ao_lco_display_test(void)
232 ao_mutex_get(&ao_lco_display_mutex);
233 ao_seven_segment_set(AO_LCO_PAD_DIGIT, 8 | 0x10);
234 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, 8 | 0x10);
235 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, 8 | 0x10);
236 ao_mutex_put(&ao_lco_display_mutex);
237 ao_led_on(AO_LEDS_AVAILABLE);
238 ao_delay(AO_MS_TO_TICKS(1000));
239 ao_led_off(AO_LEDS_AVAILABLE);
243 ao_lco_batt_voltage(void)
245 ao_lco_show_lco_voltage();
246 ao_delay(AO_MS_TO_TICKS(1000));
249 static struct ao_task ao_lco_input_task;
250 static struct ao_task ao_lco_monitor_task;
251 static struct ao_task ao_lco_arm_warn_task;
252 static struct ao_task ao_lco_igniter_status_task;
257 ao_lco_display_test();
258 ao_lco_batt_voltage();
260 ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
261 ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
262 ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
263 ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
269 ao_lco_set_debug(void)
271 uint32_t r = ao_cmd_decimal();
272 if (ao_cmd_status == ao_cmd_success){
273 ao_lco_debug = r & 1;
274 ao_lco_event_debug = (r & 2) >> 1;
278 const struct ao_cmds ao_lco_cmds[] = {
279 { ao_lco_set_debug, "D <0 off, 1 on>\0Debug" },
280 { ao_lco_search, "s\0Search for pad boxes" },
281 { ao_lco_pretend, "p\0Pretend there are lots of pad boxes" },
289 ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
291 ao_cmd_register(&ao_lco_cmds[0]);