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));
43 ao_mutex_get(&ao_config_mutex);
45 ao_mutex_put(&ao_config_mutex);
56 ao_storage_read(ao_storage_config, &ao_config, sizeof (ao_config));
58 if (ao_config.major != AO_CONFIG_MAJOR) {
59 ao_config.major = AO_CONFIG_MAJOR;
60 ao_config.minor = AO_CONFIG_MINOR;
61 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
62 ao_config.radio_channel = AO_CONFIG_DEFAULT_RADIO_CHANNEL;
63 ao_config.accel_plus_g = 0;
64 ao_config.accel_minus_g = 0;
65 memset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
66 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
67 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
68 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
69 ao_config.radio_cal = ao_radio_cal;
72 if (ao_config.minor < AO_CONFIG_MINOR) {
73 /* Fixups for minor version 1 */
74 if (ao_config.minor < 1)
75 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
76 /* Fixups for minor version 2 */
77 if (ao_config.minor < 2) {
78 ao_config.accel_plus_g = 0;
79 ao_config.accel_minus_g = 0;
81 /* Fixups for minor version 3 */
82 if (ao_config.minor < 3)
83 ao_config.radio_cal = ao_radio_cal;
84 ao_config.minor = AO_CONFIG_MINOR;
93 ao_mutex_get(&ao_config_mutex);
95 ao_mutex_put(&ao_config_mutex);
99 ao_config_callsign_show(void)
101 printf ("Callsign: \"%s\"\n", ao_config.callsign);
105 ao_config_callsign_set(void) __reentrant
108 char callsign[AO_MAX_CALLSIGN + 1];
112 while (ao_cmd_lex_c != '\n') {
113 if (c < AO_MAX_CALLSIGN)
114 callsign[c++] = ao_cmd_lex_c;
116 ao_cmd_status = ao_cmd_lex_error;
119 if (ao_cmd_status != ao_cmd_success)
121 ao_mutex_get(&ao_config_mutex);
123 while (c < AO_MAX_CALLSIGN + 1)
124 callsign[c++] = '\0';
125 memcpy(&ao_config.callsign, &callsign,
126 AO_MAX_CALLSIGN + 1);
128 ao_mutex_put(&ao_config_mutex);
129 ao_config_callsign_show();
133 ao_config_radio_channel_show(void) __reentrant
135 uint32_t freq = 434550L + ao_config.radio_channel * 100L;
136 uint16_t mhz = freq / 1000L;
137 uint16_t khz = freq % 1000L;
139 printf("Radio channel: %d (%d.%03dMHz)\n",
140 ao_config.radio_channel, mhz, khz);
144 ao_config_radio_channel_set(void) __reentrant
147 if (ao_cmd_status != ao_cmd_success)
149 ao_mutex_get(&ao_config_mutex);
151 ao_config.radio_channel = ao_cmd_lex_i;
153 ao_mutex_put(&ao_config_mutex);
154 ao_config_radio_channel_show();
155 ao_radio_recv_abort();
161 ao_config_main_deploy_show(void) __reentrant
163 printf("Main deploy: %d meters (%d feet)\n",
164 ao_config.main_deploy,
165 (int16_t) ((int32_t) ao_config.main_deploy * 328 / 100));
169 ao_config_main_deploy_set(void) __reentrant
172 if (ao_cmd_status != ao_cmd_success)
174 ao_mutex_get(&ao_config_mutex);
176 ao_config.main_deploy = ao_cmd_lex_i;
178 ao_mutex_put(&ao_config_mutex);
179 ao_config_main_deploy_show();
183 ao_config_accel_calibrate_show(void) __reentrant
185 printf("Accel cal +1g: %d -1g: %d\n",
186 ao_config.accel_plus_g, ao_config.accel_minus_g);
189 #define ACCEL_CALIBRATE_SAMPLES 1024
190 #define ACCEL_CALIBRATE_SHIFT 10
193 ao_config_accel_calibrate_auto(char *orientation) __reentrant
197 uint8_t cal_adc_ring;
199 printf("Orient %s and press a key...", orientation);
202 puts("\r\n"); flush();
203 puts("Calibrating..."); flush();
204 i = ACCEL_CALIBRATE_SAMPLES;
206 cal_adc_ring = ao_adc_head;
208 ao_sleep(&ao_adc_ring);
209 while (i && cal_adc_ring != ao_adc_head) {
210 accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
211 cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
215 return accel_total >> ACCEL_CALIBRATE_SHIFT;
219 ao_config_accel_calibrate_set(void) __reentrant
223 if (ao_cmd_status != ao_cmd_success)
225 if (ao_cmd_lex_i == 0) {
226 up = ao_config_accel_calibrate_auto("antenna up");
227 down = ao_config_accel_calibrate_auto("antenna down");
231 if (ao_cmd_status != ao_cmd_success)
236 printf("Invalid accel calibration: antenna up (%d) should be less than antenna down (%d)\n",
240 ao_mutex_get(&ao_config_mutex);
242 ao_config.accel_plus_g = up;
243 ao_config.accel_minus_g = down;
245 ao_mutex_put(&ao_config_mutex);
246 ao_config_accel_calibrate_show();
250 ao_config_apogee_delay_show(void) __reentrant
252 printf("Apogee delay: %d seconds\n",
253 ao_config.apogee_delay);
257 ao_config_apogee_delay_set(void) __reentrant
260 if (ao_cmd_status != ao_cmd_success)
262 ao_mutex_get(&ao_config_mutex);
264 ao_config.apogee_delay = ao_cmd_lex_i;
266 ao_mutex_put(&ao_config_mutex);
267 ao_config_apogee_delay_show();
273 ao_config_radio_cal_show(void) __reentrant
275 printf("Radio cal: %ld\n", ao_config.radio_cal);
279 ao_config_radio_cal_set(void) __reentrant
282 if (ao_cmd_status != ao_cmd_success)
284 ao_mutex_get(&ao_config_mutex);
286 ao_config.radio_cal = ao_cmd_lex_u32;
288 ao_mutex_put(&ao_config_mutex);
289 ao_config_radio_cal_show();
292 struct ao_config_var {
294 void (*set)(void) __reentrant;
295 void (*show)(void) __reentrant;
300 ao_config_help(void) __reentrant;
303 ao_config_show(void) __reentrant;
306 ao_config_write(void) __reentrant;
308 __code struct ao_config_var ao_config_vars[] = {
310 { 'm', ao_config_main_deploy_set, ao_config_main_deploy_show,
311 "m <meters> Set height above launch for main deploy (in meters)" },
312 { 'd', ao_config_apogee_delay_set, ao_config_apogee_delay_show,
313 "d <delay> Set apogee igniter delay (in seconds)" },
315 { 'r', ao_config_radio_channel_set, ao_config_radio_channel_show,
316 "r <channel> Set radio channel (freq = 434.550 + channel * .1)" },
317 { 'c', ao_config_callsign_set, ao_config_callsign_show,
318 "c <call> Set callsign broadcast in each packet (8 char max)" },
320 { 'a', ao_config_accel_calibrate_set, ao_config_accel_calibrate_show,
321 "a <+g> <-g> Set accelerometer calibration (0 for auto)" },
323 { 'f', ao_config_radio_cal_set, ao_config_radio_cal_show,
324 "f <cal> Set radio calibration value (cal = rf/(xtal/2^16))" },
325 { 's', ao_config_show, ao_config_show,
326 "s Show current config values" },
328 { 'w', ao_config_write, ao_config_write,
329 "w Write current values to eeprom" },
331 { '?', ao_config_help, ao_config_help,
332 "? Show available config variables" },
333 { 0, ao_config_help, ao_config_help,
342 void (*__xdata func)(void) __reentrant;
348 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
349 if (ao_config_vars[cmd].cmd == c) {
350 func = ao_config_vars[cmd].set;
356 ao_cmd_status = ao_cmd_syntax_error;
360 ao_config_help(void) __reentrant
363 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
364 puts (ao_config_vars[cmd].help);
368 ao_config_show(void) __reentrant
371 printf("Config version: %d.%d\n",
372 ao_config.major, ao_config.minor);
373 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
374 if (ao_config_vars[cmd].show != ao_config_vars[cmd].set)
375 (*ao_config_vars[cmd].show)();
380 ao_config_write(void) __reentrant
383 ao_mutex_get(&ao_config_mutex);
384 if (ao_config_dirty) {
389 ao_mutex_put(&ao_config_mutex);
393 puts("Nothing to save");
397 __code struct ao_cmds ao_config_cmds[] = {
398 { 'c', ao_config_set, "c <var> <value> Set config variable (? for help, s to show)" },
399 { '\0', ao_config_set, NULL },
405 ao_cmd_register(&ao_config_cmds[0]);