f73fb43f3e3187461c55c3d042a0cf716116f956
[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 #define AO_LCO_VALID_LAST       1
38 #define AO_LCO_VALID_EVER       2
39
40 /* UI values */
41 static uint16_t ao_lco_fire_tick;
42 static uint8_t  ao_lco_fire_down;
43
44 #define AO_LCO_BOX_DRAG         0x1000
45
46 static uint8_t  ao_lco_display_mutex;
47
48 void
49 ao_lco_set_pad(uint8_t pad)
50 {
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);
54 }
55
56 #define SEVEN_SEGMENT_d         ((0 << 0) |     \
57                                  (0 << 1) |     \
58                                  (1 << 2) |     \
59                                  (1 << 3) |     \
60                                  (1 << 4) |     \
61                                  (1 << 5) |     \
62                                  (1 << 6))
63
64
65 #define SEVEN_SEGMENT_r         ((0 << 0) |     \
66                                  (0 << 1) |     \
67                                  (0 << 2) |     \
68                                  (1 << 3) |     \
69                                  (1 << 4) |     \
70                                  (0 << 5) |     \
71                                  (0 << 6))
72
73 void
74 ao_lco_set_box(uint16_t box)
75 {
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));
80         } else {
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));
83         }
84         ao_mutex_put(&ao_lco_display_mutex);
85 }
86
87 void
88 ao_lco_set_voltage(uint16_t decivolts)
89 {
90         uint8_t tens, ones, tenths;
91
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);
100 }
101
102 void
103 ao_lco_set_display(void)
104 {
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);
107         } else {
108                 if (ao_lco_box == AO_LCO_BOX_DRAG)
109                         ao_lco_set_pad(ao_lco_drag_race);
110                 else
111                         ao_lco_set_pad(ao_lco_pad);
112                 ao_lco_set_box(ao_lco_box);
113         }
114 }
115
116 uint8_t
117 ao_lco_box_present(uint16_t box)
118 {
119         if (box == AO_LCO_BOX_DRAG)
120                 return 1;
121
122         if (box >= AO_PAD_MAX_BOXES)
123                 return 0;
124         return (ao_lco_box_mask[AO_LCO_MASK_ID(box)] >> AO_LCO_MASK_SHIFT(box)) & 1;
125 }
126
127 static struct ao_task   ao_lco_drag_task;
128 static uint8_t          ao_lco_drag_active;
129
130 static uint16_t
131 ao_lco_drag_button_check(uint16_t now, uint16_t delay)
132 {
133         uint16_t        button_delay = ~0;
134
135         /*
136          * Check to see if the button has been held down long enough
137          * to switch in/out of drag race mode
138          */
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;
144                         }
145                         else
146                                 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_STOP_TIME - now;
147                 } else {
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;
151                         }
152                         else
153                                 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_START_TIME - now;
154                 }
155                 if (delay > button_delay)
156                         delay = button_delay;
157         }
158         return delay;
159 }
160
161 static void
162 ao_lco_drag_monitor(void)
163 {
164         uint16_t        delay = ~0;
165         uint16_t        now;
166
167         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
168         for (;;) {
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);
173                 else
174                         ao_sleep_for(&ao_lco_drag_beep_count, delay);
175
176                 delay = ~0;
177                 if (!ao_lco_drag_active)
178                         continue;
179
180                 now = ao_time();
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);
184
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) {
187                         delay = ~0;
188                         ao_lco_drag_active = 0;
189                 }
190         }
191 }
192
193 static void
194 ao_lco_step_box(int8_t dir)
195 {
196         int16_t new_box = ao_lco_box;
197         do {
198                 if (new_box == AO_LCO_BOX_DRAG) {
199                         if (dir < 0)
200                                 new_box = ao_lco_max_box;
201                         else
202                                 new_box = ao_lco_min_box;
203                 } else {
204                         new_box += dir;
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;
209                 }
210                 if (new_box == ao_lco_box)
211                         break;
212         } while (!ao_lco_box_present(new_box));
213         if (ao_lco_box != new_box) {
214                 ao_lco_box = new_box;
215                 ao_lco_pad = 1;
216                 if (ao_lco_box != AO_LCO_BOX_DRAG)
217                         ao_lco_channels[ao_lco_box] = 0;
218                 ao_lco_set_display();
219         }
220 }
221
222 static void
223 ao_lco_input(void)
224 {
225         static struct ao_event  event;
226
227         for (;;) {
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:
235                                 if (!ao_lco_armed)
236                                         ao_lco_step_pad((int8_t) event.value);
237                                 break;
238                         case AO_QUADRATURE_BOX:
239                                 if (!ao_lco_armed)
240                                         ao_lco_step_box((int8_t) event.value);
241                                 break;
242                         }
243                         break;
244                 case AO_EVENT_BUTTON:
245                         switch (event.unit) {
246                         case AO_BUTTON_ARM:
247                                 ao_lco_set_armed(event.value);
248                                 break;
249                         case AO_BUTTON_FIRE:
250                                 if (ao_lco_armed) {
251                                         ao_lco_fire_down = 0;
252                                         ao_lco_set_firing(event.value);
253                                 } else {
254                                         if (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);
260                                                 } else {
261                                                         ao_lco_toggle_drag();
262                                                 }
263                                         } else {
264                                                 ao_lco_fire_down = 0;
265                                                 if (ao_lco_drag_active)
266                                                         ao_wakeup(&ao_lco_drag_beep_count);
267                                         }
268                                 }
269                                 break;
270                         }
271                         break;
272                 }
273         }
274 }
275
276 /*
277  * Light up everything for a second at power on to let the user
278  * visually inspect the system for correct operation
279  */
280 static void
281 ao_lco_display_test()
282 {
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);
291 }
292
293 #if HAS_ADC_SINGLE
294 static void
295 ao_lco_batt_voltage(void)
296 {
297         struct ao_adc   packet;
298         int16_t         decivolt;
299
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));
304 }
305 #endif
306
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;
311
312 static void
313 ao_lco_main(void)
314 {
315         ao_lco_display_test();
316 #if HAS_ADC_SINGLE
317         ao_lco_batt_voltage();
318 #endif
319         ao_lco_search();
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");
324         ao_lco_monitor();
325 }
326
327 #if DEBUG
328 void
329 ao_lco_set_debug(void)
330 {
331         ao_cmd_decimal();
332         if (ao_cmd_status == ao_cmd_success)
333                 ao_lco_debug = ao_cmd_lex_i != 0;
334 }
335
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" },
339         { 0, NULL }
340 };
341 #endif
342
343 void
344 ao_lco_init(void)
345 {
346         ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
347 #if DEBUG
348         ao_cmd_register(&ao_lco_cmds[0]);
349 #endif
350 }