altos: Report additional pyro channels via beeper
[fw/altos] / src / kernel / 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 /*
56  * Farnsworth spacing
57  *
58  * From: http://www.arrl.org/files/file/Technology/x9004008.pdf
59  *
60  *      c:      character rate in wpm
61  *      s:      overall rate in wpm
62  *      u:      unit rate (dit speed)
63  *
64  *      dit:                    u
65  *      dah:                    3u
66  *      intra-character-time:   u
67  *
68  *      u = 1.2/c
69  *
70  * Because our clock runs at 10ms, we'll round up to 70ms for u, which
71  * is about 17wpm
72  *
73  *      Farnsworth adds space between characters and
74  *      words:
75  *           60 c - 37.2 s
76  *      Ta = -------------
77  *                sc
78  *
79  *           3 Ta
80  *      Tc = ----
81  *            19
82  *
83  *           7 Ta
84  *      Tw = ----
85  *            19
86  *
87  *      Ta = total delay to add to the characters (31 units)
88  *           of a standard 50-unit "word", in seconds
89  *
90  *      Tc = period between characters, in seconds
91  *
92  *      Tw = period between words, in seconds
93  *
94  * We'll use Farnsworth spacing with c=18 and s=12:
95  *
96  *      u = 1.2/18 = 0.0667
97  *
98  *      Ta = (60 * 17 - 37.2 * 12) / (17 * 12) = 2.812
99  *
100  *      Tc = 3 * Ta / 19 = .444
101  *
102  *      Tw = 1.036
103  *
104  * Note that the values below are all reduced by 10ms; that's because
105  * the timer always adds a tick to make sure the task actually sleeps
106  * at least as long as the argument.
107  */
108
109 static void
110 ao_report_beep(void) __reentrant
111 {
112         uint8_t r = flight_reports[ao_flight_state];
113         uint8_t l = r & 7;
114
115         if (!r)
116                 return;
117         while (l--) {
118                 if (r & 8)
119                         mid(AO_MS_TO_TICKS(200));
120                 else
121                         mid(AO_MS_TO_TICKS(60));
122                 pause(AO_MS_TO_TICKS(60));
123                 r >>= 1;
124         }
125         pause(AO_MS_TO_TICKS(360));
126 }
127
128 static void
129 ao_report_digit(uint8_t digit) __reentrant
130 {
131         if (!digit) {
132                 mid(AO_MS_TO_TICKS(500));
133                 pause(AO_MS_TO_TICKS(200));
134         } else {
135                 while (digit--) {
136                         mid(AO_MS_TO_TICKS(200));
137                         pause(AO_MS_TO_TICKS(200));
138                 }
139         }
140         pause(AO_MS_TO_TICKS(300));
141 }
142
143 static void
144 ao_report_number(int16_t n)
145 {
146         __xdata uint8_t digits[10];
147         __pdata uint8_t ndigits, i;
148
149         if (n < 0)
150                 n = 0;
151         ndigits = 0;
152         do {
153                 digits[ndigits++] = n % 10;
154                 n /= 10;
155         } while (n);
156
157         i = ndigits;
158         do
159                 ao_report_digit(digits[--i]);
160         while (i != 0);
161 }
162
163 static void
164 ao_report_altitude(void)
165 {
166         ao_report_number(ao_max_height);
167 }
168
169 #if HAS_BATTERY_REPORT
170 static void
171 ao_report_battery(void)
172 {
173         __xdata struct ao_data packet;
174         for (;;) {
175                 ao_data_get(&packet);
176                 if (packet.adc.v_batt != 0)
177                         break;
178                 ao_sleep(DATA_TO_XDATA(&ao_sample_data));
179         }
180         ao_report_number(ao_battery_decivolt(packet.adc.v_batt));
181 }
182 #endif
183
184 #if HAS_IGNITE_REPORT
185 static uint8_t
186 ao_report_igniter_ready(enum ao_igniter igniter)
187 {
188         return ao_igniter_status(igniter) == ao_igniter_ready ? 1 : 0;
189 }
190
191 uint8_t
192 ao_report_igniter(void)
193 {
194         return (ao_report_igniter_ready(ao_igniter_drogue) |
195                      (ao_report_igniter_ready(ao_igniter_main) << 1));
196 }
197
198 static void
199 ao_report_continuity(void) __reentrant
200 {
201         uint8_t c;
202
203 #if !HAS_IGNITE
204         if (!ao_igniter_present)
205                 return;
206 #endif
207         c = ao_report_igniter();
208         if (c) {
209                 while (c--) {
210                         high(AO_MS_TO_TICKS(25));
211                         pause(AO_MS_TO_TICKS(100));
212                 }
213         } else {
214                 c = 5;
215                 while (c--) {
216                         high(AO_MS_TO_TICKS(20));
217                         low(AO_MS_TO_TICKS(20));
218                 }
219         }
220 #if AO_PYRO_NUM
221         pause(AO_MS_TO_TICKS(250));
222         for(c = 0; c < AO_PYRO_NUM; c++) {
223                 enum ao_igniter_status  status = ao_pyro_status(c);
224                 if (status == ao_igniter_ready)
225                         mid(AO_MS_TO_TICKS(25));
226                 else
227                         low(AO_MS_TO_TICKS(25));
228                 pause(AO_MS_TO_TICKS(200));
229         }
230 #endif
231 #if HAS_LOG
232         if (ao_log_full()) {
233                 pause(AO_MS_TO_TICKS(100));
234                 c = 2;
235                 while (c--) {
236                         low(AO_MS_TO_TICKS(100));
237                         mid(AO_MS_TO_TICKS(100));
238                         high(AO_MS_TO_TICKS(100));
239                         mid(AO_MS_TO_TICKS(100));
240                 }
241         }
242 #endif
243 }
244 #endif
245
246 void
247 ao_report(void)
248 {
249         ao_report_state = ao_flight_state;
250         for(;;) {
251 #if HAS_BATTERY_REPORT
252                 if (ao_flight_state == ao_flight_startup)
253                         ao_report_battery();
254                 else
255 #endif
256                         ao_report_beep();
257                 if (ao_flight_state == ao_flight_landed) {
258                         ao_report_altitude();
259 #if HAS_FLIGHT
260                         ao_delay(AO_SEC_TO_TICKS(5));
261                         continue;
262 #endif
263                 }
264 #if HAS_IGNITE_REPORT
265                 if (ao_flight_state == ao_flight_idle)
266                         ao_report_continuity();
267                 while (ao_flight_state == ao_flight_pad) {
268                         uint8_t c;
269                         ao_report_continuity();
270                         c = 50;
271                         while (c-- && ao_flight_state == ao_flight_pad)
272                                 pause(AO_MS_TO_TICKS(100));
273                 }
274 #endif
275
276                 while (ao_report_state == ao_flight_state)
277                         ao_sleep(DATA_TO_XDATA(&ao_flight_state));
278                 ao_report_state = ao_flight_state;
279         }
280 }
281
282 static __xdata struct ao_task ao_report_task;
283
284 void
285 ao_report_init(void)
286 {
287         ao_add_task(&ao_report_task, ao_report, "report");
288 }