imuboard: remove references to xbee
[fpv.git] / imuboard / commands.c
1 /*
2  *  Copyright Droids Corporation (2011)
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; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: commands.c,v 1.9 2009-11-08 17:24:33 zer0 Exp $
19  *
20  *  Olivier MATZ <zer0@droids-corp.org>
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <inttypes.h>
27 #include <aversive.h>
28 #include <aversive/pgmspace.h>
29 #include <aversive/queue.h>
30 #include <aversive/endian.h>
31 #include <aversive/error.h>
32 #include <aversive/wait.h>
33 #include <parse.h>
34 #include <rdline.h>
35 #include <parse_string.h>
36 #include <parse_num.h>
37 #include <uart.h>
38 #include <callout.h>
39
40 #include "main.h"
41 #include "cmdline.h"
42 #include "eeprom_config.h"
43 #include "gps.h"
44 #include "imu.h"
45
46 /* commands_gen.c */
47 extern const parse_inst_t PROGMEM cmd_reset;
48 extern const parse_inst_t PROGMEM cmd_bootloader;
49 extern const parse_inst_t PROGMEM cmd_log;
50 extern const parse_inst_t PROGMEM cmd_log_show;
51 extern const parse_inst_t PROGMEM cmd_log_type;
52 extern const parse_inst_t PROGMEM cmd_stack_space;
53 extern const parse_inst_t PROGMEM cmd_callout;
54
55 /**********************************************************/
56
57 /* this structure is filled when cmd_test_eeprom_config is parsed successfully */
58 struct cmd_test_eeprom_config_result {
59         fixed_string_t arg0;
60 };
61
62 static void cmd_test_eeprom_config_parsed(void *parsed_result, void *data)
63 {
64         (void)parsed_result;
65         (void)data;
66
67         eeprom_dump_cmds();
68         eeprom_append_cmd("salut1\n");
69         eeprom_dump_cmds();
70         eeprom_append_cmd("salut2\n");
71         eeprom_append_cmd("salut3\n");
72         eeprom_append_cmd("salut4\n");
73         eeprom_dump_cmds();
74         eeprom_insert_cmd_before("coin\n", 0);
75         eeprom_insert_cmd_before("coin2\n", 2);
76         eeprom_dump_cmds();
77         eeprom_delete_cmd(2);
78         eeprom_delete_cmd(0);
79         eeprom_dump_cmds();
80 }
81
82 const char PROGMEM str_test_eeprom_config_arg0[] = "test_eeprom_config";
83 const parse_token_string_t PROGMEM cmd_test_eeprom_config_arg0 =
84         TOKEN_STRING_INITIALIZER(struct cmd_test_eeprom_config_result, arg0,
85                                  str_test_eeprom_config_arg0);
86
87 const char PROGMEM help_test_eeprom_config[] = "Test the eeprom configuration";
88 const parse_inst_t PROGMEM cmd_test_eeprom_config = {
89         .f = cmd_test_eeprom_config_parsed,  /* function to call */
90         .data = NULL,      /* 2nd arg of func */
91         .help_str = help_test_eeprom_config,
92         .tokens = {        /* token list, NULL terminated */
93                 (PGM_P)&cmd_test_eeprom_config_arg0,
94                 NULL,
95         },
96 };
97
98 /* ************* */
99
100 struct cmd_eeprom_del_result {
101         fixed_string_t cmd;
102         fixed_string_t action;
103         uint8_t n;
104 };
105
106 static void cmd_eeprom_del_parsed(void *parsed_result,
107                                 void *data)
108 {
109         struct cmd_eeprom_del_result *res = parsed_result;
110
111         (void)data;
112         if (eeprom_delete_cmd(res->n) < 0)
113                 printf_P(PSTR("cannot delete command\n"));
114         eeprom_dump_cmds();
115 }
116
117 const char PROGMEM str_eeprom_del_eeprom[] = "eeprom";
118 const parse_token_string_t PROGMEM cmd_eeprom_del_cmd =
119         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_del_result, cmd,
120                                  str_eeprom_del_eeprom);
121 const char PROGMEM str_eeprom_del_del[] = "del";
122 const parse_token_string_t PROGMEM cmd_eeprom_del_action =
123         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_del_result, action,
124                                  str_eeprom_del_del);
125 const parse_token_num_t PROGMEM cmd_eeprom_del_num =
126         TOKEN_NUM_INITIALIZER(struct cmd_eeprom_del_result, n,
127                               UINT8);
128
129 const char PROGMEM help_eeprom_del[] = "delete an eeprom init command";
130 const parse_inst_t PROGMEM cmd_eeprom_del = {
131         .f = cmd_eeprom_del_parsed,  /* function to call */
132         .data = NULL,      /* 2nd arg of func */
133         .help_str = help_eeprom_del,
134         .tokens = {        /* token list, NULL terminated */
135                 (PGM_P)&cmd_eeprom_del_cmd,
136                 (PGM_P)&cmd_eeprom_del_action,
137                 (PGM_P)&cmd_eeprom_del_num,
138                 NULL,
139         },
140 };
141
142 /* ************* */
143
144 struct cmd_eeprom_add_result {
145         fixed_string_t cmd;
146         fixed_string_t action;
147         uint8_t n;
148 };
149
150 static void cmd_eeprom_add_parsed(void *parsed_result,
151                                  void *data)
152 {
153         struct cmd_eeprom_add_result *res = parsed_result;
154         struct rdline rdl;
155         const char *buffer;
156         int8_t ret;
157         int16_t c;
158
159         rdline_init(&rdl, cmdline_write_char, NULL, NULL);
160         rdline_newline(&rdl, "> ");
161
162         while (1) {
163                 c = cmdline_dev_recv(NULL);
164                 if (c < 0)
165                         continue;
166
167                 ret = rdline_char_in(&rdl, c);
168                 if (ret == -2) {
169                         printf_P(PSTR("abort\n"));
170                         return;
171                 }
172                 if (ret == 1)
173                         break;
174         }
175
176         buffer = rdline_get_buffer(&rdl);
177         if (data == NULL)
178                 eeprom_insert_cmd_before(buffer, res->n);
179         else
180                 eeprom_append_cmd(buffer);
181         eeprom_dump_cmds();
182 }
183
184 const char PROGMEM str_eeprom_add_eeprom[] = "eeprom";
185 const parse_token_string_t PROGMEM cmd_eeprom_add_cmd =
186         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_add_result, cmd,
187                                  str_eeprom_add_eeprom);
188 const char PROGMEM str_eeprom_add_add[] = "add";
189 const parse_token_string_t PROGMEM cmd_eeprom_add_action =
190         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_add_result, action,
191                                  str_eeprom_add_add);
192 const parse_token_num_t PROGMEM cmd_eeprom_add_num =
193         TOKEN_NUM_INITIALIZER(struct cmd_eeprom_add_result, n,
194                               UINT8);
195
196 const char PROGMEM help_eeprom_add[] = "insert an eeprom init command";
197 const parse_inst_t PROGMEM cmd_eeprom_add = {
198         .f = cmd_eeprom_add_parsed,  /* function to call */
199         .data = NULL,      /* 2nd arg of func */
200         .help_str = help_eeprom_add,
201         .tokens = {        /* token list, NULL terminated */
202                 (PGM_P)&cmd_eeprom_add_cmd,
203                 (PGM_P)&cmd_eeprom_add_action,
204                 (PGM_P)&cmd_eeprom_add_num,
205                 NULL,
206         },
207 };
208
209 const char PROGMEM help_eeprom_add2[] = "append an eeprom init command";
210 const parse_inst_t PROGMEM cmd_eeprom_add2 = {
211         .f = cmd_eeprom_add_parsed,  /* function to call */
212         .data = (void *)1,      /* 2nd arg of func */
213         .help_str = help_eeprom_add2,
214         .tokens = {        /* token list, NULL terminated */
215                 (PGM_P)&cmd_eeprom_add_cmd,
216                 (PGM_P)&cmd_eeprom_add_action,
217                 NULL,
218         },
219 };
220
221 /* ************* */
222
223 struct cmd_eeprom_list_result {
224         fixed_string_t cmd;
225         fixed_string_t action;
226 };
227
228 static void cmd_eeprom_list_parsed(void *parsed_result,
229                                 void *data)
230 {
231         struct cmd_eeprom_list_result *res = parsed_result;
232
233         (void)data;
234         if (!strcmp_P(PSTR("list"), res->cmd))
235                 eeprom_dump_cmds();
236         else
237                 eeprom_reset();
238 }
239
240 const char PROGMEM str_eeprom_list_eeprom[] = "eeprom";
241 const parse_token_string_t PROGMEM cmd_eeprom_list_cmd =
242         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_list_result, cmd,
243                                  str_eeprom_list_eeprom);
244 const char PROGMEM str_eeprom_list_list[] = "list#reset";
245 const parse_token_string_t PROGMEM cmd_eeprom_list_action =
246         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_list_result, action,
247                                  str_eeprom_list_list);
248
249 const char PROGMEM help_eeprom_list[] = "list all eeprom init commands";
250 const parse_inst_t PROGMEM cmd_eeprom_list = {
251         .f = cmd_eeprom_list_parsed,  /* function to call */
252         .data = NULL,      /* 2nd arg of func */
253         .help_str = help_eeprom_list,
254         .tokens = {        /* token list, NULL terminated */
255                 (PGM_P)&cmd_eeprom_list_cmd,
256                 (PGM_P)&cmd_eeprom_list_action,
257                 NULL,
258         },
259 };
260
261 /* ************* */
262
263 struct cmd_gps_show_result {
264         fixed_string_t cmd;
265         fixed_string_t action;
266 };
267
268 static void cmd_gps_show_parsed(void *parsed_result,
269                                 void *data)
270 {
271         (void)parsed_result;
272         (void)data;
273
274         while (!cmdline_keypressed()) {
275                 gps_log(1);
276         }
277 }
278
279 const char PROGMEM str_gps_show_gps[] = "gps";
280 const parse_token_string_t PROGMEM cmd_gps_show_cmd =
281         TOKEN_STRING_INITIALIZER(struct cmd_gps_show_result, cmd,
282                                  str_gps_show_gps);
283 const char PROGMEM str_gps_show_show[] = "show";
284 const parse_token_string_t PROGMEM cmd_gps_show_action =
285         TOKEN_STRING_INITIALIZER(struct cmd_gps_show_result, action,
286                                  str_gps_show_show);
287
288 const char PROGMEM help_gps_show[] = "show gps position";
289 const parse_inst_t PROGMEM cmd_gps_show = {
290         .f = cmd_gps_show_parsed,  /* function to call */
291         .data = NULL,      /* 2nd arg of func */
292         .help_str = help_gps_show,
293         .tokens = {        /* token show, NULL terminated */
294                 (PGM_P)&cmd_gps_show_cmd,
295                 (PGM_P)&cmd_gps_show_action,
296                 NULL,
297         },
298 };
299
300 /* ************* */
301
302 struct cmd_imu_show_result {
303         fixed_string_t cmd;
304         fixed_string_t action;
305 };
306
307 static void cmd_imu_show_parsed(void *parsed_result,
308                                 void *data)
309 {
310         (void)parsed_result;
311         (void)data;
312
313         while (!cmdline_keypressed()) {
314                 imu_log(1);
315         }
316 }
317
318 const char PROGMEM str_imu_show_imu[] = "imu";
319 const parse_token_string_t PROGMEM cmd_imu_show_cmd =
320         TOKEN_STRING_INITIALIZER(struct cmd_imu_show_result, cmd,
321                                  str_imu_show_imu);
322 const char PROGMEM str_imu_show_show[] = "show";
323 const parse_token_string_t PROGMEM cmd_imu_show_action =
324         TOKEN_STRING_INITIALIZER(struct cmd_imu_show_result, action,
325                                  str_imu_show_show);
326
327 const char PROGMEM help_imu_show[] = "show imu position";
328 const parse_inst_t PROGMEM cmd_imu_show = {
329         .f = cmd_imu_show_parsed,  /* function to call */
330         .data = NULL,      /* 2nd arg of func */
331         .help_str = help_imu_show,
332         .tokens = {        /* token show, NULL terminated */
333                 (PGM_P)&cmd_imu_show_cmd,
334                 (PGM_P)&cmd_imu_show_action,
335                 NULL,
336         },
337 };
338
339
340 /* ************* */
341
342 /* in progmem */
343 const parse_ctx_t PROGMEM main_ctx[] = {
344
345         /* commands_gen.c */
346         &cmd_reset,
347         &cmd_bootloader,
348         &cmd_log,
349         &cmd_log_show,
350         &cmd_log_type,
351         &cmd_stack_space,
352         &cmd_callout,
353         &cmd_test_eeprom_config,
354         &cmd_eeprom_del,
355         &cmd_eeprom_add,
356         &cmd_eeprom_add2,
357         &cmd_eeprom_list,
358         &cmd_gps_show,
359         &cmd_imu_show,
360         NULL,
361 };