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