7c9287928e28a5cdbd5ac8b6bce75a48594c88ee
[fw/altos] / src / core / 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 #define BIT(i,x)           ((x) ? (1 << (i)) : 0)
21 #define MORSE1(a)          (1 | BIT(3,a))
22 #define MORSE2(a,b)        (2 | BIT(3,a) | BIT(4,b))
23 #define MORSE3(a,b,c)      (3 | BIT(3,a) | BIT(4,b) | BIT(5,c))
24 #define MORSE4(a,b,c,d)    (4 | BIT(3,a) | BIT(4,b) | BIT(5,c) | BIT(6,d))
25 #define MORSE5(a,b,c,d,e)  (5 | BIT(3,a) | BIT(4,b) | BIT(5,c) | BIT(6,d) | BIT(7,e))
26
27 static const uint8_t flight_reports[] = {
28         MORSE3(0,0,0),          /* startup, 'S' */
29         MORSE2(0,0),            /* idle 'I' */
30         MORSE4(0,1,1,0),        /* pad 'P' */
31         MORSE4(1,0,0,0),        /* boost 'B' */
32         MORSE4(0,0,1,0),        /* fast 'F' */
33         MORSE4(1,0,1,0),        /* coast 'C' */
34         MORSE3(1,0,0),          /* drogue 'D' */
35         MORSE2(1,1),            /* main 'M' */
36         MORSE4(0,1,0,0),        /* landed 'L' */
37         MORSE4(1,0,0,1),        /* invalid 'X' */
38 };
39
40 #if HAS_BEEP
41 #define low(time)       ao_beep_for(AO_BEEP_LOW, time)
42 #define mid(time)       ao_beep_for(AO_BEEP_MID, time)
43 #define high(time)      ao_beep_for(AO_BEEP_HIGH, time)
44 #else
45 #define low(time)       ao_led_for(AO_LED_GREEN, time)
46 #define mid(time)       ao_led_for(AO_LED_RED, time)
47 #define high(time)      ao_led_for(AO_LED_GREEN|AO_LED_RED, time)
48 #endif
49 #define pause(time)     ao_delay(time)
50
51 static __pdata enum ao_flight_state ao_report_state;
52
53 static void
54 ao_report_beep(void) __reentrant
55 {
56         uint8_t r = flight_reports[ao_flight_state];
57         uint8_t l = r & 7;
58
59         if (!r)
60                 return;
61         while (l--) {
62                 if (r & 8)
63                         mid(AO_MS_TO_TICKS(600));
64                 else
65                         mid(AO_MS_TO_TICKS(200));
66                 pause(AO_MS_TO_TICKS(200));
67                 r >>= 1;
68         }
69         pause(AO_MS_TO_TICKS(400));
70 }
71
72 static void
73 ao_report_digit(uint8_t digit) __reentrant
74 {
75         if (!digit) {
76                 mid(AO_MS_TO_TICKS(500));
77                 pause(AO_MS_TO_TICKS(200));
78         } else {
79                 while (digit--) {
80                         mid(AO_MS_TO_TICKS(200));
81                         pause(AO_MS_TO_TICKS(200));
82                 }
83         }
84         pause(AO_MS_TO_TICKS(300));
85 }
86
87 static void
88 ao_report_altitude(void)
89 {
90         __pdata int16_t agl = ao_max_height;
91         __xdata uint8_t digits[10];
92         __pdata uint8_t ndigits, i;
93
94         if (agl < 0)
95                 agl = 0;
96         ndigits = 0;
97         do {
98                 digits[ndigits++] = agl % 10;
99                 agl /= 10;
100         } while (agl);
101
102         i = ndigits;
103         do
104                 ao_report_digit(digits[--i]);
105         while (i != 0);
106 }
107
108 #if HAS_IGNITE_REPORT
109 static uint8_t
110 ao_report_igniter_ready(enum ao_igniter igniter)
111 {
112         return ao_igniter_status(igniter) == ao_igniter_ready ? 1 : 0;
113 }
114
115 static void
116 ao_report_continuity(void) __reentrant
117 {
118         uint8_t c;
119
120 #if !HAS_IGNITE
121         if (!ao_igniter_present)
122                 return;
123 #endif
124         c = (ao_report_igniter_ready(ao_igniter_drogue) |
125                      (ao_report_igniter_ready(ao_igniter_main) << 1));
126         if (c) {
127                 while (c--) {
128                         high(AO_MS_TO_TICKS(25));
129                         pause(AO_MS_TO_TICKS(100));
130                 }
131         } else {
132                 c = 10;
133                 while (c--) {
134                         high(AO_MS_TO_TICKS(20));
135                         low(AO_MS_TO_TICKS(20));
136                 }
137         }
138 #if HAS_LOG
139         if (ao_log_full()) {
140                 pause(AO_MS_TO_TICKS(100));
141                 c = 2;
142                 while (c--) {
143                         low(AO_MS_TO_TICKS(100));
144                         mid(AO_MS_TO_TICKS(100));
145                         high(AO_MS_TO_TICKS(100));
146                         mid(AO_MS_TO_TICKS(100));
147                 }
148         }
149 #endif
150 }
151 #endif
152
153 void
154 ao_report(void)
155 {
156         ao_report_state = ao_flight_state;
157         for(;;) {
158                 ao_report_beep();
159                 if (ao_flight_state == ao_flight_landed) {
160                         ao_report_altitude();
161 #if HAS_FLIGHT
162                         ao_delay(AO_SEC_TO_TICKS(5));
163                         continue;
164 #endif
165                 }
166 #if HAS_IGNITE_REPORT
167                 if (ao_flight_state == ao_flight_idle)
168                         ao_report_continuity();
169                 while (ao_flight_state == ao_flight_pad) {
170                         uint8_t c;
171                         ao_report_continuity();
172                         c = 50;
173                         while (c-- && ao_flight_state == ao_flight_pad)
174                                 pause(AO_MS_TO_TICKS(100));
175                 }
176 #endif
177
178                 while (ao_report_state == ao_flight_state)
179                         ao_sleep(DATA_TO_XDATA(&ao_flight_state));
180                 ao_report_state = ao_flight_state;
181         }
182 }
183
184 static __xdata struct ao_task ao_report_task;
185
186 void
187 ao_report_init(void)
188 {
189         ao_add_task(&ao_report_task, ao_report, "report");
190 }