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
36 ao_storage_erase(ao_storage_config);
37 ao_storage_write(ao_storage_config, &ao_config, sizeof (ao_config));
38 ao_log_write_erase(0);
45 ao_mutex_get(&ao_config_mutex);
47 ao_mutex_put(&ao_config_mutex);
58 ao_storage_read(ao_storage_config, &ao_config, sizeof (ao_config));
60 if (ao_config.major != AO_CONFIG_MAJOR) {
61 ao_config.major = AO_CONFIG_MAJOR;
62 ao_config.minor = AO_CONFIG_MINOR;
63 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
64 ao_config.radio_channel = AO_CONFIG_DEFAULT_RADIO_CHANNEL;
65 ao_config.accel_plus_g = 0;
66 ao_config.accel_minus_g = 0;
67 memset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
68 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
69 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
70 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
71 ao_config.radio_cal = ao_radio_cal;
74 if (ao_config.minor < AO_CONFIG_MINOR) {
75 /* Fixups for minor version 1 */
76 if (ao_config.minor < 1)
77 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
78 /* Fixups for minor version 2 */
79 if (ao_config.minor < 2) {
80 ao_config.accel_plus_g = 0;
81 ao_config.accel_minus_g = 0;
83 /* Fixups for minor version 3 */
84 if (ao_config.minor < 3)
85 ao_config.radio_cal = ao_radio_cal;
86 ao_config.minor = AO_CONFIG_MINOR;
95 ao_mutex_get(&ao_config_mutex);
97 ao_mutex_put(&ao_config_mutex);
101 ao_config_callsign_show(void)
103 printf ("Callsign: \"%s\"\n", ao_config.callsign);
107 ao_config_callsign_set(void) __reentrant
110 char callsign[AO_MAX_CALLSIGN + 1];
114 while (ao_cmd_lex_c != '\n') {
115 if (c < AO_MAX_CALLSIGN)
116 callsign[c++] = ao_cmd_lex_c;
118 ao_cmd_status = ao_cmd_lex_error;
121 if (ao_cmd_status != ao_cmd_success)
123 ao_mutex_get(&ao_config_mutex);
125 while (c < AO_MAX_CALLSIGN + 1)
126 callsign[c++] = '\0';
127 memcpy(&ao_config.callsign, &callsign,
128 AO_MAX_CALLSIGN + 1);
130 ao_mutex_put(&ao_config_mutex);
131 ao_config_callsign_show();
135 ao_config_radio_channel_show(void) __reentrant
137 uint32_t freq = 434550L + ao_config.radio_channel * 100L;
138 uint16_t mhz = freq / 1000L;
139 uint16_t khz = freq % 1000L;
141 printf("Radio channel: %d (%d.%03dMHz)\n",
142 ao_config.radio_channel, mhz, khz);
146 ao_config_radio_channel_set(void) __reentrant
149 if (ao_cmd_status != ao_cmd_success)
151 ao_mutex_get(&ao_config_mutex);
153 ao_config.radio_channel = ao_cmd_lex_i;
155 ao_mutex_put(&ao_config_mutex);
156 ao_config_radio_channel_show();
157 ao_radio_recv_abort();
163 ao_config_main_deploy_show(void) __reentrant
165 printf("Main deploy: %d meters (%d feet)\n",
166 ao_config.main_deploy,
167 (int16_t) ((int32_t) ao_config.main_deploy * 328 / 100));
171 ao_config_main_deploy_set(void) __reentrant
174 if (ao_cmd_status != ao_cmd_success)
176 ao_mutex_get(&ao_config_mutex);
178 ao_config.main_deploy = ao_cmd_lex_i;
180 ao_mutex_put(&ao_config_mutex);
181 ao_config_main_deploy_show();
185 ao_config_accel_calibrate_show(void) __reentrant
187 printf("Accel cal +1g: %d -1g: %d\n",
188 ao_config.accel_plus_g, ao_config.accel_minus_g);
191 #define ACCEL_CALIBRATE_SAMPLES 1024
192 #define ACCEL_CALIBRATE_SHIFT 10
195 ao_config_accel_calibrate_auto(char *orientation) __reentrant
199 uint8_t cal_adc_ring;
201 printf("Orient %s and press a key...", orientation);
204 puts("\r\n"); flush();
205 puts("Calibrating..."); flush();
206 i = ACCEL_CALIBRATE_SAMPLES;
208 cal_adc_ring = ao_adc_head;
210 ao_sleep(&ao_adc_ring);
211 while (i && cal_adc_ring != ao_adc_head) {
212 accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
213 cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
217 return accel_total >> ACCEL_CALIBRATE_SHIFT;
221 ao_config_accel_calibrate_set(void) __reentrant
225 if (ao_cmd_status != ao_cmd_success)
227 if (ao_cmd_lex_i == 0) {
228 up = ao_config_accel_calibrate_auto("antenna up");
229 down = ao_config_accel_calibrate_auto("antenna down");
233 if (ao_cmd_status != ao_cmd_success)
238 printf("Invalid accel calibration: antenna up (%d) should be less than antenna down (%d)\n",
242 ao_mutex_get(&ao_config_mutex);
244 ao_config.accel_plus_g = up;
245 ao_config.accel_minus_g = down;
247 ao_mutex_put(&ao_config_mutex);
248 ao_config_accel_calibrate_show();
252 ao_config_apogee_delay_show(void) __reentrant
254 printf("Apogee delay: %d seconds\n",
255 ao_config.apogee_delay);
259 ao_config_apogee_delay_set(void) __reentrant
262 if (ao_cmd_status != ao_cmd_success)
264 ao_mutex_get(&ao_config_mutex);
266 ao_config.apogee_delay = ao_cmd_lex_i;
268 ao_mutex_put(&ao_config_mutex);
269 ao_config_apogee_delay_show();
275 ao_config_radio_cal_show(void) __reentrant
277 printf("Radio cal: %ld\n", ao_config.radio_cal);
281 ao_config_radio_cal_set(void) __reentrant
284 if (ao_cmd_status != ao_cmd_success)
286 ao_mutex_get(&ao_config_mutex);
288 ao_config.radio_cal = ao_cmd_lex_u32;
290 ao_mutex_put(&ao_config_mutex);
291 ao_config_radio_cal_show();
294 struct ao_config_var {
296 void (*set)(void) __reentrant;
297 void (*show)(void) __reentrant;
302 ao_config_help(void) __reentrant;
305 ao_config_show(void) __reentrant;
308 ao_config_write(void) __reentrant;
310 __code struct ao_config_var ao_config_vars[] = {
312 { 'm', ao_config_main_deploy_set, ao_config_main_deploy_show,
313 "m <meters> Set height above launch for main deploy (in meters)" },
314 { 'd', ao_config_apogee_delay_set, ao_config_apogee_delay_show,
315 "d <delay> Set apogee igniter delay (in seconds)" },
317 { 'r', ao_config_radio_channel_set, ao_config_radio_channel_show,
318 "r <channel> Set radio channel (freq = 434.550 + channel * .1)" },
319 { 'c', ao_config_callsign_set, ao_config_callsign_show,
320 "c <call> Set callsign broadcast in each packet (8 char max)" },
322 { 'a', ao_config_accel_calibrate_set, ao_config_accel_calibrate_show,
323 "a <+g> <-g> Set accelerometer calibration (0 for auto)" },
325 { 'f', ao_config_radio_cal_set, ao_config_radio_cal_show,
326 "f <cal> Set radio calibration value (cal = rf/(xtal/2^16))" },
327 { 's', ao_config_show, ao_config_show,
328 "s Show current config values" },
330 { 'w', ao_config_write, ao_config_write,
331 "w Write current values to eeprom" },
333 { '?', ao_config_help, ao_config_help,
334 "? Show available config variables" },
335 { 0, ao_config_help, ao_config_help,
344 void (*__xdata func)(void) __reentrant;
350 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
351 if (ao_config_vars[cmd].cmd == c) {
352 func = ao_config_vars[cmd].set;
358 ao_cmd_status = ao_cmd_syntax_error;
362 ao_config_help(void) __reentrant
365 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
366 puts (ao_config_vars[cmd].help);
370 ao_config_show(void) __reentrant
373 printf("Config version: %d.%d\n",
374 ao_config.major, ao_config.minor);
375 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
376 if (ao_config_vars[cmd].show != ao_config_vars[cmd].set)
377 (*ao_config_vars[cmd].show)();
382 ao_config_write(void) __reentrant
385 ao_mutex_get(&ao_config_mutex);
386 if (ao_config_dirty) {
391 ao_mutex_put(&ao_config_mutex);
395 puts("Nothing to save");
399 __code struct ao_cmds ao_config_cmds[] = {
400 { 'c', ao_config_set, "c <var> <value> Set config variable (? for help, s to show)" },
401 { '\0', ao_config_set, NULL },
407 ao_cmd_register(&ao_config_cmds[0]);