Keep reporting flight state while it changes
[fw/altos] / ao_report.c
1 /*
2  * Copyright © 2009 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
21 static const char * __xdata flight_reports[] = {
22         "...",          /* startup, 'S' */
23         "..",           /* idle 'I' */
24         ".--.",         /* launchpad 'P' */
25         "-...",         /* boost 'B' */
26         "-.-.",         /* coast 'C' */
27         ".-",           /* apogee 'A' */
28         "-..",          /* drogue 'D' */
29         "--",           /* main 'M' */
30         ".-..",         /* landed 'L' */
31         ".-.-.-",       /* invalid */
32 };
33
34 #if 1
35 #define signal(time)    ao_beep_for(AO_BEEP_MID, time)
36 #else
37 #define signal(time)    ao_led_for(AO_LED_RED, time)
38 #endif
39 #define pause(time)     ao_delay(time)
40
41 static __xdata enum ao_flight_state ao_report_state;
42
43 static void
44 ao_report_beep(void)
45 {
46         char *r = flight_reports[ao_report_state];
47         char c;
48
49         if (!r)
50                 return;
51         while (c = *r++) {
52                 if (c == '.')
53                         signal(AO_MS_TO_TICKS(200));
54                 else
55                         signal(AO_MS_TO_TICKS(600));
56                 pause(AO_MS_TO_TICKS(200));
57         }
58         pause(AO_MS_TO_TICKS(400));
59 }
60
61 void
62 ao_report_notify(void)
63 {
64         ao_wakeup(&ao_report_state);
65 }
66
67 void
68 ao_report(void)
69 {
70         ao_report_state = ao_flight_state;
71         for(;;) {
72                 ao_report_beep();
73                 __critical {
74                         while (ao_report_state == ao_flight_state)
75                                 ao_sleep(&ao_report_state);
76                         ao_report_state = ao_flight_state;
77                 }
78         }
79 }
80
81 static __xdata struct ao_task ao_report_task;
82
83 void
84 ao_report_init(void)
85 {
86         ao_add_task(&ao_report_task, ao_report);
87 }