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; version 2 of the License.
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.
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.
19 #include <ao_flight.h>
20 #include <ao_sample.h>
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))
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' */
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)
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)
51 #define pause(time) ao_delay(time)
53 static __pdata enum ao_flight_state ao_report_state;
58 * From: http://www.arrl.org/files/file/Technology/x9004008.pdf
60 * c: character rate in wpm
61 * s: overall rate in wpm
62 * u: unit rate (dit speed)
66 * intra-character-time: u
70 * Because our clock runs at 10ms, we'll round up to 70ms for u, which
73 * Farnsworth adds space between characters and
87 * Ta = total delay to add to the characters (31 units)
88 * of a standard 50-unit "word", in seconds
90 * Tc = period between characters, in seconds
92 * Tw = period between words, in seconds
94 * We'll use Farnsworth spacing with c=18 and s=12:
98 * Ta = (60 * 17 - 37.2 * 12) / (17 * 12) = 2.812
100 * Tc = 3 * Ta / 19 = .444
104 * Note that the values below are all reduced by 10ms; that's because
105 * the timer always adds a tick to make sure the task actually sleeps
106 * at least as long as the argument.
110 ao_report_beep(void) __reentrant
112 uint8_t r = flight_reports[ao_flight_state];
119 mid(AO_MS_TO_TICKS(200));
121 mid(AO_MS_TO_TICKS(60));
122 pause(AO_MS_TO_TICKS(60));
125 pause(AO_MS_TO_TICKS(360));
129 ao_report_digit(uint8_t digit) __reentrant
132 mid(AO_MS_TO_TICKS(500));
133 pause(AO_MS_TO_TICKS(200));
136 mid(AO_MS_TO_TICKS(200));
137 pause(AO_MS_TO_TICKS(200));
140 pause(AO_MS_TO_TICKS(300));
144 ao_report_number(int16_t n)
146 __xdata uint8_t digits[10];
147 __pdata uint8_t ndigits, i;
153 digits[ndigits++] = n % 10;
159 ao_report_digit(digits[--i]);
164 ao_report_altitude(void)
166 ao_report_number(ao_max_height);
169 #if HAS_BATTERY_REPORT
171 ao_report_battery(void)
173 __xdata struct ao_data packet;
175 ao_data_get(&packet);
176 if (packet.adc.v_batt != 0)
178 ao_sleep(DATA_TO_XDATA(&ao_sample_data));
180 ao_report_number(ao_battery_decivolt(packet.adc.v_batt));
184 #if HAS_IGNITE_REPORT
186 ao_report_igniter_ready(enum ao_igniter igniter)
188 return ao_igniter_status(igniter) == ao_igniter_ready ? 1 : 0;
192 ao_report_igniter(void)
194 return (ao_report_igniter_ready(ao_igniter_drogue) |
195 (ao_report_igniter_ready(ao_igniter_main) << 1));
199 ao_report_continuity(void) __reentrant
204 if (!ao_igniter_present)
207 c = ao_report_igniter();
210 high(AO_MS_TO_TICKS(25));
211 pause(AO_MS_TO_TICKS(100));
216 high(AO_MS_TO_TICKS(20));
217 low(AO_MS_TO_TICKS(20));
222 pause(AO_MS_TO_TICKS(100));
225 low(AO_MS_TO_TICKS(100));
226 mid(AO_MS_TO_TICKS(100));
227 high(AO_MS_TO_TICKS(100));
228 mid(AO_MS_TO_TICKS(100));
238 ao_report_state = ao_flight_state;
240 #if HAS_BATTERY_REPORT
241 if (ao_flight_state == ao_flight_startup)
246 if (ao_flight_state == ao_flight_landed) {
247 ao_report_altitude();
249 ao_delay(AO_SEC_TO_TICKS(5));
253 #if HAS_IGNITE_REPORT
254 if (ao_flight_state == ao_flight_idle)
255 ao_report_continuity();
256 while (ao_flight_state == ao_flight_pad) {
258 ao_report_continuity();
260 while (c-- && ao_flight_state == ao_flight_pad)
261 pause(AO_MS_TO_TICKS(100));
265 while (ao_report_state == ao_flight_state)
266 ao_sleep(DATA_TO_XDATA(&ao_flight_state));
267 ao_report_state = ao_flight_state;
271 static __xdata struct ao_task ao_report_task;
276 ao_add_task(&ao_report_task, ao_report, "report");