197bf8aa3c6c5b974092e232e827a0b5458b7429
[fw/altos] / src / drivers / ao_lco_bits.c
1 /*
2  * Copyright © 2018 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
15 #include <ao.h>
16 #include <ao_lco.h>
17 #include <ao_radio_cmac.h>
18
19 uint8_t         ao_lco_debug;
20
21 uint8_t         ao_lco_pad;
22 int16_t         ao_lco_box;
23 uint8_t         ao_lco_drag_race;
24
25 uint8_t         ao_lco_armed;   /* arm active */
26 uint8_t         ao_lco_firing;  /* fire active */
27
28 uint8_t         ao_lco_min_box, ao_lco_max_box;
29 uint8_t         ao_lco_selected[AO_PAD_MAX_BOXES];
30 uint8_t         ao_lco_valid[AO_PAD_MAX_BOXES];
31 uint8_t         ao_lco_channels[AO_PAD_MAX_BOXES];
32 uint16_t        ao_lco_tick_offset[AO_PAD_MAX_BOXES];
33
34 struct ao_pad_query     ao_pad_query;
35
36 static AO_LED_TYPE      continuity_led[AO_LED_CONTINUITY_NUM] = {
37 #ifdef AO_LED_CONTINUITY_0
38         AO_LED_CONTINUITY_0,
39 #endif
40 #ifdef AO_LED_CONTINUITY_1
41         AO_LED_CONTINUITY_1,
42 #endif
43 #ifdef AO_LED_CONTINUITY_2
44         AO_LED_CONTINUITY_2,
45 #endif
46 #ifdef AO_LED_CONTINUITY_3
47         AO_LED_CONTINUITY_3,
48 #endif
49 #ifdef AO_LED_CONTINUITY_4
50         AO_LED_CONTINUITY_4,
51 #endif
52 #ifdef AO_LED_CONTINUITY_5
53         AO_LED_CONTINUITY_5,
54 #endif
55 #ifdef AO_LED_CONTINUITY_6
56         AO_LED_CONTINUITY_6,
57 #endif
58 #ifdef AO_LED_CONTINUITY_7
59         AO_LED_CONTINUITY_7,
60 #endif
61 };
62
63 /* Set LEDs to match remote box status */
64 void
65 ao_lco_igniter_status(void)
66 {
67         uint8_t         c;
68         uint8_t         t = 0;
69
70         for (;;) {
71                 if (ao_lco_drag_race)
72                         ao_sleep_for(&ao_pad_query, AO_MS_TO_TICKS(50));
73                 else
74                         ao_sleep(&ao_pad_query);
75                 PRINTD("RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_valid[ao_lco_box]);
76                 if (!(ao_lco_valid[ao_lco_box] & AO_LCO_VALID_LAST)) {
77                         ao_led_on(AO_LED_RED);
78                         ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
79                         continue;
80                 }
81                 if (ao_radio_cmac_rssi < -90) {
82                         ao_led_on(AO_LED_AMBER);
83                         ao_led_off(AO_LED_RED|AO_LED_GREEN);
84                 } else {
85                         ao_led_on(AO_LED_GREEN);
86                         ao_led_off(AO_LED_RED|AO_LED_AMBER);
87                 }
88                 if (ao_pad_query.arm_status)
89                         ao_led_on(AO_LED_REMOTE_ARM);
90                 else
91                         ao_led_off(AO_LED_REMOTE_ARM);
92
93                 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
94                         uint8_t status;
95
96                         if (ao_pad_query.channels & (1 << c))
97                                 status = ao_pad_query.igniter_status[c];
98                         else
99                                 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
100
101                         if (ao_lco_drag_race && (ao_lco_selected[ao_lco_box] & (1 << c))) {
102                                 uint8_t on = 0;
103                                 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN) {
104                                         if (t)
105                                                 on = 1;
106                                 } else {
107                                         if (t == 1)
108                                                 on = 1;
109                                 }
110                                 if (on)
111                                         ao_led_on(continuity_led[c]);
112                                 else
113                                         ao_led_off(continuity_led[c]);
114                         } else {
115                                 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
116                                         ao_led_on(continuity_led[c]);
117                                 else
118                                         ao_led_off(continuity_led[c]);
119                         }
120                 }
121                 t = (t + 1) & 3;
122         }
123 }
124
125 uint8_t
126 ao_lco_pad_present(uint8_t box, uint8_t pad)
127 {
128         /* voltage measurement is always valid */
129         if (pad == AO_LCO_PAD_VOLTAGE)
130                 return 1;
131         if (!ao_lco_channels[box])
132                 return 0;
133         if (pad > AO_PAD_MAX_CHANNELS)
134                 return 0;
135         return (ao_lco_channels[box] >> (pad - 1)) & 1;
136 }
137
138 uint8_t
139 ao_lco_pad_first(uint8_t box)
140 {
141         uint8_t pad;
142
143         for (pad = 1; pad <= AO_PAD_MAX_CHANNELS; pad++)
144                 if (ao_lco_pad_present(box, pad))
145                         return pad;
146         return 0;
147 }
148
149 static uint8_t
150 ao_lco_get_channels(uint8_t box, struct ao_pad_query *query)
151 {
152         int8_t                  r;
153
154         r = ao_lco_query(box, query, &ao_lco_tick_offset[box]);
155         if (r == AO_RADIO_CMAC_OK) {
156                 ao_lco_channels[box] = query->channels;
157                 ao_lco_valid[box] = AO_LCO_VALID_LAST | AO_LCO_VALID_EVER;
158         } else
159                 ao_lco_valid[box] &= ~AO_LCO_VALID_LAST;
160         PRINTD("ao_lco_get_channels(%d) rssi %d valid %d ret %d offset %d\n", box, ao_radio_cmac_rssi, ao_lco_valid[box], r, ao_lco_tick_offset[box]);
161         ao_wakeup(&ao_pad_query);
162         return ao_lco_valid[box];
163 }
164
165 void
166 ao_lco_update(void)
167 {
168         uint8_t previous_valid = ao_lco_valid[ao_lco_box];
169
170         if (ao_lco_get_channels(ao_lco_box, &ao_pad_query) & AO_LCO_VALID_LAST) {
171                 if (!(previous_valid & AO_LCO_VALID_EVER)) {
172                         if (ao_lco_pad != AO_LCO_PAD_VOLTAGE)
173                                 ao_lco_set_pad(ao_lco_pad_first(ao_lco_box));
174                 }
175                 if (ao_lco_pad == AO_LCO_PAD_VOLTAGE)
176                         ao_lco_show();
177         }
178 }
179
180 uint8_t ao_lco_box_mask[AO_LCO_MASK_SIZE(AO_PAD_MAX_BOXES)];
181
182 static void
183 ao_lco_box_reset_present(void)
184 {
185         ao_lco_min_box = 0xff;
186         ao_lco_max_box = 0x00;
187         memset(ao_lco_box_mask, 0, sizeof (ao_lco_box_mask));
188 }
189
190 static void
191 ao_lco_box_set_present(uint8_t box)
192 {
193         if (box < ao_lco_min_box)
194                 ao_lco_min_box = box;
195         if (box > ao_lco_max_box)
196                 ao_lco_max_box = box;
197         if (box >= AO_PAD_MAX_BOXES)
198                 return;
199         ao_lco_box_mask[AO_LCO_MASK_ID(box)] |= 1 << AO_LCO_MASK_SHIFT(box);
200 }
201
202 void
203 ao_lco_set_pad(uint8_t new_pad)
204 {
205         ao_lco_pad = new_pad;
206         ao_lco_show();
207 }
208
209 void
210 ao_lco_set_box(uint16_t new_box)
211 {
212         ao_lco_box = new_box;
213 #if AO_LCO_DRAG
214         if (ao_lco_box != AO_LCO_BOX_DRAG)
215 #endif
216                 ao_lco_channels[ao_lco_box] = 0;
217         ao_lco_pad = 1;
218         ao_lco_show();
219 }
220
221 void
222 ao_lco_step_pad(int8_t dir)
223 {
224         int8_t  new_pad;
225
226         new_pad = ao_lco_pad;
227         do {
228                 new_pad += dir;
229                 if (new_pad > AO_PAD_MAX_CHANNELS)
230                         new_pad = AO_LCO_PAD_VOLTAGE;
231                 if (new_pad < 0)
232                         new_pad = AO_PAD_MAX_CHANNELS;
233                 if (new_pad == ao_lco_pad)
234                         break;
235         } while (!ao_lco_pad_present(ao_lco_box, new_pad));
236         ao_lco_set_pad(new_pad);
237 }
238
239 void
240 ao_lco_set_armed(uint8_t armed)
241 {
242         ao_lco_armed = armed;
243         PRINTD("Armed %d\n", ao_lco_armed);
244         if (ao_lco_armed) {
245                 if (ao_lco_drag_race) {
246                         uint8_t box;
247
248                         for (box = ao_lco_min_box; box <= ao_lco_max_box; box++)
249                                 if (ao_lco_selected[box])
250                                         break;
251                         if (box > ao_lco_max_box)
252                                 ao_lco_armed = 0;
253                 } else {
254                         memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
255                         if (ao_lco_pad != 0)
256                                 ao_lco_selected[ao_lco_box] = (1 << (ao_lco_pad - 1));
257                         else
258                                 ao_lco_armed = 0;
259                 }
260         }
261         ao_wakeup(&ao_lco_armed);
262 }
263
264 void
265 ao_lco_set_firing(uint8_t firing)
266 {
267         ao_lco_firing = firing;
268         PRINTD("Firing %d\n", ao_lco_firing);
269         ao_wakeup(&ao_lco_armed);
270 }
271
272 void
273 ao_lco_search(void)
274 {
275         int8_t          r;
276         int8_t          try;
277         uint8_t         box;
278         uint8_t         boxes = 0;
279
280         ao_lco_box_reset_present();
281         ao_lco_show_box(0);
282         ao_lco_show_pad(0);
283         for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
284                 if ((box % 10) == 0)
285                         ao_lco_show_box(box);
286                 for (try = 0; try < 3; try++) {
287                         ao_lco_tick_offset[box] = 0;
288                         r = ao_lco_query(box, &ao_pad_query, &ao_lco_tick_offset[box]);
289                         PRINTD("box %d result %d offset %d\n", box, r, ao_lco_tick_offset[box]);
290                         if (r == AO_RADIO_CMAC_OK) {
291                                 ++boxes;
292                                 ao_lco_box_set_present(box);
293                                 ao_lco_show_pad(boxes % 10);
294                                 ao_delay(AO_MS_TO_TICKS(30));
295                                 break;
296                         }
297                 }
298         }
299         if (ao_lco_min_box <= ao_lco_max_box)
300                 ao_lco_box = ao_lco_min_box;
301         else
302                 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
303         memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
304         memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
305         ao_lco_set_box(ao_lco_min_box);
306 }
307
308 void
309 ao_lco_monitor(void)
310 {
311         uint16_t                delay;
312         uint8_t                 box;
313
314         for (;;) {
315                 PRINTD("monitor armed %d firing %d\n",
316                        ao_lco_armed, ao_lco_firing);
317
318                 if (ao_lco_armed && ao_lco_firing) {
319                         ao_lco_ignite(AO_PAD_FIRE);
320                 } else {
321                         ao_lco_update();
322                         if (ao_lco_armed) {
323                                 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
324                                         if (ao_lco_selected[box]) {
325                                                 PRINTD("Arming box %d pads %x\n",
326                                                        box, ao_lco_selected[box]);
327                                                 if (ao_lco_valid[box] & AO_LCO_VALID_EVER) {
328                                                         ao_lco_arm(box, ao_lco_selected[box], ao_lco_tick_offset[box]);
329                                                         ao_delay(AO_MS_TO_TICKS(10));
330                                                 }
331                                         }
332                                 }
333                         }
334                 }
335                 if (ao_lco_armed && ao_lco_firing)
336                         delay = AO_MS_TO_TICKS(100);
337                 else
338                         delay = AO_SEC_TO_TICKS(1);
339                 ao_sleep_for(&ao_lco_armed, delay);
340         }
341 }
342
343 #if AO_LCO_DRAG
344
345 uint8_t                 ao_lco_drag_beep_count;
346 static uint8_t          ao_lco_drag_beep_on;
347 static uint16_t         ao_lco_drag_beep_time;
348 static uint16_t         ao_lco_drag_warn_time;
349
350 #define AO_LCO_DRAG_BEEP_TIME   AO_MS_TO_TICKS(50)
351 #define AO_LCO_DRAG_WARN_TIME   AO_SEC_TO_TICKS(5)
352
353 /* Request 'beeps' additional drag race beeps */
354 void
355 ao_lco_drag_add_beeps(uint8_t beeps)
356 {
357         PRINTD("beep %d\n", beeps);
358         if (ao_lco_drag_beep_count == 0)
359                 ao_lco_drag_beep_time = ao_time();
360         ao_lco_drag_beep_count += beeps;
361         ao_wakeup(&ao_lco_drag_beep_count);
362 }
363
364 /* Toggle current pad in drag set */
365 void
366 ao_lco_toggle_drag(void)
367 {
368         if (ao_lco_drag_race && ao_lco_pad != AO_LCO_PAD_VOLTAGE) {
369                 ao_lco_selected[ao_lco_box] ^= (1 << (ao_lco_pad - 1));
370                 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
371                        ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
372                 ao_lco_drag_add_beeps(ao_lco_pad);
373         }
374 }
375
376 /* Check whether it's time to change the beeper status, then either
377  * turn it on or off as necessary and bump the remaining beep counts
378  */
379
380 uint16_t
381 ao_lco_drag_beep_check(uint16_t now, uint16_t delay)
382 {
383         PRINTD("beep check count %d delta %d\n",
384                ao_lco_drag_beep_count,
385                (int16_t) (now - ao_lco_drag_beep_time));
386         if (ao_lco_drag_beep_count) {
387                 if ((int16_t) (now - ao_lco_drag_beep_time) >= 0) {
388                         if (ao_lco_drag_beep_on) {
389                                 ao_beep(0);
390                                 PRINTD("beep stop\n");
391                                 ao_lco_drag_beep_on = 0;
392                                 if (ao_lco_drag_beep_count) {
393                                         --ao_lco_drag_beep_count;
394                                         if (ao_lco_drag_beep_count)
395                                                 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
396                                 }
397                         } else {
398                                 ao_beep(AO_BEEP_HIGH);
399                                 PRINTD("beep start\n");
400                                 ao_lco_drag_beep_on = 1;
401                                 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
402                         }
403                 }
404         }
405
406         if (ao_lco_drag_beep_count) {
407                 uint16_t beep_delay = 0;
408
409                 if (ao_lco_drag_beep_time > now)
410                         beep_delay = ao_lco_drag_beep_time - now;
411
412                 if (delay > beep_delay)
413                         delay = beep_delay;
414         }
415         return delay;
416 }
417
418 void
419 ao_lco_drag_enable(void)
420 {
421         if (!ao_lco_drag_race) {
422                 PRINTD("Drag enable\n");
423                 ao_lco_drag_race = 1;
424                 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
425 #ifdef AO_LED_DRAG
426                 ao_led_on(AO_LED_DRAG);
427 #endif
428                 ao_lco_drag_add_beeps(5);
429                 ao_lco_show();
430         }
431 }
432
433 void
434 ao_lco_drag_disable(void)
435 {
436         if (ao_lco_drag_race) {
437                 PRINTD("Drag disable\n");
438                 ao_lco_drag_race = 0;
439 #ifdef AO_LED_DRAG
440                 ao_led_off(AO_LED_DRAG);
441 #endif
442                 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
443                 ao_lco_drag_add_beeps(2);
444                 ao_lco_show();
445         }
446 }
447
448 /* add a beep if it's time to warn the user that drag race mode is
449  * active
450  */
451
452 uint16_t
453 ao_lco_drag_warn_check(uint16_t now, uint16_t delay)
454 {
455         if (ao_lco_drag_race) {
456                 uint16_t        warn_delay;
457
458                 if ((int16_t) (now - ao_lco_drag_warn_time) >= 0) {
459                         ao_lco_drag_add_beeps(1);
460                         ao_lco_drag_warn_time = now + AO_LCO_DRAG_WARN_TIME;
461                 }
462                 warn_delay = ao_lco_drag_warn_time - now;
463                 if (delay > warn_delay)
464                         delay = warn_delay;
465         }
466         return delay;
467 }
468 #endif /* AO_LCO_DRAG */
469
470 /* task function for beeping while arm is active */
471 void
472 ao_lco_arm_warn(void)
473 {
474         for (;;) {
475                 while (!ao_lco_armed) {
476 #ifdef AO_LED_FIRE
477                         ao_led_off(AO_LED_FIRE);
478 #endif
479                         ao_sleep(&ao_lco_armed);
480                 }
481 #ifdef AO_LED_FIRE
482                 ao_led_on(AO_LED_FIRE);
483 #endif
484                 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
485                 ao_delay(AO_MS_TO_TICKS(200));
486         }
487 }