altos: Allow flight support without logging
[fw/altos] / src / kernel / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 #include "ao.h"
20 #include "ao_log.h"
21 #include <ao_config.h>
22 #if HAS_FLIGHT
23 #include <ao_sample.h>
24 #include <ao_data.h>
25 #endif
26 #if HAS_BEEP
27 #include <ao_beep.h>
28 #endif
29 #if HAS_TRACKER
30 #include <ao_tracker.h>
31 #endif
32
33 struct ao_config ao_config;
34 uint8_t ao_config_loaded;
35 uint8_t ao_config_dirty;
36 uint8_t ao_config_mutex;
37
38 #if HAS_FORCE_FREQ
39 uint8_t ao_force_freq;
40 #endif
41
42 #ifndef HAS_CONFIG_SAVE
43 #define HAS_CONFIG_SAVE HAS_EEPROM
44 #endif
45
46 #ifndef AO_CONFIG_DEFAULT_APRS_INTERVAL
47 #define AO_CONFIG_DEFAULT_APRS_INTERVAL 0
48 #endif
49 #define AO_CONFIG_DEFAULT_MAIN_DEPLOY   250
50 #define AO_CONFIG_DEFAULT_RADIO_CHANNEL 0
51 #define AO_CONFIG_DEFAULT_CALLSIGN      "N0CALL"
52 #define AO_CONFIG_DEFAULT_ACCEL_ZERO_G  16000
53 #define AO_CONFIG_DEFAULT_APOGEE_DELAY  0
54 #define AO_CONFIG_DEFAULT_IGNITE_MODE   AO_IGNITE_MODE_DUAL
55 #define AO_CONFIG_DEFAULT_PAD_ORIENTATION       AO_PAD_ORIENTATION_ANTENNA_UP
56 #define AO_CONFIG_DEFAULT_PYRO_TIME     AO_MS_TO_TICKS(50)
57 #if HAS_CONFIG_SAVE
58 #ifndef USE_INTERNAL_FLASH
59 #error Please define USE_INTERNAL_FLASH
60 #endif
61 #endif
62
63 #ifndef AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX
64 # if FLIGHT_LOG_APPEND
65 #  define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX      ao_storage_log_max
66 # else
67 #  if USE_INTERNAL_FLASH
68 #   define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX     ao_storage_config
69 #  else
70 #   define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX     ((uint32_t) 192 * (uint32_t) 1024)
71 #  endif
72 # endif
73 #endif
74
75 #ifndef AO_CONFIG_DEFAULT_RADIO_POWER
76 #define AO_CONFIG_DEFAULT_RADIO_POWER           0x60
77 #endif
78 #define AO_CONFIG_DEFAULT_RADIO_AMP             0
79 #define AO_CONFIG_DEFAULT_APRS_SSID             (ao_serial_number % 10)
80 #define AO_CONFIG_DEFAULT_RADIO_RATE            AO_RADIO_RATE_38400
81
82 #if HAS_CONFIG_SAVE
83 static void
84 _ao_config_put(void)
85 {
86         ao_config_setup();
87         ao_config_erase();
88         ao_config_write(0, &ao_config, sizeof (ao_config));
89 #if HAS_FLIGHT && HAS_LOG
90         ao_log_write_erase(0);
91 #endif
92         ao_config_flush();
93 }
94
95 void
96 ao_config_put(void)
97 {
98         ao_mutex_get(&ao_config_mutex);
99         _ao_config_put();
100         ao_mutex_put(&ao_config_mutex);
101 }
102 #endif
103
104 #if HAS_RADIO
105
106 #if HAS_RADIO_FORWARD
107 uint32_t        ao_send_radio_setting;
108 #endif
109
110 void
111 ao_config_set_radio(void)
112 {
113         ao_config.radio_setting = ao_freq_to_set(ao_config.frequency, ao_config.radio_cal);
114 #if HAS_RADIO_FORWARD
115         ao_send_radio_setting = ao_freq_to_set(ao_config.send_frequency, ao_config.radio_cal);
116 #endif
117 }
118 #endif /* HAS_RADIO */
119
120 static void
121 _ao_config_get(void)
122 {
123         uint8_t minor;
124
125         if (ao_config_loaded)
126                 return;
127 #if HAS_CONFIG_SAVE
128         /* Yes, I know ao_storage_read calls ao_storage_setup,
129          * but ao_storage_setup *also* sets ao_storage_config, which we
130          * need before calling ao_storage_read here
131          */
132         ao_config_setup();
133         ao_config_read(0, &ao_config, sizeof (ao_config));
134 #endif
135         if (ao_config.major != AO_CONFIG_MAJOR) {
136                 ao_config.major = AO_CONFIG_MAJOR;
137                 ao_config.minor = 0;
138
139                 /* Version 0 stuff */
140                 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
141                 memset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
142                 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
143                        sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
144                 ao_config._legacy_radio_channel = 0;
145         }
146         minor = ao_config.minor;
147         if (minor != AO_CONFIG_MINOR) {
148                 /* Fixups for minor version 1 */
149                 if (minor < 1)
150                         ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
151                 /* Fixups for minor version 2 */
152                 if (minor < 2) {
153                         ao_config.accel_plus_g = 0;
154                         ao_config.accel_minus_g = 0;
155                 }
156                 /* Fixups for minor version 3 */
157 #if HAS_RADIO
158                 if (minor < 3)
159                         ao_config.radio_cal = ao_radio_cal;
160 #endif
161                 /* Fixups for minor version 4 */
162 #if HAS_LOG
163                 if (minor < 4)
164                         ao_config.flight_log_max = AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX;
165 #endif
166                 /* Fixupes for minor version 5 */
167                 if (minor < 5)
168                         ao_config.ignite_mode = AO_CONFIG_DEFAULT_IGNITE_MODE;
169                 if (minor < 6)
170                         ao_config.pad_orientation = AO_CONFIG_DEFAULT_PAD_ORIENTATION;
171                 if (minor < 8)
172                         ao_config.radio_enable = AO_RADIO_ENABLE_CORE;
173                 if (minor < 9)
174                         memset(&ao_config.aes_key, '\0', AO_AES_LEN);
175                 if (minor < 10)
176                         ao_config.frequency = 434550 + ao_config._legacy_radio_channel * 100;
177                 if (minor < 11)
178                         ao_config.apogee_lockout = 0;
179 #if AO_PYRO_NUM
180                 if (minor < 12)
181                         memset(&ao_config.pyro, '\0', sizeof (ao_config.pyro));
182 #endif
183                 if (minor < 13)
184                         ao_config.aprs_interval = AO_CONFIG_DEFAULT_APRS_INTERVAL;
185 #if HAS_RADIO_POWER
186                 if (minor < 14)
187                         ao_config.radio_power = AO_CONFIG_DEFAULT_RADIO_POWER;
188                 #endif
189 #if HAS_RADIO_AMP
190                 if (minor  < 14)
191                         ao_config.radio_amp = AO_CONFIG_DEFAULT_RADIO_AMP;
192 #endif
193 #if HAS_GYRO
194                 if (minor < 15) {
195                         ao_config.accel_zero_along = 0;
196                         ao_config.accel_zero_across = 0;
197                         ao_config.accel_zero_through = 0;
198
199                         /* Reset the main accel offsets to force
200                          * re-calibration
201                          */
202                         ao_config.accel_plus_g = 0;
203                         ao_config.accel_minus_g = 0;
204                 }
205 #endif
206 #if HAS_BEEP_CONFIG
207                 if (minor < 16)
208                         ao_config.mid_beep = AO_BEEP_MID_DEFAULT;
209 #endif
210 #if HAS_TRACKER
211                 if (minor < 17) {
212                         ao_config.tracker_motion = AO_TRACKER_MOTION_DEFAULT;
213                         ao_config.tracker_interval = AO_TRACKER_INTERVAL_DEFAULT;
214                 }
215 #endif
216 #if AO_PYRO_NUM
217                 if (minor < 18)
218                         ao_config.pyro_time = AO_CONFIG_DEFAULT_PYRO_TIME;
219 #endif
220 #if HAS_APRS
221                 if (minor < 19)
222                         ao_config.aprs_ssid = AO_CONFIG_DEFAULT_APRS_SSID;
223 #endif
224 #if HAS_RADIO_RATE
225                 if (minor < 20)
226                         ao_config.radio_rate = AO_CONFIG_DEFAULT_RADIO_RATE;
227 #endif
228 #if HAS_RADIO_FORWARD
229                 if (minor < 21)
230                         ao_config.send_frequency = 434550;
231 #endif
232 #if HAS_APRS
233                 if (minor < 22)
234                         ao_config.aprs_format = AO_CONFIG_DEFAULT_APRS_FORMAT;
235 #endif
236 #if HAS_FIXED_PAD_BOX
237                 if (minor < 22)
238                         ao_config.pad_box = 1;
239                 if (minor < 23)
240                         ao_config.pad_idle = 120;
241 #endif
242 #if HAS_APRS
243                 if (minor < 24)
244                         ao_config.aprs_offset = 0;
245 #endif
246                 ao_config.minor = AO_CONFIG_MINOR;
247                 ao_config_dirty = 1;
248         }
249 #if HAS_RADIO
250 #if HAS_FORCE_FREQ
251         if (ao_force_freq) {
252                 ao_config.frequency = 434550;
253                 ao_config.radio_cal = ao_radio_cal;
254 #if HAS_RADIO_RATE
255                 ao_config.radio_rate = AO_CONFIG_DEFAULT_RADIO_RATE;
256 #endif
257                 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
258                        sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
259         }
260 #endif
261         ao_config_set_radio();
262 #endif
263         ao_config_loaded = 1;
264 }
265
266 void
267 _ao_config_edit_start(void)
268 {
269         ao_mutex_get(&ao_config_mutex);
270         _ao_config_get();
271 }
272
273 void
274 _ao_config_edit_finish(void)
275 {
276         ao_config_dirty = 1;
277         ao_mutex_put(&ao_config_mutex);
278 }
279
280 void
281 ao_config_get(void)
282 {
283         _ao_config_edit_start();
284         ao_mutex_put(&ao_config_mutex);
285 }
286
287 #if HAS_RADIO
288
289 static void
290 ao_config_callsign_show(void)
291 {
292         printf ("Callsign: \"%s\"\n", ao_config.callsign);
293 }
294
295 static void
296 ao_config_callsign_set(void) 
297 {
298         uint8_t c;
299         static char callsign[AO_MAX_CALLSIGN + 1];
300
301         memset(callsign, '\0', sizeof callsign);
302         ao_cmd_white();
303         c = 0;
304         while (ao_cmd_lex_c != '\n') {
305                 if (c < AO_MAX_CALLSIGN)
306                         callsign[c++] = ao_cmd_lex_c;
307                 else
308                         ao_cmd_status = ao_cmd_lex_error;
309                 ao_cmd_lex();
310         }
311         if (ao_cmd_status != ao_cmd_success)
312                 return;
313         _ao_config_edit_start();
314         memcpy(&ao_config.callsign, &callsign,
315                AO_MAX_CALLSIGN + 1);
316         _ao_config_edit_finish();
317 }
318
319 static void
320 ao_config_frequency_show(void) 
321 {
322         printf("Frequency: %ld\n",
323                ao_config.frequency);
324 }
325
326 static void
327 ao_config_frequency_set(void) 
328 {
329         uint32_t r = ao_cmd_decimal();
330         if (ao_cmd_status != ao_cmd_success)
331                 return;
332         _ao_config_edit_start();
333         ao_config.frequency = r;
334         ao_config_set_radio();
335         _ao_config_edit_finish();
336 #if HAS_RADIO_RECV
337         ao_radio_recv_abort();
338 #endif
339 }
340
341 #endif
342
343 #if HAS_RADIO_FORWARD
344 void
345 ao_config_send_frequency_show(void) 
346 {
347         printf("Send frequency: %ld\n",
348                ao_config.send_frequency);
349 }
350
351 void
352 ao_config_send_frequency_set(void) 
353 {
354         ao_cmd_decimal();
355         if (ao_cmd_status != ao_cmd_success)
356                 return;
357         _ao_config_edit_start();
358         ao_config.send_frequency = ao_cmd_lex_u32;
359         ao_config_set_radio();
360         _ao_config_edit_finish();
361 #if HAS_RADIO_RECV
362         ao_radio_recv_abort();
363 #endif
364 }
365
366 #endif
367
368 #if HAS_FLIGHT
369
370 static void
371 ao_config_main_deploy_show(void) 
372 {
373         printf("Main deploy: %d meters\n",
374                ao_config.main_deploy);
375 }
376
377 static void
378 ao_config_main_deploy_set(void) 
379 {
380         uint32_t r = ao_cmd_decimal();
381         if (ao_cmd_status != ao_cmd_success)
382                 return;
383         _ao_config_edit_start();
384         ao_config.main_deploy = r;
385         _ao_config_edit_finish();
386 }
387
388 #if HAS_ACCEL
389 static void
390 ao_config_accel_calibrate_show(void) 
391 {
392         printf("Accel cal +1g: %d -1g: %d\n",
393                ao_config.accel_plus_g, ao_config.accel_minus_g);
394 #if HAS_GYRO
395         printf ("IMU cal along %d across %d through %d\n",
396                 ao_config.accel_zero_along,
397                 ao_config.accel_zero_across,
398                 ao_config.accel_zero_through);
399 #endif
400 }
401
402 #define ACCEL_CALIBRATE_SAMPLES 1024
403 #define ACCEL_CALIBRATE_SHIFT   10
404
405 #if HAS_GYRO
406 static int16_t accel_cal_along;
407 static int16_t accel_cal_across;
408 static int16_t accel_cal_through;
409 #endif
410
411 static int16_t
412 ao_config_accel_calibrate_auto(char *orientation) 
413 {
414         uint16_t        i;
415         int32_t         accel_total;
416         uint8_t         cal_data_ring;
417 #if HAS_GYRO
418         int32_t         accel_along_total = 0;
419         int32_t         accel_across_total = 0;
420         int32_t         accel_through_total = 0;
421 #endif
422
423         printf("Orient antenna %s and press a key...", orientation);
424         flush();
425         (void) getchar();
426         puts("\r\n"); flush();
427         puts("Calibrating..."); flush();
428         i = ACCEL_CALIBRATE_SAMPLES;
429         accel_total = 0;
430         cal_data_ring = ao_sample_data;
431         while (i) {
432                 ao_sleep(&ao_sample_data);
433                 while (i && cal_data_ring != ao_sample_data) {
434                         accel_total += (int32_t) ao_data_accel(&ao_data_ring[cal_data_ring]);
435 #if HAS_GYRO
436                         accel_along_total += (int32_t) ao_data_along(&ao_data_ring[cal_data_ring]);
437                         accel_across_total += (int32_t) ao_data_across(&ao_data_ring[cal_data_ring]);
438                         accel_through_total += (int32_t) ao_data_through(&ao_data_ring[cal_data_ring]);
439 #endif
440                         cal_data_ring = ao_data_ring_next(cal_data_ring);
441                         i--;
442                 }
443         }
444 #if HAS_GYRO
445         accel_cal_along = accel_along_total >> ACCEL_CALIBRATE_SHIFT;
446         accel_cal_across = accel_across_total >> ACCEL_CALIBRATE_SHIFT;
447         accel_cal_through = accel_through_total >> ACCEL_CALIBRATE_SHIFT;
448 #endif
449         return accel_total >> ACCEL_CALIBRATE_SHIFT;
450 }
451
452 static void
453 ao_config_accel_calibrate_set(void) 
454 {
455         int16_t up, down;
456         uint16_t r;
457 #if HAS_GYRO
458         int16_t accel_along_up = 0, accel_along_down = 0;
459         int16_t accel_across_up = 0, accel_across_down = 0;
460         int16_t accel_through_up = 0, accel_through_down = 0;
461 #endif
462
463         r = ao_cmd_decimal();
464         if (ao_cmd_status != ao_cmd_success)
465                 return;
466         if (r == 0) {
467                 up = ao_config_accel_calibrate_auto("up");
468 #if HAS_GYRO
469                 accel_along_up = accel_cal_along;
470                 accel_across_up = accel_cal_across;
471                 accel_through_up = accel_cal_through;
472 #endif
473                 down = ao_config_accel_calibrate_auto("down");
474 #if HAS_GYRO
475                 accel_along_down = accel_cal_along;
476                 accel_across_down = accel_cal_across;
477                 accel_through_down = accel_cal_through;
478 #endif
479         } else {
480                 up = r;
481                 r = ao_cmd_decimal();
482                 if (ao_cmd_status != ao_cmd_success)
483                         return;
484                 down = r;
485         }
486         if (up >= down) {
487                 printf("Invalid accel: up (%d) down (%d)\n",
488                        up, down);
489                 return;
490         }
491         _ao_config_edit_start();
492         ao_config.accel_plus_g = up;
493         ao_config.accel_minus_g = down;
494 #if HAS_GYRO
495         if (r == 0) {
496                 ao_config.accel_zero_along = (accel_along_up + accel_along_down) / 2;
497                 ao_config.accel_zero_across = (accel_across_up + accel_across_down) / 2;
498                 ao_config.accel_zero_through = (accel_through_up + accel_through_down) / 2;
499         }
500 #endif
501         _ao_config_edit_finish();
502 }
503 #endif /* HAS_ACCEL */
504
505 static void
506 ao_config_apogee_delay_show(void) 
507 {
508         printf("Apogee delay: %d seconds\n",
509                ao_config.apogee_delay);
510 }
511
512 static void
513 ao_config_apogee_delay_set(void) 
514 {
515         uint32_t r = ao_cmd_decimal();
516         if (ao_cmd_status != ao_cmd_success)
517                 return;
518         _ao_config_edit_start();
519         ao_config.apogee_delay = r;
520         _ao_config_edit_finish();
521 }
522
523 static void
524 ao_config_apogee_lockout_show(void) 
525 {
526         printf ("Apogee lockout: %d seconds\n",
527                 ao_config.apogee_lockout);
528 }
529
530 static void
531 ao_config_apogee_lockout_set(void) 
532 {
533         uint16_t r = ao_cmd_decimal();
534         if (ao_cmd_status != ao_cmd_success)
535                 return;
536         _ao_config_edit_start();
537         ao_config.apogee_lockout = r;
538         _ao_config_edit_finish();
539 }
540
541 #endif /* HAS_FLIGHT */
542
543 #if HAS_RADIO
544 static void
545 ao_config_radio_cal_show(void) 
546 {
547         printf("Radio cal: %ld\n", ao_config.radio_cal);
548 }
549
550 static void
551 ao_config_radio_cal_set(void) 
552 {
553         uint32_t r = ao_cmd_decimal();
554         if (ao_cmd_status != ao_cmd_success)
555                 return;
556         _ao_config_edit_start();
557         ao_config.radio_cal = r;
558         ao_config_set_radio();
559         _ao_config_edit_finish();
560 }
561
562 #endif
563
564 #if HAS_RADIO_RATE
565 #ifndef HAS_TELEMETRY
566 #error Please define HAS_TELEMETRY
567 #endif
568
569 static void
570 ao_config_radio_rate_show(void) 
571 {
572         printf("Telemetry rate: %d\n", ao_config.radio_rate);
573 }
574
575 static void
576 ao_config_radio_rate_set(void) 
577 {
578         uint16_t r = ao_cmd_decimal();
579         if (ao_cmd_status != ao_cmd_success)
580                 return;
581         if (AO_RADIO_RATE_MAX < r) {
582                 ao_cmd_status = ao_cmd_lex_error;
583                 return;
584         }
585         _ao_config_edit_start();
586         ao_config.radio_rate = r;
587         _ao_config_edit_finish();
588 #if HAS_TELEMETRY
589         ao_telemetry_reset_interval();
590 #endif
591 #if HAS_RADIO_RECV
592         ao_radio_recv_abort();
593 #endif
594 }
595 #endif
596
597 #if HAS_LOG
598
599 static void
600 ao_config_log_show(void) 
601 {
602         printf("Max flight log: %d kB\n", (int16_t) (ao_config.flight_log_max >> 10));
603 #if FLIGHT_LOG_APPEND
604         printf("Log fixed: 1\n");
605 #endif
606 }
607
608 #if FLIGHT_LOG_APPEND && HAS_CONFIG_SAVE
609 void
610 ao_config_log_fix_append(void)
611 {
612         _ao_config_edit_start();
613         ao_config.flight_log_max = ao_storage_log_max;
614         _ao_config_edit_finish();
615         ao_mutex_get(&ao_config_mutex);
616         _ao_config_put();
617         ao_config_dirty = 0;
618         ao_mutex_put(&ao_config_mutex);
619 }
620 #endif
621
622 static void
623 ao_config_log_set(void) 
624 {
625 #if FLIGHT_LOG_APPEND
626         printf("Flight log fixed size %d kB\n", ao_storage_log_max >> 10);
627 #else
628         uint16_t        block = (uint16_t) (ao_storage_block >> 10);
629         uint16_t        log_max = (uint16_t) (ao_storage_log_max >> 10);
630         uint32_t        r;
631
632         r = ao_cmd_decimal();
633         if (ao_cmd_status != ao_cmd_success)
634                 return;
635         if (ao_log_present())
636                 printf("Storage must be empty before changing log size\n");
637         else if (block > 1024 && (r & (block - 1)))
638                 printf("Flight log size must be multiple of %d kB\n", block);
639         else if (r > log_max)
640                 printf("Flight log max %d kB\n", log_max);
641         else {
642                 _ao_config_edit_start();
643                 ao_config.flight_log_max = r << 10;
644                 _ao_config_edit_finish();
645         }
646 #endif
647 }
648 #endif /* HAS_LOG */
649
650 #if HAS_IGNITE
651 static void
652 ao_config_ignite_mode_show(void) 
653 {
654         printf("Ignite mode: %d\n", ao_config.ignite_mode);
655 }
656
657 static void
658 ao_config_ignite_mode_set(void) 
659 {
660         uint16_t r = ao_cmd_decimal();
661         if (ao_cmd_status != ao_cmd_success)
662                 return;
663         _ao_config_edit_start();
664         ao_config.ignite_mode = r;
665         _ao_config_edit_finish();
666 }
667 #endif
668
669 #if HAS_ACCEL
670 static void
671 ao_config_pad_orientation_show(void) 
672 {
673         printf("Pad orientation: %d\n", ao_config.pad_orientation);
674 }
675
676 static void
677 ao_config_pad_orientation_set(void) 
678 {
679         uint16_t r = ao_cmd_decimal() & 1;
680         if (ao_cmd_status != ao_cmd_success)
681                 return;
682         _ao_config_edit_start();
683         if (ao_config.pad_orientation != r) {
684                 accel_t t;
685                 t = ao_config.accel_plus_g;
686                 ao_config.accel_plus_g = ao_data_accel_invert(ao_config.accel_minus_g);
687                 ao_config.accel_minus_g = ao_data_accel_invert(t);
688         }
689         ao_config.pad_orientation = r;
690         _ao_config_edit_finish();
691 }
692 #endif
693
694 #if HAS_RADIO
695 static void
696 ao_config_radio_enable_show(void) 
697 {
698         printf("Radio enable: %d\n", ao_config.radio_enable);
699 }
700
701 static void
702 ao_config_radio_enable_set(void) 
703 {
704         uint16_t r = ao_cmd_decimal();
705         if (ao_cmd_status != ao_cmd_success)
706                 return;
707         _ao_config_edit_start();
708         ao_config.radio_enable = r;
709         _ao_config_edit_finish();
710 #if HAS_TELEMETRY && HAS_RADIO_RATE
711         ao_telemetry_reset_interval();
712 #endif
713 }
714 #endif /* HAS_RADIO */
715
716 #if HAS_AES
717
718 uint8_t ao_config_aes_seq = 1;
719
720 static void
721 ao_config_key_show(void) 
722 {
723         uint8_t i;
724         printf("AES key: ");
725         for (i = 0; i < AO_AES_LEN; i++)
726                 printf ("%02x", ao_config.aes_key[i]);
727         printf("\n");
728 }
729
730 static void
731 ao_config_key_set(void) 
732 {
733         uint8_t i;
734
735         _ao_config_edit_start();
736         for (i = 0; i < AO_AES_LEN; i++) {
737                 uint8_t b = ao_cmd_hexbyte();
738                 if (ao_cmd_status != ao_cmd_success)
739                         break;
740                 ao_config.aes_key[i] = b;
741         }
742         ++ao_config_aes_seq;
743         _ao_config_edit_finish();
744 }
745 #endif
746
747 #if HAS_APRS
748
749 static void
750 ao_config_aprs_show(void)
751 {
752         printf ("APRS interval: %d\n", ao_config.aprs_interval);
753 }
754
755 static void
756 ao_config_aprs_set(void)
757 {
758         uint16_t r = ao_cmd_decimal();
759         if (ao_cmd_status != ao_cmd_success)
760                 return;
761         _ao_config_edit_start();
762         ao_config.aprs_interval = r;
763         _ao_config_edit_finish();
764         ao_telemetry_reset_interval();
765 }
766
767 static void
768 ao_config_aprs_offset_show(void)
769 {
770         printf ("APRS offset: %d\n", ao_config.aprs_offset);
771 }
772
773 static void
774 ao_config_aprs_offset_set(void)
775 {
776         uint16_t r = ao_cmd_decimal();
777         if (ao_cmd_status != ao_cmd_success)
778                 return;
779         _ao_config_edit_start();
780         ao_config.aprs_offset = r;
781         _ao_config_edit_finish();
782         ao_telemetry_reset_interval();
783 }
784
785 #endif /* HAS_APRS */
786
787 #if HAS_RADIO_AMP
788
789 void
790 ao_config_radio_amp_show(void)
791 {
792         printf ("Radio amp setting: %d\n", ao_config.radio_amp);
793 }
794
795 void
796 ao_config_radio_amp_set(void)
797 {
798         uint16_t r = ao_cmd_decimal();
799         if (ao_cmd_status != ao_cmd_success)
800                 return;
801         _ao_config_edit_start();
802         ao_config.radio_amp = r;
803         _ao_config_edit_finish();
804 }
805
806 #endif
807
808 #if HAS_RADIO_POWER
809
810 void
811 ao_config_radio_power_show(void)
812 {
813         printf ("Radio power setting: %d\n", ao_config.radio_power);
814 }
815
816 void
817 ao_config_radio_power_set(void)
818 {
819         uint16_t r = ao_cmd_decimal();
820         if (ao_cmd_status != ao_cmd_success)
821                 return;
822         _ao_config_edit_start();
823         ao_config.radio_power = r;
824         _ao_config_edit_finish();
825 }
826
827 #endif
828
829 #if HAS_BEEP_CONFIG
830 static void
831 ao_config_beep_show(void)
832 {
833         printf ("Beeper setting: %d\n", ao_config.mid_beep);
834 }
835
836 static void
837 ao_config_beep_set(void)
838 {
839         uint16_t r = ao_cmd_decimal();
840         if (ao_cmd_status != ao_cmd_success)
841                 return;
842         _ao_config_edit_start();
843         ao_config.mid_beep = r;
844         _ao_config_edit_finish();
845 }
846 #endif
847
848 #if HAS_TRACKER
849 static void
850 ao_config_tracker_show(void)
851 {
852         printf ("Tracker setting: %d %d\n",
853                 ao_config.tracker_motion,
854                 ao_config.tracker_interval);
855 }
856
857 static void
858 ao_config_tracker_set(void)
859 {
860         uint16_t        m, i;
861         m = ao_cmd_decimal();
862         if (ao_cmd_status != ao_cmd_success)
863                 return;
864         i = ao_cmd_decimal();
865         if (ao_cmd_status != ao_cmd_success)
866                 return;
867         _ao_config_edit_start();
868         ao_config.tracker_motion = m;
869         ao_config.tracker_interval = i;
870         _ao_config_edit_finish();
871 #if HAS_TELEMETRY
872         ao_telemetry_reset_interval();
873 #endif
874 }
875 #endif /* HAS_TRACKER */
876
877 #if AO_PYRO_NUM
878 static void
879 ao_config_pyro_time_show(void)
880 {
881         printf ("Pyro time: %d\n", ao_config.pyro_time);
882 }
883
884 static void
885 ao_config_pyro_time_set(void)
886 {
887         uint16_t r = ao_cmd_decimal();
888         if (ao_cmd_status != ao_cmd_success)
889                 return;
890         _ao_config_edit_start();
891         ao_config.pyro_time = r;
892         _ao_config_edit_finish();
893 }
894 #endif
895
896 #if HAS_APRS
897 static void
898 ao_config_aprs_ssid_show(void)
899 {
900         printf ("APRS SSID: %d\n",
901                 ao_config.aprs_ssid);
902 }
903
904 static void
905 ao_config_aprs_ssid_set(void)
906 {
907         uint16_t r = ao_cmd_decimal();
908         if (ao_cmd_status != ao_cmd_success)
909                 return;
910         if (15 < r) {
911                 ao_cmd_status = ao_cmd_lex_error;
912                 return;
913         }
914         _ao_config_edit_start();
915         ao_config.aprs_ssid = r;
916         _ao_config_edit_finish();
917 }
918
919 static void
920 ao_config_aprs_format_set(void)
921 {
922         uint16_t r = ao_cmd_decimal();
923         if (ao_cmd_status != ao_cmd_success)
924                 return;
925         _ao_config_edit_start();
926         ao_config.aprs_format = r != 0;
927         _ao_config_edit_finish();
928 }
929
930 static void
931 ao_config_aprs_format_show(void)
932 {
933         printf ("APRS format: %d\n", ao_config.aprs_format);
934 }
935 #endif /* HAS_APRS */
936
937 #if HAS_FIXED_PAD_BOX
938 static void
939 ao_config_pad_box_show(void)
940 {
941         printf ("Pad box: %d\n", ao_config.pad_box);
942 }
943
944 static void
945 ao_config_pad_box_set(void)
946 {
947         uint16_t r = ao_cmd_decimal();
948         if (ao_cmd_status != ao_cmd_success)
949                 return;
950         _ao_config_edit_start();
951         ao_config.pad_box = r;
952         _ao_config_edit_finish();
953 }
954
955 static void
956 ao_config_pad_idle_show(void)
957 {
958         printf ("Idle timeout: %d\n", ao_config.pad_idle);
959 }
960
961 static void
962 ao_config_pad_idle_set(void)
963 {
964         uint16_t r = ao_cmd_decimal();
965         if (ao_cmd_status != ao_cmd_success)
966                 return;
967         _ao_config_edit_start();
968         ao_config.pad_idle = r;
969         _ao_config_edit_finish();
970 }
971 #endif
972
973 struct ao_config_var {
974         const char      *str;
975         void            (*set)(void);
976         void            (*show)(void);
977 };
978
979 static void
980 ao_config_help(void);
981
982 static void
983 ao_config_show(void);
984
985 #if HAS_CONFIG_SAVE
986 static void
987 ao_config_save(void);
988 #endif
989
990 const struct ao_config_var ao_config_vars[] = {
991 #if HAS_FLIGHT
992         { "m <meters>\0Main deploy (m)",
993           ao_config_main_deploy_set,    ao_config_main_deploy_show, },
994         { "d <delay>\0Apogee delay (s)",
995           ao_config_apogee_delay_set,   ao_config_apogee_delay_show },
996         { "L <seconds>\0Apogee detect lockout (s)",
997           ao_config_apogee_lockout_set, ao_config_apogee_lockout_show, },
998 #endif /* HAS_FLIGHT */
999 #if HAS_RADIO
1000         { "F <freq>\0Frequency (kHz)",
1001           ao_config_frequency_set, ao_config_frequency_show },
1002 #if HAS_RADIO_FORWARD
1003         { "R <freq>\0Repeater output frequency (kHz)",
1004           ao_config_send_frequency_set, ao_config_send_frequency_show },
1005 #endif
1006         { "c <call>\0Callsign (8 char max)",
1007           ao_config_callsign_set,       ao_config_callsign_show },
1008         { "e <0 disable, 1 enable>\0Enable telemetry and RDF",
1009           ao_config_radio_enable_set, ao_config_radio_enable_show },
1010         { "f <cal>\0Radio calib (cal = rf/(xtal/2^16))",
1011           ao_config_radio_cal_set,      ao_config_radio_cal_show },
1012 #if HAS_RADIO_RATE
1013         { "T <rate>\0Telemetry rate (0=38.4, 1=9.6, 2=2.4)",
1014           ao_config_radio_rate_set,     ao_config_radio_rate_show },
1015 #endif
1016 #if HAS_RADIO_POWER
1017         { "p <setting>\0Radio power setting (0-255)",
1018           ao_config_radio_power_set,    ao_config_radio_power_show },
1019 #endif
1020 #if HAS_RADIO_AMP
1021         { "d <setting>\0Radio amplifier setting (0-3)",
1022           ao_config_radio_amp_set,      ao_config_radio_amp_show },
1023 #endif
1024 #endif /* HAS_RADIO */
1025 #if HAS_ACCEL
1026         { "a <+g> <-g>\0Accel calib (0 for auto)",
1027           ao_config_accel_calibrate_set,ao_config_accel_calibrate_show },
1028         { "o <0 antenna up, 1 antenna down>\0Pad orientation",
1029           ao_config_pad_orientation_set,ao_config_pad_orientation_show },
1030 #endif /* HAS_ACCEL */
1031 #if HAS_LOG
1032         { "l <size>\0Flight log size (kB)",
1033           ao_config_log_set,            ao_config_log_show },
1034 #endif
1035 #if HAS_IGNITE
1036         { "i <0 dual, 1 apogee, 2 main>\0Igniter mode",
1037           ao_config_ignite_mode_set,    ao_config_ignite_mode_show },
1038 #endif
1039 #if HAS_AES
1040         { "k <32 hex digits>\0AES encryption key",
1041           ao_config_key_set, ao_config_key_show },
1042 #endif
1043 #if AO_PYRO_NUM
1044         { "P <n,?>\0Pyro channels",
1045           ao_pyro_set, ao_pyro_show },
1046         { "I <ticks>\0Pyro firing time",
1047           ao_config_pyro_time_set, ao_config_pyro_time_show },
1048 #endif
1049 #if HAS_APRS
1050         { "A <secs>\0APRS packet interval (0 disable)",
1051           ao_config_aprs_set, ao_config_aprs_show },
1052 #endif
1053 #if HAS_BEEP_CONFIG
1054         { "b <val>\0Beeper tone (freq = 1/2 (24e6/32) / beep",
1055           ao_config_beep_set, ao_config_beep_show },
1056 #endif
1057 #if HAS_TRACKER
1058         { "t <motion> <interval>\0Tracker configuration",
1059           ao_config_tracker_set, ao_config_tracker_show },
1060 #endif
1061 #if HAS_APRS
1062         { "S <ssid>\0Set APRS SSID (0-15)",
1063           ao_config_aprs_ssid_set, ao_config_aprs_ssid_show },
1064         { "C <0 compressed, 1 uncompressed>\0APRS format",
1065           ao_config_aprs_format_set, ao_config_aprs_format_show },
1066         { "O <aprs-offset>\0APRS Offset from top of minute",
1067           ao_config_aprs_offset_set, ao_config_aprs_offset_show },
1068 #endif
1069 #if HAS_FIXED_PAD_BOX
1070         { "B <box>\0Set pad box (1-99)",
1071           ao_config_pad_box_set, ao_config_pad_box_show },
1072         { "i <seconds>\0Set idle timeout (0 disable)",
1073           ao_config_pad_idle_set, ao_config_pad_idle_show },
1074 #endif
1075         { "s\0Show",
1076           ao_config_show,               0 },
1077 #if HAS_CONFIG_SAVE
1078         { "w\0Write to eeprom",
1079           ao_config_save,               0 },
1080 #endif
1081         { "?\0Help",
1082           ao_config_help,               0 },
1083         { 0, 0, 0 }
1084 };
1085
1086 static void
1087 ao_config_set(void)
1088 {
1089         char    c;
1090         uint8_t cmd;
1091
1092         ao_cmd_white();
1093         c = ao_cmd_lex_c;
1094         ao_cmd_lex();
1095         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
1096                 if (ao_config_vars[cmd].str[0] == c) {
1097                         (*ao_config_vars[cmd].set)();
1098                         return;
1099                 }
1100         ao_cmd_status = ao_cmd_syntax_error;
1101 }
1102
1103 static void
1104 ao_config_help(void) 
1105 {
1106         uint8_t cmd;
1107         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
1108                 printf("%-20s %s\n",
1109                        ao_config_vars[cmd].str,
1110                        ao_config_vars[cmd].str+1+
1111                        strlen(ao_config_vars[cmd].str));
1112 }
1113
1114 static void
1115 ao_config_show(void) 
1116 {
1117         uint8_t cmd;
1118         ao_config_get();
1119         printf("Config version: %d.%d\n",
1120                ao_config.major, ao_config.minor);
1121         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
1122                 if (ao_config_vars[cmd].show)
1123                         (*ao_config_vars[cmd].show)();
1124 #if HAS_MS5607
1125         ao_ms5607_info();
1126 #endif
1127 }
1128
1129 #if HAS_CONFIG_SAVE
1130 static void
1131 ao_config_save(void) 
1132 {
1133         uint8_t saved = 0;
1134         ao_mutex_get(&ao_config_mutex);
1135         if (ao_config_dirty) {
1136                 _ao_config_put();
1137                 ao_config_dirty = 0;
1138                 saved = 1;
1139         }
1140         ao_mutex_put(&ao_config_mutex);
1141         if (saved)
1142                 puts("Saved");
1143         else
1144                 puts("Nothing to save");
1145 }
1146 #endif
1147
1148 const struct ao_cmds ao_config_cmds[] = {
1149         { ao_config_set,        "c <var> <value>\0Set config (? for help, s to show)" },
1150         { 0, NULL },
1151 };
1152
1153 void
1154 ao_config_init(void)
1155 {
1156         ao_cmd_register(&ao_config_cmds[0]);
1157 }