first cut at turnon scripts for EasyTimer v2
[fw/altos] / src / drivers / ao_lco.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <ao.h>
20 #include <ao_lco.h>
21 #include <ao_event.h>
22 #include <ao_seven_segment.h>
23 #include <ao_quadrature.h>
24 #include <ao_lco_func.h>
25 #include <ao_radio_cmac.h>
26 #if HAS_ADC_SINGLE
27 #include <ao_adc_single.h>
28 #endif
29
30 #define AO_LCO_PAD_DIGIT        0
31 #define AO_LCO_BOX_DIGIT_1      1
32 #define AO_LCO_BOX_DIGIT_10     2
33
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)
36
37 /* UI values */
38 static AO_TICK_TYPE     ao_lco_fire_tick;
39 static uint8_t  ao_lco_fire_down;
40
41 static uint8_t  ao_lco_display_mutex;
42
43 void
44 ao_lco_show_pad(int8_t pad)
45 {
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);
49 }
50
51 #define SEVEN_SEGMENT_d         ((0 << 0) |     \
52                                  (0 << 1) |     \
53                                  (1 << 2) |     \
54                                  (1 << 3) |     \
55                                  (1 << 4) |     \
56                                  (1 << 5) |     \
57                                  (1 << 6))
58
59
60 #define SEVEN_SEGMENT_r         ((0 << 0) |     \
61                                  (0 << 1) |     \
62                                  (0 << 2) |     \
63                                  (1 << 3) |     \
64                                  (1 << 4) |     \
65                                  (0 << 5) |     \
66                                  (0 << 6))
67
68 void
69 ao_lco_show_box(int16_t box)
70 {
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)));
75         } else {
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)));
78         }
79         ao_mutex_put(&ao_lco_display_mutex);
80 }
81
82 static void
83 ao_lco_show_voltage(uint16_t decivolts)
84 {
85         uint8_t tens, ones, tenths;
86
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);
95 }
96
97 void
98 ao_lco_show(void)
99 {
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);
102         } else {
103                 if (ao_lco_box == AO_LCO_BOX_DRAG)
104                         ao_lco_show_pad((int8_t) ao_lco_drag_race);
105                 else
106                         ao_lco_show_pad(ao_lco_pad);
107                 ao_lco_show_box(ao_lco_box);
108         }
109 }
110
111 static struct ao_task   ao_lco_drag_task;
112 static uint8_t          ao_lco_drag_active;
113
114 static AO_TICK_TYPE
115 ao_lco_drag_button_check(AO_TICK_TYPE now, AO_TICK_TYPE delay)
116 {
117         AO_TICK_TYPE    button_delay = ~0UL;
118
119         /*
120          * Check to see if the button has been held down long enough
121          * to switch in/out of drag race mode
122          */
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;
128                         }
129                         else
130                                 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_STOP_TIME - now;
131                 } else {
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;
135                         }
136                         else
137                                 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_START_TIME - now;
138                 }
139                 if (delay > button_delay)
140                         delay = button_delay;
141         }
142         return delay;
143 }
144
145 static void
146 ao_lco_drag_monitor(void)
147 {
148         AO_TICK_TYPE    delay = ~0UL;
149         AO_TICK_TYPE    now;
150
151         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
152         for (;;) {
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);
157                 else
158                         ao_sleep_for(&ao_lco_drag_beep_count, delay);
159
160                 delay = ~0UL;
161                 if (!ao_lco_drag_active)
162                         continue;
163
164                 now = ao_time();
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);
168
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) {
171                         delay = ~0UL;
172                         ao_lco_drag_active = 0;
173                 }
174         }
175 }
176
177 static void
178 ao_lco_input(void)
179 {
180         static struct ao_event  event;
181
182         for (;;) {
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:
190                                 if (!ao_lco_armed)
191                                         ao_lco_step_pad((int8_t) event.value);
192                                 break;
193                         case AO_QUADRATURE_BOX:
194                                 if (!ao_lco_armed)
195                                         ao_lco_step_box((int8_t) event.value);
196                                 break;
197                         }
198                         break;
199                 case AO_EVENT_BUTTON:
200                         switch (event.unit) {
201                         case AO_BUTTON_ARM:
202                                 ao_lco_set_armed((uint8_t) event.value);
203                                 break;
204                         case AO_BUTTON_FIRE:
205                                 if (ao_lco_armed) {
206                                         ao_lco_fire_down = 0;
207                                         ao_lco_set_firing((uint8_t) event.value);
208                                 } else {
209                                         if (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);
215                                                 } else {
216                                                         ao_lco_toggle_drag();
217                                                 }
218                                         } else {
219                                                 ao_lco_fire_down = 0;
220                                                 if (ao_lco_drag_active)
221                                                         ao_wakeup(&ao_lco_drag_beep_count);
222                                         }
223                                 }
224                                 break;
225                         }
226                         break;
227                 }
228         }
229 }
230
231 /*
232  * Light up everything for a second at power on to let the user
233  * visually inspect the system for correct operation
234  */
235 static void
236 ao_lco_display_test(void)
237 {
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);
246 }
247
248 #if HAS_ADC_SINGLE
249 static void
250 ao_lco_batt_voltage(void)
251 {
252         struct ao_adc   packet;
253         int16_t         decivolt;
254
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));
259 }
260 #endif
261
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;
266
267 static void
268 ao_lco_main(void)
269 {
270         ao_lco_display_test();
271 #if HAS_ADC_SINGLE
272         ao_lco_batt_voltage();
273 #endif
274         ao_lco_search();
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");
279         ao_lco_monitor();
280 }
281
282 #if DEBUG
283 static void
284 ao_lco_set_debug(void)
285 {
286         uint32_t r  = ao_cmd_decimal();
287         if (ao_cmd_status == ao_cmd_success)
288                 ao_lco_debug = r != 0;
289 }
290
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" },
294         { 0, NULL }
295 };
296 #endif
297
298 void
299 ao_lco_init(void)
300 {
301         ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
302 #if DEBUG
303         ao_cmd_register(&ao_lco_cmds[0]);
304 #endif
305 }