Format GPS seconds as %02d.%04d to avoid spaces in the value
[fw/altos] / src / 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 static const char * __xdata flight_reports[] = {
21         "...",          /* startup, 'S' */
22         "..",           /* idle 'I' */
23         ".--.",         /* launchpad 'P' */
24         "-...",         /* boost 'B' */
25         "-.-.",         /* coast 'C' */
26         ".-",           /* apogee 'A' */
27         "-..",          /* drogue 'D' */
28         "--",           /* main 'M' */
29         ".-..",         /* landed 'L' */
30         ".-.-.-",       /* invalid */
31 };
32
33 #if 1
34 #define signal(time)    ao_beep_for(AO_BEEP_MID, time)
35 #else
36 #define signal(time)    ao_led_for(AO_LED_RED, time)
37 #endif
38 #define pause(time)     ao_delay(time)
39
40 static __xdata enum ao_flight_state ao_report_state;
41
42 static void
43 ao_report_beep(void) __reentrant
44 {
45         char *r = flight_reports[ao_flight_state];
46         char c;
47
48         if (!r)
49                 return;
50         while (c = *r++) {
51                 if (c == '.')
52                         signal(AO_MS_TO_TICKS(200));
53                 else
54                         signal(AO_MS_TO_TICKS(600));
55                 pause(AO_MS_TO_TICKS(200));
56         }
57         pause(AO_MS_TO_TICKS(400));
58 }
59
60 static void
61 ao_report_digit(uint8_t digit) __reentrant
62 {
63         if (!digit) {
64                 signal(AO_MS_TO_TICKS(500));
65                 pause(AO_MS_TO_TICKS(200));
66         } else {
67                 while (digit--) {
68                         signal(AO_MS_TO_TICKS(200));
69                         pause(AO_MS_TO_TICKS(200));
70                 }
71         }
72         pause(AO_MS_TO_TICKS(300));
73 }
74
75 static void
76 ao_report_altitude(void)
77 {
78         __xdata int16_t agl = ao_pres_to_altitude(ao_min_pres) - ao_pres_to_altitude(ao_ground_pres);
79         __xdata uint8_t digits[10];
80         __xdata uint8_t ndigits, i;
81
82         if (agl < 0)
83                 agl = 0;
84         ndigits = 0;
85         do {
86                 digits[ndigits++] = agl % 10;
87                 agl /= 10;
88         } while (agl);
89
90         for (;;) {
91                 ao_report_beep();
92                 i = ndigits;
93                 do
94                         ao_report_digit(digits[--i]);
95                 while (i != 0);
96                 pause(AO_SEC_TO_TICKS(5));
97         }
98 }
99
100 void
101 ao_report(void)
102 {
103         ao_report_state = ao_flight_state;
104         for(;;) {
105                 if (ao_flight_state == ao_flight_landed)
106                         ao_report_altitude();
107                 ao_report_beep();
108                 __critical {
109                         while (ao_report_state == ao_flight_state)
110                                 ao_sleep(DATA_TO_XDATA(&ao_flight_state));
111                         ao_report_state = ao_flight_state;
112                 }
113         }
114 }
115
116 static __xdata struct ao_task ao_report_task;
117
118 void
119 ao_report_init(void)
120 {
121         ao_add_task(&ao_report_task, ao_report, "report");
122 }