altos: Report battery voltage instead of S at startup
[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_number(int16_t n)
91 {
92         __xdata uint8_t digits[10];
93         __pdata uint8_t ndigits, i;
94
95         if (n < 0)
96                 n = 0;
97         ndigits = 0;
98         do {
99                 digits[ndigits++] = n % 10;
100                 n /= 10;
101         } while (n);
102
103         i = ndigits;
104         do
105                 ao_report_digit(digits[--i]);
106         while (i != 0);
107 }
108
109 static void
110 ao_report_altitude(void)
111 {
112         ao_report_number(ao_max_height);
113 }
114
115 #if HAS_BATTERY_REPORT
116 static void
117 ao_report_battery(void)
118 {
119         __xdata struct ao_data packet;
120         for (;;) {
121                 ao_data_get(&packet);
122                 if (packet.adc.v_batt != 0)
123                         break;
124                 ao_sleep(DATA_TO_XDATA(&ao_sample_data));
125         }
126         ao_report_number(ao_battery_decivolt(packet.adc.v_batt));
127 }
128 #endif
129
130 #if HAS_IGNITE_REPORT
131 static uint8_t
132 ao_report_igniter_ready(enum ao_igniter igniter)
133 {
134         return ao_igniter_status(igniter) == ao_igniter_ready ? 1 : 0;
135 }
136
137 uint8_t
138 ao_report_igniter(void)
139 {
140         return (ao_report_igniter_ready(ao_igniter_drogue) |
141                      (ao_report_igniter_ready(ao_igniter_main) << 1));
142 }
143
144 static void
145 ao_report_continuity(void) __reentrant
146 {
147         uint8_t c;
148
149 #if !HAS_IGNITE
150         if (!ao_igniter_present)
151                 return;
152 #endif
153         c = ao_report_igniter();
154         if (c) {
155                 while (c--) {
156                         high(AO_MS_TO_TICKS(25));
157                         pause(AO_MS_TO_TICKS(100));
158                 }
159         } else {
160                 c = 10;
161                 while (c--) {
162                         high(AO_MS_TO_TICKS(20));
163                         low(AO_MS_TO_TICKS(20));
164                 }
165         }
166 #if HAS_LOG
167         if (ao_log_full()) {
168                 pause(AO_MS_TO_TICKS(100));
169                 c = 2;
170                 while (c--) {
171                         low(AO_MS_TO_TICKS(100));
172                         mid(AO_MS_TO_TICKS(100));
173                         high(AO_MS_TO_TICKS(100));
174                         mid(AO_MS_TO_TICKS(100));
175                 }
176         }
177 #endif
178 }
179 #endif
180
181 void
182 ao_report(void)
183 {
184         ao_report_state = ao_flight_state;
185         for(;;) {
186 #if HAS_BATTERY_REPORT
187                 if (ao_flight_state == ao_flight_startup)
188                         ao_report_battery();
189                 else
190 #endif
191                         ao_report_beep();
192                 if (ao_flight_state == ao_flight_landed) {
193                         ao_report_altitude();
194 #if HAS_FLIGHT
195                         ao_delay(AO_SEC_TO_TICKS(5));
196                         continue;
197 #endif
198                 }
199 #if HAS_IGNITE_REPORT
200                 if (ao_flight_state == ao_flight_idle)
201                         ao_report_continuity();
202                 while (ao_flight_state == ao_flight_pad) {
203                         uint8_t c;
204                         ao_report_continuity();
205                         c = 50;
206                         while (c-- && ao_flight_state == ao_flight_pad)
207                                 pause(AO_MS_TO_TICKS(100));
208                 }
209 #endif
210
211                 while (ao_report_state == ao_flight_state)
212                         ao_sleep(DATA_TO_XDATA(&ao_flight_state));
213                 ao_report_state = ao_flight_state;
214         }
215 }
216
217 static __xdata struct ao_task ao_report_task;
218
219 void
220 ao_report_init(void)
221 {
222         ao_add_task(&ao_report_task, ao_report, "report");
223 }