2 * Copyright Droids Corporation
3 * Olivier Matz <zer0@droids-corp.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * Revision : $Id: cmdline.c,v 1.7 2009-11-08 17:24:33 zer0 Exp $
27 #include <aversive/error.h>
36 #include <clock_time.h>
40 #include <control_system_manager.h>
41 #include <trajectory_manager.h>
42 #include <vect_base.h>
45 #include <obstacle_avoidance.h>
46 #include <blocking_detection_manager.h>
47 #include <robot_system.h>
48 #include <position_manager.h>
52 #include "strat_base.h"
55 /******** See in commands.c for the list of commands. */
56 extern parse_pgm_ctx_t main_ctx[];
58 static void write_char(char c)
60 uart_send(CMDLINE_UART, c);
64 valid_buffer(const char *buf, uint8_t size)
68 /* reset CTRL-C for trajectory interruption each time we
69 * receive a new command */
70 interrupt_traj_reset();
72 ret = parse(main_ctx, buf);
73 if (ret == PARSE_AMBIGUOUS)
74 printf_P(PSTR("Ambiguous command\r\n"));
75 else if (ret == PARSE_NOMATCH)
76 printf_P(PSTR("Command not found\r\n"));
77 else if (ret == PARSE_BAD_ARGS)
78 printf_P(PSTR("Bad arguments\r\n"));
82 complete_buffer(const char *buf, char *dstbuf, uint8_t dstsize,
85 return complete(main_ctx, buf, state, dstbuf, dstsize);
89 /* sending "pop" on cmdline uart resets the robot */
90 void emergency(char c)
94 /* interrupt traj here */
98 if ((i == 0 && c == 'p') ||
99 (i == 1 && c == 'o') ||
100 (i == 2 && c == 'p'))
102 else if ( !(i == 1 && c == 'p') )
112 /* log function, add a command to configure
114 void mylog(struct error * e, ...)
118 u16 stream_flags = stdout->flags;
123 if (e->severity > ERROR_SEVERITY_ERROR) {
124 if (gen.log_level < e->severity)
127 for (i=0; i<NB_LOGS+1; i++)
128 if (gen.logs[i] == e->err_num)
135 tv = time_get_time();
136 printf_P(PSTR("%d.%.3d: "), (int)tv.s, (int)(tv.us/1000UL));
138 printf_P(PSTR("(%d,%d,%d) "),
139 position_get_x_s16(&mainboard.pos),
140 position_get_y_s16(&mainboard.pos),
141 position_get_a_deg_s16(&mainboard.pos));
143 vfprintf_P(stdout, e->text, ap);
144 printf_P(PSTR("\r\n"));
147 stdout->flags = stream_flags;
151 int cmdline_interact(void)
153 const char *history, *buffer;
154 int8_t ret, same = 0;
157 rdline_init(&gen.rdl, write_char, valid_buffer, complete_buffer);
158 snprintf(gen.prompt, sizeof(gen.prompt), "mainboard > ");
159 rdline_newline(&gen.rdl, gen.prompt);
162 c = uart_recv_nowait(CMDLINE_UART);
165 ret = rdline_char_in(&gen.rdl, c);
166 if (ret != 2 && ret != 0) {
167 buffer = rdline_get_buffer(&gen.rdl);
168 history = rdline_get_history_item(&gen.rdl, 0);
170 same = !memcmp(buffer, history, strlen(history)) &&
171 buffer[strlen(history)] == '\n';
175 if (strlen(buffer) > 1 && !same)
176 rdline_add_history(&gen.rdl, buffer);
177 rdline_newline(&gen.rdl, gen.prompt);