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
35 ao_ee_write_config((uint8_t *) &ao_config, sizeof (ao_config));
45 ao_ee_read_config((uint8_t *) &ao_config, sizeof (ao_config));
47 if (ao_config.major != AO_CONFIG_MAJOR) {
48 ao_config.major = AO_CONFIG_MAJOR;
49 ao_config.minor = AO_CONFIG_MINOR;
50 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
51 ao_config.radio_channel = AO_CONFIG_DEFAULT_RADIO_CHANNEL;
52 ao_config.accel_plus_g = 0;
53 ao_config.accel_minus_g = 0;
54 memset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
55 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
56 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
57 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
58 ao_config.radio_cal = ao_radio_cal;
61 if (ao_config.minor < AO_CONFIG_MINOR) {
62 /* Fixups for minor version 1 */
63 if (ao_config.minor < 1)
64 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
65 /* Fixups for minor version 2 */
66 if (ao_config.minor < 2) {
67 ao_config.accel_plus_g = 0;
68 ao_config.accel_minus_g = 0;
70 /* Fixups for minor version 3 */
71 if (ao_config.minor < 3)
72 ao_config.radio_cal = ao_radio_cal;
73 ao_config.minor = AO_CONFIG_MINOR;
82 ao_mutex_get(&ao_config_mutex);
84 ao_mutex_put(&ao_config_mutex);
88 ao_config_callsign_show(void)
90 printf ("Callsign: \"%s\"\n", ao_config.callsign);
94 ao_config_callsign_set(void) __reentrant
97 char callsign[AO_MAX_CALLSIGN + 1];
101 while (ao_cmd_lex_c != '\n') {
102 if (c < AO_MAX_CALLSIGN)
103 callsign[c++] = ao_cmd_lex_c;
105 ao_cmd_status = ao_cmd_lex_error;
108 if (ao_cmd_status != ao_cmd_success)
110 ao_mutex_get(&ao_config_mutex);
112 while (c < AO_MAX_CALLSIGN + 1)
113 callsign[c++] = '\0';
114 memcpy(&ao_config.callsign, &callsign,
115 AO_MAX_CALLSIGN + 1);
117 ao_mutex_put(&ao_config_mutex);
118 ao_config_callsign_show();
122 ao_config_radio_channel_show(void) __reentrant
124 uint32_t freq = 434550L + ao_config.radio_channel * 100L;
125 uint16_t mhz = freq / 1000L;
126 uint16_t khz = freq % 1000L;
128 printf("Radio channel: %d (%d.%03dMHz)\n",
129 ao_config.radio_channel, mhz, khz);
133 ao_config_radio_channel_set(void) __reentrant
136 if (ao_cmd_status != ao_cmd_success)
138 ao_mutex_get(&ao_config_mutex);
140 ao_config.radio_channel = ao_cmd_lex_i;
142 ao_mutex_put(&ao_config_mutex);
143 ao_config_radio_channel_show();
150 ao_config_main_deploy_show(void) __reentrant
152 printf("Main deploy: %d meters (%d feet)\n",
153 ao_config.main_deploy,
154 (int16_t) ((int32_t) ao_config.main_deploy * 328 / 100));
158 ao_config_main_deploy_set(void) __reentrant
161 if (ao_cmd_status != ao_cmd_success)
163 ao_mutex_get(&ao_config_mutex);
165 ao_config.main_deploy = ao_cmd_lex_i;
167 ao_mutex_put(&ao_config_mutex);
168 ao_config_main_deploy_show();
172 ao_config_accel_calibrate_show(void) __reentrant
174 printf("Accel cal +1g: %d -1g: %d\n",
175 ao_config.accel_plus_g, ao_config.accel_minus_g);
178 #define ACCEL_CALIBRATE_SAMPLES 1024
179 #define ACCEL_CALIBRATE_SHIFT 10
182 ao_config_accel_calibrate_auto(char *orientation) __reentrant
186 uint8_t cal_adc_ring;
188 printf("Orient %s and press a key...", orientation);
191 puts("\r\n"); flush();
192 puts("Calibrating..."); flush();
193 i = ACCEL_CALIBRATE_SAMPLES;
195 cal_adc_ring = ao_adc_head;
197 ao_sleep(&ao_adc_ring);
198 while (i && cal_adc_ring != ao_adc_head) {
199 accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
200 cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
204 return accel_total >> ACCEL_CALIBRATE_SHIFT;
208 ao_config_accel_calibrate_set(void) __reentrant
212 if (ao_cmd_status != ao_cmd_success)
214 if (ao_cmd_lex_i == 0) {
215 up = ao_config_accel_calibrate_auto("antenna up");
216 down = ao_config_accel_calibrate_auto("antenna down");
220 if (ao_cmd_status != ao_cmd_success)
225 printf("Invalid accel calibration: antenna up (%d) should be less than antenna down (%d)\n",
229 ao_mutex_get(&ao_config_mutex);
231 ao_config.accel_plus_g = up;
232 ao_config.accel_minus_g = down;
234 ao_mutex_put(&ao_config_mutex);
235 ao_config_accel_calibrate_show();
239 ao_config_apogee_delay_show(void) __reentrant
241 printf("Apogee delay: %d seconds\n",
242 ao_config.apogee_delay);
246 ao_config_apogee_delay_set(void) __reentrant
249 if (ao_cmd_status != ao_cmd_success)
251 ao_mutex_get(&ao_config_mutex);
253 ao_config.apogee_delay = ao_cmd_lex_i;
255 ao_mutex_put(&ao_config_mutex);
256 ao_config_apogee_delay_show();
262 ao_config_radio_cal_show(void) __reentrant
264 printf("Radio cal: %ld\n", ao_config.radio_cal);
268 ao_config_radio_cal_set(void) __reentrant
271 if (ao_cmd_status != ao_cmd_success)
273 ao_mutex_get(&ao_config_mutex);
275 ao_config.radio_cal = ao_cmd_lex_u32;
277 ao_mutex_put(&ao_config_mutex);
278 ao_config_radio_cal_show();
281 struct ao_config_var {
283 void (*set)(void) __reentrant;
284 void (*show)(void) __reentrant;
289 ao_config_help(void) __reentrant;
292 ao_config_show(void) __reentrant;
295 ao_config_write(void) __reentrant;
297 __code struct ao_config_var ao_config_vars[] = {
299 { 'm', ao_config_main_deploy_set, ao_config_main_deploy_show,
300 "m <meters> Set height above launch for main deploy (in meters)" },
301 { 'd', ao_config_apogee_delay_set, ao_config_apogee_delay_show,
302 "d <delay> Set apogee igniter delay (in seconds)" },
304 { 'r', ao_config_radio_channel_set, ao_config_radio_channel_show,
305 "r <channel> Set radio channel (freq = 434.550 + channel * .1)" },
306 { 'c', ao_config_callsign_set, ao_config_callsign_show,
307 "c <call> Set callsign broadcast in each packet (8 char max)" },
309 { 'a', ao_config_accel_calibrate_set, ao_config_accel_calibrate_show,
310 "a <+g> <-g> Set accelerometer calibration (0 for auto)" },
312 { 'f', ao_config_radio_cal_set, ao_config_radio_cal_show,
313 "f <cal> Set radio calibration value (cal = rf/(xtal/2^16))" },
314 { 's', ao_config_show, ao_config_show,
315 "s Show current config values" },
317 { 'w', ao_config_write, ao_config_write,
318 "w Write current values to eeprom" },
320 { '?', ao_config_help, ao_config_help,
321 "? Show available config variables" },
322 { 0, ao_config_help, ao_config_help,
331 void (*__xdata func)(void) __reentrant;
337 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
338 if (ao_config_vars[cmd].cmd == c) {
339 func = ao_config_vars[cmd].set;
345 ao_cmd_status = ao_cmd_syntax_error;
349 ao_config_help(void) __reentrant
352 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
353 puts (ao_config_vars[cmd].help);
357 ao_config_show(void) __reentrant
360 printf("Config version: %d.%d\n",
361 ao_config.major, ao_config.minor);
362 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
363 if (ao_config_vars[cmd].show != ao_config_vars[cmd].set)
364 (*ao_config_vars[cmd].show)();
369 ao_config_write(void) __reentrant
372 ao_mutex_get(&ao_config_mutex);
373 if (ao_config_dirty) {
378 ao_mutex_put(&ao_config_mutex);
382 puts("Nothing to save");
386 __code struct ao_cmds ao_config_cmds[] = {
387 { 'c', ao_config_set, "c <var> <value> Set config variable (? for help, s to show)" },
388 { '\0', ao_config_set, NULL },
394 ao_cmd_register(&ao_config_cmds[0]);