2 * Copyright Droids Corporation (2008)
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.
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.
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
18 * Revision : $Id: commands_gen.c,v 1.4 2009-05-27 20:04:07 zer0 Exp $
20 * Olivier MATZ <zer0@droids-corp.org>
26 #include <aversive/pgmspace.h>
27 #include <aversive/wait.h>
28 #include <aversive/error.h>
34 #include <encoders_spi.h>
37 #include <scheduler.h>
40 #include <control_system_manager.h>
41 #include <blocking_detection_manager.h>
45 #include <parse_string.h>
46 #include <parse_num.h>
48 #include <diagnostic.h>
54 /**********************************************************/
57 /* this structure is filled when cmd_reset is parsed successfully */
58 struct cmd_reset_result {
62 /* function called when cmd_reset is parsed successfully */
63 static void cmd_reset_parsed(void * parsed_result, void * data)
68 prog_char str_reset_arg0[] = "reset";
69 parse_pgm_token_string_t cmd_reset_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_reset_result, arg0, str_reset_arg0);
71 prog_char help_reset[] = "Reset the board";
72 parse_pgm_inst_t cmd_reset = {
73 .f = cmd_reset_parsed, /* function to call */
74 .data = NULL, /* 2nd arg of func */
75 .help_str = help_reset,
76 .tokens = { /* token list, NULL terminated */
77 (prog_void *)&cmd_reset_arg0,
82 /**********************************************************/
85 /* this structure is filled when cmd_bootloader is parsed successfully */
86 struct cmd_bootloader_result {
90 /* function called when cmd_bootloader is parsed successfully */
91 static void cmd_bootloader_parsed(void *parsed_result, void *data)
96 prog_char str_bootloader_arg0[] = "bootloader";
97 parse_pgm_token_string_t cmd_bootloader_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_bootloader_result, arg0, str_bootloader_arg0);
99 prog_char help_bootloader[] = "Launch the bootloader";
100 parse_pgm_inst_t cmd_bootloader = {
101 .f = cmd_bootloader_parsed, /* function to call */
102 .data = NULL, /* 2nd arg of func */
103 .help_str = help_bootloader,
104 .tokens = { /* token list, NULL terminated */
105 (prog_void *)&cmd_bootloader_arg0,
110 /**********************************************************/
113 /* this structure is filled when cmd_encoders is parsed successfully */
114 struct cmd_encoders_result {
119 /* function called when cmd_encoders is parsed successfully */
120 static void cmd_encoders_parsed(void *parsed_result, void *data)
122 struct cmd_encoders_result *res = parsed_result;
124 if (!strcmp_P(res->arg1, PSTR("reset"))) {
125 encoders_spi_set_value((void *)0, 0);
126 encoders_spi_set_value((void *)1, 0);
127 encoders_spi_set_value((void *)2, 0);
128 encoders_spi_set_value((void *)3, 0);
133 while(!cmdline_keypressed()) {
134 printf_P(PSTR("% .8ld % .8ld % .8ld % .8ld\r\n"),
135 encoders_spi_get_value((void *)0),
136 encoders_spi_get_value((void *)1),
137 encoders_spi_get_value((void *)2),
138 encoders_spi_get_value((void *)3));
143 prog_char str_encoders_arg0[] = "encoders";
144 parse_pgm_token_string_t cmd_encoders_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_encoders_result, arg0, str_encoders_arg0);
145 prog_char str_encoders_arg1[] = "show#reset";
146 parse_pgm_token_string_t cmd_encoders_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_encoders_result, arg1, str_encoders_arg1);
148 prog_char help_encoders[] = "Show encoders values";
149 parse_pgm_inst_t cmd_encoders = {
150 .f = cmd_encoders_parsed, /* function to call */
151 .data = NULL, /* 2nd arg of func */
152 .help_str = help_encoders,
153 .tokens = { /* token list, NULL terminated */
154 (prog_void *)&cmd_encoders_arg0,
155 (prog_void *)&cmd_encoders_arg1,
160 /**********************************************************/
163 /* this structure is filled when cmd_scheduler is parsed successfully */
164 struct cmd_scheduler_result {
169 /* function called when cmd_scheduler is parsed successfully */
170 static void cmd_scheduler_parsed(void *parsed_result, void *data)
172 scheduler_dump_events();
175 prog_char str_scheduler_arg0[] = "scheduler";
176 parse_pgm_token_string_t cmd_scheduler_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_scheduler_result, arg0, str_scheduler_arg0);
177 prog_char str_scheduler_arg1[] = "show";
178 parse_pgm_token_string_t cmd_scheduler_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_scheduler_result, arg1, str_scheduler_arg1);
180 prog_char help_scheduler[] = "Show scheduler events";
181 parse_pgm_inst_t cmd_scheduler = {
182 .f = cmd_scheduler_parsed, /* function to call */
183 .data = NULL, /* 2nd arg of func */
184 .help_str = help_scheduler,
185 .tokens = { /* token list, NULL terminated */
186 (prog_void *)&cmd_scheduler_arg0,
187 (prog_void *)&cmd_scheduler_arg1,
192 /**********************************************************/
195 /* this structure is filled when cmd_pwm is parsed successfully */
196 struct cmd_pwm_result {
202 /* function called when cmd_pwm is parsed successfully */
203 static void cmd_pwm_parsed(void * parsed_result, void * data)
205 void * pwm_ptr = NULL;
206 struct cmd_pwm_result * res = parsed_result;
208 if (!strcmp_P(res->arg1, PSTR("1(4A)")))
209 pwm_ptr = &gen.pwm1_4A;
210 else if (!strcmp_P(res->arg1, PSTR("2(4B)")))
211 pwm_ptr = &gen.pwm2_4B;
212 else if (!strcmp_P(res->arg1, PSTR("3(1A)")))
213 pwm_ptr = &gen.pwm3_1A;
214 else if (!strcmp_P(res->arg1, PSTR("4(1B)")))
215 pwm_ptr = &gen.pwm4_1B;
217 else if (!strcmp_P(res->arg1, PSTR("s1(3C)")))
218 pwm_ptr = &gen.servo1;
219 else if (!strcmp_P(res->arg1, PSTR("s2(5A)")))
220 pwm_ptr = &gen.servo2;
221 else if (!strcmp_P(res->arg1, PSTR("s3(5B)")))
222 pwm_ptr = &gen.servo3;
223 else if (!strcmp_P(res->arg1, PSTR("s3(5C)")))
224 pwm_ptr = &gen.servo4;
227 pwm_ng_set(pwm_ptr, res->arg2);
229 printf_P(PSTR("done\r\n"));
232 prog_char str_pwm_arg0[] = "pwm";
233 parse_pgm_token_string_t cmd_pwm_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_pwm_result, arg0, str_pwm_arg0);
234 prog_char str_pwm_arg1[] = "1(4A)#2(4B)#3(1A)#4(1B)#s1(3C)#s2(5A)#s3(5B)#s4(5C)";
235 parse_pgm_token_string_t cmd_pwm_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_pwm_result, arg1, str_pwm_arg1);
236 parse_pgm_token_num_t cmd_pwm_arg2 = TOKEN_NUM_INITIALIZER(struct cmd_pwm_result, arg2, INT16);
238 prog_char help_pwm[] = "Set pwm values [-4096 ; 4095]";
239 parse_pgm_inst_t cmd_pwm = {
240 .f = cmd_pwm_parsed, /* function to call */
241 .data = NULL, /* 2nd arg of func */
242 .help_str = help_pwm,
243 .tokens = { /* token list, NULL terminated */
244 (prog_void *)&cmd_pwm_arg0,
245 (prog_void *)&cmd_pwm_arg1,
246 (prog_void *)&cmd_pwm_arg2,
251 /**********************************************************/
254 /* this structure is filled when cmd_adc is parsed successfully */
255 struct cmd_adc_result {
260 /* function called when cmd_adc is parsed successfully */
261 static void cmd_adc_parsed(void *parsed_result, void *data)
263 struct cmd_adc_result *res = parsed_result;
266 if (!strcmp_P(res->arg1, PSTR("loop_show")))
270 printf_P(PSTR("ADC values: "));
271 for (i=0; i<ADC_MAX; i++) {
272 printf_P(PSTR("%.4d "), sensor_get_adc(i));
274 printf_P(PSTR("\r\n"));
276 } while (loop && !cmdline_keypressed());
279 prog_char str_adc_arg0[] = "adc";
280 parse_pgm_token_string_t cmd_adc_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_adc_result, arg0, str_adc_arg0);
281 prog_char str_adc_arg1[] = "show#loop_show";
282 parse_pgm_token_string_t cmd_adc_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_adc_result, arg1, str_adc_arg1);
284 prog_char help_adc[] = "Show adc values";
285 parse_pgm_inst_t cmd_adc = {
286 .f = cmd_adc_parsed, /* function to call */
287 .data = NULL, /* 2nd arg of func */
288 .help_str = help_adc,
289 .tokens = { /* token list, NULL terminated */
290 (prog_void *)&cmd_adc_arg0,
291 (prog_void *)&cmd_adc_arg1,
297 /**********************************************************/
300 /* this structure is filled when cmd_sensor is parsed successfully */
301 struct cmd_sensor_result {
306 /* function called when cmd_sensor is parsed successfully */
307 static void cmd_sensor_parsed(void *parsed_result, void *data)
309 struct cmd_sensor_result *res = parsed_result;
312 if (!strcmp_P(res->arg1, PSTR("loop_show")))
316 printf_P(PSTR("SENSOR values: "));
317 for (i=0; i<SENSOR_MAX; i++) {
318 printf_P(PSTR("%d "), !!sensor_get(i));
320 printf_P(PSTR("\r\n"));
322 } while (loop && !cmdline_keypressed());
325 prog_char str_sensor_arg0[] = "sensor";
326 parse_pgm_token_string_t cmd_sensor_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_sensor_result, arg0, str_sensor_arg0);
327 prog_char str_sensor_arg1[] = "show#loop_show";
328 parse_pgm_token_string_t cmd_sensor_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_sensor_result, arg1, str_sensor_arg1);
330 prog_char help_sensor[] = "Show sensor values";
331 parse_pgm_inst_t cmd_sensor = {
332 .f = cmd_sensor_parsed, /* function to call */
333 .data = NULL, /* 2nd arg of func */
334 .help_str = help_sensor,
335 .tokens = { /* token list, NULL terminated */
336 (prog_void *)&cmd_sensor_arg0,
337 (prog_void *)&cmd_sensor_arg1,
343 /**********************************************************/
346 /* this structure is filled when cmd_log is parsed successfully */
347 struct cmd_log_result {
354 /* keep it sync with string choice */
355 static const prog_char uart_log[] = "uart";
356 static const prog_char i2c_log[] = "i2c";
357 static const prog_char i2cproto_log[] = "i2cproto";
358 static const prog_char sensor_log[] = "sensor";
359 static const prog_char block_log[] = "bd";
360 static const prog_char beacon_log[] = "beacon";
361 static const prog_char scanner_log[] = "scanner";
362 static const prog_char imgprocess_log[] = "imgprocess";
364 struct log_name_and_num {
365 const prog_char * name;
369 static const struct log_name_and_num log_name_and_num[] = {
370 { uart_log, E_UART },
372 { i2cproto_log, E_USER_I2C_PROTO },
373 { sensor_log, E_USER_SENSOR },
374 { block_log, E_BLOCKING_DETECTION_MANAGER },
375 { beacon_log, E_USER_BEACON },
376 { scanner_log, E_USER_SCANNER },
377 { imgprocess_log, E_USER_IMGPROCESS },
381 log_name2num(const char * s)
385 for (i=0; i<sizeof(log_name_and_num)/sizeof(struct log_name_and_num); i++) {
386 if (!strcmp_P(s, log_name_and_num[i].name)) {
387 return log_name_and_num[i].num;
394 log_num2name(uint8_t num)
398 for (i=0; i<sizeof(log_name_and_num)/sizeof(struct log_name_and_num); i++) {
399 if (num == log_name_and_num[i].num) {
400 return log_name_and_num[i].name;
406 /* function called when cmd_log is parsed successfully */
407 static void cmd_log_do_show(void)
410 const prog_char * name;
412 printf_P(PSTR("log level is %d\r\n"), gen.log_level);
413 for (i=0; i<NB_LOGS; i++) {
414 name = log_num2name(gen.logs[i]);
416 printf_P(PSTR("log type %S is on\r\n"), name);
421 printf_P(PSTR("no log configured\r\n"), gen.logs[i]);
424 /* function called when cmd_log is parsed successfully */
425 static void cmd_log_parsed(void * parsed_result, void * data)
427 struct cmd_log_result *res = (struct cmd_log_result *) parsed_result;
429 if (!strcmp_P(res->arg1, PSTR("level"))) {
430 gen.log_level = res->arg2;
433 /* else it is a show */
437 prog_char str_log_arg0[] = "log";
438 parse_pgm_token_string_t cmd_log_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_log_result, arg0, str_log_arg0);
439 prog_char str_log_arg1[] = "level";
440 parse_pgm_token_string_t cmd_log_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_log_result, arg1, str_log_arg1);
441 parse_pgm_token_num_t cmd_log_arg2 = TOKEN_NUM_INITIALIZER(struct cmd_log_result, arg2, INT32);
443 prog_char help_log[] = "Set log options: level (0 -> 5)";
444 parse_pgm_inst_t cmd_log = {
445 .f = cmd_log_parsed, /* function to call */
446 .data = NULL, /* 2nd arg of func */
447 .help_str = help_log,
448 .tokens = { /* token list, NULL terminated */
449 (prog_void *)&cmd_log_arg0,
450 (prog_void *)&cmd_log_arg1,
451 (prog_void *)&cmd_log_arg2,
456 prog_char str_log_arg1_show[] = "show";
457 parse_pgm_token_string_t cmd_log_arg1_show = TOKEN_STRING_INITIALIZER(struct cmd_log_result, arg1, str_log_arg1_show);
459 prog_char help_log_show[] = "Show configured logs";
460 parse_pgm_inst_t cmd_log_show = {
461 .f = cmd_log_parsed, /* function to call */
462 .data = NULL, /* 2nd arg of func */
463 .help_str = help_log_show,
464 .tokens = { /* token list, NULL terminated */
465 (prog_void *)&cmd_log_arg0,
466 (prog_void *)&cmd_log_arg1_show,
471 /* this structure is filled when cmd_log is parsed successfully */
472 struct cmd_log_type_result {
479 /* function called when cmd_log is parsed successfully */
480 static void cmd_log_type_parsed(void * parsed_result, void * data)
482 struct cmd_log_type_result *res = (struct cmd_log_type_result *) parsed_result;
486 lognum = log_name2num(res->arg2);
488 printf_P(PSTR("Cannot find log num\r\n"));
492 if (!strcmp_P(res->arg3, PSTR("on"))) {
493 for (i=0; i<NB_LOGS; i++) {
494 if (gen.logs[i] == lognum) {
495 printf_P(PSTR("Already on\r\n"));
499 for (i=0; i<NB_LOGS; i++) {
500 if (gen.logs[i] == 0) {
501 gen.logs[i] = lognum;
506 printf_P(PSTR("no more room\r\n"));
509 else if (!strcmp_P(res->arg3, PSTR("off"))) {
510 for (i=0; i<NB_LOGS; i++) {
511 if (gen.logs[i] == lognum) {
517 printf_P(PSTR("already off\r\n"));
523 prog_char str_log_arg1_type[] = "type";
524 parse_pgm_token_string_t cmd_log_arg1_type = TOKEN_STRING_INITIALIZER(struct cmd_log_type_result, arg1, str_log_arg1_type);
525 /* keep it sync with log_name_and_num above */
526 prog_char str_log_arg2_type[] = "uart#i2c#i2cproto#sensor#bd#beacon#scanner#imgprocess";
527 parse_pgm_token_string_t cmd_log_arg2_type = TOKEN_STRING_INITIALIZER(struct cmd_log_type_result, arg2, str_log_arg2_type);
528 prog_char str_log_arg3[] = "on#off";
529 parse_pgm_token_string_t cmd_log_arg3 = TOKEN_STRING_INITIALIZER(struct cmd_log_type_result, arg3, str_log_arg3);
531 prog_char help_log_type[] = "Set log type";
532 parse_pgm_inst_t cmd_log_type = {
533 .f = cmd_log_type_parsed, /* function to call */
534 .data = NULL, /* 2nd arg of func */
535 .help_str = help_log_type,
536 .tokens = { /* token list, NULL terminated */
537 (prog_void *)&cmd_log_arg0,
538 (prog_void *)&cmd_log_arg1_type,
539 (prog_void *)&cmd_log_arg2_type,
540 (prog_void *)&cmd_log_arg3,
546 /**********************************************************/
549 /* this structure is filled when cmd_stack_space is parsed successfully */
550 struct cmd_stack_space_result {
554 /* function called when cmd_stack_space is parsed successfully */
555 static void cmd_stack_space_parsed(void *parsed_result, void *data)
557 printf("res stack: %d\r\n", min_stack_space_available());
561 prog_char str_stack_space_arg0[] = "stack_space";
562 parse_pgm_token_string_t cmd_stack_space_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_stack_space_result, arg0, str_stack_space_arg0);
564 prog_char help_stack_space[] = "Display remaining stack space";
565 parse_pgm_inst_t cmd_stack_space = {
566 .f = cmd_stack_space_parsed, /* function to call */
567 .data = NULL, /* 2nd arg of func */
568 .help_str = help_stack_space,
569 .tokens = { /* token list, NULL terminated */
570 (prog_void *)&cmd_stack_space_arg0,