altos: Remove 8051 address space specifiers
[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_BOX_DRAG         0x1000
38
39 /* UI values */
40 static uint16_t ao_lco_fire_tick;
41 static uint8_t  ao_lco_fire_down;
42
43 static uint8_t  ao_lco_display_mutex;
44
45 void
46 ao_lco_show_pad(uint8_t pad)
47 {
48         ao_mutex_get(&ao_lco_display_mutex);
49         ao_seven_segment_set(AO_LCO_PAD_DIGIT, pad | (ao_lco_drag_race << 4));
50         ao_mutex_put(&ao_lco_display_mutex);
51 }
52
53 #define SEVEN_SEGMENT_d         ((0 << 0) |     \
54                                  (0 << 1) |     \
55                                  (1 << 2) |     \
56                                  (1 << 3) |     \
57                                  (1 << 4) |     \
58                                  (1 << 5) |     \
59                                  (1 << 6))
60
61
62 #define SEVEN_SEGMENT_r         ((0 << 0) |     \
63                                  (0 << 1) |     \
64                                  (0 << 2) |     \
65                                  (1 << 3) |     \
66                                  (1 << 4) |     \
67                                  (0 << 5) |     \
68                                  (0 << 6))
69
70 void
71 ao_lco_show_box(uint16_t box)
72 {
73         ao_mutex_get(&ao_lco_display_mutex);
74         if (box == AO_LCO_BOX_DRAG) {
75                 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_10, SEVEN_SEGMENT_d | (ao_lco_drag_race << 7));
76                 ao_seven_segment_direct(AO_LCO_BOX_DIGIT_1, SEVEN_SEGMENT_r | (ao_lco_drag_race << 7));
77         } else {
78                 ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, box % 10 | (ao_lco_drag_race << 4));
79                 ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, box / 10 | (ao_lco_drag_race << 4));
80         }
81         ao_mutex_put(&ao_lco_display_mutex);
82 }
83
84 static void
85 ao_lco_show_voltage(uint16_t decivolts)
86 {
87         uint8_t tens, ones, tenths;
88
89         tenths = decivolts % 10;
90         ones = (decivolts / 10) % 10;
91         tens = (decivolts / 100) % 10;
92         ao_mutex_get(&ao_lco_display_mutex);
93         ao_seven_segment_set(AO_LCO_PAD_DIGIT, tenths);
94         ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, ones | 0x10);
95         ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, tens);
96         ao_mutex_put(&ao_lco_display_mutex);
97 }
98
99 void
100 ao_lco_show(void)
101 {
102         if (ao_lco_pad == AO_LCO_PAD_VOLTAGE && ao_lco_box != AO_LCO_BOX_DRAG) {
103                 ao_lco_show_voltage(ao_pad_query.battery);
104         } else {
105                 if (ao_lco_box == AO_LCO_BOX_DRAG)
106                         ao_lco_show_pad(ao_lco_drag_race);
107                 else
108                         ao_lco_show_pad(ao_lco_pad);
109                 ao_lco_show_box(ao_lco_box);
110         }
111 }
112
113 uint8_t
114 ao_lco_box_present(uint16_t box)
115 {
116         if (box == AO_LCO_BOX_DRAG)
117                 return 1;
118
119         if (box >= AO_PAD_MAX_BOXES)
120                 return 0;
121         return (ao_lco_box_mask[AO_LCO_MASK_ID(box)] >> AO_LCO_MASK_SHIFT(box)) & 1;
122 }
123
124 static struct ao_task   ao_lco_drag_task;
125 static uint8_t          ao_lco_drag_active;
126
127 static uint16_t
128 ao_lco_drag_button_check(uint16_t now, uint16_t delay)
129 {
130         uint16_t        button_delay = ~0;
131
132         /*
133          * Check to see if the button has been held down long enough
134          * to switch in/out of drag race mode
135          */
136         if (ao_lco_fire_down) {
137                 if (ao_lco_drag_race) {
138                         if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_STOP_TIME) {
139                                 ao_lco_drag_disable();
140                                 ao_lco_fire_down = 0;
141                         }
142                         else
143                                 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_STOP_TIME - now;
144                 } else {
145                         if ((int16_t) (now - ao_lco_fire_tick) >= AO_LCO_DRAG_RACE_START_TIME) {
146                                 ao_lco_drag_enable();
147                                 ao_lco_fire_down = 0;
148                         }
149                         else
150                                 button_delay = ao_lco_fire_tick + AO_LCO_DRAG_RACE_START_TIME - now;
151                 }
152                 if (delay > button_delay)
153                         delay = button_delay;
154         }
155         return delay;
156 }
157
158 static void
159 ao_lco_drag_monitor(void)
160 {
161         uint16_t        delay = ~0;
162         uint16_t        now;
163
164         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
165         for (;;) {
166                 PRINTD("Drag monitor count %d active %d delay %d\n",
167                        ao_lco_drag_beep_count, ao_lco_drag_active, delay);
168                 if (delay == (uint16_t) ~0)
169                         ao_sleep(&ao_lco_drag_beep_count);
170                 else
171                         ao_sleep_for(&ao_lco_drag_beep_count, delay);
172
173                 delay = ~0;
174                 if (!ao_lco_drag_active)
175                         continue;
176
177                 now = ao_time();
178                 delay = ao_lco_drag_button_check(now, delay);
179                 delay = ao_lco_drag_warn_check(now, delay);
180                 delay = ao_lco_drag_beep_check(now, delay);
181
182                 /* check to see if there's anything left to do here */
183                 if (!ao_lco_fire_down && !ao_lco_drag_race && !ao_lco_drag_beep_count) {
184                         delay = ~0;
185                         ao_lco_drag_active = 0;
186                 }
187         }
188 }
189
190 static void
191 ao_lco_step_box(int8_t dir)
192 {
193         int16_t new_box = ao_lco_box;
194         do {
195                 if (new_box == AO_LCO_BOX_DRAG) {
196                         if (dir < 0)
197                                 new_box = ao_lco_max_box;
198                         else
199                                 new_box = ao_lco_min_box;
200                 } else {
201                         new_box += dir;
202                         if (new_box > ao_lco_max_box)
203                                 new_box = AO_LCO_BOX_DRAG;
204                         else if (new_box < ao_lco_min_box)
205                                 new_box = AO_LCO_BOX_DRAG;
206                 }
207                 if (new_box == ao_lco_box)
208                         break;
209         } while (!ao_lco_box_present(new_box));
210         ao_lco_set_box(new_box);
211 }
212
213 static void
214 ao_lco_input(void)
215 {
216         static struct ao_event  event;
217
218         for (;;) {
219                 ao_event_get(&event);
220                 PRINTD("event type %d unit %d value %d\n",
221                        event.type, event.unit, event.value);
222                 switch (event.type) {
223                 case AO_EVENT_QUADRATURE:
224                         switch (event.unit) {
225                         case AO_QUADRATURE_PAD:
226                                 if (!ao_lco_armed)
227                                         ao_lco_step_pad((int8_t) event.value);
228                                 break;
229                         case AO_QUADRATURE_BOX:
230                                 if (!ao_lco_armed)
231                                         ao_lco_step_box((int8_t) event.value);
232                                 break;
233                         }
234                         break;
235                 case AO_EVENT_BUTTON:
236                         switch (event.unit) {
237                         case AO_BUTTON_ARM:
238                                 ao_lco_set_armed(event.value);
239                                 break;
240                         case AO_BUTTON_FIRE:
241                                 if (ao_lco_armed) {
242                                         ao_lco_fire_down = 0;
243                                         ao_lco_set_firing(event.value);
244                                 } else {
245                                         if (event.value) {
246                                                 if (ao_lco_box == AO_LCO_BOX_DRAG) {
247                                                         ao_lco_fire_down = 1;
248                                                         ao_lco_fire_tick = ao_time();
249                                                         ao_lco_drag_active = 1;
250                                                         ao_wakeup(&ao_lco_drag_beep_count);
251                                                 } else {
252                                                         ao_lco_toggle_drag();
253                                                 }
254                                         } else {
255                                                 ao_lco_fire_down = 0;
256                                                 if (ao_lco_drag_active)
257                                                         ao_wakeup(&ao_lco_drag_beep_count);
258                                         }
259                                 }
260                                 break;
261                         }
262                         break;
263                 }
264         }
265 }
266
267 /*
268  * Light up everything for a second at power on to let the user
269  * visually inspect the system for correct operation
270  */
271 static void
272 ao_lco_display_test()
273 {
274         ao_mutex_get(&ao_lco_display_mutex);
275         ao_seven_segment_set(AO_LCO_PAD_DIGIT, 8 | 0x10);
276         ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, 8 | 0x10);
277         ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, 8 | 0x10);
278         ao_mutex_put(&ao_lco_display_mutex);
279         ao_led_on(LEDS_AVAILABLE);
280         ao_delay(AO_MS_TO_TICKS(1000));
281         ao_led_off(LEDS_AVAILABLE);
282 }
283
284 #if HAS_ADC_SINGLE
285 static void
286 ao_lco_batt_voltage(void)
287 {
288         struct ao_adc   packet;
289         int16_t         decivolt;
290
291         ao_adc_single_get(&packet);
292         decivolt = ao_battery_decivolt(packet.v_batt);
293         ao_lco_show_voltage(decivolt);
294         ao_delay(AO_MS_TO_TICKS(1000));
295 }
296 #endif
297
298 static struct ao_task ao_lco_input_task;
299 static struct ao_task ao_lco_monitor_task;
300 static struct ao_task ao_lco_arm_warn_task;
301 static struct ao_task ao_lco_igniter_status_task;
302
303 static void
304 ao_lco_main(void)
305 {
306         ao_lco_display_test();
307 #if HAS_ADC_SINGLE
308         ao_lco_batt_voltage();
309 #endif
310         ao_lco_search();
311         ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
312         ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
313         ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
314         ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
315         ao_lco_monitor();
316 }
317
318 #if DEBUG
319 void
320 ao_lco_set_debug(void)
321 {
322         ao_cmd_decimal();
323         if (ao_cmd_status == ao_cmd_success)
324                 ao_lco_debug = ao_cmd_lex_i != 0;
325 }
326
327 const struct ao_cmds ao_lco_cmds[] = {
328         { ao_lco_set_debug,     "D <0 off, 1 on>\0Debug" },
329         { ao_lco_search,        "s\0Search for pad boxes" },
330         { 0, NULL }
331 };
332 #endif
333
334 void
335 ao_lco_init(void)
336 {
337         ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
338 #if DEBUG
339         ao_cmd_register(&ao_lco_cmds[0]);
340 #endif
341 }