2673e472b066a23d556eb333582e88f3431e1401
[fw/altos] / src / ao_config.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19
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;
24
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
30
31 #if HAS_EEPROM
32 static void
33 _ao_config_put(void)
34 {
35         ao_storage_setup();
36         ao_storage_write(ao_storage_config, &ao_config, sizeof (ao_config));
37         ao_storage_flush();
38 }
39
40 void
41 ao_config_put(void)
42 {
43         ao_mutex_get(&ao_config_mutex);
44         _ao_config_put();
45         ao_mutex_put(&ao_config_mutex);
46 }
47 #endif
48
49 static void
50 _ao_config_get(void)
51 {
52         if (ao_config_loaded)
53                 return;
54 #if HAS_EEPROM
55         ao_storage_setup();
56         ao_storage_read(ao_storage_config, &ao_config, sizeof (ao_config));
57 #endif
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;
70                 ao_config_dirty = 1;
71         }
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;
80                 }
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;
85                 ao_config_dirty = 1;
86         }
87         ao_config_loaded = 1;
88 }
89
90 void
91 ao_config_get(void)
92 {
93         ao_mutex_get(&ao_config_mutex);
94         _ao_config_get();
95         ao_mutex_put(&ao_config_mutex);
96 }
97
98 void
99 ao_config_callsign_show(void)
100 {
101         printf ("Callsign: \"%s\"\n", ao_config.callsign);
102 }
103
104 void
105 ao_config_callsign_set(void) __reentrant
106 {
107         uint8_t c;
108         char callsign[AO_MAX_CALLSIGN + 1];
109
110         ao_cmd_white();
111         c = 0;
112         while (ao_cmd_lex_c != '\n') {
113                 if (c < AO_MAX_CALLSIGN)
114                         callsign[c++] = ao_cmd_lex_c;
115                 else
116                         ao_cmd_status = ao_cmd_lex_error;
117                 ao_cmd_lex();
118         }
119         if (ao_cmd_status != ao_cmd_success)
120                 return;
121         ao_mutex_get(&ao_config_mutex);
122         _ao_config_get();
123         while (c < AO_MAX_CALLSIGN + 1)
124                 callsign[c++] = '\0';
125         memcpy(&ao_config.callsign, &callsign,
126                AO_MAX_CALLSIGN + 1);
127         ao_config_dirty = 1;
128         ao_mutex_put(&ao_config_mutex);
129         ao_config_callsign_show();
130 }
131
132 void
133 ao_config_radio_channel_show(void) __reentrant
134 {
135         uint32_t        freq = 434550L + ao_config.radio_channel * 100L;
136         uint16_t        mhz = freq / 1000L;
137         uint16_t        khz = freq % 1000L;
138
139         printf("Radio channel: %d (%d.%03dMHz)\n",
140                ao_config.radio_channel, mhz, khz);
141 }
142
143 void
144 ao_config_radio_channel_set(void) __reentrant
145 {
146         ao_cmd_decimal();
147         if (ao_cmd_status != ao_cmd_success)
148                 return;
149         ao_mutex_get(&ao_config_mutex);
150         _ao_config_get();
151         ao_config.radio_channel = ao_cmd_lex_i;
152         ao_config_dirty = 1;
153         ao_mutex_put(&ao_config_mutex);
154         ao_config_radio_channel_show();
155         ao_radio_recv_abort();
156 }
157
158 #if HAS_ADC
159
160 void
161 ao_config_main_deploy_show(void) __reentrant
162 {
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));
166 }
167
168 void
169 ao_config_main_deploy_set(void) __reentrant
170 {
171         ao_cmd_decimal();
172         if (ao_cmd_status != ao_cmd_success)
173                 return;
174         ao_mutex_get(&ao_config_mutex);
175         _ao_config_get();
176         ao_config.main_deploy = ao_cmd_lex_i;
177         ao_config_dirty = 1;
178         ao_mutex_put(&ao_config_mutex);
179         ao_config_main_deploy_show();
180 }
181
182 void
183 ao_config_accel_calibrate_show(void) __reentrant
184 {
185         printf("Accel cal +1g: %d -1g: %d\n",
186                ao_config.accel_plus_g, ao_config.accel_minus_g);
187 }
188
189 #define ACCEL_CALIBRATE_SAMPLES 1024
190 #define ACCEL_CALIBRATE_SHIFT   10
191
192 static int16_t
193 ao_config_accel_calibrate_auto(char *orientation) __reentrant
194 {
195         uint16_t        i;
196         int32_t         accel_total;
197         uint8_t         cal_adc_ring;
198
199         printf("Orient %s and press a key...", orientation);
200         flush();
201         (void) getchar();
202         puts("\r\n"); flush();
203         puts("Calibrating..."); flush();
204         i = ACCEL_CALIBRATE_SAMPLES;
205         accel_total = 0;
206         cal_adc_ring = ao_adc_head;
207         while (i) {
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);
212                         i--;
213                 }
214         }
215         return accel_total >> ACCEL_CALIBRATE_SHIFT;
216 }
217
218 void
219 ao_config_accel_calibrate_set(void) __reentrant
220 {
221         int16_t up, down;
222         ao_cmd_decimal();
223         if (ao_cmd_status != ao_cmd_success)
224                 return;
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");
228         } else {
229                 up = ao_cmd_lex_i;
230                 ao_cmd_decimal();
231                 if (ao_cmd_status != ao_cmd_success)
232                         return;
233                 down = ao_cmd_lex_i;
234         }
235         if (up >= down) {
236                 printf("Invalid accel calibration: antenna up (%d) should be less than antenna down (%d)\n",
237                        up, down);
238                 return;
239         }
240         ao_mutex_get(&ao_config_mutex);
241         _ao_config_get();
242         ao_config.accel_plus_g = up;
243         ao_config.accel_minus_g = down;
244         ao_config_dirty = 1;
245         ao_mutex_put(&ao_config_mutex);
246         ao_config_accel_calibrate_show();
247 }
248
249 void
250 ao_config_apogee_delay_show(void) __reentrant
251 {
252         printf("Apogee delay: %d seconds\n",
253                ao_config.apogee_delay);
254 }
255
256 void
257 ao_config_apogee_delay_set(void) __reentrant
258 {
259         ao_cmd_decimal();
260         if (ao_cmd_status != ao_cmd_success)
261                 return;
262         ao_mutex_get(&ao_config_mutex);
263         _ao_config_get();
264         ao_config.apogee_delay = ao_cmd_lex_i;
265         ao_config_dirty = 1;
266         ao_mutex_put(&ao_config_mutex);
267         ao_config_apogee_delay_show();
268 }
269
270 #endif /* HAS_ADC */
271
272 void
273 ao_config_radio_cal_show(void) __reentrant
274 {
275         printf("Radio cal: %ld\n", ao_config.radio_cal);
276 }
277
278 void
279 ao_config_radio_cal_set(void) __reentrant
280 {
281         ao_cmd_decimal();
282         if (ao_cmd_status != ao_cmd_success)
283                 return;
284         ao_mutex_get(&ao_config_mutex);
285         _ao_config_get();
286         ao_config.radio_cal = ao_cmd_lex_u32;
287         ao_config_dirty = 1;
288         ao_mutex_put(&ao_config_mutex);
289         ao_config_radio_cal_show();
290 }
291
292 struct ao_config_var {
293         char            cmd;
294         void            (*set)(void) __reentrant;
295         void            (*show)(void) __reentrant;
296         const char      *help;
297 };
298
299 static void
300 ao_config_help(void) __reentrant;
301
302 static void
303 ao_config_show(void) __reentrant;
304
305 static void
306 ao_config_write(void) __reentrant;
307
308 __code struct ao_config_var ao_config_vars[] = {
309 #if HAS_ADC
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)" },
314 #endif /* HAS_ADC */
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)" },
319 #if HAS_ADC
320         { 'a',  ao_config_accel_calibrate_set,  ao_config_accel_calibrate_show,
321                 "a <+g> <-g> Set accelerometer calibration (0 for auto)" },
322 #endif /* HAS_ADC */
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" },
327 #if HAS_EEPROM
328         { 'w',  ao_config_write,                ao_config_write,
329                 "w           Write current values to eeprom" },
330 #endif
331         { '?',  ao_config_help,                 ao_config_help,
332                 "?           Show available config variables" },
333         { 0,    ao_config_help, ao_config_help,
334                 NULL },
335 };
336
337 void
338 ao_config_set(void)
339 {
340         char    c;
341         uint8_t cmd;
342         void (*__xdata func)(void) __reentrant;
343
344         ao_cmd_white();
345         c = ao_cmd_lex_c;
346         ao_cmd_lex();
347         func = 0;
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;
351                         break;
352                 }
353         if (func)
354                 (*func)();
355         else
356                 ao_cmd_status = ao_cmd_syntax_error;
357 }
358
359 static void
360 ao_config_help(void) __reentrant
361 {
362         uint8_t cmd;
363         for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
364                 puts (ao_config_vars[cmd].help);
365 }
366
367 static void
368 ao_config_show(void) __reentrant
369 {
370         uint8_t cmd;
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)();
376 }
377
378 #if HAS_EEPROM
379 static void
380 ao_config_write(void) __reentrant
381 {
382         uint8_t saved = 0;
383         ao_mutex_get(&ao_config_mutex);
384         if (ao_config_dirty) {
385                 _ao_config_put();
386                 ao_config_dirty = 0;
387                 saved = 1;
388         }
389         ao_mutex_put(&ao_config_mutex);
390         if (saved)
391                 puts("Saved");
392         else
393                 puts("Nothing to save");
394 }
395 #endif
396
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 },
400 };
401
402 void
403 ao_config_init(void)
404 {
405         ao_cmd_register(&ao_config_cmds[0]);
406 }