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
33 ao_ee_write_config((uint8_t *) &ao_config, sizeof (ao_config));
41 ao_ee_read_config((uint8_t *) &ao_config, sizeof (ao_config));
42 if (ao_config.major != AO_CONFIG_MAJOR) {
43 ao_config.major = AO_CONFIG_MAJOR;
44 ao_config.minor = AO_CONFIG_MINOR;
45 ao_config.main_deploy = AO_CONFIG_DEFAULT_MAIN_DEPLOY;
46 ao_config.radio_channel = AO_CONFIG_DEFAULT_RADIO_CHANNEL;
47 ao_config.accel_zero_g = AO_CONFIG_DEFAULT_ACCEL_ZERO_G;
48 memset(&ao_config.callsign, '\0', sizeof (ao_config.callsign));
49 memcpy(&ao_config.callsign, AO_CONFIG_DEFAULT_CALLSIGN,
50 sizeof(AO_CONFIG_DEFAULT_CALLSIGN) - 1);
53 /* deal with minor version issues here, at 0 we haven't any */
60 ao_mutex_get(&ao_config_mutex);
62 ao_mutex_put(&ao_config_mutex);
66 ao_config_callsign_show(void)
68 printf ("Callsign: \"%s\"\n", ao_config.callsign);
72 ao_config_callsign_set(void) __reentrant
75 char callsign[AO_MAX_CALLSIGN + 1];
79 while (ao_cmd_lex_c != '\n') {
80 if (c < AO_MAX_CALLSIGN)
81 callsign[c++] = ao_cmd_lex_c;
83 ao_cmd_status = ao_cmd_lex_error;
86 if (ao_cmd_status != ao_cmd_success)
88 ao_mutex_get(&ao_config_mutex);
90 while (c < AO_MAX_CALLSIGN + 1)
92 memcpy(&ao_config.callsign, &callsign,
95 ao_mutex_put(&ao_config_mutex);
96 ao_config_callsign_show();
100 ao_config_radio_channel_show(void) __reentrant
102 uint32_t freq = 434550L + ao_config.radio_channel * 100L;
103 uint16_t mhz = freq / 1000L;
104 uint16_t khz = freq % 1000L;
106 printf("Radio channel: %d (%d.%03dMHz)\n",
107 ao_config.radio_channel, mhz, khz);
111 ao_config_radio_channel_set(void) __reentrant
114 if (ao_cmd_status != ao_cmd_success)
116 ao_mutex_get(&ao_config_mutex);
118 ao_config.radio_channel = ao_cmd_lex_i;
120 ao_mutex_put(&ao_config_mutex);
121 ao_config_radio_channel_show();
125 ao_config_main_deploy_show(void) __reentrant
127 printf("Main deploy set to %d meters (%d feet)\n",
128 ao_config.main_deploy,
129 (int16_t) ((int32_t) ao_config.main_deploy * 328 / 100));
133 ao_config_main_deploy_set(void) __reentrant
136 if (ao_cmd_status != ao_cmd_success)
138 ao_mutex_get(&ao_config_mutex);
140 ao_config.main_deploy = ao_cmd_lex_i;
142 ao_mutex_put(&ao_config_mutex);
143 ao_config_main_deploy_show();
147 ao_config_accel_zero_g_show(void) __reentrant
149 printf("Accel zero g point set to %d\n",
150 ao_config.accel_zero_g);
153 #define ZERO_G_SAMPLES 1000
156 ao_config_accel_zero_g_auto(void) __reentrant
160 uint8_t cal_adc_ring;
162 puts("Calibrating accelerometer..."); flush();
165 cal_adc_ring = ao_adc_head;
167 ao_sleep(&ao_adc_ring);
168 while (i && cal_adc_ring != ao_adc_head) {
169 accel_total += (int32_t) ao_adc_ring[cal_adc_ring].accel;
170 cal_adc_ring = ao_adc_ring_next(cal_adc_ring);
174 return (int16_t) (accel_total / ZERO_G_SAMPLES);
177 ao_config_accel_zero_g_set(void) __reentrant
180 if (ao_cmd_status != ao_cmd_success)
182 if (ao_cmd_lex_i == 0)
183 ao_cmd_lex_i = ao_config_accel_zero_g_auto();
184 ao_mutex_get(&ao_config_mutex);
186 ao_config.accel_zero_g = ao_cmd_lex_i;
188 ao_mutex_put(&ao_config_mutex);
189 ao_config_accel_zero_g_show();
192 struct ao_config_var {
194 void (*set)(void) __reentrant;
195 void (*show)(void) __reentrant;
200 ao_config_help(void) __reentrant;
203 ao_config_show(void) __reentrant;
206 ao_config_write(void) __reentrant;
208 __code struct ao_config_var ao_config_vars[] = {
209 { 'm', ao_config_main_deploy_set, ao_config_main_deploy_show,
210 "m <meters> Set height above launch for main deploy (in meters)" },
211 { 'a', ao_config_accel_zero_g_set, ao_config_accel_zero_g_show,
212 "a <value> Set accelerometer zero g point (0 for auto)" },
213 { 'r', ao_config_radio_channel_set, ao_config_radio_channel_show,
214 "r <channel> Set radio channel (freq = 434.550 + channel * .1)" },
215 { 'c', ao_config_callsign_set, ao_config_callsign_show,
216 "c <call> Set callsign broadcast in each packet (8 char max)" },
217 { 's', ao_config_show, ao_config_show,
218 "s Show current config values" },
219 { 'w', ao_config_write, ao_config_write,
220 "w Write current values to eeprom" },
221 { '?', ao_config_help, ao_config_help,
222 "? Show available config variables" },
223 { 0, ao_config_main_deploy_set, ao_config_main_deploy_show,
232 void (*__xdata func)(void) __reentrant;
238 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
239 if (ao_config_vars[cmd].cmd == c) {
240 func = ao_config_vars[cmd].set;
246 ao_cmd_status = ao_cmd_syntax_error;
250 ao_config_help(void) __reentrant
253 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
254 puts (ao_config_vars[cmd].help);
258 ao_config_show(void) __reentrant
261 for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
262 if (ao_config_vars[cmd].show != ao_config_vars[cmd].set)
263 (*ao_config_vars[cmd].show)();
267 ao_config_write(void) __reentrant
269 ao_mutex_get(&ao_config_mutex);
270 if (ao_config_dirty) {
275 ao_mutex_put(&ao_config_mutex);
278 __code struct ao_cmds ao_config_cmds[] = {
279 { 'c', ao_config_set, "c <var> <value> Set config variable (? for help, s to show)" },
280 { '\0', ao_config_set, NULL },
286 ao_cmd_register(&ao_config_cmds[0]);