Merge remote branch 'origin/master' into new-packet-format
[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_ee_write_config((uint8_t *) &ao_config, sizeof (ao_config));
36 }
37 #endif
38
39 static void
40 _ao_config_get(void)
41 {
42         if (ao_config_loaded)
43                 return;
44 #if HAS_EEPROM
45         ao_ee_read_config((uint8_t *) &ao_config, sizeof (ao_config));
46 #endif
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;
59                 ao_config_dirty = 1;
60         }
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;
69                 }
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;
74                 ao_config_dirty = 1;
75         }
76         ao_config_loaded = 1;
77 }
78
79 void
80 ao_config_get(void)
81 {
82         ao_mutex_get(&ao_config_mutex);
83         _ao_config_get();
84         ao_mutex_put(&ao_config_mutex);
85 }
86
87 void
88 ao_config_callsign_show(void)
89 {
90         printf ("Callsign: \"%s\"\n", ao_config.callsign);
91 }
92
93 void
94 ao_config_callsign_set(void) __reentrant
95 {
96         uint8_t c;
97         char callsign[AO_MAX_CALLSIGN + 1];
98
99         ao_cmd_white();
100         c = 0;
101         while (ao_cmd_lex_c != '\n') {
102                 if (c < AO_MAX_CALLSIGN)
103                         callsign[c++] = ao_cmd_lex_c;
104                 else
105                         ao_cmd_status = ao_cmd_lex_error;
106                 ao_cmd_lex();
107         }
108         if (ao_cmd_status != ao_cmd_success)
109                 return;
110         ao_mutex_get(&ao_config_mutex);
111         _ao_config_get();
112         while (c < AO_MAX_CALLSIGN + 1)
113                 callsign[c++] = '\0';
114         memcpy(&ao_config.callsign, &callsign,
115                AO_MAX_CALLSIGN + 1);
116         ao_config_dirty = 1;
117         ao_mutex_put(&ao_config_mutex);
118         ao_config_callsign_show();
119 }
120
121 void
122 ao_config_radio_channel_show(void) __reentrant
123 {
124         uint32_t        freq = 434550L + ao_config.radio_channel * 100L;
125         uint16_t        mhz = freq / 1000L;
126         uint16_t        khz = freq % 1000L;
127
128         printf("Radio channel: %d (%d.%03dMHz)\n",
129                ao_config.radio_channel, mhz, khz);
130 }
131
132 void
133 ao_config_radio_channel_set(void) __reentrant
134 {
135         ao_cmd_decimal();
136         if (ao_cmd_status != ao_cmd_success)
137                 return;
138         ao_mutex_get(&ao_config_mutex);
139         _ao_config_get();
140         ao_config.radio_channel = ao_cmd_lex_i;
141         ao_config_dirty = 1;
142         ao_mutex_put(&ao_config_mutex);
143         ao_config_radio_channel_show();
144         ao_radio_abort();
145 }
146
147 #if HAS_ADC
148
149 void
150 ao_config_main_deploy_show(void) __reentrant
151 {
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));
155 }
156
157 void
158 ao_config_main_deploy_set(void) __reentrant
159 {
160         ao_cmd_decimal();
161         if (ao_cmd_status != ao_cmd_success)
162                 return;
163         ao_mutex_get(&ao_config_mutex);
164         _ao_config_get();
165         ao_config.main_deploy = ao_cmd_lex_i;
166         ao_config_dirty = 1;
167         ao_mutex_put(&ao_config_mutex);
168         ao_config_main_deploy_show();
169 }
170
171 void
172 ao_config_accel_calibrate_show(void) __reentrant
173 {
174         printf("Accel cal +1g: %d -1g: %d\n",
175                ao_config.accel_plus_g, ao_config.accel_minus_g);
176 }
177
178 #define ACCEL_CALIBRATE_SAMPLES 1024
179 #define ACCEL_CALIBRATE_SHIFT   10
180
181 static int16_t
182 ao_config_accel_calibrate_auto(char *orientation) __reentrant
183 {
184         uint16_t        i;
185         int32_t         accel_total;
186         uint8_t         cal_adc_ring;
187
188         printf("Orient %s and press a key...", orientation);
189         flush();
190         (void) getchar();
191         puts("\r\n"); flush();
192         puts("Calibrating..."); flush();
193         i = ACCEL_CALIBRATE_SAMPLES;
194         accel_total = 0;
195         cal_adc_ring = ao_adc_head;
196         while (i) {
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);
201                         i--;
202                 }
203         }
204         return accel_total >> ACCEL_CALIBRATE_SHIFT;
205 }
206
207 void
208 ao_config_accel_calibrate_set(void) __reentrant
209 {
210         int16_t up, down;
211         ao_cmd_decimal();
212         if (ao_cmd_status != ao_cmd_success)
213                 return;
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");
217         } else {
218                 up = ao_cmd_lex_i;
219                 ao_cmd_decimal();
220                 if (ao_cmd_status != ao_cmd_success)
221                         return;
222                 down = ao_cmd_lex_i;
223         }
224         if (up >= down) {
225                 printf("Invalid accel calibration: antenna up (%d) should be less than antenna down (%d)\n",
226                        up, down);
227                 return;
228         }
229         ao_mutex_get(&ao_config_mutex);
230         _ao_config_get();
231         ao_config.accel_plus_g = up;
232         ao_config.accel_minus_g = down;
233         ao_config_dirty = 1;
234         ao_mutex_put(&ao_config_mutex);
235         ao_config_accel_calibrate_show();
236 }
237
238 void
239 ao_config_apogee_delay_show(void) __reentrant
240 {
241         printf("Apogee delay: %d seconds\n",
242                ao_config.apogee_delay);
243 }
244
245 void
246 ao_config_apogee_delay_set(void) __reentrant
247 {
248         ao_cmd_decimal();
249         if (ao_cmd_status != ao_cmd_success)
250                 return;
251         ao_mutex_get(&ao_config_mutex);
252         _ao_config_get();
253         ao_config.apogee_delay = ao_cmd_lex_i;
254         ao_config_dirty = 1;
255         ao_mutex_put(&ao_config_mutex);
256         ao_config_apogee_delay_show();
257 }
258
259 #endif /* HAS_ADC */
260
261 void
262 ao_config_radio_cal_show(void) __reentrant
263 {
264         printf("Radio cal: %ld\n", ao_config.radio_cal);
265 }
266
267 void
268 ao_config_radio_cal_set(void) __reentrant
269 {
270         ao_cmd_decimal();
271         if (ao_cmd_status != ao_cmd_success)
272                 return;
273         ao_mutex_get(&ao_config_mutex);
274         _ao_config_get();
275         ao_config.radio_cal = ao_cmd_lex_u32;
276         ao_config_dirty = 1;
277         ao_mutex_put(&ao_config_mutex);
278         ao_config_radio_cal_show();
279 }
280
281 struct ao_config_var {
282         char            cmd;
283         void            (*set)(void) __reentrant;
284         void            (*show)(void) __reentrant;
285         const char      *help;
286 };
287
288 void
289 ao_config_help(void) __reentrant;
290
291 void
292 ao_config_show(void) __reentrant;
293
294 void
295 ao_config_write(void) __reentrant;
296
297 __code struct ao_config_var ao_config_vars[] = {
298 #if HAS_ADC
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)" },
303 #endif /* HAS_ADC */
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)" },
308 #if HAS_ADC
309         { 'a',  ao_config_accel_calibrate_set,  ao_config_accel_calibrate_show,
310                 "a <+g> <-g> Set accelerometer calibration (0 for auto)" },
311 #endif /* HAS_ADC */
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" },
316 #if HAS_EEPROM
317         { 'w',  ao_config_write,                ao_config_write,
318                 "w           Write current values to eeprom" },
319 #endif
320         { '?',  ao_config_help,                 ao_config_help,
321                 "?           Show available config variables" },
322         { 0,    ao_config_help, ao_config_help,
323                 NULL },
324 };
325
326 void
327 ao_config_set(void)
328 {
329         char    c;
330         uint8_t cmd;
331         void (*__xdata func)(void) __reentrant;
332
333         ao_cmd_white();
334         c = ao_cmd_lex_c;
335         ao_cmd_lex();
336         func = 0;
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;
340                         break;
341                 }
342         if (func)
343                 (*func)();
344         else
345                 ao_cmd_status = ao_cmd_syntax_error;
346 }
347
348 void
349 ao_config_help(void) __reentrant
350 {
351         uint8_t cmd;
352         for (cmd = 0; ao_config_vars[cmd].cmd != '\0'; cmd++)
353                 puts (ao_config_vars[cmd].help);
354 }
355
356 void
357 ao_config_show(void) __reentrant
358 {
359         uint8_t cmd;
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)();
365 }
366
367 #if HAS_EEPROM
368 void
369 ao_config_write(void) __reentrant
370 {
371         uint8_t saved = 0;
372         ao_mutex_get(&ao_config_mutex);
373         if (ao_config_dirty) {
374                 _ao_config_put();
375                 ao_config_dirty = 0;
376                 saved = 1;
377         }
378         ao_mutex_put(&ao_config_mutex);
379         if (saved)
380                 puts("Saved");
381         else
382                 puts("Nothing to save");
383 }
384 #endif
385
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 },
389 };
390
391 void
392 ao_config_init(void)
393 {
394         ao_cmd_register(&ao_config_cmds[0]);
395 }