altos: Allow six-axis orientation
[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         uint32_t        r;
629
630         r = ao_cmd_decimal();
631         if (ao_cmd_status != ao_cmd_success)
632                 return;
633         r = r << 10;
634         if (ao_log_present()) {
635                 if (r != ao_config.flight_log_max)
636                         printf("Storage must be empty before changing log size\n");
637                 return;
638         }
639         if (r > ao_storage_log_max) {
640                 printf("Flight log max %d kB\n", ao_storage_log_max >> 10);
641                 return;
642         }
643         _ao_config_edit_start();
644         ao_config.flight_log_max = r & ~(ao_storage_block - 1);
645         _ao_config_edit_finish();
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 #if ALLOW_SIX_AXIS_PAD
677 #define PAD_ORIENTATION_MAX     5
678 #else
679 #define PAD_ORIENTATION_MAX     1
680 #endif
681
682 static void
683 ao_config_pad_orientation_set(void) 
684 {
685         uint16_t r = ao_cmd_decimal();
686         if (ao_cmd_status != ao_cmd_success)
687                 return;
688         if (r > PAD_ORIENTATION_MAX) {
689                 ao_cmd_status = ao_cmd_lex_error;
690                 return;
691         }
692         _ao_config_edit_start();
693         if (ao_config.pad_orientation != r) {
694 #if ALLOW_SIX_AXIS_PAD
695                 accel_t zero;
696                 switch (r >> 1) {
697                 case 0:
698                 default:
699                         zero = ao_config.accel_zero_along;
700                         break;
701                 case 1:
702                         zero = ao_config.accel_zero_across;
703                         break;
704                 case 2:
705                         zero = ao_config.accel_zero_through;
706                         break;
707                 }
708                 accel_t plus2 = ao_config.accel_minus_g - ao_config.accel_plus_g;
709                 if (plus2 < 0)
710                         plus2 = -plus2;
711                 accel_t plus1 = (plus2 >> 1);
712                 accel_t minus1 = plus2 - plus1;
713                 if (r & 1) {
714                         ao_config.accel_plus_g = ao_data_accel_invert(zero + minus1);
715                         ao_config.accel_minus_g = ao_data_accel_invert(zero - plus1);
716                 } else {
717                         ao_config.accel_plus_g = zero - plus1;
718                         ao_config.accel_minus_g = zero + minus1;
719                 }
720 #else
721                 accel_t t;
722                 t = ao_config.accel_plus_g;
723                 ao_config.accel_plus_g = ao_data_accel_invert(ao_config.accel_minus_g);
724                 ao_config.accel_minus_g = ao_data_accel_invert(t);
725 #endif
726         }
727         ao_config.pad_orientation = r;
728         _ao_config_edit_finish();
729 }
730 #endif
731
732 #if HAS_RADIO
733 static void
734 ao_config_radio_enable_show(void) 
735 {
736         printf("Radio enable: %d\n", ao_config.radio_enable);
737 }
738
739 static void
740 ao_config_radio_enable_set(void) 
741 {
742         uint16_t r = ao_cmd_decimal();
743         if (ao_cmd_status != ao_cmd_success)
744                 return;
745         _ao_config_edit_start();
746         ao_config.radio_enable = r;
747         _ao_config_edit_finish();
748 #if HAS_TELEMETRY && HAS_RADIO_RATE
749         ao_telemetry_reset_interval();
750 #endif
751 }
752 #endif /* HAS_RADIO */
753
754 #if HAS_AES
755
756 uint8_t ao_config_aes_seq = 1;
757
758 static void
759 ao_config_key_show(void) 
760 {
761         uint8_t i;
762         printf("AES key: ");
763         for (i = 0; i < AO_AES_LEN; i++)
764                 printf ("%02x", ao_config.aes_key[i]);
765         printf("\n");
766 }
767
768 static void
769 ao_config_key_set(void) 
770 {
771         uint8_t i;
772
773         _ao_config_edit_start();
774         for (i = 0; i < AO_AES_LEN; i++) {
775                 uint8_t b = ao_cmd_hexbyte();
776                 if (ao_cmd_status != ao_cmd_success)
777                         break;
778                 ao_config.aes_key[i] = b;
779         }
780         ++ao_config_aes_seq;
781         _ao_config_edit_finish();
782 }
783 #endif
784
785 #if HAS_APRS
786
787 static void
788 ao_config_aprs_show(void)
789 {
790         printf ("APRS interval: %d\n", ao_config.aprs_interval);
791 }
792
793 static void
794 ao_config_aprs_set(void)
795 {
796         uint16_t r = ao_cmd_decimal();
797         if (ao_cmd_status != ao_cmd_success)
798                 return;
799         _ao_config_edit_start();
800         ao_config.aprs_interval = r;
801         _ao_config_edit_finish();
802         ao_telemetry_reset_interval();
803 }
804
805 static void
806 ao_config_aprs_offset_show(void)
807 {
808         printf ("APRS offset: %d\n", ao_config.aprs_offset);
809 }
810
811 static void
812 ao_config_aprs_offset_set(void)
813 {
814         uint16_t r = ao_cmd_decimal();
815         if (ao_cmd_status != ao_cmd_success)
816                 return;
817         _ao_config_edit_start();
818         ao_config.aprs_offset = r;
819         _ao_config_edit_finish();
820         ao_telemetry_reset_interval();
821 }
822
823 #endif /* HAS_APRS */
824
825 #if HAS_RADIO_AMP
826
827 void
828 ao_config_radio_amp_show(void)
829 {
830         printf ("Radio amp setting: %d\n", ao_config.radio_amp);
831 }
832
833 void
834 ao_config_radio_amp_set(void)
835 {
836         uint16_t r = ao_cmd_decimal();
837         if (ao_cmd_status != ao_cmd_success)
838                 return;
839         _ao_config_edit_start();
840         ao_config.radio_amp = r;
841         _ao_config_edit_finish();
842 }
843
844 #endif
845
846 #if HAS_RADIO_POWER
847
848 void
849 ao_config_radio_power_show(void)
850 {
851         printf ("Radio power setting: %d\n", ao_config.radio_power);
852 }
853
854 void
855 ao_config_radio_power_set(void)
856 {
857         uint16_t r = ao_cmd_decimal();
858         if (ao_cmd_status != ao_cmd_success)
859                 return;
860         _ao_config_edit_start();
861         ao_config.radio_power = r;
862         _ao_config_edit_finish();
863 }
864
865 #endif
866
867 #if HAS_BEEP_CONFIG
868 static void
869 ao_config_beep_show(void)
870 {
871         printf ("Beeper setting: %d\n", ao_config.mid_beep);
872 }
873
874 static void
875 ao_config_beep_set(void)
876 {
877         uint16_t r = ao_cmd_decimal();
878         if (ao_cmd_status != ao_cmd_success)
879                 return;
880         _ao_config_edit_start();
881         ao_config.mid_beep = r;
882         _ao_config_edit_finish();
883 }
884 #endif
885
886 #if HAS_TRACKER
887 static void
888 ao_config_tracker_show(void)
889 {
890         printf ("Tracker setting: %d %d\n",
891                 ao_config.tracker_motion,
892                 ao_config.tracker_interval);
893 }
894
895 static void
896 ao_config_tracker_set(void)
897 {
898         uint16_t        m, i;
899         m = ao_cmd_decimal();
900         if (ao_cmd_status != ao_cmd_success)
901                 return;
902         i = ao_cmd_decimal();
903         if (ao_cmd_status != ao_cmd_success)
904                 return;
905         _ao_config_edit_start();
906         ao_config.tracker_motion = m;
907         ao_config.tracker_interval = i;
908         _ao_config_edit_finish();
909 #if HAS_TELEMETRY
910         ao_telemetry_reset_interval();
911 #endif
912 }
913 #endif /* HAS_TRACKER */
914
915 #if AO_PYRO_NUM
916 static void
917 ao_config_pyro_time_show(void)
918 {
919         printf ("Pyro time: %d\n", ao_config.pyro_time);
920 }
921
922 static void
923 ao_config_pyro_time_set(void)
924 {
925         uint16_t r = ao_cmd_decimal();
926         if (ao_cmd_status != ao_cmd_success)
927                 return;
928         _ao_config_edit_start();
929         ao_config.pyro_time = r;
930         _ao_config_edit_finish();
931 }
932 #endif
933
934 #if HAS_APRS
935 static void
936 ao_config_aprs_ssid_show(void)
937 {
938         printf ("APRS SSID: %d\n",
939                 ao_config.aprs_ssid);
940 }
941
942 static void
943 ao_config_aprs_ssid_set(void)
944 {
945         uint16_t r = ao_cmd_decimal();
946         if (ao_cmd_status != ao_cmd_success)
947                 return;
948         if (15 < r) {
949                 ao_cmd_status = ao_cmd_lex_error;
950                 return;
951         }
952         _ao_config_edit_start();
953         ao_config.aprs_ssid = r;
954         _ao_config_edit_finish();
955 }
956
957 static void
958 ao_config_aprs_format_set(void)
959 {
960         uint16_t r = ao_cmd_decimal();
961         if (ao_cmd_status != ao_cmd_success)
962                 return;
963         _ao_config_edit_start();
964         ao_config.aprs_format = r != 0;
965         _ao_config_edit_finish();
966 }
967
968 static void
969 ao_config_aprs_format_show(void)
970 {
971         printf ("APRS format: %d\n", ao_config.aprs_format);
972 }
973 #endif /* HAS_APRS */
974
975 #if HAS_FIXED_PAD_BOX
976 static void
977 ao_config_pad_box_show(void)
978 {
979         printf ("Pad box: %d\n", ao_config.pad_box);
980 }
981
982 static void
983 ao_config_pad_box_set(void)
984 {
985         uint16_t r = ao_cmd_decimal();
986         if (ao_cmd_status != ao_cmd_success)
987                 return;
988         _ao_config_edit_start();
989         ao_config.pad_box = r;
990         _ao_config_edit_finish();
991 }
992
993 static void
994 ao_config_pad_idle_show(void)
995 {
996         printf ("Idle timeout: %d\n", ao_config.pad_idle);
997 }
998
999 static void
1000 ao_config_pad_idle_set(void)
1001 {
1002         uint16_t r = ao_cmd_decimal();
1003         if (ao_cmd_status != ao_cmd_success)
1004                 return;
1005         _ao_config_edit_start();
1006         ao_config.pad_idle = r;
1007         _ao_config_edit_finish();
1008 }
1009 #endif
1010
1011 struct ao_config_var {
1012         const char      *str;
1013         void            (*set)(void);
1014         void            (*show)(void);
1015 };
1016
1017 static void
1018 ao_config_help(void);
1019
1020 static void
1021 ao_config_show(void);
1022
1023 #if HAS_CONFIG_SAVE
1024 static void
1025 ao_config_save(void);
1026 #endif
1027
1028 const struct ao_config_var ao_config_vars[] = {
1029 #if HAS_FLIGHT
1030         { "m <meters>\0Main deploy (m)",
1031           ao_config_main_deploy_set,    ao_config_main_deploy_show, },
1032         { "d <delay>\0Apogee delay (s)",
1033           ao_config_apogee_delay_set,   ao_config_apogee_delay_show },
1034         { "L <seconds>\0Apogee detect lockout (s)",
1035           ao_config_apogee_lockout_set, ao_config_apogee_lockout_show, },
1036 #endif /* HAS_FLIGHT */
1037 #if HAS_RADIO
1038         { "F <freq>\0Frequency (kHz)",
1039           ao_config_frequency_set, ao_config_frequency_show },
1040 #if HAS_RADIO_FORWARD
1041         { "R <freq>\0Repeater output frequency (kHz)",
1042           ao_config_send_frequency_set, ao_config_send_frequency_show },
1043 #endif
1044         { "c <call>\0Callsign (8 char max)",
1045           ao_config_callsign_set,       ao_config_callsign_show },
1046         { "e <0 disable, 1 enable>\0Enable telemetry and RDF",
1047           ao_config_radio_enable_set, ao_config_radio_enable_show },
1048         { "f <cal>\0Radio calib (cal = rf/(xtal/2^16))",
1049           ao_config_radio_cal_set,      ao_config_radio_cal_show },
1050 #if HAS_RADIO_RATE
1051         { "T <rate>\0Telemetry rate (0=38.4, 1=9.6, 2=2.4)",
1052           ao_config_radio_rate_set,     ao_config_radio_rate_show },
1053 #endif
1054 #if HAS_RADIO_POWER
1055         { "p <setting>\0Radio power setting (0-255)",
1056           ao_config_radio_power_set,    ao_config_radio_power_show },
1057 #endif
1058 #if HAS_RADIO_AMP
1059         { "d <setting>\0Radio amplifier setting (0-3)",
1060           ao_config_radio_amp_set,      ao_config_radio_amp_show },
1061 #endif
1062 #endif /* HAS_RADIO */
1063 #if HAS_ACCEL
1064         { "a <+g> <-g>\0Accel calib (0 for auto)",
1065           ao_config_accel_calibrate_set,ao_config_accel_calibrate_show },
1066         { "o <0 antenna up, 1 antenna down>\0Pad orientation",
1067           ao_config_pad_orientation_set,ao_config_pad_orientation_show },
1068 #endif /* HAS_ACCEL */
1069 #if HAS_LOG
1070         { "l <size>\0Flight log size (kB)",
1071           ao_config_log_set,            ao_config_log_show },
1072 #endif
1073 #if HAS_IGNITE
1074         { "i <0 dual, 1 apogee, 2 main>\0Igniter mode",
1075           ao_config_ignite_mode_set,    ao_config_ignite_mode_show },
1076 #endif
1077 #if HAS_AES
1078         { "k <32 hex digits>\0AES encryption key",
1079           ao_config_key_set, ao_config_key_show },
1080 #endif
1081 #if AO_PYRO_NUM
1082         { "P <n,?>\0Pyro channels",
1083           ao_pyro_set, ao_pyro_show },
1084         { "I <ticks>\0Pyro firing time",
1085           ao_config_pyro_time_set, ao_config_pyro_time_show },
1086 #endif
1087 #if HAS_APRS
1088         { "A <secs>\0APRS packet interval (0 disable)",
1089           ao_config_aprs_set, ao_config_aprs_show },
1090 #endif
1091 #if HAS_BEEP_CONFIG
1092         { "b <val>\0Beeper tone (freq = 1/2 (24e6/32) / beep",
1093           ao_config_beep_set, ao_config_beep_show },
1094 #endif
1095 #if HAS_TRACKER
1096         { "t <motion> <interval>\0Tracker configuration",
1097           ao_config_tracker_set, ao_config_tracker_show },
1098 #endif
1099 #if HAS_APRS
1100         { "S <ssid>\0Set APRS SSID (0-15)",
1101           ao_config_aprs_ssid_set, ao_config_aprs_ssid_show },
1102         { "C <0 compressed, 1 uncompressed>\0APRS format",
1103           ao_config_aprs_format_set, ao_config_aprs_format_show },
1104         { "O <aprs-offset>\0APRS Offset from top of minute",
1105           ao_config_aprs_offset_set, ao_config_aprs_offset_show },
1106 #endif
1107 #if HAS_FIXED_PAD_BOX
1108         { "B <box>\0Set pad box (1-99)",
1109           ao_config_pad_box_set, ao_config_pad_box_show },
1110         { "i <seconds>\0Set idle timeout (0 disable)",
1111           ao_config_pad_idle_set, ao_config_pad_idle_show },
1112 #endif
1113         { "s\0Show",
1114           ao_config_show,               0 },
1115 #if HAS_CONFIG_SAVE
1116         { "w\0Write to eeprom",
1117           ao_config_save,               0 },
1118 #endif
1119         { "?\0Help",
1120           ao_config_help,               0 },
1121         { 0, 0, 0 }
1122 };
1123
1124 static void
1125 ao_config_set(void)
1126 {
1127         char    c;
1128         uint8_t cmd;
1129
1130         ao_cmd_white();
1131         c = ao_cmd_lex_c;
1132         ao_cmd_lex();
1133         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
1134                 if (ao_config_vars[cmd].str[0] == c) {
1135                         (*ao_config_vars[cmd].set)();
1136                         return;
1137                 }
1138         ao_cmd_status = ao_cmd_syntax_error;
1139 }
1140
1141 static void
1142 ao_config_help(void) 
1143 {
1144         uint8_t cmd;
1145         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
1146                 printf("%-20s %s\n",
1147                        ao_config_vars[cmd].str,
1148                        ao_config_vars[cmd].str+1+
1149                        strlen(ao_config_vars[cmd].str));
1150 }
1151
1152 static void
1153 ao_config_show(void) 
1154 {
1155         uint8_t cmd;
1156         ao_config_get();
1157         printf("Config version: %d.%d\n",
1158                ao_config.major, ao_config.minor);
1159         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
1160                 if (ao_config_vars[cmd].show)
1161                         (*ao_config_vars[cmd].show)();
1162 #if HAS_MS5607
1163         ao_ms5607_info();
1164 #endif
1165 }
1166
1167 #if HAS_CONFIG_SAVE
1168 static void
1169 ao_config_save(void) 
1170 {
1171         uint8_t saved = 0;
1172         ao_mutex_get(&ao_config_mutex);
1173         if (ao_config_dirty) {
1174                 _ao_config_put();
1175                 ao_config_dirty = 0;
1176                 saved = 1;
1177         }
1178         ao_mutex_put(&ao_config_mutex);
1179         if (saved)
1180                 puts("Saved");
1181         else
1182                 puts("Nothing to save");
1183 }
1184 #endif
1185
1186 const struct ao_cmds ao_config_cmds[] = {
1187         { ao_config_set,        "c <var> <value>\0Set config (? for help, s to show)" },
1188         { 0, NULL },
1189 };
1190
1191 void
1192 ao_config_init(void)
1193 {
1194         ao_cmd_register(&ao_config_cmds[0]);
1195 }