altos: Test APRS altitude encoding
[fw/altos] / src / test / ao_aprs_test.c
1 /*
2  * Copyright © 2012 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 <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdint.h>
22 #include <stdarg.h>
23
24 #include <ao_telemetry.h>
25
26 #define AO_GPS_NUM_SAT_MASK     (0xf << 0)
27 #define AO_GPS_NUM_SAT_SHIFT    (0)
28
29 #define AO_GPS_VALID            (1 << 4)
30 #define AO_GPS_RUNNING          (1 << 5)
31 #define AO_GPS_DATE_VALID       (1 << 6)
32 #define AO_GPS_COURSE_VALID     (1 << 7)
33
34 struct ao_telemetry_location ao_gps_data;
35 struct ao_telemetry_satellite ao_gps_tracking_data;
36
37 #define AO_APRS_TEST
38
39 typedef int16_t (*ao_radio_fill_func)(uint8_t *buffer, int16_t len);
40
41 #define DEBUG 0
42 #if DEBUG
43 void
44 ao_aprs_bit(uint8_t bit)
45 {
46         static int      seq = 0;
47         printf ("%6d %d\n", seq++, bit ? 1 : 0);
48 }
49 #else
50 void
51 ao_aprs_bit(uint8_t bit)
52 {
53         putchar (bit ? 0xc0 : 0x40);
54 }
55 #endif
56
57 void
58 ao_radio_send_aprs(ao_radio_fill_func fill);
59
60 #include <ao_aprs.c>
61
62 /*
63  * @section copyright_sec Copyright
64  *
65  * Copyright (c) 2001-2009 Michael Gray, KD7LMO
66
67
68  *
69  *
70  * @section gpl_sec GNU General Public License
71  *
72  *  This program is free software; you can redistribute it and/or modify
73  *  it under the terms of the GNU General Public License as published by
74  *  the Free Software Foundation; either version 2 of the License, or
75  *  (at your option) any later version.
76  *
77  *  This program is distributed in the hope that it will be useful,
78  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
79  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
80  *  GNU General Public License for more details.
81  *
82  *  You should have received a copy of the GNU General Public License
83  *  along with this program; if not, write to the Free Software
84  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
85  *
86
87  */
88
89 static void
90 audio_gap(int secs)
91 {
92 #if !DEBUG
93         int     samples = secs * 9600;
94
95         while (samples--)
96                 ao_aprs_bit(0);
97 #endif
98 }
99
100 #include <math.h>
101
102 int
103 ao_aprs_encode_altitude_expensive(int meters)
104 {
105         double  feet = meters / 0.3048;
106
107         double  encode = log(feet) / log(1.002);
108         return floor(encode + 0.5);
109 }
110
111 // This is where we go after reset.
112 int main(int argc, char **argv)
113 {
114         int     e, x;
115         int     a;
116
117         for (a = 1; a < 100000; a++) {
118                 e = ao_aprs_encode_altitude(a);
119                 x = ao_aprs_encode_altitude_expensive(a);
120
121                 if (e != x) {
122                         double  back_feet, back_meters;
123                         back_feet = pow(1.002, e);
124                         back_meters = back_feet * 0.3048;
125                         fprintf (stderr, "APRS altitude encoding failure: altitude %d actual %d expected %d actual meters %f\n",
126                                  a, e, x, back_meters);
127                 }
128         }
129
130     audio_gap(1);
131
132     ao_gps_data.latitude = (45.0 + 28.25 / 60.0) * 10000000;
133     ao_gps_data.longitude = (-(122 + 44.2649 / 60.0)) * 10000000;
134     ao_gps_data.altitude = 84;
135     ao_gps_data.flags = (AO_GPS_VALID|AO_GPS_RUNNING);
136
137     /* Transmit one packet */
138     ao_aprs_send();
139
140     tncBuffer[strlen((char *) tncBuffer) - 2] = '\0';
141     fprintf(stderr, "packet: %s\n", tncBuffer);
142
143     exit(0);
144 }
145
146 void
147 ao_radio_send_aprs(ao_radio_fill_func fill)
148 {
149         int16_t len;
150         uint8_t done = 0;
151         uint8_t buf[16], *b, c;
152         uint8_t bit;
153
154         while (!done) {
155                 len = (*fill)(buf, sizeof (buf));
156                 if (len < 0) {
157                         done = 1;
158                         len = -len;
159                 }
160                 b = buf;
161                 while (len--) {
162                         c = *b++;
163                         for (bit = 0; bit < 8; bit++) {
164                                 ao_aprs_bit(c & 0x80);
165                                 c <<= 1;
166                         }
167                 }
168         }
169 }