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