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>
34 #include <clock_time.h>
38 #include <control_system_manager.h>
39 #include <trajectory_manager.h>
40 #include <vect_base.h>
43 #include <blocking_detection_manager.h>
44 #include <robot_system.h>
45 #include <position_manager.h>
49 #include "strat_base.h"
52 /******** See in commands.c for the list of commands. */
53 extern parse_pgm_ctx_t main_ctx[];
55 static void write_char(char c)
57 uart_send(CMDLINE_UART, c);
61 valid_buffer(const char *buf, uint8_t size)
65 /* reset CTRL-C for trajectory interruption each time we
66 * receive a new command */
67 interrupt_traj_reset();
69 ret = parse(main_ctx, buf);
70 if (ret == PARSE_AMBIGUOUS)
71 printf_P(PSTR("Ambiguous command\r\n"));
72 else if (ret == PARSE_NOMATCH)
73 printf_P(PSTR("Command not found\r\n"));
74 else if (ret == PARSE_BAD_ARGS)
75 printf_P(PSTR("Bad arguments\r\n"));
79 complete_buffer(const char *buf, char *dstbuf, uint8_t dstsize,
82 return complete(main_ctx, buf, state, dstbuf, dstsize);
85 /* sending "pop" on cmdline uart resets the robot */
86 void emergency(char c)
90 /* interrupt traj here */
94 if ((i == 0 && c == 'p') ||
95 (i == 1 && c == 'o') ||
98 else if ( !(i == 1 && c == 'p') )
106 /* log function, add a command to configure
108 void mylog(struct error * e, ...)
111 // u16 stream_flags = stdout->flags;
115 if (e->severity > ERROR_SEVERITY_ERROR) {
116 if (gen.log_level < e->severity)
119 for (i=0; i<NB_LOGS+1; i++)
120 if (gen.logs[i] == e->err_num)
127 tv = time_get_time();
128 printf_P(PSTR("%d.%.3d: "), (int)tv.s, (int)(tv.us/1000UL));
130 printf_P(PSTR("(%d,%d,%d) "),
131 position_get_x_s16(&mainboard.pos),
132 position_get_y_s16(&mainboard.pos),
133 position_get_a_deg_s16(&mainboard.pos));
135 vfprintf_P(stdout, e->text, ap);
136 printf_P(PSTR("\r\n"));
138 //stdout->flags = stream_flags;
141 int cmdline_interact(void)
143 const char *history, *buffer;
144 int8_t ret, same = 0;
147 rdline_init(&gen.rdl, write_char, valid_buffer, complete_buffer);
148 snprintf(gen.prompt, sizeof(gen.prompt), "mainboard > ");
149 rdline_newline(&gen.rdl, gen.prompt);
152 c = uart_recv_nowait(CMDLINE_UART);
155 ret = rdline_char_in(&gen.rdl, c);
156 if (ret != 2 && ret != 0) {
157 buffer = rdline_get_buffer(&gen.rdl);
158 history = rdline_get_history_item(&gen.rdl, 0);
160 same = !memcmp(buffer, history, strlen(history)) &&
161 buffer[strlen(history)] == '\n';
165 if (strlen(buffer) > 1 && !same)
166 rdline_add_history(&gen.rdl, buffer);
167 rdline_newline(&gen.rdl, gen.prompt);