altos: Check requested log max size against available space
[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 1
41 #define signal(time)    ao_beep_for(AO_BEEP_MID, time)
42 #else
43 #define signal(time)    ao_led_for(AO_LED_RED, time)
44 #endif
45 #define pause(time)     ao_delay(time)
46
47 static __xdata enum ao_flight_state ao_report_state;
48
49 static void
50 ao_report_beep(void) __reentrant
51 {
52         uint8_t r = flight_reports[ao_flight_state];
53         uint8_t l = r & 7;
54
55         if (!r)
56                 return;
57         while (l--) {
58                 if (r & 8)
59                         signal(AO_MS_TO_TICKS(600));
60                 else
61                         signal(AO_MS_TO_TICKS(200));
62                 pause(AO_MS_TO_TICKS(200));
63                 r >>= 1;
64         }
65         pause(AO_MS_TO_TICKS(400));
66 }
67
68 static void
69 ao_report_digit(uint8_t digit) __reentrant
70 {
71         if (!digit) {
72                 signal(AO_MS_TO_TICKS(500));
73                 pause(AO_MS_TO_TICKS(200));
74         } else {
75                 while (digit--) {
76                         signal(AO_MS_TO_TICKS(200));
77                         pause(AO_MS_TO_TICKS(200));
78                 }
79         }
80         pause(AO_MS_TO_TICKS(300));
81 }
82
83 static void
84 ao_report_altitude(void)
85 {
86         __xdata int16_t agl = ao_pres_to_altitude(ao_min_pres) - ao_pres_to_altitude(ao_ground_pres);
87         __xdata uint8_t digits[10];
88         __xdata uint8_t ndigits, i;
89
90         if (agl < 0)
91                 agl = 0;
92         ndigits = 0;
93         do {
94                 digits[ndigits++] = agl % 10;
95                 agl /= 10;
96         } while (agl);
97
98         for (;;) {
99                 ao_report_beep();
100                 i = ndigits;
101                 do
102                         ao_report_digit(digits[--i]);
103                 while (i != 0);
104                 pause(AO_SEC_TO_TICKS(5));
105         }
106 }
107
108 static uint8_t
109 ao_report_igniter_ready(enum ao_igniter igniter)
110 {
111         return ao_igniter_status(igniter) == ao_igniter_ready ? 1 : 0;
112 }
113
114 static void
115 ao_report_continuity(void) __reentrant
116 {
117         uint8_t c = (ao_report_igniter_ready(ao_igniter_drogue) |
118                      (ao_report_igniter_ready(ao_igniter_main) << 1));
119         if (c) {
120                 while (c--) {
121                         ao_beep_for(AO_BEEP_HIGH, AO_MS_TO_TICKS(25));
122                         pause(AO_MS_TO_TICKS(100));
123                 }
124         } else {
125                 c = 10;
126                 while (c--) {
127                         ao_beep_for(AO_BEEP_HIGH, AO_MS_TO_TICKS(20));
128                         ao_beep_for(AO_BEEP_LOW, AO_MS_TO_TICKS(20));
129                 }
130         }
131         c = 50;
132         while (c-- && ao_flight_state == ao_flight_pad)
133                 pause(AO_MS_TO_TICKS(100));
134 }
135
136 void
137 ao_report(void)
138 {
139         ao_report_state = ao_flight_state;
140         for(;;) {
141                 if (ao_flight_state == ao_flight_landed)
142                         ao_report_altitude();
143                 ao_report_beep();
144                 if (ao_flight_state == ao_flight_idle)
145                         ao_report_continuity();
146                 while (ao_flight_state == ao_flight_pad)
147                         ao_report_continuity();
148                 __critical {
149                         while (ao_report_state == ao_flight_state)
150                                 ao_sleep(DATA_TO_XDATA(&ao_flight_state));
151                         ao_report_state = ao_flight_state;
152                 }
153         }
154 }
155
156 static __xdata struct ao_task ao_report_task;
157
158 void
159 ao_report_init(void)
160 {
161         ao_add_task(&ao_report_task, ao_report, "report");
162 }