altos: Rename 'core' to 'kernel'
[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 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 uint8_t
118 ao_report_igniter(void)
119 {
120         return (ao_report_igniter_ready(ao_igniter_drogue) |
121                      (ao_report_igniter_ready(ao_igniter_main) << 1));
122 }
123
124 static void
125 ao_report_continuity(void) __reentrant
126 {
127         uint8_t c;
128
129 #if !HAS_IGNITE
130         if (!ao_igniter_present)
131                 return;
132 #endif
133         c = ao_report_igniter();
134         if (c) {
135                 while (c--) {
136                         high(AO_MS_TO_TICKS(25));
137                         pause(AO_MS_TO_TICKS(100));
138                 }
139         } else {
140                 c = 10;
141                 while (c--) {
142                         high(AO_MS_TO_TICKS(20));
143                         low(AO_MS_TO_TICKS(20));
144                 }
145         }
146 #if HAS_LOG
147         if (ao_log_full()) {
148                 pause(AO_MS_TO_TICKS(100));
149                 c = 2;
150                 while (c--) {
151                         low(AO_MS_TO_TICKS(100));
152                         mid(AO_MS_TO_TICKS(100));
153                         high(AO_MS_TO_TICKS(100));
154                         mid(AO_MS_TO_TICKS(100));
155                 }
156         }
157 #endif
158 }
159 #endif
160
161 void
162 ao_report(void)
163 {
164         ao_report_state = ao_flight_state;
165         for(;;) {
166                 ao_report_beep();
167                 if (ao_flight_state == ao_flight_landed) {
168                         ao_report_altitude();
169 #if HAS_FLIGHT
170                         ao_delay(AO_SEC_TO_TICKS(5));
171                         continue;
172 #endif
173                 }
174 #if HAS_IGNITE_REPORT
175                 if (ao_flight_state == ao_flight_idle)
176                         ao_report_continuity();
177                 while (ao_flight_state == ao_flight_pad) {
178                         uint8_t c;
179                         ao_report_continuity();
180                         c = 50;
181                         while (c-- && ao_flight_state == ao_flight_pad)
182                                 pause(AO_MS_TO_TICKS(100));
183                 }
184 #endif
185
186                 while (ao_report_state == ao_flight_state)
187                         ao_sleep(DATA_TO_XDATA(&ao_flight_state));
188                 ao_report_state = ao_flight_state;
189         }
190 }
191
192 static __xdata struct ao_task ao_report_task;
193
194 void
195 ao_report_init(void)
196 {
197         ao_add_task(&ao_report_task, ao_report, "report");
198 }