2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 #include <ao_flight.h>
21 #include <ao_sample.h>
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))
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' */
43 static enum ao_flight_state ao_report_state;
46 #define low(time) ao_beep_for(AO_BEEP_LOW, time)
47 #define mid(time) ao_beep_for(AO_BEEP_MID, time)
48 #define high(time) ao_beep_for(AO_BEEP_HIGH, time)
51 #define AO_LED_LOW AO_LED_GREEN
54 #define AO_LED_MID AO_LED_RED
57 #define low(time) ao_led_for(AO_LED_LOW, time)
58 #define mid(time) ao_led_for(AO_LED_MID, time)
59 #define high(time) ao_led_for(AO_LED_MID|AO_LED_LOW, time)
61 #define pause(time) ao_delay(time)
66 * From: http://www.arrl.org/files/file/Technology/x9004008.pdf
68 * c: character rate in wpm
69 * s: overall rate in wpm
70 * u: unit rate (dit speed)
74 * intra-character-time: u
78 * Because our clock runs at 10ms, we'll round up to 70ms for u, which
81 * Farnsworth adds space between characters and
95 * Ta = total delay to add to the characters (31 units)
96 * of a standard 50-unit "word", in seconds
98 * Tc = period between characters, in seconds
100 * Tw = period between words, in seconds
102 * We'll use Farnsworth spacing with c=18 and s=12:
104 * u = 1.2/18 = 0.0667
106 * Ta = (60 * 17 - 37.2 * 12) / (17 * 12) = 2.812
108 * Tc = 3 * Ta / 19 = .444
112 * Note that the values below are all reduced by 10ms; that's because
113 * the timer always adds a tick to make sure the task actually sleeps
114 * at least as long as the argument.
118 ao_report_flight_state(void)
120 uint8_t r = flight_reports[ao_flight_state];
127 mid(AO_MS_TO_TICKS(200));
129 mid(AO_MS_TO_TICKS(60));
130 pause(AO_MS_TO_TICKS(60));
133 pause(AO_MS_TO_TICKS(360));
137 ao_report_digit(uint8_t digit)
140 mid(AO_MS_TO_TICKS(500));
141 pause(AO_MS_TO_TICKS(200));
144 mid(AO_MS_TO_TICKS(200));
145 pause(AO_MS_TO_TICKS(200));
148 pause(AO_MS_TO_TICKS(300));
152 ao_report_number(int32_t n)
161 digits[ndigits++] = (uint8_t) (n % 10);
167 ao_report_digit(digits[--i]);
173 ao_report_altitude(void)
175 alt_t max_h = ao_max_height;
176 if (ao_config.report_feet) {
177 max_h = max_h * 39 / 12;
178 /* report a leading zero to distinguish */
182 ao_report_number(max_h);
186 #if HAS_BATTERY_REPORT
188 ao_report_battery(void)
190 struct ao_data packet;
192 ao_data_get(&packet);
193 if (packet.adc.v_batt != 0)
195 ao_sleep(&ao_sample_data);
197 ao_report_number(ao_battery_decivolt(packet.adc.v_batt));
201 #if HAS_IGNITE_REPORT
204 ao_report_igniter_ready(enum ao_igniter igniter)
206 return ao_igniter_status(igniter) == ao_igniter_ready ? 1 : 0;
210 ao_report_igniter(void)
212 return (uint8_t) (ao_report_igniter_ready(ao_igniter_drogue) |
213 (ao_report_igniter_ready(ao_igniter_main) << 1));
218 ao_report_continuity(void)
222 c = ao_report_igniter();
225 high(AO_MS_TO_TICKS(25));
226 pause(AO_MS_TO_TICKS(100));
231 high(AO_MS_TO_TICKS(20));
232 low(AO_MS_TO_TICKS(20));
238 pause(AO_MS_TO_TICKS(250));
240 for(c = 0; c < AO_PYRO_NUM; c++) {
241 enum ao_igniter_status status = ao_pyro_status(c);
242 if (status == ao_igniter_ready)
243 mid(AO_MS_TO_TICKS(25));
245 low(AO_MS_TO_TICKS(25));
246 pause(AO_MS_TO_TICKS(200));
251 pause(AO_MS_TO_TICKS(100));
254 low(AO_MS_TO_TICKS(100));
255 mid(AO_MS_TO_TICKS(100));
256 high(AO_MS_TO_TICKS(100));
257 mid(AO_MS_TO_TICKS(100));
268 ao_report_state = ao_flight_state;
269 #if HAS_BATTERY_REPORT
270 if (ao_report_state == ao_flight_startup)
274 ao_report_flight_state();
275 #if HAS_SENSOR_ERRORS
276 if (ao_report_state == ao_flight_invalid && ao_sensor_errors)
277 ao_report_number(ao_sensor_errors);
280 if (ao_report_state == ao_flight_landed) {
282 ao_report_altitude();
285 ao_delay(AO_SEC_TO_TICKS(5));
289 #if HAS_IGNITE_REPORT
290 if (ao_report_state == ao_flight_idle)
291 ao_report_continuity();
292 while (ao_flight_state == ao_flight_pad) {
294 ao_report_continuity();
296 while (c-- && ao_flight_state == ao_flight_pad)
297 pause(AO_MS_TO_TICKS(100));
301 while (ao_flight_state == ao_flight_pad) {
303 ao_report_flight_state();
305 while (c-- && ao_flight_state == ao_flight_pad)
306 pause(AO_MS_TO_TICKS(100));
309 while (ao_report_state == ao_flight_state)
310 ao_sleep(&ao_flight_state);
314 static struct ao_task ao_report_task;
319 ao_add_task(&ao_report_task, ao_report, "report");