78849bc9b43bc00f551a70133aef6fbdcd66327a
[fw/altos] / src / telelco-v2.0 / ao_lco_v2.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_radio_cmac.h>
25 #include <ao_adc_single.h>
26
27 #define AO_LCO_PAD_DIGIT        0
28 #define AO_LCO_BOX_DIGIT_1      1
29 #define AO_LCO_BOX_DIGIT_10     2
30
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)
33
34 /* UI values */
35 static uint8_t  ao_lco_select_mode;
36 static uint8_t  ao_lco_event_debug;
37
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
41
42 static uint8_t  ao_lco_display_mutex;
43
44 void
45 ao_lco_show_pad(uint8_t pad)
46 {
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);
50 }
51
52 #define SEVEN_SEGMENT_d         ((0 << 0) |     \
53                                  (0 << 1) |     \
54                                  (1 << 2) |     \
55                                  (1 << 3) |     \
56                                  (1 << 4) |     \
57                                  (1 << 5) |     \
58                                  (1 << 6))
59
60
61 #define SEVEN_SEGMENT_r         ((0 << 0) |     \
62                                  (0 << 1) |     \
63                                  (0 << 2) |     \
64                                  (1 << 3) |     \
65                                  (1 << 4) |     \
66                                  (0 << 5) |     \
67                                  (0 << 6))
68
69 void
70 ao_lco_show_box(uint16_t box)
71 {
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);
76 }
77
78 static void
79 ao_lco_show_voltage(uint16_t decivolts)
80 {
81         uint8_t tens, ones, tenths;
82
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);
92 }
93
94 void
95 ao_lco_show(void)
96 {
97         if (ao_lco_pad == AO_LCO_PAD_VOLTAGE) {
98                 ao_lco_show_voltage(ao_pad_query.battery);
99         } else {
100                 ao_lco_show_pad(ao_lco_pad);
101                 ao_lco_show_box(ao_lco_box);
102         }
103 }
104
105 uint8_t
106 ao_lco_box_present(uint16_t box)
107 {
108         if (box >= AO_PAD_MAX_BOXES)
109                 return 0;
110         return (ao_lco_box_mask[AO_LCO_MASK_ID(box)] >> AO_LCO_MASK_SHIFT(box)) & 1;
111 }
112
113 static void
114 ao_lco_set_select(void)
115 {
116         if (ao_lco_armed) {
117                 ao_led_off(AO_LED_PAD);
118                 ao_led_off(AO_LED_BOX);
119         } else {
120                 switch (ao_lco_select_mode) {
121                 case AO_LCO_SELECT_PAD:
122                         ao_led_off(AO_LED_BOX);
123                         ao_led_on(AO_LED_PAD);
124                         break;
125                 case AO_LCO_SELECT_BOX:
126                         ao_led_off(AO_LED_PAD);
127                         ao_led_on(AO_LED_BOX);
128                         break;
129                 default:
130                         break;
131                 }
132         }
133 }
134
135 static void
136 ao_lco_step_box(int8_t dir)
137 {
138         int32_t new_box = (int32_t) ao_lco_box;
139
140         do {
141                 new_box += dir;
142                 if (new_box > ao_lco_max_box)
143                         new_box = ao_lco_min_box;
144                 else if (new_box < ao_lco_min_box)
145                         new_box = ao_lco_max_box;
146                 if (new_box == ao_lco_box)
147                         break;
148         } while (!ao_lco_box_present((uint16_t) new_box));
149         ao_lco_set_box((uint16_t) new_box);
150 }
151
152 static struct ao_task   ao_lco_drag_task;
153
154 static void
155 ao_lco_drag_monitor(void)
156 {
157         AO_TICK_TYPE    delay = ~0UL;
158         AO_TICK_TYPE    now;
159
160         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
161         for (;;) {
162                 PRINTD("Drag monitor count %d delay %lu\n", ao_lco_drag_beep_count, (unsigned long) delay);
163                 if (delay == (AO_TICK_TYPE) ~0)
164                         ao_sleep(&ao_lco_drag_beep_count);
165                 else
166                         ao_sleep_for(&ao_lco_drag_beep_count, delay);
167
168                 delay = ~0UL;
169                 now = ao_time();
170                 delay = ao_lco_drag_warn_check(now, delay);
171                 delay = ao_lco_drag_beep_check(now, delay);
172         }
173 }
174
175 static void
176 ao_lco_input(void)
177 {
178         static struct ao_event  event;
179
180         for (;;) {
181                 ao_event_get(&event);
182                 PRINTE("event type %d unit %d value %ld\n",
183                        event.type, event.unit, (long) event.value);
184                 switch (event.type) {
185                 case AO_EVENT_QUADRATURE:
186                         switch (event.unit) {
187                         case AO_QUADRATURE_SELECT:
188                                 if (!ao_lco_armed) {
189                                         switch (ao_lco_select_mode) {
190                                         case AO_LCO_SELECT_PAD:
191                                                 ao_lco_step_pad((int8_t) event.value);
192                                                 break;
193                                         case AO_LCO_SELECT_BOX:
194                                                 ao_lco_step_box((int8_t) event.value);
195                                                 break;
196                                         default:
197                                                 break;
198                                         }
199                                 }
200                                 break;
201                         }
202                         break;
203                 case AO_EVENT_BUTTON:
204                         switch (event.unit) {
205                         case AO_BUTTON_ARM:
206                                 ao_lco_set_armed((uint8_t) event.value);
207                                 ao_lco_set_select();
208                                 break;
209                         case AO_BUTTON_FIRE:
210                                 if (ao_lco_armed)
211                                         ao_lco_set_firing((uint8_t) event.value);
212                                 break;
213                         case AO_BUTTON_DRAG_SELECT:
214                                 if (event.value)
215                                         ao_lco_toggle_drag();
216                                 break;
217                         case AO_BUTTON_DRAG_MODE:
218                                 if (event.value)
219                                         ao_lco_drag_enable();
220                                 else
221                                         ao_lco_drag_disable();
222                                 break;
223                         case AO_BUTTON_ENCODER_SELECT:
224                                 if (event.value) {
225                                         if (!ao_lco_armed) {
226                                                 ao_lco_select_mode = 1 - ao_lco_select_mode;
227                                                 ao_lco_set_select();
228                                         }
229                                 }
230                                 break;
231                         }
232                         break;
233                 }
234         }
235 }
236
237 /*
238  * Light up everything for a second at power on to let the user
239  * visually inspect the system for correct operation
240  */
241 static void
242 ao_lco_display_test(void)
243 {
244         ao_mutex_get(&ao_lco_display_mutex);
245         ao_seven_segment_set(AO_LCO_PAD_DIGIT, 8 | 0x10);
246         ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, 8 | 0x10);
247         ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, 8 | 0x10);
248         ao_mutex_put(&ao_lco_display_mutex);
249         ao_led_on(AO_LEDS_AVAILABLE);
250         ao_delay(AO_MS_TO_TICKS(1000));
251         ao_led_off(AO_LEDS_AVAILABLE);
252 }
253
254 static void
255 ao_lco_batt_voltage(void)
256 {
257         struct ao_adc   packet;
258         int16_t         decivolt;
259
260         ao_adc_single_get(&packet);
261         decivolt = ao_battery_decivolt(packet.v_batt);
262         ao_lco_show_voltage((uint16_t) decivolt);
263         ao_delay(AO_MS_TO_TICKS(1000));
264 }
265
266 static struct ao_task ao_lco_input_task;
267 static struct ao_task ao_lco_monitor_task;
268 static struct ao_task ao_lco_arm_warn_task;
269 static struct ao_task ao_lco_igniter_status_task;
270
271 static void
272 ao_lco_main(void)
273 {
274         ao_lco_display_test();
275         ao_lco_batt_voltage();
276         ao_lco_search();
277         ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
278         ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
279         ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
280         ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
281         ao_lco_monitor();
282 }
283
284 #if DEBUG
285 static void
286 ao_lco_set_debug(void)
287 {
288         uint32_t r = ao_cmd_decimal();
289         if (ao_cmd_status == ao_cmd_success){
290                 ao_lco_debug = r & 1;
291                 ao_lco_event_debug = (r & 2) >> 1;
292         }
293 }
294
295 const struct ao_cmds ao_lco_cmds[] = {
296         { ao_lco_set_debug,     "D <0 off, 1 on>\0Debug" },
297         { ao_lco_search,        "s\0Search for pad boxes" },
298         { ao_lco_pretend,       "p\0Pretend there are lots of pad boxes" },
299         { 0, NULL }
300 };
301 #endif
302
303 void
304 ao_lco_init(void)
305 {
306         ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
307 #if DEBUG
308         ao_cmd_register(&ao_lco_cmds[0]);
309 #endif
310 }