Revert "altos: Debugging TBT issues -- check pin configuration after boot"
[fw/altos] / src / ao_telemetry_tiny.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 __xdata uint8_t ao_rdf = 0;
21 __xdata uint16_t ao_rdf_time;
22 __xdata uint16_t ao_telemetry_tiny_interval = 0;
23
24 #define AO_RDF_INTERVAL_TICKS   AO_SEC_TO_TICKS(5)
25 #define AO_RDF_LENGTH_MS        500
26
27 void
28 ao_telemetry_tiny(void)
29 {
30         uint16_t        time;
31         int16_t         delay;
32         static __xdata struct ao_telemetry_tiny telemetry_tiny;
33
34         ao_config_get();
35         while (!ao_flight_number)
36                 ao_sleep(&ao_flight_number);
37         memcpy(telemetry_tiny.callsign, ao_config.callsign, AO_MAX_CALLSIGN);
38         telemetry_tiny.serial = ao_serial_number;
39         telemetry_tiny.flight = ao_log_full() ? 0 : ao_flight_number;
40         for (;;) {
41                 while (ao_telemetry_tiny_interval == 0)
42                         ao_sleep(&ao_telemetry_tiny_interval);
43                 time = ao_rdf_time = ao_time();
44                 while (ao_telemetry_tiny_interval) {
45                         telemetry_tiny.flight_state = ao_flight_state;
46                         telemetry_tiny.height = ao_height;
47                         telemetry_tiny.speed = ao_speed;
48                         telemetry_tiny.accel = ao_accel;
49                         telemetry_tiny.ground_pres = ao_ground_pres;
50                         ao_adc_get(&telemetry_tiny.adc);
51                         ao_radio_send(&telemetry_tiny, sizeof (telemetry_tiny));
52                         if (ao_rdf &&
53                             (int16_t) (ao_time() - ao_rdf_time) >= 0)
54                         {
55                                 ao_rdf_time = ao_time() + AO_RDF_INTERVAL_TICKS;
56                                 ao_radio_rdf(AO_RDF_LENGTH_MS);
57                         }
58                         time += ao_telemetry_tiny_interval;
59                         delay = time - ao_time();
60                         if (delay > 0)
61                                 ao_delay(delay);
62                         else
63                                 time = ao_time();
64                 }
65         }
66 }
67
68 void
69 ao_telemetry_set_interval(uint16_t interval)
70 {
71         ao_telemetry_tiny_interval = interval;
72         ao_wakeup(&ao_telemetry_tiny_interval);
73 }
74
75 void
76 ao_rdf_set(uint8_t rdf)
77 {
78         ao_rdf = rdf;
79         if (rdf == 0)
80                 ao_radio_rdf_abort();
81         else
82                 ao_rdf_time = ao_time();
83 }
84
85 __xdata struct ao_task  ao_telemetry_tiny_task;
86
87 void
88 ao_telemetry_tiny_init()
89 {
90         ao_add_task(&ao_telemetry_tiny_task, ao_telemetry_tiny, "telemetry_tiny");
91 }