altosdroid: Implement voice just like altosui
[fw/altos] / src / core / ao_config.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19 #include "ao_log.h"
20 #include <ao_sample.h>
21 #include <ao_data.h>
22
23 __xdata struct ao_config ao_config;
24 __pdata uint8_t ao_config_loaded;
25 __pdata uint8_t ao_config_dirty;
26 __xdata uint8_t ao_config_mutex;
27
28 #define AO_CONFIG_DEFAULT_MAIN_DEPLOY   250
29 #define AO_CONFIG_DEFAULT_RADIO_CHANNEL 0
30 #define AO_CONFIG_DEFAULT_CALLSIGN      "N0CALL"
31 #define AO_CONFIG_DEFAULT_ACCEL_ZERO_G  16000
32 #define AO_CONFIG_DEFAULT_APOGEE_DELAY  0
33 #define AO_CONFIG_DEFAULT_IGNITE_MODE   AO_IGNITE_MODE_DUAL
34 #define AO_CONFIG_DEFAULT_PAD_ORIENTATION       AO_PAD_ORIENTATION_ANTENNA_UP
35 #if HAS_EEPROM
36 #ifndef USE_INTERNAL_FLASH
37 #error Please define USE_INTERNAL_FLASH
38 #endif
39 #endif
40 #ifndef AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX
41 #if USE_INTERNAL_FLASH
42 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX        ao_storage_config
43 #else
44 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX        ((uint32_t) 192 * (uint32_t) 1024)
45 #endif
46 #endif
47
48 #if HAS_EEPROM
49 static void
50 _ao_config_put(void)
51 {
52         ao_storage_setup();
53         ao_storage_erase(ao_storage_config);
54         ao_storage_write(ao_storage_config, &ao_config, sizeof (ao_config));
55 #if HAS_FLIGHT
56         ao_log_write_erase(0);
57 #endif
58         ao_storage_flush();
59 }
60
61 void
62 ao_config_put(void)
63 {
64         ao_mutex_get(&ao_config_mutex);
65         _ao_config_put();
66         ao_mutex_put(&ao_config_mutex);
67 }
68 #endif
69
70 #if HAS_RADIO
71 void
72 ao_config_set_radio(void)
73 {
74         ao_config.radio_setting = ao_freq_to_set(ao_config.frequency, ao_config.radio_cal);
75 }
76 #endif /* HAS_RADIO */
77
78 static void
79 _ao_config_get(void)
80 {
81         uint8_t minor;
82
83         if (ao_config_loaded)
84                 return;
85 #if HAS_EEPROM
86         /* Yes, I know ao_storage_read calls ao_storage_setup,
87          * but ao_storage_setup *also* sets ao_storage_config, which we
88          * need before calling ao_storage_read here
89          */
90         ao_storage_setup();
91         ao_storage_read(ao_storage_config, &ao_config, sizeof (ao_config));
92 #endif
93         if (ao_config.major != AO_CONFIG_MAJOR) {
94                 ao_config.major = AO_CONFIG_MAJOR;
95                 ao_config.minor = 0;
96
97                 /* Version 0 stuff */
98                 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
99                 ao_xmemset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
100                 ao_xmemcpy(&ao_config.callsign, CODE_TO_XDATA(AO_CONFIG_DEFAULT_CALLSIGN),
101                        sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
102         }
103         minor = ao_config.minor;
104         if (minor != AO_CONFIG_MINOR) {
105                 /* Fixups for minor version 1 */
106                 if (minor < 1)
107                         ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
108                 /* Fixups for minor version 2 */
109                 if (minor < 2) {
110                         ao_config.accel_plus_g = 0;
111                         ao_config.accel_minus_g = 0;
112                 }
113                 /* Fixups for minor version 3 */
114 #if HAS_RADIO
115                 if (minor < 3)
116                         ao_config.radio_cal = ao_radio_cal;
117 #endif
118                 /* Fixups for minor version 4 */
119                 if (minor < 4)
120                         ao_config.flight_log_max = AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX;
121                 /* Fixupes for minor version 5 */
122                 if (minor < 5)
123                         ao_config.ignite_mode = AO_CONFIG_DEFAULT_IGNITE_MODE;
124                 if (minor < 6)
125                         ao_config.pad_orientation = AO_CONFIG_DEFAULT_PAD_ORIENTATION;
126                 if (minor < 8)
127                         ao_config.radio_enable = TRUE;
128                 if (minor < 9)
129                         ao_xmemset(&ao_config.aes_key, '\0', AO_AES_LEN);
130                 if (minor < 10)
131                         ao_config.frequency = 434550;
132                 if (minor < 11)
133                         ao_config.apogee_lockout = 0;
134 #if AO_PYRO_NUM
135                 if (minor < 12)
136                         memset(&ao_config.pyro, '\0', sizeof (ao_config.pyro));
137 #endif
138                 ao_config.minor = AO_CONFIG_MINOR;
139                 ao_config_dirty = 1;
140         }
141 #if HAS_RADIO
142 #if HAS_FORCE_FREQ
143         if (ao_force_freq)
144                 ao_config.frequency = 434550;
145 #endif
146         ao_config_set_radio();
147 #endif
148         ao_config_loaded = 1;
149 }
150
151 void
152 _ao_config_edit_start(void)
153 {
154         ao_mutex_get(&ao_config_mutex);
155         _ao_config_get();
156 }
157
158 void
159 _ao_config_edit_finish(void)
160 {
161         ao_config_dirty = 1;
162         ao_mutex_put(&ao_config_mutex);
163 }
164
165 void
166 ao_config_get(void)
167 {
168         _ao_config_edit_start();
169         ao_mutex_put(&ao_config_mutex);
170 }
171
172 void
173 ao_config_callsign_show(void)
174 {
175         printf ("Callsign: \"%s\"\n", ao_config.callsign);
176 }
177
178 void
179 ao_config_callsign_set(void) __reentrant
180 {
181         uint8_t c;
182         static __xdata char callsign[AO_MAX_CALLSIGN + 1];
183
184         ao_xmemset(callsign, '\0', sizeof callsign);
185         ao_cmd_white();
186         c = 0;
187         while (ao_cmd_lex_c != '\n') {
188                 if (c < AO_MAX_CALLSIGN)
189                         callsign[c++] = ao_cmd_lex_c;
190                 else
191                         ao_cmd_status = ao_cmd_lex_error;
192                 ao_cmd_lex();
193         }
194         if (ao_cmd_status != ao_cmd_success)
195                 return;
196         _ao_config_edit_start();
197         ao_xmemcpy(&ao_config.callsign, &callsign,
198                AO_MAX_CALLSIGN + 1);
199         _ao_config_edit_finish();
200 }
201
202 #if HAS_RADIO
203 void
204 ao_config_frequency_show(void) __reentrant
205 {
206         printf("Frequency: %ld\n",
207                ao_config.frequency);
208 }
209
210 void
211 ao_config_frequency_set(void) __reentrant
212 {
213         ao_cmd_decimal();
214         if (ao_cmd_status != ao_cmd_success)
215                 return;
216         _ao_config_edit_start();
217         ao_config.frequency = ao_cmd_lex_u32;
218         ao_config_set_radio();
219         _ao_config_edit_finish();
220         ao_radio_recv_abort();
221 }
222 #endif
223
224 #if HAS_FLIGHT
225
226 void
227 ao_config_main_deploy_show(void) __reentrant
228 {
229         printf("Main deploy: %d meters\n",
230                ao_config.main_deploy);
231 }
232
233 void
234 ao_config_main_deploy_set(void) __reentrant
235 {
236         ao_cmd_decimal();
237         if (ao_cmd_status != ao_cmd_success)
238                 return;
239         _ao_config_edit_start();
240         ao_config.main_deploy = ao_cmd_lex_i;
241         _ao_config_edit_finish();
242 }
243
244 #if HAS_ACCEL
245 void
246 ao_config_accel_calibrate_show(void) __reentrant
247 {
248         printf("Accel cal +1g: %d -1g: %d\n",
249                ao_config.accel_plus_g, ao_config.accel_minus_g);
250 }
251
252 #define ACCEL_CALIBRATE_SAMPLES 1024
253 #define ACCEL_CALIBRATE_SHIFT   10
254
255 static int16_t
256 ao_config_accel_calibrate_auto(char *orientation) __reentrant
257 {
258         uint16_t        i;
259         int32_t         accel_total;
260         uint8_t         cal_data_ring;
261
262         printf("Orient antenna %s and press a key...", orientation);
263         flush();
264         (void) getchar();
265         puts("\r\n"); flush();
266         puts("Calibrating..."); flush();
267         i = ACCEL_CALIBRATE_SAMPLES;
268         accel_total = 0;
269         cal_data_ring = ao_sample_data;
270         while (i) {
271                 ao_sleep(DATA_TO_XDATA(&ao_sample_data));
272                 while (i && cal_data_ring != ao_sample_data) {
273                         accel_total += (int32_t) ao_data_accel(&ao_data_ring[cal_data_ring]);
274                         cal_data_ring = ao_data_ring_next(cal_data_ring);
275                         i--;
276                 }
277         }
278         return accel_total >> ACCEL_CALIBRATE_SHIFT;
279 }
280
281 void
282 ao_config_accel_calibrate_set(void) __reentrant
283 {
284         int16_t up, down;
285         ao_cmd_decimal();
286         if (ao_cmd_status != ao_cmd_success)
287                 return;
288         if (ao_cmd_lex_i == 0) {
289                 up = ao_config_accel_calibrate_auto("up");
290                 down = ao_config_accel_calibrate_auto("down");
291         } else {
292                 up = ao_cmd_lex_i;
293                 ao_cmd_decimal();
294                 if (ao_cmd_status != ao_cmd_success)
295                         return;
296                 down = ao_cmd_lex_i;
297         }
298         if (up >= down) {
299                 printf("Invalid accel: up (%d) down (%d)\n",
300                        up, down);
301                 return;
302         }
303         _ao_config_edit_start();
304         ao_config.accel_plus_g = up;
305         ao_config.accel_minus_g = down;
306         _ao_config_edit_finish();
307 }
308 #endif /* HAS_ACCEL */
309
310 void
311 ao_config_apogee_delay_show(void) __reentrant
312 {
313         printf("Apogee delay: %d seconds\n",
314                ao_config.apogee_delay);
315 }
316
317 void
318 ao_config_apogee_delay_set(void) __reentrant
319 {
320         ao_cmd_decimal();
321         if (ao_cmd_status != ao_cmd_success)
322                 return;
323         _ao_config_edit_start();
324         ao_config.apogee_delay = ao_cmd_lex_i;
325         _ao_config_edit_finish();
326 }
327
328 void
329 ao_config_apogee_lockout_show(void) __reentrant
330 {
331         printf ("Apogee lockout: %d seconds\n",
332                 ao_config.apogee_lockout);
333 }
334
335 void
336 ao_config_apogee_lockout_set(void) __reentrant
337 {
338         ao_cmd_decimal();
339         if (ao_cmd_status != ao_cmd_success)
340                 return;
341         _ao_config_edit_start();
342         ao_config.apogee_lockout = ao_cmd_lex_i;
343         _ao_config_edit_finish();
344 }
345
346 #endif /* HAS_FLIGHT */
347
348 #if HAS_RADIO
349 void
350 ao_config_radio_cal_show(void) __reentrant
351 {
352         printf("Radio cal: %ld\n", ao_config.radio_cal);
353 }
354
355 void
356 ao_config_radio_cal_set(void) __reentrant
357 {
358         ao_cmd_decimal();
359         if (ao_cmd_status != ao_cmd_success)
360                 return;
361         _ao_config_edit_start();
362         ao_config.radio_cal = ao_cmd_lex_u32;
363         ao_config_set_radio();
364         _ao_config_edit_finish();
365 }
366 #endif
367
368 #if HAS_LOG
369 void
370 ao_config_log_show(void) __reentrant
371 {
372         printf("Max flight log: %d kB\n", (int16_t) (ao_config.flight_log_max >> 10));
373 }
374
375 void
376 ao_config_log_set(void) __reentrant
377 {
378         uint16_t        block = (uint16_t) (ao_storage_block >> 10);
379         uint16_t        config = (uint16_t) (ao_storage_config >> 10);
380
381         ao_cmd_decimal();
382         if (ao_cmd_status != ao_cmd_success)
383                 return;
384         if (ao_log_present())
385                 printf("Storage must be empty before changing log size\n");
386         else if (block > 1024 && (ao_cmd_lex_i & (block - 1)))
387                 printf("Flight log size must be multiple of %d kB\n", block);
388         else if (ao_cmd_lex_i > config)
389                 printf("Flight log max %d kB\n", config);
390         else {
391                 _ao_config_edit_start();
392                 ao_config.flight_log_max = (uint32_t) ao_cmd_lex_i << 10;
393                 _ao_config_edit_finish();
394         }
395 }
396 #endif /* HAS_LOG */
397
398 #if HAS_IGNITE
399 void
400 ao_config_ignite_mode_show(void) __reentrant
401 {
402         printf("Ignite mode: %d\n", ao_config.ignite_mode);
403 }
404
405 void
406 ao_config_ignite_mode_set(void) __reentrant
407 {
408         ao_cmd_decimal();
409         if (ao_cmd_status != ao_cmd_success)
410                 return;
411         _ao_config_edit_start();
412         ao_config.ignite_mode = ao_cmd_lex_i;
413         _ao_config_edit_finish();
414 }
415 #endif
416
417 #if HAS_ACCEL
418 void
419 ao_config_pad_orientation_show(void) __reentrant
420 {
421         printf("Pad orientation: %d\n", ao_config.pad_orientation);
422 }
423
424 void
425 ao_config_pad_orientation_set(void) __reentrant
426 {
427         ao_cmd_decimal();
428         if (ao_cmd_status != ao_cmd_success)
429                 return;
430         _ao_config_edit_start();
431         ao_cmd_lex_i &= 1;
432         if (ao_config.pad_orientation != ao_cmd_lex_i) {
433                 uint16_t t;
434                 t = ao_config.accel_plus_g;
435                 ao_config.accel_plus_g = 0x7fff - ao_config.accel_minus_g;
436                 ao_config.accel_minus_g = 0x7fff - t;
437         }
438         ao_config.pad_orientation = ao_cmd_lex_i;
439         _ao_config_edit_finish();
440 }
441 #endif
442
443 #if HAS_RADIO
444 void
445 ao_config_radio_enable_show(void) __reentrant
446 {
447         printf("Radio enable: %d\n", ao_config.radio_enable);
448 }
449
450 void
451 ao_config_radio_enable_set(void) __reentrant
452 {
453         ao_cmd_decimal();
454         if (ao_cmd_status != ao_cmd_success)
455                 return;
456         _ao_config_edit_start();
457         ao_config.radio_enable = ao_cmd_lex_i;
458         _ao_config_edit_finish();
459 }
460 #endif /* HAS_RADIO */
461         
462 #if HAS_AES
463 void
464 ao_config_key_show(void) __reentrant
465 {
466         uint8_t i;
467         printf("AES key: ");
468         for (i = 0; i < AO_AES_LEN; i++)
469                 printf ("%02x", ao_config.aes_key[i]);
470         printf("\n");
471 }
472
473 void
474 ao_config_key_set(void) __reentrant
475 {
476         uint8_t i;
477
478         _ao_config_edit_start();
479         for (i = 0; i < AO_AES_LEN; i++) {
480                 ao_cmd_hexbyte();
481                 if (ao_cmd_status != ao_cmd_success)
482                         break;
483                 ao_config.aes_key[i] = ao_cmd_lex_i;
484         }
485         _ao_config_edit_finish();
486 }
487 #endif
488
489 struct ao_config_var {
490         __code char     *str;
491         void            (*set)(void) __reentrant;
492         void            (*show)(void) __reentrant;
493 };
494
495 static void
496 ao_config_help(void) __reentrant;
497
498 static void
499 ao_config_show(void) __reentrant;
500
501 static void
502 ao_config_write(void) __reentrant;
503
504 __code struct ao_config_var ao_config_vars[] = {
505 #if HAS_FLIGHT
506         { "m <meters>\0Main deploy (m)",
507           ao_config_main_deploy_set,    ao_config_main_deploy_show, },
508         { "d <delay>\0Apogee delay (s)",
509           ao_config_apogee_delay_set,   ao_config_apogee_delay_show },
510         { "L <seconds>\0Apogee detect lockout (s)",
511           ao_config_apogee_lockout_set, ao_config_apogee_lockout_show, },
512 #endif /* HAS_FLIGHT */
513 #if HAS_RADIO
514         { "F <freq>\0Frequency (kHz)",
515           ao_config_frequency_set, ao_config_frequency_show },
516         { "c <call>\0Callsign (8 char max)",
517           ao_config_callsign_set,       ao_config_callsign_show },
518         { "e <0 disable, 1 enable>\0Enable telemetry and RDF",
519           ao_config_radio_enable_set, ao_config_radio_enable_show },
520 #endif /* HAS_RADIO */
521 #if HAS_ACCEL
522         { "a <+g> <-g>\0Accel calib (0 for auto)",
523           ao_config_accel_calibrate_set,ao_config_accel_calibrate_show },
524 #endif /* HAS_ACCEL */
525 #if HAS_RADIO
526         { "f <cal>\0Radio calib (cal = rf/(xtal/2^16))",
527           ao_config_radio_cal_set,      ao_config_radio_cal_show },
528 #endif /* HAS_RADIO */
529 #if HAS_LOG
530         { "l <size>\0Flight log size (kB)",
531           ao_config_log_set,            ao_config_log_show },
532 #endif
533 #if HAS_IGNITE
534         { "i <0 dual, 1 apogee, 2 main>\0Set igniter mode",
535           ao_config_ignite_mode_set,    ao_config_ignite_mode_show },
536 #endif
537 #if HAS_ACCEL
538         { "o <0 antenna up, 1 antenna down>\0Set pad orientation",
539           ao_config_pad_orientation_set,ao_config_pad_orientation_show },
540 #endif
541 #if HAS_AES
542         { "k <32 hex digits>\0Set AES encryption key",
543           ao_config_key_set, ao_config_key_show },
544 #endif
545 #if AO_PYRO_NUM
546         { "P <n,?>\0Configure pyro channels",
547           ao_pyro_set, ao_pyro_show },
548 #endif
549         { "s\0Show",
550           ao_config_show,               0 },
551 #if HAS_EEPROM
552         { "w\0Write to eeprom",
553           ao_config_write,              0 },
554 #endif
555         { "?\0Help",
556           ao_config_help,               0 },
557         { 0, 0, 0 }
558 };
559
560 void
561 ao_config_set(void)
562 {
563         char    c;
564         uint8_t cmd;
565
566         ao_cmd_white();
567         c = ao_cmd_lex_c;
568         ao_cmd_lex();
569         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
570                 if (ao_config_vars[cmd].str[0] == c) {
571                         (*ao_config_vars[cmd].set)();
572                         return;
573                 }
574         ao_cmd_status = ao_cmd_syntax_error;
575 }
576
577 static void
578 ao_config_help(void) __reentrant
579 {
580         uint8_t cmd;
581         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
582                 printf("%-20s %s\n",
583                        ao_config_vars[cmd].str,
584                        ao_config_vars[cmd].str+1+
585                        strlen(ao_config_vars[cmd].str));
586 }
587
588 static void
589 ao_config_show(void) __reentrant
590 {
591         uint8_t cmd;
592         ao_config_get();
593         printf("Config version: %d.%d\n",
594                ao_config.major, ao_config.minor);
595         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
596                 if (ao_config_vars[cmd].show)
597                         (*ao_config_vars[cmd].show)();
598 }
599
600 #if HAS_EEPROM
601 static void
602 ao_config_write(void) __reentrant
603 {
604         uint8_t saved = 0;
605         ao_mutex_get(&ao_config_mutex);
606         if (ao_config_dirty) {
607                 _ao_config_put();
608                 ao_config_dirty = 0;
609                 saved = 1;
610         }
611         ao_mutex_put(&ao_config_mutex);
612         if (saved)
613                 puts("Saved");
614         else
615                 puts("Nothing to save");
616 }
617 #endif
618
619 __code struct ao_cmds ao_config_cmds[] = {
620         { ao_config_set,        "c <var> <value>\0Set config (? for help, s to show)" },
621         { 0, NULL },
622 };
623
624 void
625 ao_config_init(void)
626 {
627         ao_cmd_register(&ao_config_cmds[0]);
628 }