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