altos: Fix telelcotwo build for new ao_lco_bits.c file
[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_display();
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         if (new_pad != ao_lco_pad) {
206                 ao_lco_pad = new_pad;
207                 ao_lco_show_display();
208         }
209 }
210
211 void
212 ao_lco_set_box(uint16_t new_box)
213 {
214         if (ao_lco_box != new_box) {
215                 ao_lco_box = new_box;
216 #if AO_LCO_DRAG
217                 if (ao_lco_box != AO_LCO_BOX_DRAG)
218 #endif
219                         ao_lco_channels[ao_lco_box] = 0;
220                 ao_lco_set_pad(1);
221         }
222 }
223
224 void
225 ao_lco_step_pad(int8_t dir)
226 {
227         int8_t  new_pad;
228
229         new_pad = ao_lco_pad;
230         do {
231                 new_pad += dir;
232                 if (new_pad > AO_PAD_MAX_CHANNELS)
233                         new_pad = AO_LCO_PAD_VOLTAGE;
234                 if (new_pad < 0)
235                         new_pad = AO_PAD_MAX_CHANNELS;
236                 if (new_pad == ao_lco_pad)
237                         break;
238         } while (!ao_lco_pad_present(ao_lco_box, new_pad));
239         ao_lco_set_pad(new_pad);
240 }
241
242 void
243 ao_lco_set_armed(uint8_t armed)
244 {
245         ao_lco_armed = armed;
246         PRINTD("Armed %d\n", ao_lco_armed);
247         if (ao_lco_armed) {
248                 if (ao_lco_drag_race) {
249                         uint8_t box;
250
251                         for (box = ao_lco_min_box; box <= ao_lco_max_box; box++)
252                                 if (ao_lco_selected[box])
253                                         break;
254                         if (box > ao_lco_max_box)
255                                 ao_lco_armed = 0;
256                 } else {
257                         memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
258                         if (ao_lco_pad != 0)
259                                 ao_lco_selected[ao_lco_box] = (1 << (ao_lco_pad - 1));
260                         else
261                                 ao_lco_armed = 0;
262                 }
263         }
264         ao_wakeup(&ao_lco_armed);
265 }
266
267 void
268 ao_lco_set_firing(uint8_t firing)
269 {
270         ao_lco_firing = firing;
271         PRINTD("Firing %d\n", ao_lco_firing);
272         ao_wakeup(&ao_lco_armed);
273 }
274
275 void
276 ao_lco_search(void)
277 {
278         int8_t          r;
279         int8_t          try;
280         uint8_t         box;
281         uint8_t         boxes = 0;
282
283         ao_lco_box_reset_present();
284         ao_lco_set_pad(0);
285         for (box = 0; box < AO_PAD_MAX_BOXES; box++) {
286                 if ((box % 10) == 0)
287                         ao_lco_set_box(box);
288                 for (try = 0; try < 3; try++) {
289                         ao_lco_tick_offset[box] = 0;
290                         r = ao_lco_query(box, &ao_pad_query, &ao_lco_tick_offset[box]);
291                         PRINTD("box %d result %d offset %d\n", box, r, ao_lco_tick_offset[box]);
292                         if (r == AO_RADIO_CMAC_OK) {
293                                 ++boxes;
294                                 ao_lco_box_set_present(box);
295                                 ao_lco_set_pad(boxes % 10);
296                                 ao_delay(AO_MS_TO_TICKS(30));
297                                 break;
298                         }
299                 }
300         }
301         if (ao_lco_min_box <= ao_lco_max_box)
302                 ao_lco_box = ao_lco_min_box;
303         else
304                 ao_lco_min_box = ao_lco_max_box = ao_lco_box = 0;
305         memset(ao_lco_valid, 0, sizeof (ao_lco_valid));
306         memset(ao_lco_channels, 0, sizeof (ao_lco_channels));
307         ao_lco_set_pad(1);
308 }
309
310 void
311 ao_lco_monitor(void)
312 {
313         uint16_t                delay;
314         uint8_t                 box;
315
316         for (;;) {
317                 PRINTD("monitor armed %d firing %d\n",
318                        ao_lco_armed, ao_lco_firing);
319
320                 if (ao_lco_armed && ao_lco_firing) {
321                         ao_lco_ignite(AO_PAD_FIRE);
322                 } else {
323                         ao_lco_update();
324                         if (ao_lco_armed) {
325                                 for (box = ao_lco_min_box; box <= ao_lco_max_box; box++) {
326                                         if (ao_lco_selected[box]) {
327                                                 PRINTD("Arming box %d pads %x\n",
328                                                        box, ao_lco_selected[box]);
329                                                 if (ao_lco_valid[box] & AO_LCO_VALID_EVER) {
330                                                         ao_lco_arm(box, ao_lco_selected[box], ao_lco_tick_offset[box]);
331                                                         ao_delay(AO_MS_TO_TICKS(10));
332                                                 }
333                                         }
334                                 }
335                         }
336                 }
337                 if (ao_lco_armed && ao_lco_firing)
338                         delay = AO_MS_TO_TICKS(100);
339                 else
340                         delay = AO_SEC_TO_TICKS(1);
341                 ao_sleep_for(&ao_lco_armed, delay);
342         }
343 }
344
345 #if AO_LCO_DRAG
346
347 uint8_t                 ao_lco_drag_beep_count;
348 static uint8_t          ao_lco_drag_beep_on;
349 static uint16_t         ao_lco_drag_beep_time;
350 static uint16_t         ao_lco_drag_warn_time;
351
352 #define AO_LCO_DRAG_BEEP_TIME   AO_MS_TO_TICKS(50)
353 #define AO_LCO_DRAG_WARN_TIME   AO_SEC_TO_TICKS(5)
354
355 /* Request 'beeps' additional drag race beeps */
356 void
357 ao_lco_drag_add_beeps(uint8_t beeps)
358 {
359         PRINTD("beep %d\n", beeps);
360         if (ao_lco_drag_beep_count == 0)
361                 ao_lco_drag_beep_time = ao_time();
362         ao_lco_drag_beep_count += beeps;
363         ao_wakeup(&ao_lco_drag_beep_count);
364 }
365
366 /* Toggle current pad in drag set */
367 void
368 ao_lco_toggle_drag(void)
369 {
370         if (ao_lco_drag_race && ao_lco_pad != AO_LCO_PAD_VOLTAGE) {
371                 ao_lco_selected[ao_lco_box] ^= (1 << (ao_lco_pad - 1));
372                 PRINTD("Toggle box %d pad %d (pads now %x) to drag race\n",
373                        ao_lco_pad, ao_lco_box, ao_lco_selected[ao_lco_box]);
374                 ao_lco_drag_add_beeps(ao_lco_pad);
375         }
376 }
377
378 /* Check whether it's time to change the beeper status, then either
379  * turn it on or off as necessary and bump the remaining beep counts
380  */
381
382 uint16_t
383 ao_lco_drag_beep_check(uint16_t now, uint16_t delay)
384 {
385         PRINTD("beep check count %d delta %d\n",
386                ao_lco_drag_beep_count,
387                (int16_t) (now - ao_lco_drag_beep_time));
388         if (ao_lco_drag_beep_count) {
389                 if ((int16_t) (now - ao_lco_drag_beep_time) >= 0) {
390                         if (ao_lco_drag_beep_on) {
391                                 ao_beep(0);
392                                 PRINTD("beep stop\n");
393                                 ao_lco_drag_beep_on = 0;
394                                 if (ao_lco_drag_beep_count) {
395                                         --ao_lco_drag_beep_count;
396                                         if (ao_lco_drag_beep_count)
397                                                 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
398                                 }
399                         } else {
400                                 ao_beep(AO_BEEP_HIGH);
401                                 PRINTD("beep start\n");
402                                 ao_lco_drag_beep_on = 1;
403                                 ao_lco_drag_beep_time = now + AO_LCO_DRAG_BEEP_TIME;
404                         }
405                 }
406         }
407
408         if (ao_lco_drag_beep_count) {
409                 uint16_t beep_delay = 0;
410
411                 if (ao_lco_drag_beep_time > now)
412                         beep_delay = ao_lco_drag_beep_time - now;
413
414                 if (delay > beep_delay)
415                         delay = beep_delay;
416         }
417         return delay;
418 }
419
420 void
421 ao_lco_drag_enable(void)
422 {
423         if (!ao_lco_drag_race) {
424                 PRINTD("Drag enable\n");
425                 ao_lco_drag_race = 1;
426                 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
427 #ifdef AO_LED_DRAG
428                 ao_led_on(AO_LED_DRAG);
429 #endif
430                 ao_lco_drag_add_beeps(5);
431                 ao_lco_show_display();
432         }
433 }
434
435 void
436 ao_lco_drag_disable(void)
437 {
438         if (ao_lco_drag_race) {
439                 PRINTD("Drag disable\n");
440                 ao_lco_drag_race = 0;
441 #ifdef AO_LED_DRAG
442                 ao_led_off(AO_LED_DRAG);
443 #endif
444                 memset(ao_lco_selected, 0, sizeof (ao_lco_selected));
445                 ao_lco_drag_add_beeps(2);
446                 ao_lco_show_display();
447         }
448 }
449
450 /* add a beep if it's time to warn the user that drag race mode is
451  * active
452  */
453
454 uint16_t
455 ao_lco_drag_warn_check(uint16_t now, uint16_t delay)
456 {
457         if (ao_lco_drag_race) {
458                 uint16_t        warn_delay;
459
460                 if ((int16_t) (now - ao_lco_drag_warn_time) >= 0) {
461                         ao_lco_drag_add_beeps(1);
462                         ao_lco_drag_warn_time = now + AO_LCO_DRAG_WARN_TIME;
463                 }
464                 warn_delay = ao_lco_drag_warn_time - now;
465                 if (delay > warn_delay)
466                         delay = warn_delay;
467         }
468         return delay;
469 }
470 #endif /* AO_LCO_DRAG */
471
472 /* task function for beeping while arm is active */
473 void
474 ao_lco_arm_warn(void)
475 {
476         for (;;) {
477                 while (!ao_lco_armed) {
478 #ifdef AO_LED_FIRE
479                         ao_led_off(AO_LED_FIRE);
480 #endif
481                         ao_sleep(&ao_lco_armed);
482                 }
483 #ifdef AO_LED_FIRE
484                 ao_led_on(AO_LED_FIRE);
485 #endif
486                 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
487                 ao_delay(AO_MS_TO_TICKS(200));
488         }
489 }