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 "KD7SQG"
28 #define AO_CONFIG_DEFAULT_ACCEL_ZERO_G 16000
29 #define AO_CONFIG_DEFAULT_APOGEE_DELAY 0
34 ao_ee_write_config((uint8_t *) &ao_config, sizeof (ao_config));
42 ao_ee_read_config((uint8_t *) &ao_config, sizeof (ao_config));
43 if (ao_config.major != AO_CONFIG_MAJOR) {
44 ao_config.major = AO_CONFIG_MAJOR;
45 ao_config.minor = AO_CONFIG_MINOR;
46 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
47 ao_config.radio_channel = AO_CONFIG_DEFAULT_RADIO_CHANNEL;
48 ao_config.accel_zero_g = AO_CONFIG_DEFAULT_ACCEL_ZERO_G;
49 memset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
50 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
51 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
52 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
55 if (ao_config.minor < AO_CONFIG_MINOR) {
56 /* Fixups for major version 1 */
57 if (ao_config.minor < 1)
58 ao_config.apogee_delay = AO_CONFIG_DEFAULT_APOGEE_DELAY;
59 ao_config.minor = AO_CONFIG_MINOR;
68 ao_mutex_get(&ao_config_mutex);
70 ao_mutex_put(&ao_config_mutex);
74 ao_config_callsign_show(void)
76 printf ("Callsign: \"%s\"\n", ao_config.callsign);
80 ao_config_callsign_set(void) __reentrant
83 char callsign[AO_MAX_CALLSIGN + 1];
87 while (ao_cmd_lex_c != '\n') {
88 if (c < AO_MAX_CALLSIGN)
89 callsign[c++] = ao_cmd_lex_c;
91 ao_cmd_status = ao_cmd_lex_error;
94 if (ao_cmd_status != ao_cmd_success)
96 ao_mutex_get(&ao_config_mutex);
98 while (c < AO_MAX_CALLSIGN + 1)
100 memcpy(&ao_config.callsign, &callsign,
101 AO_MAX_CALLSIGN + 1);
103 ao_mutex_put(&ao_config_mutex);
104 ao_config_callsign_show();
108 ao_config_radio_channel_show(void) __reentrant
110 uint32_t freq = 434550L + ao_config.radio_channel * 100L;
111 uint16_t mhz = freq / 1000L;
112 uint16_t khz = freq % 1000L;
114 printf("Radio channel: %d (%d.%03dMHz)\n",
115 ao_config.radio_channel, mhz, khz);
119 ao_config_radio_channel_set(void) __reentrant
122 if (ao_cmd_status != ao_cmd_success)
124 ao_mutex_get(&ao_config_mutex);
126 ao_config.radio_channel = ao_cmd_lex_i;
128 ao_mutex_put(&ao_config_mutex);
129 ao_config_radio_channel_show();
133 ao_config_main_deploy_show(void) __reentrant
135 printf("Main deploy: %d meters (%d feet)\n",
136 ao_config.main_deploy,
137 (int16_t) ((int32_t) ao_config.main_deploy * 328 / 100));
141 ao_config_main_deploy_set(void) __reentrant
144 if (ao_cmd_status != ao_cmd_success)
146 ao_mutex_get(&ao_config_mutex);
148 ao_config.main_deploy = ao_cmd_lex_i;
150 ao_mutex_put(&ao_config_mutex);
151 ao_config_main_deploy_show();
155 ao_config_accel_zero_g_show(void) __reentrant
157 printf("Accel zero g point: %d\n",
158 ao_config.accel_zero_g);
161 #define ZERO_G_SAMPLES 1000
164 ao_config_accel_zero_g_auto(void) __reentrant
168 uint8_t cal_adc_ring;
170 puts("Calibrating accelerometer..."); flush();
173 cal_adc_ring = ao_adc_head;
175 ao_sleep(&ao_adc_ring);
176 while (i && cal_adc_ring != ao_adc_head) {
177 accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
178 cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
182 return (int16_t) (accel_total / ZERO_G_SAMPLES);
185 ao_config_accel_zero_g_set(void) __reentrant
188 if (ao_cmd_status != ao_cmd_success)
190 if (ao_cmd_lex_i == 0)
191 ao_cmd_lex_i = ao_config_accel_zero_g_auto();
192 ao_mutex_get(&ao_config_mutex);
194 ao_config.accel_zero_g = ao_cmd_lex_i;
196 ao_mutex_put(&ao_config_mutex);
197 ao_config_accel_zero_g_show();
201 ao_config_apogee_delay_show(void) __reentrant
203 printf("Apogee delay: %d seconds\n",
204 ao_config.apogee_delay);
208 ao_config_apogee_delay_set(void) __reentrant
211 if (ao_cmd_status != ao_cmd_success)
213 ao_mutex_get(&ao_config_mutex);
215 ao_config.apogee_delay = ao_cmd_lex_i;
217 ao_mutex_put(&ao_config_mutex);
218 ao_config_apogee_delay_show();
221 struct ao_config_var {
223 void (*set)(void) __reentrant;
224 void (*show)(void) __reentrant;
229 ao_config_help(void) __reentrant;
232 ao_config_show(void) __reentrant;
235 ao_config_write(void) __reentrant;
237 __code struct ao_config_var ao_config_vars[] = {
238 { 'm', ao_config_main_deploy_set, ao_config_main_deploy_show,
239 "m <meters> Set height above launch for main deploy (in meters)" },
240 { 'a', ao_config_accel_zero_g_set, ao_config_accel_zero_g_show,
241 "a <value> Set accelerometer zero g point (0 for auto)" },
242 { 'r', ao_config_radio_channel_set, ao_config_radio_channel_show,
243 "r <channel> Set radio channel (freq = 434.550 + channel * .1)" },
244 { 'c', ao_config_callsign_set, ao_config_callsign_show,
245 "c <call> Set callsign broadcast in each packet (8 char max)" },
246 { 'd', ao_config_apogee_delay_set, ao_config_apogee_delay_show,
247 "d <delay> Set apogee igniter delay (in seconds)" },
248 { 's', ao_config_show, ao_config_show,
249 "s Show current config values" },
250 { 'w', ao_config_write, ao_config_write,
251 "w Write current values to eeprom" },
252 { '?', ao_config_help, ao_config_help,
253 "? Show available config variables" },
254 { 0, ao_config_main_deploy_set, ao_config_main_deploy_show,
263 void (*__xdata func)(void) __reentrant;
269 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
270 if (ao_config_vars[cmd].cmd == c) {
271 func = ao_config_vars[cmd].set;
277 ao_cmd_status = ao_cmd_syntax_error;
281 ao_config_help(void) __reentrant
284 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
285 puts (ao_config_vars[cmd].help);
289 ao_config_show(void) __reentrant
292 printf("Config version: %d.%d\n",
293 ao_config.major, ao_config.minor);
294 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
295 if (ao_config_vars[cmd].show != ao_config_vars[cmd].set)
296 (*ao_config_vars[cmd].show)();
300 ao_config_write(void) __reentrant
302 ao_mutex_get(&ao_config_mutex);
303 if (ao_config_dirty) {
308 ao_mutex_put(&ao_config_mutex);
311 __code struct ao_cmds ao_config_cmds[] = {
312 { 'c', ao_config_set, "c <var> <value> Set config variable (? for help, s to show)" },
313 { '\0', ao_config_set, NULL },
319 ao_cmd_register(&ao_config_cmds[0]);