altos/test: Adjust CRC error rate after FEC fix
[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(int8_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(int16_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_value(uint16_t value, uint8_t point)
80 {
81         uint8_t hundreds, tens, ones;
82
83         PRINTD("value %d\n", value);
84         ones = (uint8_t) (value % 10);
85         tens = (uint8_t) ((value / 10) % 10);
86         hundreds = (uint8_t) ((value / 100) % 10);
87         switch (point) {
88         case 2:
89                 hundreds |= 0x10;
90                 break;
91         case 1:
92                 tens |= 0x10;
93                 break;
94         case 0:
95                 ones |= 0x10;
96                 break;
97         default:
98                 break;
99         }
100         ao_mutex_get(&ao_lco_display_mutex);
101         ao_seven_segment_set(AO_LCO_PAD_DIGIT, ones);
102         ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, tens);
103         ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, hundreds);
104         ao_mutex_put(&ao_lco_display_mutex);
105 }
106
107 static void
108 ao_lco_show_lco_voltage(void)
109 {
110         struct ao_adc   packet;
111         int16_t         decivolt;
112
113         ao_adc_single_get(&packet);
114         decivolt = ao_battery_decivolt(packet.v_batt);
115         ao_lco_show_value((uint16_t) decivolt, 1);
116 }
117
118 void
119 ao_lco_show(void)
120 {
121         switch (ao_lco_box) {
122         case AO_LCO_LCO_VOLTAGE:
123                 ao_lco_show_lco_voltage();
124                 break;
125         default:
126                 switch (ao_lco_pad) {
127                 case AO_LCO_PAD_VOLTAGE:
128                         ao_lco_show_value(ao_pad_query.battery, 1);
129                         break;
130                 case AO_LCO_PAD_RSSI:
131                         if (!(ao_lco_valid[ao_lco_box] & AO_LCO_VALID_LAST))
132                                 ao_lco_show_value(888, 0);
133                         else
134                                 ao_lco_show_value((uint16_t) (-ao_radio_cmac_rssi), 0);
135                         break;
136                 default:
137                         ao_lco_show_pad(ao_lco_pad);
138                         ao_lco_show_box(ao_lco_box);
139                         break;
140                 }
141         }
142 }
143
144 static void
145 ao_lco_set_select(void)
146 {
147         if (ao_lco_armed) {
148                 ao_led_off(AO_LED_PAD);
149                 ao_led_off(AO_LED_BOX);
150         } else {
151                 switch (ao_lco_select_mode) {
152                 case AO_LCO_SELECT_PAD:
153                         ao_led_off(AO_LED_BOX);
154                         ao_led_on(AO_LED_PAD);
155                         break;
156                 case AO_LCO_SELECT_BOX:
157                         ao_led_off(AO_LED_PAD);
158                         ao_led_on(AO_LED_BOX);
159                         break;
160                 default:
161                         break;
162                 }
163         }
164 }
165
166 static struct ao_task   ao_lco_drag_task;
167
168 static void
169 ao_lco_drag_monitor(void)
170 {
171         AO_TICK_TYPE    delay = ~0UL;
172         AO_TICK_TYPE    now;
173
174         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
175         for (;;) {
176                 PRINTD("Drag monitor count %d delay %lu\n", ao_lco_drag_beep_count, (unsigned long) delay);
177                 if (delay == (AO_TICK_TYPE) ~0)
178                         ao_sleep(&ao_lco_drag_beep_count);
179                 else
180                         ao_sleep_for(&ao_lco_drag_beep_count, delay);
181
182                 delay = ~0UL;
183                 now = ao_time();
184                 delay = ao_lco_drag_warn_check(now, delay);
185                 delay = ao_lco_drag_beep_check(now, delay);
186         }
187 }
188
189 static void
190 ao_lco_input(void)
191 {
192         static struct ao_event  event;
193
194         for (;;) {
195                 ao_event_get(&event);
196                 PRINTE("event type %d unit %d value %ld\n",
197                        event.type, event.unit, (long) event.value);
198                 switch (event.type) {
199                 case AO_EVENT_QUADRATURE:
200                         switch (event.unit) {
201                         case AO_QUADRATURE_SELECT:
202                                 if (!ao_lco_armed) {
203                                         switch (ao_lco_select_mode) {
204                                         case AO_LCO_SELECT_PAD:
205                                                 ao_lco_step_pad((int8_t) event.value);
206                                                 break;
207                                         case AO_LCO_SELECT_BOX:
208                                                 ao_lco_step_box((int8_t) event.value);
209                                                 break;
210                                         default:
211                                                 break;
212                                         }
213                                 }
214                                 break;
215                         }
216                         break;
217                 case AO_EVENT_BUTTON:
218                         switch (event.unit) {
219                         case AO_BUTTON_ARM:
220                                 ao_lco_set_armed((uint8_t) event.value);
221                                 ao_lco_set_select();
222                                 break;
223                         case AO_BUTTON_FIRE:
224                                 if (ao_lco_armed)
225                                         ao_lco_set_firing((uint8_t) event.value);
226                                 break;
227                         case AO_BUTTON_DRAG_SELECT:
228                                 if (event.value)
229                                         ao_lco_toggle_drag();
230                                 break;
231                         case AO_BUTTON_DRAG_MODE:
232                                 if (event.value)
233                                         ao_lco_drag_enable();
234                                 else
235                                         ao_lco_drag_disable();
236                                 break;
237                         case AO_BUTTON_ENCODER_SELECT:
238                                 if (event.value) {
239                                         if (!ao_lco_armed) {
240                                                 ao_lco_select_mode = 1 - ao_lco_select_mode;
241                                                 ao_lco_set_select();
242                                         }
243                                 }
244                                 break;
245                         }
246                         break;
247                 }
248         }
249 }
250
251 /*
252  * Light up everything for a second at power on to let the user
253  * visually inspect the system for correct operation
254  */
255 static void
256 ao_lco_display_test(void)
257 {
258         ao_mutex_get(&ao_lco_display_mutex);
259         ao_seven_segment_set(AO_LCO_PAD_DIGIT, 8 | 0x10);
260         ao_seven_segment_set(AO_LCO_BOX_DIGIT_1, 8 | 0x10);
261         ao_seven_segment_set(AO_LCO_BOX_DIGIT_10, 8 | 0x10);
262         ao_mutex_put(&ao_lco_display_mutex);
263         ao_led_on(AO_LEDS_AVAILABLE);
264         ao_delay(AO_MS_TO_TICKS(1000));
265         ao_led_off(AO_LEDS_AVAILABLE);
266 }
267
268 static void
269 ao_lco_batt_voltage(void)
270 {
271         ao_lco_show_lco_voltage();
272         ao_delay(AO_MS_TO_TICKS(1000));
273 }
274
275 static struct ao_task ao_lco_input_task;
276 static struct ao_task ao_lco_monitor_task;
277 static struct ao_task ao_lco_arm_warn_task;
278 static struct ao_task ao_lco_igniter_status_task;
279
280 static void
281 ao_lco_main(void)
282 {
283         ao_lco_display_test();
284         ao_lco_batt_voltage();
285         ao_lco_search();
286         ao_add_task(&ao_lco_input_task, ao_lco_input, "lco input");
287         ao_add_task(&ao_lco_arm_warn_task, ao_lco_arm_warn, "lco arm warn");
288         ao_add_task(&ao_lco_igniter_status_task, ao_lco_igniter_status, "lco igniter status");
289         ao_add_task(&ao_lco_drag_task, ao_lco_drag_monitor, "drag race");
290         ao_lco_monitor();
291 }
292
293 #if DEBUG
294 static void
295 ao_lco_set_debug(void)
296 {
297         uint32_t r = ao_cmd_decimal();
298         if (ao_cmd_status == ao_cmd_success){
299                 ao_lco_debug = r & 1;
300                 ao_lco_event_debug = (r & 2) >> 1;
301         }
302 }
303
304 const struct ao_cmds ao_lco_cmds[] = {
305         { ao_lco_set_debug,     "D <0 off, 1 on>\0Debug" },
306         { ao_lco_search,        "s\0Search for pad boxes" },
307         { ao_lco_pretend,       "p\0Pretend there are lots of pad boxes" },
308         { 0, NULL }
309 };
310 #endif
311
312 void
313 ao_lco_init(void)
314 {
315         ao_add_task(&ao_lco_monitor_task, ao_lco_main, "lco monitor");
316 #if DEBUG
317         ao_cmd_register(&ao_lco_cmds[0]);
318 #endif
319 }