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