b574437d9e6517acccc666febeea71fac8e8c826
[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
24 uint8_t         ao_lco_armed;                                   /* arm active */
25 uint8_t         ao_lco_firing;                                  /* fire active */
26
27 int16_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                 if (ao_lco_box_pseudo(ao_lco_box)) {
88                         ao_led_off(AO_LED_GREEN|AO_LED_AMBER|AO_LED_RED);
89                         continue;
90                 }
91                 PRINTD("RSSI %d VALID %d\n", ao_radio_cmac_rssi, ao_lco_valid[ao_lco_box]);
92                 if (!(ao_lco_valid[ao_lco_box] & AO_LCO_VALID_LAST)) {
93                         ao_led_on(AO_LED_RED);
94                         ao_led_off(AO_LED_GREEN|AO_LED_AMBER);
95                         continue;
96                 }
97                 if (ao_radio_cmac_rssi < -90) {
98                         ao_led_on(AO_LED_AMBER);
99                         ao_led_off(AO_LED_RED|AO_LED_GREEN);
100                 } else {
101                         ao_led_on(AO_LED_GREEN);
102                         ao_led_off(AO_LED_RED|AO_LED_AMBER);
103                 }
104                 if (ao_pad_query.arm_status)
105                         ao_led_on(AO_LED_REMOTE_ARM);
106                 else
107                         ao_led_off(AO_LED_REMOTE_ARM);
108
109                 for (c = 0; c < AO_LED_CONTINUITY_NUM; c++) {
110                         uint8_t status;
111
112                         if (ao_pad_query.channels & (1 << c))
113                                 status = ao_pad_query.igniter_status[c];
114                         else
115                                 status = AO_PAD_IGNITER_STATUS_NO_IGNITER_RELAY_OPEN;
116
117 #if AO_LCO_DRAG
118                         if (ao_lco_drag_race && (ao_lco_selected[ao_lco_box] & (1 << c))) {
119                                 uint8_t on = 0;
120                                 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN) {
121                                         if (t)
122                                                 on = 1;
123                                 } else {
124                                         if (t == 1)
125                                                 on = 1;
126                                 }
127                                 if (on)
128                                         ao_led_on(continuity_led[c]);
129                                 else
130                                         ao_led_off(continuity_led[c]);
131                         } else
132 #endif
133                         {
134                                 if (status == AO_PAD_IGNITER_STATUS_GOOD_IGNITER_RELAY_OPEN)
135                                         ao_led_on(continuity_led[c]);
136                                 else
137                                         ao_led_off(continuity_led[c]);
138                         }
139                 }
140                 t = (t + 1) & 3;
141         }
142 }
143
144 uint8_t
145 ao_lco_pad_present(int16_t box, uint8_t pad)
146 {
147         /* voltage measurement is always valid */
148         if (pad == AO_LCO_PAD_VOLTAGE)
149                 return 1;
150         if (!ao_lco_channels[box])
151                 return 0;
152         if (pad > AO_PAD_MAX_CHANNELS)
153                 return 0;
154         return (ao_lco_channels[box] >> (pad - 1)) & 1;
155 }
156
157 uint8_t
158 ao_lco_pad_first(int16_t box)
159 {
160         uint8_t pad;
161
162         for (pad = 1; pad <= AO_PAD_MAX_CHANNELS; pad++)
163                 if (ao_lco_pad_present(box, pad))
164                         return pad;
165         return 0;
166 }
167
168 static uint8_t
169 ao_lco_get_channels(int16_t box, struct ao_pad_query *query)
170 {
171         int8_t                  r;
172
173         r = ao_lco_query((uint16_t) box, query, &ao_lco_tick_offset[box]);
174         if (r == AO_RADIO_CMAC_OK) {
175                 ao_lco_channels[box] = query->channels;
176                 ao_lco_valid[box] = AO_LCO_VALID_LAST | AO_LCO_VALID_EVER;
177         } else
178                 ao_lco_valid[box] &= (uint8_t) ~AO_LCO_VALID_LAST;
179         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]);
180         ao_wakeup(&ao_pad_query);
181         return ao_lco_valid[box];
182 }
183
184 void
185 ao_lco_update(void)
186 {
187         if (ao_lco_box_pseudo(ao_lco_box)) {
188                 ao_lco_show();
189                 return;
190         }
191
192         uint8_t previous_valid = ao_lco_valid[ao_lco_box];
193
194         if (ao_lco_get_channels(ao_lco_box, &ao_pad_query) & AO_LCO_VALID_LAST) {
195                 if (!(previous_valid & AO_LCO_VALID_EVER)) {
196                         if (ao_lco_pad != AO_LCO_PAD_VOLTAGE)
197                                 ao_lco_set_pad(ao_lco_pad_first(ao_lco_box));
198                 }
199                 if (ao_lco_pad == AO_LCO_PAD_VOLTAGE)
200                         ao_lco_show();
201         }
202 }
203
204 uint8_t ao_lco_box_mask[AO_LCO_MASK_SIZE(AO_PAD_MAX_BOXES)];
205
206 static void
207 ao_lco_box_reset_present(void)
208 {
209         ao_lco_min_box = 0xff;
210         ao_lco_max_box = 0x00;
211         ao_lco_pretending = 0;
212         memset(ao_lco_box_mask, 0, sizeof (ao_lco_box_mask));
213 }
214
215 static void
216 ao_lco_box_set_present(int16_t box)
217 {
218         if (box < ao_lco_min_box)
219                 ao_lco_min_box = box;
220         if (box > ao_lco_max_box)
221                 ao_lco_max_box = box;
222         if (box >= AO_PAD_MAX_BOXES)
223                 return;
224         ao_lco_box_mask[AO_LCO_MASK_ID(box)] |= (uint8_t) (1 << AO_LCO_MASK_SHIFT(box));
225 }
226
227 void
228 ao_lco_set_pad(uint8_t new_pad)
229 {
230         ao_lco_pad = new_pad;
231         ao_lco_show();
232 }
233
234 void
235 ao_lco_set_box(int16_t new_box)
236 {
237         ao_lco_box = new_box;
238         if (!ao_lco_box_pseudo(ao_lco_box)) {
239                 if (ao_lco_box < AO_PAD_MAX_BOXES) {
240                         if (ao_lco_pretending)
241                                 ao_lco_channels[ao_lco_box] = 0xff;
242                         else
243                                 ao_lco_channels[ao_lco_box] = 0;
244                 }
245         }
246         ao_lco_pad = 1;
247         ao_lco_show();
248 }
249
250 void
251 ao_lco_step_pad(int8_t dir)
252 {
253         int16_t new_pad;
254
255         switch (ao_lco_box) {
256 #ifdef AO_LCO_HAS_CONTRAST
257         case AO_LCO_CONTRAST: {
258                 int32_t contrast = ao_lco_get_contrast();
259
260                 contrast = (contrast + AO_LCO_CONTRAST_STEP - 1) / AO_LCO_CONTRAST_STEP;
261                 contrast += dir;
262                 contrast *= AO_LCO_CONTRAST_STEP;
263                 if (contrast < AO_LCO_MIN_CONTRAST)
264                         contrast = AO_LCO_MIN_CONTRAST;
265                 if (contrast > AO_LCO_MAX_CONTRAST)
266                         contrast = AO_LCO_MAX_CONTRAST;
267                 ao_lco_set_contrast(contrast);
268                 break;
269         }
270 #endif
271 #ifdef AO_LCO_HAS_BACKLIGHT
272         case AO_LCO_BACKLIGHT: {
273                 int32_t backlight = ao_lco_get_backlight();
274
275                 backlight = (backlight + AO_LCO_BACKLIGHT_STEP - 1) / AO_LCO_BACKLIGHT_STEP;
276                 backlight += dir;
277                 backlight *= AO_LCO_BACKLIGHT_STEP;
278                 if (backlight < AO_LCO_MIN_BACKLIGHT)
279                         backlight = AO_LCO_MIN_BACKLIGHT;
280                 if (backlight > AO_LCO_MAX_BACKLIGHT)
281                         backlight = AO_LCO_MAX_BACKLIGHT;
282                 ao_lco_set_backlight(backlight);
283                 break;
284         }
285 #endif
286 #ifdef AO_LCO_HAS_INFO
287         case AO_LCO_INFO: {
288 #if AO_LCO_MIN_INFO_PAGE < AO_LCO_MAX_INFO_PAGE
289                 int32_t info_page = ao_lco_get_info_page();
290
291                 info += dir;
292                 if (info_page < AO_LCO_MIN_INFO_PAGE)
293                         info_page = AO_LCO_MIN_INFO_PAGE;
294                 if (info_page > AO_LCO_MAX_INFO_PAGE)
295                         info_page = AO_LCO_MAX_INFO_PAGE;
296                 ao_lco_set_info_page();
297 #endif
298                 break;
299         }
300 #endif
301         default:
302                 new_pad = (int16_t) ao_lco_pad;
303                 do {
304                         new_pad += dir;
305                         if (new_pad > AO_PAD_MAX_CHANNELS)
306                                 new_pad = AO_LCO_PAD_VOLTAGE;
307                         if (new_pad < 0)
308                                 new_pad = AO_PAD_MAX_CHANNELS;
309                         if (new_pad == ao_lco_pad)
310                                 break;
311                 } while (!ao_lco_pad_present(ao_lco_box, (uint8_t) new_pad));
312                 PRINTD("New pad %d\n", new_pad);
313                 ao_lco_set_pad((uint8_t) new_pad);
314                 break;
315         }
316 }
317
318 uint8_t
319 ao_lco_box_present(int16_t box)
320 {
321         if (ao_lco_box_pseudo(box))
322                 return 1;
323         if (box >= AO_PAD_MAX_BOXES)
324                 return 0;
325         return (ao_lco_box_mask[AO_LCO_MASK_ID(box)] >> AO_LCO_MASK_SHIFT(box)) & 1;
326 }
327
328 void
329 ao_lco_step_box(int8_t dir)
330 {
331         int16_t new_box = ao_lco_box;
332
333         do {
334                 new_box += dir;
335                 if (new_box > ao_lco_max_box)
336                         new_box = AO_LCO_BOX_FIRST;
337                 else if (new_box < AO_LCO_BOX_FIRST)
338                         new_box = ao_lco_max_box;
339                 if (new_box == ao_lco_box)
340                         break;
341         } while (!ao_lco_box_present(new_box));
342         PRINTD("New box %d\n", new_box);
343         ao_lco_set_box(new_box);
344 }
345
346 void
347 ao_lco_set_armed(uint8_t armed)
348 {
349         if (ao_lco_box_pseudo(ao_lco_box))
350                 return;
351
352         ao_lco_armed = armed;
353         PRINTD("Armed %d\n", ao_lco_armed);
354         if (ao_lco_armed) {
355 #if AO_LCO_DRAG
356                 if (ao_lco_drag_race) {
357                         int16_t box;
358
359                         for (box = ao_lco_min_box; box <= ao_lco_max_box; box++)
360                                 if (ao_lco_selected[box])
361                                         break;
362                         if (box > ao_lco_max_box)
363                                 ao_lco_armed = 0;
364                 } else
365 #endif
366                 {
367                         memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
368                         if (ao_lco_pad != 0)
369                                 ao_lco_selected[ao_lco_box] = (1 << (ao_lco_pad - 1));
370                         else
371                                 ao_lco_armed = 0;
372                 }
373         }
374         ao_wakeup(&ao_lco_armed);
375 }
376
377 void
378 ao_lco_set_firing(uint8_t firing)
379 {
380         ao_lco_firing = firing;
381         PRINTD("Firing %d\n", ao_lco_firing);
382         ao_wakeup(&ao_lco_armed);
383 }
384
385 void
386 ao_lco_search(void)
387 {
388         int8_t          r;
389         int8_t          try;
390         int16_t         box;
391         uint16_t        boxes = 0;
392
393         ao_lco_box_reset_present();
394 #ifdef AO_LCO_SEARCH_API
395         ao_lco_search_start();
396 #else
397         ao_lco_show_box(0);
398         ao_lco_show_pad(0);
399 #endif
400         for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
401 #ifdef AO_LCO_SEARCH_API
402                 ao_lco_search_box_check(box);
403 #else
404                 if ((box % 10) == 0)
405                         ao_lco_show_box(box);
406 #endif
407                 for (try = 0; try < 3; try++) {
408                         ao_lco_tick_offset[box] = 0;
409                         r = ao_lco_query((uint16_t) box, &ao_pad_query, &ao_lco_tick_offset[box]);
410                         PRINTD("box %d result %d offset %d\n", box, r, ao_lco_tick_offset[box]);
411                         if (r == AO_RADIO_CMAC_OK) {
412                                 ++boxes;
413                                 ao_lco_box_set_present(box);
414 #ifdef AO_LCO_SEARCH_API
415                                 ao_lco_search_box_present(box);
416 #else
417                                 ao_lco_show_pad((uint8_t) (boxes % 10));
418 #endif
419                                 ao_delay(AO_MS_TO_TICKS(30));
420                                 break;
421                         }
422                 }
423         }
424         if (ao_lco_min_box <= ao_lco_max_box)
425                 ao_lco_box = ao_lco_min_box;
426         else
427                 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
428         memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
429         memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
430 #ifdef AO_LCO_SEARCH_API
431         ao_lco_search_done();
432 #endif
433         ao_lco_set_box(ao_lco_min_box);
434 }
435
436 void
437 ao_lco_pretend(void)
438 {
439         int16_t box;
440
441         ao_lco_pretending = 1;
442         ao_lco_min_box = 1;
443         ao_lco_max_box = AO_PAD_MAX_BOXES - 1;
444         for (box = ao_lco_min_box; box <= ao_lco_max_box; box++)
445                 ao_lco_box_set_present(box);
446         ao_lco_box = ao_lco_min_box;
447         memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
448         memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
449         ao_lco_set_box(ao_lco_min_box);
450 }
451
452 void
453 ao_lco_monitor(void)
454 {
455         AO_TICK_TYPE    delay;
456         int16_t         box;
457
458         for (;;) {
459                 PRINTD("monitor armed %d firing %d\n",
460                        ao_lco_armed, ao_lco_firing);
461
462                 if (ao_lco_armed && ao_lco_firing) {
463                         ao_lco_ignite(AO_PAD_FIRE);
464                 } else {
465                         ao_lco_update();
466                         if (ao_lco_armed) {
467                                 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
468                                         if (ao_lco_selected[box]) {
469                                                 PRINTD("Arming box %d pads %x\n",
470                                                        box, ao_lco_selected[box]);
471                                                 if (ao_lco_valid[box] & AO_LCO_VALID_EVER) {
472                                                         ao_lco_arm((uint16_t) box, ao_lco_selected[box], ao_lco_tick_offset[box]);
473                                                         ao_delay(AO_MS_TO_TICKS(10));
474                                                 }
475                                         }
476                                 }
477                         }
478                 }
479                 if (ao_lco_armed && ao_lco_firing)
480                         delay = AO_MS_TO_TICKS(100);
481                 else
482                         delay = AO_SEC_TO_TICKS(1);
483                 ao_sleep_for(&ao_lco_armed, delay);
484         }
485 }
486
487 #if AO_LCO_DRAG
488
489 uint8_t                 ao_lco_drag_beep_count;
490 static uint8_t          ao_lco_drag_beep_on;
491 static AO_TICK_TYPE     ao_lco_drag_beep_time;
492 static AO_TICK_TYPE     ao_lco_drag_warn_time;
493
494 #define AO_LCO_DRAG_BEEP_TIME   AO_MS_TO_TICKS(50)
495 #define AO_LCO_DRAG_WARN_TIME   AO_SEC_TO_TICKS(5)
496
497 /* Request 'beeps' additional drag race beeps */
498 void
499 ao_lco_drag_add_beeps(uint8_t beeps)
500 {
501         PRINTD("beep %d\n", beeps);
502         if (ao_lco_drag_beep_count == 0)
503                 ao_lco_drag_beep_time = ao_time();
504         ao_lco_drag_beep_count += beeps;
505         ao_wakeup(&ao_lco_drag_beep_count);
506 }
507
508 /* Toggle current pad in drag set */
509 void
510 ao_lco_toggle_drag(void)
511 {
512         if (ao_lco_drag_race && ao_lco_pad != AO_LCO_PAD_VOLTAGE && !ao_lco_box_pseudo(ao_lco_box)) {
513                 ao_lco_selected[ao_lco_box] ^= (uint8_t) (1 << (ao_lco_pad - 1));
514                 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
515                        ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
516                 ao_lco_drag_add_beeps(ao_lco_pad);
517         }
518 }
519
520 /* Check whether it's time to change the beeper status, then either
521  * turn it on or off as necessary and bump the remaining beep counts
522  */
523
524 AO_TICK_TYPE
525 ao_lco_drag_beep_check(AO_TICK_TYPE now, AO_TICK_TYPE delay)
526 {
527         PRINTD("beep check count %d delta %ld\n",
528                ao_lco_drag_beep_count,
529                (long) (AO_TICK_SIGNED) (now - ao_lco_drag_beep_time));
530         if (ao_lco_drag_beep_count) {
531                 if ((AO_TICK_SIGNED) (now - ao_lco_drag_beep_time) >= 0) {
532                         if (ao_lco_drag_beep_on) {
533                                 ao_beep(0);
534                                 PRINTD("beep stop\n");
535                                 ao_lco_drag_beep_on = 0;
536                                 if (ao_lco_drag_beep_count) {
537                                         --ao_lco_drag_beep_count;
538                                         if (ao_lco_drag_beep_count)
539                                                 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
540                                 }
541                         } else {
542                                 ao_beep(AO_BEEP_HIGH);
543                                 PRINTD("beep start\n");
544                                 ao_lco_drag_beep_on = 1;
545                                 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
546                         }
547                 }
548         }
549
550         if (ao_lco_drag_beep_count) {
551                 AO_TICK_TYPE beep_delay = 0;
552
553                 if (ao_lco_drag_beep_time > now)
554                         beep_delay = ao_lco_drag_beep_time - now;
555
556                 if (delay > beep_delay)
557                         delay = beep_delay;
558         }
559         return delay;
560 }
561
562 void
563 ao_lco_drag_enable(void)
564 {
565         if (!ao_lco_drag_race) {
566                 PRINTD("Drag enable\n");
567                 ao_lco_drag_race = 1;
568                 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
569 #ifdef AO_LED_DRAG
570                 ao_led_on(AO_LED_DRAG);
571 #endif
572                 ao_lco_drag_add_beeps(5);
573                 ao_lco_show();
574         }
575 }
576
577 void
578 ao_lco_drag_disable(void)
579 {
580         if (ao_lco_drag_race) {
581                 PRINTD("Drag disable\n");
582                 ao_lco_drag_race = 0;
583 #ifdef AO_LED_DRAG
584                 ao_led_off(AO_LED_DRAG);
585 #endif
586                 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
587                 ao_lco_drag_add_beeps(2);
588                 ao_lco_show();
589         }
590 }
591
592 /* add a beep if it's time to warn the user that drag race mode is
593  * active
594  */
595
596 AO_TICK_TYPE
597 ao_lco_drag_warn_check(AO_TICK_TYPE now, AO_TICK_TYPE delay)
598 {
599         if (ao_lco_drag_race) {
600                 AO_TICK_TYPE    warn_delay;
601
602                 if ((AO_TICK_SIGNED) (now - ao_lco_drag_warn_time) >= 0) {
603                         ao_lco_drag_add_beeps(1);
604                         ao_lco_drag_warn_time = now + AO_LCO_DRAG_WARN_TIME;
605                 }
606                 warn_delay = ao_lco_drag_warn_time - now;
607                 if (delay > warn_delay)
608                         delay = warn_delay;
609         }
610         return delay;
611 }
612 #endif /* AO_LCO_DRAG */
613
614 /* task function for beeping while arm is active */
615 void
616 ao_lco_arm_warn(void)
617 {
618         for (;;) {
619                 while (!ao_lco_armed) {
620 #ifdef AO_LED_FIRE
621                         ao_led_off(AO_LED_FIRE);
622 #endif
623                         ao_sleep(&ao_lco_armed);
624                 }
625 #ifdef AO_LED_FIRE
626                 ao_led_on(AO_LED_FIRE);
627 #endif
628                 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
629                 ao_delay(AO_MS_TO_TICKS(200));
630         }
631 }