4834988658db70053a109d621d4920e9020e8ba6
[fw/altos] / src / ao_config.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 __xdata struct ao_config ao_config;
21 __pdata uint8_t ao_config_loaded;
22 __pdata uint8_t ao_config_dirty;
23 __xdata uint8_t ao_config_mutex;
24
25 #define AO_CONFIG_DEFAULT_MAIN_DEPLOY   250
26 #define AO_CONFIG_DEFAULT_RADIO_CHANNEL 0
27 #define AO_CONFIG_DEFAULT_CALLSIGN      "N0CALL"
28 #define AO_CONFIG_DEFAULT_ACCEL_ZERO_G  16000
29 #define AO_CONFIG_DEFAULT_APOGEE_DELAY  0
30 #define AO_CONFIG_DEFAULT_IGNITE_MODE   AO_IGNITE_MODE_DUAL
31 #if USE_INTERNAL_EEPROM
32 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX        ao_storage_config
33 #else
34 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX        ((uint32_t) 192 * (uint32_t) 1024)
35 #endif
36
37 #if HAS_EEPROM
38 static void
39 _ao_config_put(void)
40 {
41         ao_storage_setup();
42         ao_storage_erase(ao_storage_config);
43         ao_storage_write(ao_storage_config, &ao_config, sizeof (ao_config));
44         ao_log_write_erase(0);
45         ao_storage_flush();
46 }
47
48 void
49 ao_config_put(void)
50 {
51         ao_mutex_get(&ao_config_mutex);
52         _ao_config_put();
53         ao_mutex_put(&ao_config_mutex);
54 }
55 #endif
56
57 static void
58 _ao_config_get(void)
59 {
60         if (ao_config_loaded)
61                 return;
62 #if HAS_EEPROM
63         ao_storage_setup();
64         ao_storage_read(ao_storage_config, &ao_config, sizeof (ao_config));
65 #endif
66         if (ao_config.major != AO_CONFIG_MAJOR) {
67                 ao_config.major = AO_CONFIG_MAJOR;
68                 ao_config.minor = AO_CONFIG_MINOR;
69                 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
70                 ao_config.radio_channel = AO_CONFIG_DEFAULT_RADIO_CHANNEL;
71                 ao_config.accel_plus_g = 0;
72                 ao_config.accel_minus_g = 0;
73                 memset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
74                 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
75                        sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
76                 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
77                 ao_config.radio_cal = ao_radio_cal;
78                 ao_config.flight_log_max = AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX;
79                 ao_config.ignite_mode = AO_CONFIG_DEFAULT_IGNITE_MODE;
80                 ao_config_dirty = 1;
81         }
82         if (ao_config.minor < AO_CONFIG_MINOR) {
83                 /* Fixups for minor version 1 */
84                 if (ao_config.minor < 1)
85                         ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
86                 /* Fixups for minor version 2 */
87                 if (ao_config.minor < 2) {
88                         ao_config.accel_plus_g = 0;
89                         ao_config.accel_minus_g = 0;
90                 }
91                 /* Fixups for minor version 3 */
92                 if (ao_config.minor < 3)
93                         ao_config.radio_cal = ao_radio_cal;
94                 /* Fixups for minor version 4 */
95                 if (ao_config.minor < 4)
96                         ao_config.flight_log_max = AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX;
97                 /* Fixupes for minor version 5 */
98                 if (ao_config.minor < 5)
99                         ao_config.ignite_mode = AO_CONFIG_DEFAULT_IGNITE_MODE;
100                 ao_config.minor = AO_CONFIG_MINOR;
101                 ao_config_dirty = 1;
102         }
103         ao_config_loaded = 1;
104 }
105
106 void
107 ao_config_get(void)
108 {
109         ao_mutex_get(&ao_config_mutex);
110         _ao_config_get();
111         ao_mutex_put(&ao_config_mutex);
112 }
113
114 void
115 ao_config_callsign_show(void)
116 {
117         printf ("Callsign: \"%s\"\n", ao_config.callsign);
118 }
119
120 void
121 ao_config_callsign_set(void) __reentrant
122 {
123         uint8_t c;
124         static __xdata char callsign[AO_MAX_CALLSIGN + 1];
125
126         memset(callsign, '\0', sizeof callsign);
127         ao_cmd_white();
128         c = 0;
129         while (ao_cmd_lex_c != '\n') {
130                 if (c < AO_MAX_CALLSIGN)
131                         callsign[c++] = ao_cmd_lex_c;
132                 else
133                         ao_cmd_status = ao_cmd_lex_error;
134                 ao_cmd_lex();
135         }
136         if (ao_cmd_status != ao_cmd_success)
137                 return;
138         ao_mutex_get(&ao_config_mutex);
139         _ao_config_get();
140         memcpy(&ao_config.callsign, &callsign,
141                AO_MAX_CALLSIGN + 1);
142         ao_config_dirty = 1;
143         ao_mutex_put(&ao_config_mutex);
144         ao_config_callsign_show();
145 }
146
147 void
148 ao_config_radio_channel_show(void) __reentrant
149 {
150         uint32_t        freq = 434550L + ao_config.radio_channel * 100L;
151         uint16_t        mhz = freq / 1000L;
152         uint16_t        khz = freq % 1000L;
153
154         printf("Radio channel: %d (%d.%03dMHz)\n",
155                ao_config.radio_channel, mhz, khz);
156 }
157
158 void
159 ao_config_radio_channel_set(void) __reentrant
160 {
161         ao_cmd_decimal();
162         if (ao_cmd_status != ao_cmd_success)
163                 return;
164         ao_mutex_get(&ao_config_mutex);
165         _ao_config_get();
166         ao_config.radio_channel = ao_cmd_lex_i;
167         ao_config_dirty = 1;
168         ao_mutex_put(&ao_config_mutex);
169         ao_config_radio_channel_show();
170         ao_radio_recv_abort();
171 }
172
173 #if HAS_ADC
174
175 void
176 ao_config_main_deploy_show(void) __reentrant
177 {
178         printf("Main deploy: %d meters (%d feet)\n",
179                ao_config.main_deploy,
180                (int16_t) ((int32_t) ao_config.main_deploy * 328 / 100));
181 }
182
183 void
184 ao_config_main_deploy_set(void) __reentrant
185 {
186         ao_cmd_decimal();
187         if (ao_cmd_status != ao_cmd_success)
188                 return;
189         ao_mutex_get(&ao_config_mutex);
190         _ao_config_get();
191         ao_config.main_deploy = ao_cmd_lex_i;
192         ao_config_dirty = 1;
193         ao_mutex_put(&ao_config_mutex);
194         ao_config_main_deploy_show();
195 }
196
197 #if HAS_ACCEL
198 void
199 ao_config_accel_calibrate_show(void) __reentrant
200 {
201         printf("Accel cal +1g: %d -1g: %d\n",
202                ao_config.accel_plus_g, ao_config.accel_minus_g);
203 }
204
205 #define ACCEL_CALIBRATE_SAMPLES 1024
206 #define ACCEL_CALIBRATE_SHIFT   10
207
208 static int16_t
209 ao_config_accel_calibrate_auto(char *orientation) __reentrant
210 {
211         uint16_t        i;
212         int32_t         accel_total;
213         uint8_t         cal_adc_ring;
214
215         printf("Orient antenna %s and press a key...", orientation);
216         flush();
217         (void) getchar();
218         puts("\r\n"); flush();
219         puts("Calibrating..."); flush();
220         i = ACCEL_CALIBRATE_SAMPLES;
221         accel_total = 0;
222         cal_adc_ring = ao_sample_adc;
223         while (i) {
224                 ao_sleep(DATA_TO_XDATA(&ao_sample_adc));
225                 while (i && cal_adc_ring != ao_sample_adc) {
226                         accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
227                         cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
228                         i--;
229                 }
230         }
231         return accel_total >> ACCEL_CALIBRATE_SHIFT;
232 }
233
234 void
235 ao_config_accel_calibrate_set(void) __reentrant
236 {
237         int16_t up, down;
238         ao_cmd_decimal();
239         if (ao_cmd_status != ao_cmd_success)
240                 return;
241         if (ao_cmd_lex_i == 0) {
242                 up = ao_config_accel_calibrate_auto("up");
243                 down = ao_config_accel_calibrate_auto("down");
244         } else {
245                 up = ao_cmd_lex_i;
246                 ao_cmd_decimal();
247                 if (ao_cmd_status != ao_cmd_success)
248                         return;
249                 down = ao_cmd_lex_i;
250         }
251         if (up >= down) {
252                 printf("Invalid accel: up (%d) down (%d)\n",
253                        up, down);
254                 return;
255         }
256         ao_mutex_get(&ao_config_mutex);
257         _ao_config_get();
258         ao_config.accel_plus_g = up;
259         ao_config.accel_minus_g = down;
260         ao_config_dirty = 1;
261         ao_mutex_put(&ao_config_mutex);
262         ao_config_accel_calibrate_show();
263 }
264 #endif /* HAS_ACCEL */
265
266 void
267 ao_config_apogee_delay_show(void) __reentrant
268 {
269         printf("Apogee delay: %d seconds\n",
270                ao_config.apogee_delay);
271 }
272
273 void
274 ao_config_apogee_delay_set(void) __reentrant
275 {
276         ao_cmd_decimal();
277         if (ao_cmd_status != ao_cmd_success)
278                 return;
279         ao_mutex_get(&ao_config_mutex);
280         _ao_config_get();
281         ao_config.apogee_delay = ao_cmd_lex_i;
282         ao_config_dirty = 1;
283         ao_mutex_put(&ao_config_mutex);
284         ao_config_apogee_delay_show();
285 }
286
287 #endif /* HAS_ADC */
288
289 void
290 ao_config_radio_cal_show(void) __reentrant
291 {
292         printf("Radio cal: %ld\n", ao_config.radio_cal);
293 }
294
295 void
296 ao_config_radio_cal_set(void) __reentrant
297 {
298         ao_cmd_decimal();
299         if (ao_cmd_status != ao_cmd_success)
300                 return;
301         ao_mutex_get(&ao_config_mutex);
302         _ao_config_get();
303         ao_config.radio_cal = ao_cmd_lex_u32;
304         ao_config_dirty = 1;
305         ao_mutex_put(&ao_config_mutex);
306         ao_config_radio_cal_show();
307 }
308
309 #if HAS_EEPROM
310 void
311 ao_config_log_show(void) __reentrant
312 {
313         printf("Max flight log: %d kB\n", (int16_t) (ao_config.flight_log_max >> 10));
314 }
315
316 void
317 ao_config_log_set(void) __reentrant
318 {
319         uint16_t        block = (uint16_t) (ao_storage_block >> 10);
320         uint16_t        config = (uint16_t) (ao_storage_config >> 10);
321
322         ao_cmd_decimal();
323         if (ao_cmd_status != ao_cmd_success)
324                 return;
325         if (ao_log_present())
326                 printf("Storage must be empty before changing log size\n");
327         else if (block > 1024 && (ao_cmd_lex_i & (block - 1)))
328                 printf("Flight log size must be multiple of %d kB\n", block);
329         else if (ao_cmd_lex_i > config)
330                 printf("Flight log max %d kB\n", config);
331         else {
332                 ao_mutex_get(&ao_config_mutex);
333                 _ao_config_get();
334                 ao_config.flight_log_max = (uint32_t) ao_cmd_lex_i << 10;
335                 ao_config_dirty = 1;
336                 ao_mutex_put(&ao_config_mutex);
337                 ao_config_log_show();
338         }
339 }
340 #endif /* HAS_EEPROM */
341
342 #if HAS_IGNITE
343 void
344 ao_config_ignite_mode_show(void) __reentrant
345 {
346         printf("Ignite mode: %d\n", ao_config.ignite_mode);
347 }
348
349 void
350 ao_config_ignite_mode_set(void) __reentrant
351 {
352         ao_cmd_decimal();
353         if (ao_cmd_status != ao_cmd_success)
354                 return;
355         ao_mutex_get(&ao_config_mutex);
356         _ao_config_get();
357         ao_config.ignite_mode = ao_cmd_lex_i;
358         ao_config_dirty = 1;
359         ao_mutex_put(&ao_config_mutex);
360         ao_config_log_show();
361 }
362 #endif
363
364 struct ao_config_var {
365         __code char     *str;
366         void            (*set)(void) __reentrant;
367         void            (*show)(void) __reentrant;
368 };
369
370 static void
371 ao_config_help(void) __reentrant;
372
373 static void
374 ao_config_show(void) __reentrant;
375
376 static void
377 ao_config_write(void) __reentrant;
378
379 __code struct ao_config_var ao_config_vars[] = {
380 #if HAS_ADC
381         { "m <meters>\0Main deploy (in meters)",
382           ao_config_main_deploy_set,    ao_config_main_deploy_show, },
383         { "d <delay>\0Apogee delay (in seconds)",
384           ao_config_apogee_delay_set,   ao_config_apogee_delay_show },
385 #endif /* HAS_ADC */
386         { "r <channel>\0Radio channel (freq = 434.550 + chan * .1)",
387           ao_config_radio_channel_set,  ao_config_radio_channel_show },
388         { "c <call>\0Callsign (8 char max)",
389           ao_config_callsign_set,       ao_config_callsign_show },
390 #if HAS_ACCEL
391         { "a <+g> <-g>\0Accel calib (0 for auto)",
392           ao_config_accel_calibrate_set,ao_config_accel_calibrate_show },
393 #endif /* HAS_ACCEL */
394         { "f <cal>\0Radio calib (cal = rf/(xtal/2^16))",
395           ao_config_radio_cal_set,      ao_config_radio_cal_show },
396 #if HAS_EEPROM
397         { "l <size>\0Flight log size in kB",
398           ao_config_log_set,            ao_config_log_show },
399 #endif
400 #if HAS_IGNITE
401         { "i <0 dual, 1 apogee, 2 main>\0Set igniter mode",
402           ao_config_ignite_mode_set,    ao_config_ignite_mode_show },
403 #endif
404         { "s\0Show",
405           ao_config_show,               ao_config_show },
406 #if HAS_EEPROM
407         { "w\0Write to eeprom",
408           ao_config_write,              ao_config_write },
409 #endif
410         { "?\0Help",
411           ao_config_help,               ao_config_help },
412         { 0, 0, 0 }
413 };
414
415 void
416 ao_config_set(void)
417 {
418         char    c;
419         uint8_t cmd;
420         void (*__xdata func)(void) __reentrant;
421
422         ao_cmd_white();
423         c = ao_cmd_lex_c;
424         ao_cmd_lex();
425         func = 0;
426         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
427                 if (ao_config_vars[cmd].str[0] == c) {
428                         func = ao_config_vars[cmd].set;
429                         break;
430                 }
431         if (func)
432                 (*func)();
433         else
434                 ao_cmd_status = ao_cmd_syntax_error;
435 }
436
437 static void
438 ao_config_help(void) __reentrant
439 {
440         uint8_t cmd;
441         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
442                 printf("%-20s %s\n",
443                        ao_config_vars[cmd].str,
444                        ao_config_vars[cmd].str+1+strlen(ao_config_vars[cmd].str));
445 }
446
447 static void
448 ao_config_show(void) __reentrant
449 {
450         uint8_t cmd;
451         printf("Config version: %d.%d\n",
452                ao_config.major, ao_config.minor);
453         for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
454                 if (ao_config_vars[cmd].show != ao_config_vars[cmd].set)
455                         (*ao_config_vars[cmd].show)();
456 }
457
458 #if HAS_EEPROM
459 static void
460 ao_config_write(void) __reentrant
461 {
462         uint8_t saved = 0;
463         ao_mutex_get(&ao_config_mutex);
464         if (ao_config_dirty) {
465                 _ao_config_put();
466                 ao_config_dirty = 0;
467                 saved = 1;
468         }
469         ao_mutex_put(&ao_config_mutex);
470         if (saved)
471                 puts("Saved");
472         else
473                 puts("Nothing to save");
474 }
475 #endif
476
477 __code struct ao_cmds ao_config_cmds[] = {
478         { ao_config_set,        "c <var> <value>\0Set config variable (? for help, s to show)" },
479         { 0, NULL },
480 };
481
482 void
483 ao_config_init(void)
484 {
485         ao_cmd_register(&ao_config_cmds[0]);
486 }