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