altos, altosui: Add igniter mode (dual, apogee, main)
[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 __xdata uint8_t ao_config_loaded;
22 __xdata 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         char callsign[AO_MAX_CALLSIGN + 1];
125
126         ao_cmd_white();
127         c = 0;
128         while (ao_cmd_lex_c != '\n') {
129                 if (c < AO_MAX_CALLSIGN)
130                         callsign[c++] = ao_cmd_lex_c;
131                 else
132                         ao_cmd_status = ao_cmd_lex_error;
133                 ao_cmd_lex();
134         }
135         if (ao_cmd_status != ao_cmd_success)
136                 return;
137         ao_mutex_get(&ao_config_mutex);
138         _ao_config_get();
139         while (c < AO_MAX_CALLSIGN + 1)
140                 callsign[c++] = '\0';
141         memcpy(&ao_config.callsign, &callsign,
142                AO_MAX_CALLSIGN + 1);
143         ao_config_dirty = 1;
144         ao_mutex_put(&ao_config_mutex);
145         ao_config_callsign_show();
146 }
147
148 void
149 ao_config_radio_channel_show(void) __reentrant
150 {
151         uint32_t        freq = 434550L + ao_config.radio_channel * 100L;
152         uint16_t        mhz = freq / 1000L;
153         uint16_t        khz = freq % 1000L;
154
155         printf("Radio channel: %d (%d.%03dMHz)\n",
156                ao_config.radio_channel, mhz, khz);
157 }
158
159 void
160 ao_config_radio_channel_set(void) __reentrant
161 {
162         ao_cmd_decimal();
163         if (ao_cmd_status != ao_cmd_success)
164                 return;
165         ao_mutex_get(&ao_config_mutex);
166         _ao_config_get();
167         ao_config.radio_channel = ao_cmd_lex_i;
168         ao_config_dirty = 1;
169         ao_mutex_put(&ao_config_mutex);
170         ao_config_radio_channel_show();
171         ao_radio_recv_abort();
172 }
173
174 #if HAS_ADC
175
176 void
177 ao_config_main_deploy_show(void) __reentrant
178 {
179         printf("Main deploy: %d meters (%d feet)\n",
180                ao_config.main_deploy,
181                (int16_t) ((int32_t) ao_config.main_deploy * 328 / 100));
182 }
183
184 void
185 ao_config_main_deploy_set(void) __reentrant
186 {
187         ao_cmd_decimal();
188         if (ao_cmd_status != ao_cmd_success)
189                 return;
190         ao_mutex_get(&ao_config_mutex);
191         _ao_config_get();
192         ao_config.main_deploy = ao_cmd_lex_i;
193         ao_config_dirty = 1;
194         ao_mutex_put(&ao_config_mutex);
195         ao_config_main_deploy_show();
196 }
197
198 #if HAS_ACCEL
199 void
200 ao_config_accel_calibrate_show(void) __reentrant
201 {
202         printf("Accel cal +1g: %d -1g: %d\n",
203                ao_config.accel_plus_g, ao_config.accel_minus_g);
204 }
205
206 #define ACCEL_CALIBRATE_SAMPLES 1024
207 #define ACCEL_CALIBRATE_SHIFT   10
208
209 static int16_t
210 ao_config_accel_calibrate_auto(char *orientation) __reentrant
211 {
212         uint16_t        i;
213         int32_t         accel_total;
214         uint8_t         cal_adc_ring;
215
216         printf("Orient %s and press a key...", orientation);
217         flush();
218         (void) getchar();
219         puts("\r\n"); flush();
220         puts("Calibrating..."); flush();
221         i = ACCEL_CALIBRATE_SAMPLES;
222         accel_total = 0;
223         cal_adc_ring = ao_sample_adc;
224         while (i) {
225                 ao_sleep(DATA_TO_XDATA(&ao_sample_adc));
226                 while (i && cal_adc_ring != ao_sample_adc) {
227                         accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
228                         cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
229                         i--;
230                 }
231         }
232         return accel_total >> ACCEL_CALIBRATE_SHIFT;
233 }
234
235 void
236 ao_config_accel_calibrate_set(void) __reentrant
237 {
238         int16_t up, down;
239         ao_cmd_decimal();
240         if (ao_cmd_status != ao_cmd_success)
241                 return;
242         if (ao_cmd_lex_i == 0) {
243                 up = ao_config_accel_calibrate_auto("antenna up");
244                 down = ao_config_accel_calibrate_auto("antenna down");
245         } else {
246                 up = ao_cmd_lex_i;
247                 ao_cmd_decimal();
248                 if (ao_cmd_status != ao_cmd_success)
249                         return;
250                 down = ao_cmd_lex_i;
251         }
252         if (up >= down) {
253                 printf("Invalid accel calibration: antenna up (%d) should be less than antenna down (%d)\n",
254                        up, down);
255                 return;
256         }
257         ao_mutex_get(&ao_config_mutex);
258         _ao_config_get();
259         ao_config.accel_plus_g = up;
260         ao_config.accel_minus_g = down;
261         ao_config_dirty = 1;
262         ao_mutex_put(&ao_config_mutex);
263         ao_config_accel_calibrate_show();
264 }
265 #endif /* HAS_ACCEL */
266
267 void
268 ao_config_apogee_delay_show(void) __reentrant
269 {
270         printf("Apogee delay: %d seconds\n",
271                ao_config.apogee_delay);
272 }
273
274 void
275 ao_config_apogee_delay_set(void) __reentrant
276 {
277         ao_cmd_decimal();
278         if (ao_cmd_status != ao_cmd_success)
279                 return;
280         ao_mutex_get(&ao_config_mutex);
281         _ao_config_get();
282         ao_config.apogee_delay = ao_cmd_lex_i;
283         ao_config_dirty = 1;
284         ao_mutex_put(&ao_config_mutex);
285         ao_config_apogee_delay_show();
286 }
287
288 #endif /* HAS_ADC */
289
290 void
291 ao_config_radio_cal_show(void) __reentrant
292 {
293         printf("Radio cal: %ld\n", ao_config.radio_cal);
294 }
295
296 void
297 ao_config_radio_cal_set(void) __reentrant
298 {
299         ao_cmd_decimal();
300         if (ao_cmd_status != ao_cmd_success)
301                 return;
302         ao_mutex_get(&ao_config_mutex);
303         _ao_config_get();
304         ao_config.radio_cal = ao_cmd_lex_u32;
305         ao_config_dirty = 1;
306         ao_mutex_put(&ao_config_mutex);
307         ao_config_radio_cal_show();
308 }
309
310 #if HAS_EEPROM
311 void
312 ao_config_log_show(void) __reentrant
313 {
314         printf("Max flight log: %d kB\n", (int16_t) (ao_config.flight_log_max >> 10));
315 }
316
317 void
318 ao_config_log_set(void) __reentrant
319 {
320         uint16_t        block = (uint16_t) (ao_storage_block >> 10);
321         uint16_t        config = (uint16_t) (ao_storage_config >> 10);
322
323         ao_cmd_decimal();
324         if (ao_cmd_status != ao_cmd_success)
325                 return;
326         if (ao_log_present())
327                 printf("Storage must be empty before changing log size\n");
328         else if (block > 1024 && (ao_cmd_lex_i & (block - 1)))
329                 printf("Flight log size must be multiple of %d kB\n", block);
330         else if (ao_cmd_lex_i > config)
331                 printf("Flight log max %d kB\n", config);
332         else {
333                 ao_mutex_get(&ao_config_mutex);
334                 _ao_config_get();
335                 ao_config.flight_log_max = (uint32_t) ao_cmd_lex_i << 10;
336                 ao_config_dirty = 1;
337                 ao_mutex_put(&ao_config_mutex);
338                 ao_config_log_show();
339         }
340 }
341 #endif /* HAS_EEPROM */
342
343 #if HAS_ADC
344 void
345 ao_config_ignite_mode_show(void) __reentrant
346 {
347         printf("Ignite mode: %d\n", ao_config.ignite_mode);
348 }
349
350 void
351 ao_config_ignite_mode_set(void) __reentrant
352 {
353         ao_cmd_decimal();
354         if (ao_cmd_status != ao_cmd_success)
355                 return;
356         ao_mutex_get(&ao_config_mutex);
357         _ao_config_get();
358         ao_config.ignite_mode = ao_cmd_lex_i;
359         ao_config_dirty = 1;
360         ao_mutex_put(&ao_config_mutex);
361         ao_config_log_show();
362 }
363 #endif
364
365 struct ao_config_var {
366         char            cmd;
367         void            (*set)(void) __reentrant;
368         void            (*show)(void) __reentrant;
369         const char      *help;
370 };
371
372 static void
373 ao_config_help(void) __reentrant;
374
375 static void
376 ao_config_show(void) __reentrant;
377
378 static void
379 ao_config_write(void) __reentrant;
380
381 __code struct ao_config_var ao_config_vars[] = {
382 #if HAS_ADC
383         { 'm',  ao_config_main_deploy_set,      ao_config_main_deploy_show,
384                 "m <meters>  Set height above launch for main deploy (in meters)" },
385         { 'd',  ao_config_apogee_delay_set,     ao_config_apogee_delay_show,
386                 "d <delay>   Set apogee igniter delay (in seconds)" },
387 #endif /* HAS_ADC */
388         { 'r',  ao_config_radio_channel_set,    ao_config_radio_channel_show,
389                 "r <channel> Set radio channel (freq = 434.550 + channel * .1)" },
390         { 'c',  ao_config_callsign_set,         ao_config_callsign_show,
391                 "c <call>    Set callsign broadcast in each packet (8 char max)" },
392 #if HAS_ACCEL
393         { 'a',  ao_config_accel_calibrate_set,  ao_config_accel_calibrate_show,
394                 "a <+g> <-g> Set accelerometer calibration (0 for auto)" },
395 #endif /* HAS_ACCEL */
396         { 'f',  ao_config_radio_cal_set,        ao_config_radio_cal_show,
397                 "f <cal>     Set radio calibration value (cal = rf/(xtal/2^16))" },
398 #if HAS_EEPROM
399         { 'l',  ao_config_log_set,              ao_config_log_show,
400                 "l <size>    Set flight log size in kB" },
401 #endif
402 #if HAS_ADC
403         { 'i',  ao_config_ignite_mode_set,      ao_config_ignite_mode_show,
404           "i <0 dual, 1 apogee, 2 main> Set igniter mode" },
405 #endif
406         { 's',  ao_config_show,                 ao_config_show,
407                 "s           Show current config values" },
408 #if HAS_EEPROM
409         { 'w',  ao_config_write,                ao_config_write,
410                 "w           Write current values to eeprom" },
411 #endif
412         { '?',  ao_config_help,                 ao_config_help,
413                 "?           Show available config variables" },
414         { 0,    ao_config_help, ao_config_help,
415                 NULL },
416 };
417
418 void
419 ao_config_set(void)
420 {
421         char    c;
422         uint8_t cmd;
423         void (*__xdata func)(void) __reentrant;
424
425         ao_cmd_white();
426         c = ao_cmd_lex_c;
427         ao_cmd_lex();
428         func = 0;
429         for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
430                 if (ao_config_vars[cmd].cmd == c) {
431                         func = ao_config_vars[cmd].set;
432                         break;
433                 }
434         if (func)
435                 (*func)();
436         else
437                 ao_cmd_status = ao_cmd_syntax_error;
438 }
439
440 static void
441 ao_config_help(void) __reentrant
442 {
443         uint8_t cmd;
444         for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
445                 puts (ao_config_vars[cmd].help);
446 }
447
448 static void
449 ao_config_show(void) __reentrant
450 {
451         uint8_t cmd;
452         printf("Config version: %d.%d\n",
453                ao_config.major, ao_config.minor);
454         for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
455                 if (ao_config_vars[cmd].show != ao_config_vars[cmd].set)
456                         (*ao_config_vars[cmd].show)();
457 }
458
459 #if HAS_EEPROM
460 static void
461 ao_config_write(void) __reentrant
462 {
463         uint8_t saved = 0;
464         ao_mutex_get(&ao_config_mutex);
465         if (ao_config_dirty) {
466                 _ao_config_put();
467                 ao_config_dirty = 0;
468                 saved = 1;
469         }
470         ao_mutex_put(&ao_config_mutex);
471         if (saved)
472                 puts("Saved");
473         else
474                 puts("Nothing to save");
475 }
476 #endif
477
478 __code struct ao_cmds ao_config_cmds[] = {
479         { ao_config_set,        "c <var> <value>\0Set config variable (? for help, s to show)" },
480         { 0, NULL },
481 };
482
483 void
484 ao_config_init(void)
485 {
486         ao_cmd_register(&ao_config_cmds[0]);
487 }