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