update changelogs for Debian build
[fw/altos] / src / ao_config.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 struct ao_config ao_config;
21 __xdata uint8_t ao_config_loaded;
22 __xdata uint8_t ao_config_dirty;
23 __xdata uint8_t ao_config_mutex;
24
25 #define AO_CONFIG_DEFAULT_MAIN_DEPLOY   250
26 #define AO_CONFIG_DEFAULT_RADIO_CHANNEL 0
27 #define AO_CONFIG_DEFAULT_CALLSIGN      "N0CALL"
28 #define AO_CONFIG_DEFAULT_ACCEL_ZERO_G  16000
29 #define AO_CONFIG_DEFAULT_APOGEE_DELAY  0
30 /*
31  * For 434.550MHz, the frequency value is:
32  *
33  * 434.550e6 / (24e6 / 2**16) = 1186611.2
34  *
35  * This value is stored in a const variable so that
36  * ao-load can change it during programming for
37  * devices that have no eeprom for config data.
38  */
39 const uint32_t ao_radio_cal = 1186611;
40
41 #if HAS_EEPROM
42 static void
43 _ao_config_put(void)
44 {
45         ao_ee_write_config((uint8_t *) &ao_config, sizeof (ao_config));
46 }
47 #endif
48
49 static void
50 _ao_config_get(void)
51 {
52         if (ao_config_loaded)
53                 return;
54 #if HAS_EEPROM
55         ao_ee_read_config((uint8_t *) &ao_config, sizeof (ao_config));
56 #endif
57         if (ao_config.major != AO_CONFIG_MAJOR) {
58                 ao_config.major = AO_CONFIG_MAJOR;
59                 ao_config.minor = AO_CONFIG_MINOR;
60                 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
61                 ao_config.radio_channel = AO_CONFIG_DEFAULT_RADIO_CHANNEL;
62                 ao_config.accel_plus_g = 0;
63                 ao_config.accel_minus_g = 0;
64                 memset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
65                 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
66                        sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
67                 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
68                 ao_config.radio_cal = ao_radio_cal;
69                 ao_config_dirty = 1;
70         }
71         if (ao_config.minor < AO_CONFIG_MINOR) {
72                 /* Fixups for minor version 1 */
73                 if (ao_config.minor < 1)
74                         ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
75                 /* Fixups for minor version 2 */
76                 if (ao_config.minor < 2) {
77                         ao_config.accel_plus_g = 0;
78                         ao_config.accel_minus_g = 0;
79                 }
80                 /* Fixups for minor version 3 */
81                 if (ao_config.minor < 3)
82                         ao_config.radio_cal = ao_radio_cal;
83                 ao_config.minor = AO_CONFIG_MINOR;
84                 ao_config_dirty = 1;
85         }
86         ao_config_loaded = 1;
87 }
88
89 void
90 ao_config_get(void)
91 {
92         ao_mutex_get(&ao_config_mutex);
93         _ao_config_get();
94         ao_mutex_put(&ao_config_mutex);
95 }
96
97 void
98 ao_config_callsign_show(void)
99 {
100         printf ("Callsign: \"%s\"\n", ao_config.callsign);
101 }
102
103 void
104 ao_config_callsign_set(void) __reentrant
105 {
106         uint8_t c;
107         char callsign[AO_MAX_CALLSIGN + 1];
108
109         ao_cmd_white();
110         c = 0;
111         while (ao_cmd_lex_c != '\n') {
112                 if (c < AO_MAX_CALLSIGN)
113                         callsign[c++] = ao_cmd_lex_c;
114                 else
115                         ao_cmd_status = ao_cmd_lex_error;
116                 ao_cmd_lex();
117         }
118         if (ao_cmd_status != ao_cmd_success)
119                 return;
120         ao_mutex_get(&ao_config_mutex);
121         _ao_config_get();
122         while (c < AO_MAX_CALLSIGN + 1)
123                 callsign[c++] = '\0';
124         memcpy(&ao_config.callsign, &callsign,
125                AO_MAX_CALLSIGN + 1);
126         ao_config_dirty = 1;
127         ao_mutex_put(&ao_config_mutex);
128         ao_config_callsign_show();
129 }
130
131 void
132 ao_config_radio_channel_show(void) __reentrant
133 {
134         uint32_t        freq = 434550L + ao_config.radio_channel * 100L;
135         uint16_t        mhz = freq / 1000L;
136         uint16_t        khz = freq % 1000L;
137
138         printf("Radio channel: %d (%d.%03dMHz)\n",
139                ao_config.radio_channel, mhz, khz);
140 }
141
142 void
143 ao_config_radio_channel_set(void) __reentrant
144 {
145         ao_cmd_decimal();
146         if (ao_cmd_status != ao_cmd_success)
147                 return;
148         ao_mutex_get(&ao_config_mutex);
149         _ao_config_get();
150         ao_config.radio_channel = ao_cmd_lex_i;
151         ao_config_dirty = 1;
152         ao_mutex_put(&ao_config_mutex);
153         ao_config_radio_channel_show();
154         ao_radio_abort();
155 }
156
157 #if HAS_ADC
158
159 void
160 ao_config_main_deploy_show(void) __reentrant
161 {
162         printf("Main deploy: %d meters (%d feet)\n",
163                ao_config.main_deploy,
164                (int16_t) ((int32_t) ao_config.main_deploy * 328 / 100));
165 }
166
167 void
168 ao_config_main_deploy_set(void) __reentrant
169 {
170         ao_cmd_decimal();
171         if (ao_cmd_status != ao_cmd_success)
172                 return;
173         ao_mutex_get(&ao_config_mutex);
174         _ao_config_get();
175         ao_config.main_deploy = ao_cmd_lex_i;
176         ao_config_dirty = 1;
177         ao_mutex_put(&ao_config_mutex);
178         ao_config_main_deploy_show();
179 }
180
181 void
182 ao_config_accel_calibrate_show(void) __reentrant
183 {
184         printf("Accel cal +1g: %d -1g: %d\n",
185                ao_config.accel_plus_g, ao_config.accel_minus_g);
186 }
187
188 #define ACCEL_CALIBRATE_SAMPLES 1024
189 #define ACCEL_CALIBRATE_SHIFT   10
190
191 static int16_t
192 ao_config_accel_calibrate_auto(char *orientation) __reentrant
193 {
194         uint16_t        i;
195         int32_t         accel_total;
196         uint8_t         cal_adc_ring;
197
198         printf("Orient %s and press a key...", orientation);
199         flush();
200         (void) getchar();
201         puts("\r\n"); flush();
202         puts("Calibrating..."); flush();
203         i = ACCEL_CALIBRATE_SAMPLES;
204         accel_total = 0;
205         cal_adc_ring = ao_adc_head;
206         while (i) {
207                 ao_sleep(&ao_adc_ring);
208                 while (i && cal_adc_ring != ao_adc_head) {
209                         accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
210                         cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
211                         i--;
212                 }
213         }
214         return accel_total >> ACCEL_CALIBRATE_SHIFT;
215 }
216
217 void
218 ao_config_accel_calibrate_set(void) __reentrant
219 {
220         int16_t up, down;
221         ao_cmd_decimal();
222         if (ao_cmd_status != ao_cmd_success)
223                 return;
224         if (ao_cmd_lex_i == 0) {
225                 up = ao_config_accel_calibrate_auto("antenna up");
226                 down = ao_config_accel_calibrate_auto("antenna down");
227         } else {
228                 up = ao_cmd_lex_i;
229                 ao_cmd_decimal();
230                 if (ao_cmd_status != ao_cmd_success)
231                         return;
232                 down = ao_cmd_lex_i;
233         }
234         if (up >= down) {
235                 printf("Invalid accel calibration: antenna up (%d) should be less than antenna down (%d)\n",
236                        up, down);
237                 return;
238         }
239         ao_mutex_get(&ao_config_mutex);
240         _ao_config_get();
241         ao_config.accel_plus_g = up;
242         ao_config.accel_minus_g = down;
243         ao_config_dirty = 1;
244         ao_mutex_put(&ao_config_mutex);
245         ao_config_accel_calibrate_show();
246 }
247
248 void
249 ao_config_apogee_delay_show(void) __reentrant
250 {
251         printf("Apogee delay: %d seconds\n",
252                ao_config.apogee_delay);
253 }
254
255 void
256 ao_config_apogee_delay_set(void) __reentrant
257 {
258         ao_cmd_decimal();
259         if (ao_cmd_status != ao_cmd_success)
260                 return;
261         ao_mutex_get(&ao_config_mutex);
262         _ao_config_get();
263         ao_config.apogee_delay = ao_cmd_lex_i;
264         ao_config_dirty = 1;
265         ao_mutex_put(&ao_config_mutex);
266         ao_config_apogee_delay_show();
267 }
268
269 #endif /* HAS_ADC */
270
271 void
272 ao_config_radio_cal_show(void) __reentrant
273 {
274         printf("Radio cal: %ld\n", ao_config.radio_cal);
275 }
276
277 void
278 ao_config_radio_cal_set(void) __reentrant
279 {
280         ao_cmd_decimal();
281         if (ao_cmd_status != ao_cmd_success)
282                 return;
283         ao_mutex_get(&ao_config_mutex);
284         _ao_config_get();
285         ao_config.radio_cal = ao_cmd_lex_u32;
286         ao_config_dirty = 1;
287         ao_mutex_put(&ao_config_mutex);
288         ao_config_radio_cal_show();
289 }
290
291 struct ao_config_var {
292         char            cmd;
293         void            (*set)(void) __reentrant;
294         void            (*show)(void) __reentrant;
295         const char      *help;
296 };
297
298 void
299 ao_config_help(void) __reentrant;
300
301 void
302 ao_config_show(void) __reentrant;
303
304 void
305 ao_config_write(void) __reentrant;
306
307 __code struct ao_config_var ao_config_vars[] = {
308 #if HAS_ADC
309         { 'm',  ao_config_main_deploy_set,      ao_config_main_deploy_show,
310                 "m <meters>  Set height above launch for main deploy (in meters)" },
311         { 'd',  ao_config_apogee_delay_set,     ao_config_apogee_delay_show,
312                 "d <delay>   Set apogee igniter delay (in seconds)" },
313 #endif /* HAS_ADC */
314         { 'r',  ao_config_radio_channel_set,    ao_config_radio_channel_show,
315                 "r <channel> Set radio channel (freq = 434.550 + channel * .1)" },
316         { 'c',  ao_config_callsign_set,         ao_config_callsign_show,
317                 "c <call>    Set callsign broadcast in each packet (8 char max)" },
318 #if HAS_ADC
319         { 'a',  ao_config_accel_calibrate_set,  ao_config_accel_calibrate_show,
320                 "a <+g> <-g> Set accelerometer calibration (0 for auto)" },
321 #endif /* HAS_ADC */
322         { 'f',  ao_config_radio_cal_set,        ao_config_radio_cal_show,
323                 "f <cal>     Set radio calibration value (cal = rf/(xtal/2^16))" },
324         { 's',  ao_config_show,                 ao_config_show,
325                 "s           Show current config values" },
326 #if HAS_EEPROM
327         { 'w',  ao_config_write,                ao_config_write,
328                 "w           Write current values to eeprom" },
329 #endif
330         { '?',  ao_config_help,                 ao_config_help,
331                 "?           Show available config variables" },
332         { 0,    ao_config_help, ao_config_help,
333                 NULL },
334 };
335
336 void
337 ao_config_set(void)
338 {
339         char    c;
340         uint8_t cmd;
341         void (*__xdata func)(void) __reentrant;
342
343         ao_cmd_white();
344         c = ao_cmd_lex_c;
345         ao_cmd_lex();
346         func = 0;
347         for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
348                 if (ao_config_vars[cmd].cmd == c) {
349                         func = ao_config_vars[cmd].set;
350                         break;
351                 }
352         if (func)
353                 (*func)();
354         else
355                 ao_cmd_status = ao_cmd_syntax_error;
356 }
357
358 void
359 ao_config_help(void) __reentrant
360 {
361         uint8_t cmd;
362         for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
363                 puts (ao_config_vars[cmd].help);
364 }
365
366 void
367 ao_config_show(void) __reentrant
368 {
369         uint8_t cmd;
370         printf("Config version: %d.%d\n",
371                ao_config.major, ao_config.minor);
372         for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
373                 if (ao_config_vars[cmd].show != ao_config_vars[cmd].set)
374                         (*ao_config_vars[cmd].show)();
375 }
376
377 #if HAS_EEPROM
378 void
379 ao_config_write(void) __reentrant
380 {
381         uint8_t saved = 0;
382         ao_mutex_get(&ao_config_mutex);
383         if (ao_config_dirty) {
384                 _ao_config_put();
385                 ao_config_dirty = 0;
386                 saved = 1;
387         }
388         ao_mutex_put(&ao_config_mutex);
389         if (saved)
390                 puts("Saved");
391         else
392                 puts("Nothing to save");
393 }
394 #endif
395
396 __code struct ao_cmds ao_config_cmds[] = {
397         { 'c',  ao_config_set,  "c <var> <value>                    Set config variable (? for help, s to show)" },
398         { '\0', ao_config_set, NULL },
399 };
400
401 void
402 ao_config_init(void)
403 {
404         ao_cmd_register(&ao_config_cmds[0]);
405 }