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 #if USE_INTERNAL_EEPROM
31 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX ao_storage_config
33 #define AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX ((uint32_t) 192 * (uint32_t) 1024)
41 ao_storage_erase(ao_storage_config);
42 ao_storage_write(ao_storage_config, &ao_config, sizeof (ao_config));
43 ao_log_write_erase(0);
50 ao_mutex_get(&ao_config_mutex);
52 ao_mutex_put(&ao_config_mutex);
63 ao_storage_read(ao_storage_config, &ao_config, sizeof (ao_config));
65 if (ao_config.major != AO_CONFIG_MAJOR) {
66 ao_config.major = AO_CONFIG_MAJOR;
67 ao_config.minor = AO_CONFIG_MINOR;
68 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
69 ao_config.radio_channel = AO_CONFIG_DEFAULT_RADIO_CHANNEL;
70 ao_config.accel_plus_g = 0;
71 ao_config.accel_minus_g = 0;
72 memset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
73 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
74 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
75 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
76 ao_config.radio_cal = ao_radio_cal;
77 ao_config.flight_log_max = AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX;
80 if (ao_config.minor < AO_CONFIG_MINOR) {
81 /* Fixups for minor version 1 */
82 if (ao_config.minor < 1)
83 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
84 /* Fixups for minor version 2 */
85 if (ao_config.minor < 2) {
86 ao_config.accel_plus_g = 0;
87 ao_config.accel_minus_g = 0;
89 /* Fixups for minor version 3 */
90 if (ao_config.minor < 3)
91 ao_config.radio_cal = ao_radio_cal;
92 /* Fixups for minor version 4 */
93 if (ao_config.minor < 4)
94 ao_config.flight_log_max = AO_CONFIG_DEFAULT_FLIGHT_LOG_MAX;
95 ao_config.minor = AO_CONFIG_MINOR;
104 ao_mutex_get(&ao_config_mutex);
106 ao_mutex_put(&ao_config_mutex);
110 ao_config_callsign_show(void)
112 printf ("Callsign: \"%s\"\n", ao_config.callsign);
116 ao_config_callsign_set(void) __reentrant
119 static __xdata char callsign[AO_MAX_CALLSIGN + 1];
121 memset(callsign, '\0', sizeof callsign);
124 while (ao_cmd_lex_c != '\n') {
125 if (c < AO_MAX_CALLSIGN)
126 callsign[c++] = ao_cmd_lex_c;
128 ao_cmd_status = ao_cmd_lex_error;
131 if (ao_cmd_status != ao_cmd_success)
133 ao_mutex_get(&ao_config_mutex);
135 memcpy(&ao_config.callsign, &callsign,
136 AO_MAX_CALLSIGN + 1);
138 ao_mutex_put(&ao_config_mutex);
139 ao_config_callsign_show();
143 ao_config_radio_channel_show(void) __reentrant
145 uint32_t freq = 434550L + ao_config.radio_channel * 100L;
146 uint16_t mhz = freq / 1000L;
147 uint16_t khz = freq % 1000L;
149 printf("Radio channel: %d (%d.%03dMHz)\n",
150 ao_config.radio_channel, mhz, khz);
154 ao_config_radio_channel_set(void) __reentrant
157 if (ao_cmd_status != ao_cmd_success)
159 ao_mutex_get(&ao_config_mutex);
161 ao_config.radio_channel = ao_cmd_lex_i;
163 ao_mutex_put(&ao_config_mutex);
164 ao_config_radio_channel_show();
165 ao_radio_recv_abort();
171 ao_config_main_deploy_show(void) __reentrant
173 printf("Main deploy: %d meters (%d feet)\n",
174 ao_config.main_deploy,
175 (int16_t) ((int32_t) ao_config.main_deploy * 328 / 100));
179 ao_config_main_deploy_set(void) __reentrant
182 if (ao_cmd_status != ao_cmd_success)
184 ao_mutex_get(&ao_config_mutex);
186 ao_config.main_deploy = ao_cmd_lex_i;
188 ao_mutex_put(&ao_config_mutex);
189 ao_config_main_deploy_show();
194 ao_config_accel_calibrate_show(void) __reentrant
196 printf("Accel cal +1g: %d -1g: %d\n",
197 ao_config.accel_plus_g, ao_config.accel_minus_g);
200 #define ACCEL_CALIBRATE_SAMPLES 1024
201 #define ACCEL_CALIBRATE_SHIFT 10
204 ao_config_accel_calibrate_auto(char *orientation) __reentrant
208 uint8_t cal_adc_ring;
210 printf("Orient antenna %s and press a key...", orientation);
213 puts("\r\n"); flush();
214 puts("Calibrating..."); flush();
215 i = ACCEL_CALIBRATE_SAMPLES;
217 cal_adc_ring = ao_sample_adc;
219 ao_sleep(DATA_TO_XDATA(&ao_sample_adc));
220 while (i && cal_adc_ring != ao_sample_adc) {
221 accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
222 cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
226 return accel_total >> ACCEL_CALIBRATE_SHIFT;
230 ao_config_accel_calibrate_set(void) __reentrant
234 if (ao_cmd_status != ao_cmd_success)
236 if (ao_cmd_lex_i == 0) {
237 up = ao_config_accel_calibrate_auto("up");
238 down = ao_config_accel_calibrate_auto("down");
242 if (ao_cmd_status != ao_cmd_success)
247 printf("Invalid accel: up (%d) down (%d)\n",
251 ao_mutex_get(&ao_config_mutex);
253 ao_config.accel_plus_g = up;
254 ao_config.accel_minus_g = down;
256 ao_mutex_put(&ao_config_mutex);
257 ao_config_accel_calibrate_show();
259 #endif /* HAS_ACCEL */
262 ao_config_apogee_delay_show(void) __reentrant
264 printf("Apogee delay: %d seconds\n",
265 ao_config.apogee_delay);
269 ao_config_apogee_delay_set(void) __reentrant
272 if (ao_cmd_status != ao_cmd_success)
274 ao_mutex_get(&ao_config_mutex);
276 ao_config.apogee_delay = ao_cmd_lex_i;
278 ao_mutex_put(&ao_config_mutex);
279 ao_config_apogee_delay_show();
285 ao_config_radio_cal_show(void) __reentrant
287 printf("Radio cal: %ld\n", ao_config.radio_cal);
291 ao_config_radio_cal_set(void) __reentrant
294 if (ao_cmd_status != ao_cmd_success)
296 ao_mutex_get(&ao_config_mutex);
298 ao_config.radio_cal = ao_cmd_lex_u32;
300 ao_mutex_put(&ao_config_mutex);
301 ao_config_radio_cal_show();
306 ao_config_log_show(void) __reentrant
308 printf("Max flight log: %d kB\n", (int16_t) (ao_config.flight_log_max >> 10));
312 ao_config_log_set(void) __reentrant
314 uint16_t block = (uint16_t) (ao_storage_block >> 10);
315 uint16_t config = (uint16_t) (ao_storage_config >> 10);
318 if (ao_cmd_status != ao_cmd_success)
320 if (ao_log_present())
321 printf("Storage must be empty before changing log size\n");
322 else if (block > 1024 && (ao_cmd_lex_i & (block - 1)))
323 printf("Flight log size must be multiple of %d kB\n", block);
324 else if (ao_cmd_lex_i > config)
325 printf("Flight log max %d kB\n", config);
327 ao_mutex_get(&ao_config_mutex);
329 ao_config.flight_log_max = (uint32_t) ao_cmd_lex_i << 10;
331 ao_mutex_put(&ao_config_mutex);
332 ao_config_log_show();
335 #endif /* HAS_EEPROM */
337 struct ao_config_var {
339 void (*set)(void) __reentrant;
340 void (*show)(void) __reentrant;
344 ao_config_help(void) __reentrant;
347 ao_config_show(void) __reentrant;
350 ao_config_write(void) __reentrant;
352 __code struct ao_config_var ao_config_vars[] = {
354 { "m <meters>\0Main deploy (in meters)",
355 ao_config_main_deploy_set, ao_config_main_deploy_show, },
356 { "d <delay>\0Apogee delay (in seconds)",
357 ao_config_apogee_delay_set, ao_config_apogee_delay_show },
359 { "r <channel>\0Radio channel (freq = 434.550 + chan * .1)",
360 ao_config_radio_channel_set, ao_config_radio_channel_show },
361 { "c <call>\0Callsign (8 char max)",
362 ao_config_callsign_set, ao_config_callsign_show },
364 { "a <+g> <-g>\0Accel calib (0 for auto)",
365 ao_config_accel_calibrate_set,ao_config_accel_calibrate_show },
366 #endif /* HAS_ACCEL */
367 { "f <cal>\0Radio calib (cal = rf/(xtal/2^16))",
368 ao_config_radio_cal_set, ao_config_radio_cal_show },
370 { "l <size>\0Flight log size in kB",
371 ao_config_log_set, ao_config_log_show },
374 ao_config_show, ao_config_show },
376 { "w\0Write to eeprom",
377 ao_config_write, ao_config_write },
380 ao_config_help, ao_config_help },
389 void (*__xdata func)(void) __reentrant;
395 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
396 if (ao_config_vars[cmd].str[0] == c) {
397 func = ao_config_vars[cmd].set;
403 ao_cmd_status = ao_cmd_syntax_error;
407 ao_config_help(void) __reentrant
410 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
412 ao_config_vars[cmd].str,
413 ao_config_vars[cmd].str+1+strlen(ao_config_vars[cmd].str));
417 ao_config_show(void) __reentrant
420 printf("Config version: %d.%d\n",
421 ao_config.major, ao_config.minor);
422 for (cmd = 0; ao_config_vars[cmd].str != NULL; cmd++)
423 if (ao_config_vars[cmd].show != ao_config_vars[cmd].set)
424 (*ao_config_vars[cmd].show)();
429 ao_config_write(void) __reentrant
432 ao_mutex_get(&ao_config_mutex);
433 if (ao_config_dirty) {
438 ao_mutex_put(&ao_config_mutex);
442 puts("Nothing to save");
446 __code struct ao_cmds ao_config_cmds[] = {
447 { ao_config_set, "c <var> <value>\0Set config variable (? for help, s to show)" },
454 ao_cmd_register(&ao_config_cmds[0]);