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_write(ao_storage_config, &ao_config, sizeof (ao_config));
48 ao_storage_read(ao_storage_config, &ao_config, sizeof (ao_config));
50 if (ao_config.major != AO_CONFIG_MAJOR) {
51 ao_config.major = AO_CONFIG_MAJOR;
52 ao_config.minor = AO_CONFIG_MINOR;
53 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
54 ao_config.radio_channel = AO_CONFIG_DEFAULT_RADIO_CHANNEL;
55 ao_config.accel_plus_g = 0;
56 ao_config.accel_minus_g = 0;
57 memset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
58 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
59 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
60 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
61 ao_config.radio_cal = ao_radio_cal;
64 if (ao_config.minor < AO_CONFIG_MINOR) {
65 /* Fixups for minor version 1 */
66 if (ao_config.minor < 1)
67 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
68 /* Fixups for minor version 2 */
69 if (ao_config.minor < 2) {
70 ao_config.accel_plus_g = 0;
71 ao_config.accel_minus_g = 0;
73 /* Fixups for minor version 3 */
74 if (ao_config.minor < 3)
75 ao_config.radio_cal = ao_radio_cal;
76 ao_config.minor = AO_CONFIG_MINOR;
85 ao_mutex_get(&ao_config_mutex);
87 ao_mutex_put(&ao_config_mutex);
91 ao_config_callsign_show(void)
93 printf ("Callsign: \"%s\"\n", ao_config.callsign);
97 ao_config_callsign_set(void) __reentrant
100 char callsign[AO_MAX_CALLSIGN + 1];
104 while (ao_cmd_lex_c != '\n') {
105 if (c < AO_MAX_CALLSIGN)
106 callsign[c++] = ao_cmd_lex_c;
108 ao_cmd_status = ao_cmd_lex_error;
111 if (ao_cmd_status != ao_cmd_success)
113 ao_mutex_get(&ao_config_mutex);
115 while (c < AO_MAX_CALLSIGN + 1)
116 callsign[c++] = '\0';
117 memcpy(&ao_config.callsign, &callsign,
118 AO_MAX_CALLSIGN + 1);
120 ao_mutex_put(&ao_config_mutex);
121 ao_config_callsign_show();
125 ao_config_radio_channel_show(void) __reentrant
127 uint32_t freq = 434550L + ao_config.radio_channel * 100L;
128 uint16_t mhz = freq / 1000L;
129 uint16_t khz = freq % 1000L;
131 printf("Radio channel: %d (%d.%03dMHz)\n",
132 ao_config.radio_channel, mhz, khz);
136 ao_config_radio_channel_set(void) __reentrant
139 if (ao_cmd_status != ao_cmd_success)
141 ao_mutex_get(&ao_config_mutex);
143 ao_config.radio_channel = ao_cmd_lex_i;
145 ao_mutex_put(&ao_config_mutex);
146 ao_config_radio_channel_show();
147 ao_radio_recv_abort();
153 ao_config_main_deploy_show(void) __reentrant
155 printf("Main deploy: %d meters (%d feet)\n",
156 ao_config.main_deploy,
157 (int16_t) ((int32_t) ao_config.main_deploy * 328 / 100));
161 ao_config_main_deploy_set(void) __reentrant
164 if (ao_cmd_status != ao_cmd_success)
166 ao_mutex_get(&ao_config_mutex);
168 ao_config.main_deploy = ao_cmd_lex_i;
170 ao_mutex_put(&ao_config_mutex);
171 ao_config_main_deploy_show();
175 ao_config_accel_calibrate_show(void) __reentrant
177 printf("Accel cal +1g: %d -1g: %d\n",
178 ao_config.accel_plus_g, ao_config.accel_minus_g);
181 #define ACCEL_CALIBRATE_SAMPLES 1024
182 #define ACCEL_CALIBRATE_SHIFT 10
185 ao_config_accel_calibrate_auto(char *orientation) __reentrant
189 uint8_t cal_adc_ring;
191 printf("Orient %s and press a key...", orientation);
194 puts("\r\n"); flush();
195 puts("Calibrating..."); flush();
196 i = ACCEL_CALIBRATE_SAMPLES;
198 cal_adc_ring = ao_adc_head;
200 ao_sleep(&ao_adc_ring);
201 while (i && cal_adc_ring != ao_adc_head) {
202 accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
203 cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
207 return accel_total >> ACCEL_CALIBRATE_SHIFT;
211 ao_config_accel_calibrate_set(void) __reentrant
215 if (ao_cmd_status != ao_cmd_success)
217 if (ao_cmd_lex_i == 0) {
218 up = ao_config_accel_calibrate_auto("antenna up");
219 down = ao_config_accel_calibrate_auto("antenna down");
223 if (ao_cmd_status != ao_cmd_success)
228 printf("Invalid accel calibration: antenna up (%d) should be less than antenna down (%d)\n",
232 ao_mutex_get(&ao_config_mutex);
234 ao_config.accel_plus_g = up;
235 ao_config.accel_minus_g = down;
237 ao_mutex_put(&ao_config_mutex);
238 ao_config_accel_calibrate_show();
242 ao_config_apogee_delay_show(void) __reentrant
244 printf("Apogee delay: %d seconds\n",
245 ao_config.apogee_delay);
249 ao_config_apogee_delay_set(void) __reentrant
252 if (ao_cmd_status != ao_cmd_success)
254 ao_mutex_get(&ao_config_mutex);
256 ao_config.apogee_delay = ao_cmd_lex_i;
258 ao_mutex_put(&ao_config_mutex);
259 ao_config_apogee_delay_show();
265 ao_config_radio_cal_show(void) __reentrant
267 printf("Radio cal: %ld\n", ao_config.radio_cal);
271 ao_config_radio_cal_set(void) __reentrant
274 if (ao_cmd_status != ao_cmd_success)
276 ao_mutex_get(&ao_config_mutex);
278 ao_config.radio_cal = ao_cmd_lex_u32;
280 ao_mutex_put(&ao_config_mutex);
281 ao_config_radio_cal_show();
284 struct ao_config_var {
286 void (*set)(void) __reentrant;
287 void (*show)(void) __reentrant;
292 ao_config_help(void) __reentrant;
295 ao_config_show(void) __reentrant;
298 ao_config_write(void) __reentrant;
300 __code struct ao_config_var ao_config_vars[] = {
302 { 'm', ao_config_main_deploy_set, ao_config_main_deploy_show,
303 "m <meters> Set height above launch for main deploy (in meters)" },
304 { 'd', ao_config_apogee_delay_set, ao_config_apogee_delay_show,
305 "d <delay> Set apogee igniter delay (in seconds)" },
307 { 'r', ao_config_radio_channel_set, ao_config_radio_channel_show,
308 "r <channel> Set radio channel (freq = 434.550 + channel * .1)" },
309 { 'c', ao_config_callsign_set, ao_config_callsign_show,
310 "c <call> Set callsign broadcast in each packet (8 char max)" },
312 { 'a', ao_config_accel_calibrate_set, ao_config_accel_calibrate_show,
313 "a <+g> <-g> Set accelerometer calibration (0 for auto)" },
315 { 'f', ao_config_radio_cal_set, ao_config_radio_cal_show,
316 "f <cal> Set radio calibration value (cal = rf/(xtal/2^16))" },
317 { 's', ao_config_show, ao_config_show,
318 "s Show current config values" },
320 { 'w', ao_config_write, ao_config_write,
321 "w Write current values to eeprom" },
323 { '?', ao_config_help, ao_config_help,
324 "? Show available config variables" },
325 { 0, ao_config_help, ao_config_help,
334 void (*__xdata func)(void) __reentrant;
340 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
341 if (ao_config_vars[cmd].cmd == c) {
342 func = ao_config_vars[cmd].set;
348 ao_cmd_status = ao_cmd_syntax_error;
352 ao_config_help(void) __reentrant
355 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
356 puts (ao_config_vars[cmd].help);
360 ao_config_show(void) __reentrant
363 printf("Config version: %d.%d\n",
364 ao_config.major, ao_config.minor);
365 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
366 if (ao_config_vars[cmd].show != ao_config_vars[cmd].set)
367 (*ao_config_vars[cmd].show)();
372 ao_config_write(void) __reentrant
375 ao_mutex_get(&ao_config_mutex);
376 if (ao_config_dirty) {
381 ao_mutex_put(&ao_config_mutex);
385 puts("Nothing to save");
389 __code struct ao_cmds ao_config_cmds[] = {
390 { 'c', ao_config_set, "c <var> <value> Set config variable (? for help, s to show)" },
391 { '\0', ao_config_set, NULL },
397 ao_cmd_register(&ao_config_cmds[0]);