c42eed4cdc71a8e4f522fd417fd76ddace7aec25
[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 <xbee.h>
39 #include <callout.h>
40
41 #include "main.h"
42 #include "cmdline.h"
43 #include "eeprom_config.h"
44 #include "gps.h"
45 #include "imu.h"
46
47 /* commands_gen.c */
48 extern const parse_inst_t PROGMEM cmd_reset;
49 extern const parse_inst_t PROGMEM cmd_bootloader;
50 extern const parse_inst_t PROGMEM cmd_log;
51 extern const parse_inst_t PROGMEM cmd_log_show;
52 extern const parse_inst_t PROGMEM cmd_log_type;
53 extern const parse_inst_t PROGMEM cmd_stack_space;
54 extern const parse_inst_t PROGMEM cmd_callout;
55
56 /**********************************************************/
57
58 /* this structure is filled when cmd_test_eeprom_config is parsed successfully */
59 struct cmd_test_eeprom_config_result {
60         fixed_string_t arg0;
61 };
62
63 static void cmd_test_eeprom_config_parsed(void *parsed_result, void *data)
64 {
65         (void)parsed_result;
66         (void)data;
67
68         eeprom_dump_cmds();
69         eeprom_append_cmd("salut1\n");
70         eeprom_dump_cmds();
71         eeprom_append_cmd("salut2\n");
72         eeprom_append_cmd("salut3\n");
73         eeprom_append_cmd("salut4\n");
74         eeprom_dump_cmds();
75         eeprom_insert_cmd_before("coin\n", 0);
76         eeprom_insert_cmd_before("coin2\n", 2);
77         eeprom_dump_cmds();
78         eeprom_delete_cmd(2);
79         eeprom_delete_cmd(0);
80         eeprom_dump_cmds();
81 }
82
83 const char PROGMEM str_test_eeprom_config_arg0[] = "test_eeprom_config";
84 const parse_token_string_t PROGMEM cmd_test_eeprom_config_arg0 =
85         TOKEN_STRING_INITIALIZER(struct cmd_test_eeprom_config_result, arg0,
86                                  str_test_eeprom_config_arg0);
87
88 const char PROGMEM help_test_eeprom_config[] = "Test the eeprom configuration";
89 const parse_inst_t PROGMEM cmd_test_eeprom_config = {
90         .f = cmd_test_eeprom_config_parsed,  /* function to call */
91         .data = NULL,      /* 2nd arg of func */
92         .help_str = help_test_eeprom_config,
93         .tokens = {        /* token list, NULL terminated */
94                 (PGM_P)&cmd_test_eeprom_config_arg0,
95                 NULL,
96         },
97 };
98
99 /* ************* */
100
101 struct cmd_eeprom_del_result {
102         fixed_string_t cmd;
103         fixed_string_t action;
104         uint8_t n;
105 };
106
107 static void cmd_eeprom_del_parsed(void *parsed_result,
108                                 void *data)
109 {
110         struct cmd_eeprom_del_result *res = parsed_result;
111
112         (void)data;
113         if (eeprom_delete_cmd(res->n) < 0)
114                 printf_P(PSTR("cannot delete command\n"));
115         eeprom_dump_cmds();
116 }
117
118 const char PROGMEM str_eeprom_del_eeprom[] = "eeprom";
119 const parse_token_string_t PROGMEM cmd_eeprom_del_cmd =
120         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_del_result, cmd,
121                                  str_eeprom_del_eeprom);
122 const char PROGMEM str_eeprom_del_del[] = "del";
123 const parse_token_string_t PROGMEM cmd_eeprom_del_action =
124         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_del_result, action,
125                                  str_eeprom_del_del);
126 const parse_token_num_t PROGMEM cmd_eeprom_del_num =
127         TOKEN_NUM_INITIALIZER(struct cmd_eeprom_del_result, n,
128                               UINT8);
129
130 const char PROGMEM help_eeprom_del[] = "delete an eeprom init command";
131 const parse_inst_t PROGMEM cmd_eeprom_del = {
132         .f = cmd_eeprom_del_parsed,  /* function to call */
133         .data = NULL,      /* 2nd arg of func */
134         .help_str = help_eeprom_del,
135         .tokens = {        /* token list, NULL terminated */
136                 (PGM_P)&cmd_eeprom_del_cmd,
137                 (PGM_P)&cmd_eeprom_del_action,
138                 (PGM_P)&cmd_eeprom_del_num,
139                 NULL,
140         },
141 };
142
143 /* ************* */
144
145 struct cmd_eeprom_add_result {
146         fixed_string_t cmd;
147         fixed_string_t action;
148         uint8_t n;
149 };
150
151 static void cmd_eeprom_add_parsed(void *parsed_result,
152                                  void *data)
153 {
154         struct cmd_eeprom_add_result *res = parsed_result;
155         struct rdline rdl;
156         const char *buffer;
157         int8_t ret;
158         int16_t c;
159
160         rdline_init(&rdl, cmdline_write_char, NULL, NULL);
161         rdline_newline(&rdl, "> ");
162
163         while (1) {
164                 c = cmdline_dev_recv(NULL);
165                 if (c < 0)
166                         continue;
167
168                 ret = rdline_char_in(&rdl, c);
169                 if (ret == -2) {
170                         printf_P(PSTR("abort\n"));
171                         return;
172                 }
173                 if (ret == 1)
174                         break;
175         }
176
177         buffer = rdline_get_buffer(&rdl);
178         if (data == NULL)
179                 eeprom_insert_cmd_before(buffer, res->n);
180         else
181                 eeprom_append_cmd(buffer);
182         eeprom_dump_cmds();
183 }
184
185 const char PROGMEM str_eeprom_add_eeprom[] = "eeprom";
186 const parse_token_string_t PROGMEM cmd_eeprom_add_cmd =
187         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_add_result, cmd,
188                                  str_eeprom_add_eeprom);
189 const char PROGMEM str_eeprom_add_add[] = "add";
190 const parse_token_string_t PROGMEM cmd_eeprom_add_action =
191         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_add_result, action,
192                                  str_eeprom_add_add);
193 const parse_token_num_t PROGMEM cmd_eeprom_add_num =
194         TOKEN_NUM_INITIALIZER(struct cmd_eeprom_add_result, n,
195                               UINT8);
196
197 const char PROGMEM help_eeprom_add[] = "insert an eeprom init command";
198 const parse_inst_t PROGMEM cmd_eeprom_add = {
199         .f = cmd_eeprom_add_parsed,  /* function to call */
200         .data = NULL,      /* 2nd arg of func */
201         .help_str = help_eeprom_add,
202         .tokens = {        /* token list, NULL terminated */
203                 (PGM_P)&cmd_eeprom_add_cmd,
204                 (PGM_P)&cmd_eeprom_add_action,
205                 (PGM_P)&cmd_eeprom_add_num,
206                 NULL,
207         },
208 };
209
210 const char PROGMEM help_eeprom_add2[] = "append an eeprom init command";
211 const parse_inst_t PROGMEM cmd_eeprom_add2 = {
212         .f = cmd_eeprom_add_parsed,  /* function to call */
213         .data = (void *)1,      /* 2nd arg of func */
214         .help_str = help_eeprom_add2,
215         .tokens = {        /* token list, NULL terminated */
216                 (PGM_P)&cmd_eeprom_add_cmd,
217                 (PGM_P)&cmd_eeprom_add_action,
218                 NULL,
219         },
220 };
221
222 /* ************* */
223
224 struct cmd_eeprom_list_result {
225         fixed_string_t cmd;
226         fixed_string_t action;
227 };
228
229 static void cmd_eeprom_list_parsed(void *parsed_result,
230                                 void *data)
231 {
232         struct cmd_eeprom_list_result *res = parsed_result;
233
234         (void)data;
235         if (!strcmp_P(PSTR("list"), res->cmd))
236                 eeprom_dump_cmds();
237         else
238                 eeprom_reset();
239 }
240
241 const char PROGMEM str_eeprom_list_eeprom[] = "eeprom";
242 const parse_token_string_t PROGMEM cmd_eeprom_list_cmd =
243         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_list_result, cmd,
244                                  str_eeprom_list_eeprom);
245 const char PROGMEM str_eeprom_list_list[] = "list#reset";
246 const parse_token_string_t PROGMEM cmd_eeprom_list_action =
247         TOKEN_STRING_INITIALIZER(struct cmd_eeprom_list_result, action,
248                                  str_eeprom_list_list);
249
250 const char PROGMEM help_eeprom_list[] = "list all eeprom init commands";
251 const parse_inst_t PROGMEM cmd_eeprom_list = {
252         .f = cmd_eeprom_list_parsed,  /* function to call */
253         .data = NULL,      /* 2nd arg of func */
254         .help_str = help_eeprom_list,
255         .tokens = {        /* token list, NULL terminated */
256                 (PGM_P)&cmd_eeprom_list_cmd,
257                 (PGM_P)&cmd_eeprom_list_action,
258                 NULL,
259         },
260 };
261
262 /* ************* */
263
264 struct cmd_gps_show_result {
265         fixed_string_t cmd;
266         fixed_string_t action;
267 };
268
269 static void cmd_gps_show_parsed(void *parsed_result,
270                                 void *data)
271 {
272         (void)parsed_result;
273         (void)data;
274
275         while (!cmdline_keypressed()) {
276                 gps_log(1);
277         }
278 }
279
280 const char PROGMEM str_gps_show_gps[] = "gps";
281 const parse_token_string_t PROGMEM cmd_gps_show_cmd =
282         TOKEN_STRING_INITIALIZER(struct cmd_gps_show_result, cmd,
283                                  str_gps_show_gps);
284 const char PROGMEM str_gps_show_show[] = "show";
285 const parse_token_string_t PROGMEM cmd_gps_show_action =
286         TOKEN_STRING_INITIALIZER(struct cmd_gps_show_result, action,
287                                  str_gps_show_show);
288
289 const char PROGMEM help_gps_show[] = "show gps position";
290 const parse_inst_t PROGMEM cmd_gps_show = {
291         .f = cmd_gps_show_parsed,  /* function to call */
292         .data = NULL,      /* 2nd arg of func */
293         .help_str = help_gps_show,
294         .tokens = {        /* token show, NULL terminated */
295                 (PGM_P)&cmd_gps_show_cmd,
296                 (PGM_P)&cmd_gps_show_action,
297                 NULL,
298         },
299 };
300
301 /* ************* */
302
303 struct cmd_imu_show_result {
304         fixed_string_t cmd;
305         fixed_string_t action;
306 };
307
308 static void cmd_imu_show_parsed(void *parsed_result,
309                                 void *data)
310 {
311         (void)parsed_result;
312         (void)data;
313
314         while (!cmdline_keypressed()) {
315                 imu_log(1);
316         }
317 }
318
319 const char PROGMEM str_imu_show_imu[] = "imu";
320 const parse_token_string_t PROGMEM cmd_imu_show_cmd =
321         TOKEN_STRING_INITIALIZER(struct cmd_imu_show_result, cmd,
322                                  str_imu_show_imu);
323 const char PROGMEM str_imu_show_show[] = "show";
324 const parse_token_string_t PROGMEM cmd_imu_show_action =
325         TOKEN_STRING_INITIALIZER(struct cmd_imu_show_result, action,
326                                  str_imu_show_show);
327
328 const char PROGMEM help_imu_show[] = "show imu position";
329 const parse_inst_t PROGMEM cmd_imu_show = {
330         .f = cmd_imu_show_parsed,  /* function to call */
331         .data = NULL,      /* 2nd arg of func */
332         .help_str = help_imu_show,
333         .tokens = {        /* token show, NULL terminated */
334                 (PGM_P)&cmd_imu_show_cmd,
335                 (PGM_P)&cmd_imu_show_action,
336                 NULL,
337         },
338 };
339
340
341 /* ************* */
342
343 /* in progmem */
344 const parse_ctx_t PROGMEM main_ctx[] = {
345
346         /* commands_gen.c */
347         &cmd_reset,
348         &cmd_bootloader,
349         &cmd_log,
350         &cmd_log_show,
351         &cmd_log_type,
352         &cmd_stack_space,
353         &cmd_callout,
354         &cmd_test_eeprom_config,
355         &cmd_eeprom_del,
356         &cmd_eeprom_add,
357         &cmd_eeprom_add2,
358         &cmd_eeprom_list,
359         &cmd_gps_show,
360         &cmd_imu_show,
361         NULL,
362 };