2 * Copyright © 2009 Keith Packard <keithp@keithp.com>
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.
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.
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.
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;
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_FLIGHT_LOG_MAX ((uint32_t) 192 * (uint32_t) 1024)
37 ao_storage_erase(ao_storage_config);
38 ao_storage_write(ao_storage_config, &ao_config, sizeof (ao_config));
39 ao_log_write_erase(0);
46 ao_mutex_get(&ao_config_mutex);
48 ao_mutex_put(&ao_config_mutex);
59 ao_storage_read(ao_storage_config, &ao_config, sizeof (ao_config));
61 if (ao_config.major != AO_CONFIG_MAJOR) {
62 ao_config.major = AO_CONFIG_MAJOR;
63 ao_config.minor = AO_CONFIG_MINOR;
64 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
65 ao_config.radio_channel = AO_CONFIG_DEFAULT_RADIO_CHANNEL;
66 ao_config.accel_plus_g = 0;
67 ao_config.accel_minus_g = 0;
68 memset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
69 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
70 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
71 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
72 ao_config.radio_cal = ao_radio_cal;
75 if (ao_config.minor < AO_CONFIG_MINOR) {
76 /* Fixups for minor version 1 */
77 if (ao_config.minor < 1)
78 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
79 /* Fixups for minor version 2 */
80 if (ao_config.minor < 2) {
81 ao_config.accel_plus_g = 0;
82 ao_config.accel_minus_g = 0;
84 /* Fixups for minor version 3 */
85 if (ao_config.minor < 3)
86 ao_config.radio_cal = ao_radio_cal;
87 /* Fixups for minor version 4 */
88 if (ao_config.minor < 4)
89 ao_config.flight_log_max = AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX;
90 ao_config.minor = AO_CONFIG_MINOR;
99 ao_mutex_get(&ao_config_mutex);
101 ao_mutex_put(&ao_config_mutex);
105 ao_config_callsign_show(void)
107 printf ("Callsign: \"%s\"\n", ao_config.callsign);
111 ao_config_callsign_set(void) __reentrant
114 char callsign[AO_MAX_CALLSIGN + 1];
118 while (ao_cmd_lex_c != '\n') {
119 if (c < AO_MAX_CALLSIGN)
120 callsign[c++] = ao_cmd_lex_c;
122 ao_cmd_status = ao_cmd_lex_error;
125 if (ao_cmd_status != ao_cmd_success)
127 ao_mutex_get(&ao_config_mutex);
129 while (c < AO_MAX_CALLSIGN + 1)
130 callsign[c++] = '\0';
131 memcpy(&ao_config.callsign, &callsign,
132 AO_MAX_CALLSIGN + 1);
134 ao_mutex_put(&ao_config_mutex);
135 ao_config_callsign_show();
139 ao_config_radio_channel_show(void) __reentrant
141 uint32_t freq = 434550L + ao_config.radio_channel * 100L;
142 uint16_t mhz = freq / 1000L;
143 uint16_t khz = freq % 1000L;
145 printf("Radio channel: %d (%d.%03dMHz)\n",
146 ao_config.radio_channel, mhz, khz);
150 ao_config_radio_channel_set(void) __reentrant
153 if (ao_cmd_status != ao_cmd_success)
155 ao_mutex_get(&ao_config_mutex);
157 ao_config.radio_channel = ao_cmd_lex_i;
159 ao_mutex_put(&ao_config_mutex);
160 ao_config_radio_channel_show();
161 ao_radio_recv_abort();
167 ao_config_main_deploy_show(void) __reentrant
169 printf("Main deploy: %d meters (%d feet)\n",
170 ao_config.main_deploy,
171 (int16_t) ((int32_t) ao_config.main_deploy * 328 / 100));
175 ao_config_main_deploy_set(void) __reentrant
178 if (ao_cmd_status != ao_cmd_success)
180 ao_mutex_get(&ao_config_mutex);
182 ao_config.main_deploy = ao_cmd_lex_i;
184 ao_mutex_put(&ao_config_mutex);
185 ao_config_main_deploy_show();
189 ao_config_accel_calibrate_show(void) __reentrant
191 printf("Accel cal +1g: %d -1g: %d\n",
192 ao_config.accel_plus_g, ao_config.accel_minus_g);
195 #define ACCEL_CALIBRATE_SAMPLES 1024
196 #define ACCEL_CALIBRATE_SHIFT 10
199 ao_config_accel_calibrate_auto(char *orientation) __reentrant
203 uint8_t cal_adc_ring;
205 printf("Orient %s and press a key...", orientation);
208 puts("\r\n"); flush();
209 puts("Calibrating..."); flush();
210 i = ACCEL_CALIBRATE_SAMPLES;
212 cal_adc_ring = ao_adc_head;
214 ao_sleep(&ao_adc_ring);
215 while (i && cal_adc_ring != ao_adc_head) {
216 accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
217 cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
221 return accel_total >> ACCEL_CALIBRATE_SHIFT;
225 ao_config_accel_calibrate_set(void) __reentrant
229 if (ao_cmd_status != ao_cmd_success)
231 if (ao_cmd_lex_i == 0) {
232 up = ao_config_accel_calibrate_auto("antenna up");
233 down = ao_config_accel_calibrate_auto("antenna down");
237 if (ao_cmd_status != ao_cmd_success)
242 printf("Invalid accel calibration: antenna up (%d) should be less than antenna down (%d)\n",
246 ao_mutex_get(&ao_config_mutex);
248 ao_config.accel_plus_g = up;
249 ao_config.accel_minus_g = down;
251 ao_mutex_put(&ao_config_mutex);
252 ao_config_accel_calibrate_show();
256 ao_config_apogee_delay_show(void) __reentrant
258 printf("Apogee delay: %d seconds\n",
259 ao_config.apogee_delay);
263 ao_config_apogee_delay_set(void) __reentrant
266 if (ao_cmd_status != ao_cmd_success)
268 ao_mutex_get(&ao_config_mutex);
270 ao_config.apogee_delay = ao_cmd_lex_i;
272 ao_mutex_put(&ao_config_mutex);
273 ao_config_apogee_delay_show();
279 ao_config_radio_cal_show(void) __reentrant
281 printf("Radio cal: %ld\n", ao_config.radio_cal);
285 ao_config_radio_cal_set(void) __reentrant
288 if (ao_cmd_status != ao_cmd_success)
290 ao_mutex_get(&ao_config_mutex);
292 ao_config.radio_cal = ao_cmd_lex_u32;
294 ao_mutex_put(&ao_config_mutex);
295 ao_config_radio_cal_show();
300 ao_config_log_show(void) __reentrant
302 printf("Max flight log: %d kB\n", (int16_t) (ao_config.flight_log_max >> 10));
306 ao_config_log_set(void) __reentrant
309 if (ao_cmd_status != ao_cmd_success)
311 if (ao_log_present())
312 printf("Storage must be empty before changing log size\n");
313 else if (ao_storage_block > 1024 && (ao_cmd_lex_u32 & ((ao_storage_block >> 10) - 1)))
314 printf("Flight log size must be multiple of %ld\n", ao_storage_block >> 10);
316 ao_mutex_get(&ao_config_mutex);
318 ao_config.flight_log_max = ao_cmd_lex_u32 << 10;
320 ao_mutex_put(&ao_config_mutex);
321 ao_config_log_show();
324 #endif /* HAS_EEPROM */
326 struct ao_config_var {
328 void (*set)(void) __reentrant;
329 void (*show)(void) __reentrant;
334 ao_config_help(void) __reentrant;
337 ao_config_show(void) __reentrant;
340 ao_config_write(void) __reentrant;
342 __code struct ao_config_var ao_config_vars[] = {
344 { 'm', ao_config_main_deploy_set, ao_config_main_deploy_show,
345 "m <meters> Set height above launch for main deploy (in meters)" },
346 { 'd', ao_config_apogee_delay_set, ao_config_apogee_delay_show,
347 "d <delay> Set apogee igniter delay (in seconds)" },
349 { 'r', ao_config_radio_channel_set, ao_config_radio_channel_show,
350 "r <channel> Set radio channel (freq = 434.550 + channel * .1)" },
351 { 'c', ao_config_callsign_set, ao_config_callsign_show,
352 "c <call> Set callsign broadcast in each packet (8 char max)" },
354 { 'a', ao_config_accel_calibrate_set, ao_config_accel_calibrate_show,
355 "a <+g> <-g> Set accelerometer calibration (0 for auto)" },
357 { 'f', ao_config_radio_cal_set, ao_config_radio_cal_show,
358 "f <cal> Set radio calibration value (cal = rf/(xtal/2^16))" },
360 { 'l', ao_config_log_set, ao_config_log_show,
361 "l <size> Set flight log size in kB" },
363 { 's', ao_config_show, ao_config_show,
364 "s Show current config values" },
366 { 'w', ao_config_write, ao_config_write,
367 "w Write current values to eeprom" },
369 { '?', ao_config_help, ao_config_help,
370 "? Show available config variables" },
371 { 0, ao_config_help, ao_config_help,
380 void (*__xdata func)(void) __reentrant;
386 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
387 if (ao_config_vars[cmd].cmd == c) {
388 func = ao_config_vars[cmd].set;
394 ao_cmd_status = ao_cmd_syntax_error;
398 ao_config_help(void) __reentrant
401 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
402 puts (ao_config_vars[cmd].help);
406 ao_config_show(void) __reentrant
409 printf("Config version: %d.%d\n",
410 ao_config.major, ao_config.minor);
411 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
412 if (ao_config_vars[cmd].show != ao_config_vars[cmd].set)
413 (*ao_config_vars[cmd].show)();
418 ao_config_write(void) __reentrant
421 ao_mutex_get(&ao_config_mutex);
422 if (ao_config_dirty) {
427 ao_mutex_put(&ao_config_mutex);
431 puts("Nothing to save");
435 __code struct ao_cmds ao_config_cmds[] = {
436 { 'c', ao_config_set, "c <var> <value> Set config variable (? for help, s to show)" },
437 { '\0', ao_config_set, NULL },
443 ao_cmd_register(&ao_config_cmds[0]);