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.5 2009-05-02 10:08:09 zer0 Exp $
27 #include <aversive/error.h>
33 #include <clock_time.h>
37 #include <control_system_manager.h>
38 #include <trajectory_manager.h>
39 #include <blocking_detection_manager.h>
40 #include <robot_system.h>
41 #include <position_manager.h>
44 #include "strat_base.h"
48 /******** See in commands.c for the list of commands. */
49 extern parse_pgm_ctx_t main_ctx[];
51 static void write_char(char c)
53 uart_send(CMDLINE_UART, c);
57 valid_buffer(const char *buf, uint8_t size)
61 /* reset CTRL-C for trajectory interruption each time we
62 * receive a new command */
63 interrupt_traj_reset();
65 ret = parse(main_ctx, buf);
66 if (ret == PARSE_AMBIGUOUS)
67 printf_P(PSTR("Ambiguous command\r\n"));
68 else if (ret == PARSE_NOMATCH)
69 printf_P(PSTR("Command not found\r\n"));
70 else if (ret == PARSE_BAD_ARGS)
71 printf_P(PSTR("Bad arguments\r\n"));
75 complete_buffer(const char *buf, char *dstbuf, uint8_t dstsize,
78 return complete(main_ctx, buf, state, dstbuf, dstsize);
82 /* sending "pop" on cmdline uart resets the robot */
83 void emergency(char c)
87 /* interrupt traj here */
91 if ((i == 0 && c == 'p') ||
92 (i == 1 && c == 'o') ||
95 else if ( !(i == 1 && c == 'p') )
101 /* log function, add a command to configure
103 void mylog(struct error * e, ...)
106 u16 stream_flags = stdout->flags;
110 if (e->severity > ERROR_SEVERITY_ERROR) {
111 if (gen.log_level < e->severity)
114 for (i=0; i<NB_LOGS+1; i++)
115 if (gen.logs[i] == e->err_num)
122 tv = time_get_time();
123 printf_P(PSTR("%ld.%.3ld: "), tv.s, (tv.us/1000UL));
125 printf_P(PSTR("(%d,%d,%d) "),
126 position_get_x_s16(&mainboard.pos),
127 position_get_y_s16(&mainboard.pos),
128 position_get_a_deg_s16(&mainboard.pos));
130 vfprintf_P(stdout, e->text, ap);
131 printf_P(PSTR("\r\n"));
133 stdout->flags = stream_flags;
136 int cmdline_interact(void)
138 const char *history, *buffer;
139 int8_t ret, same = 0;
142 rdline_init(&gen.rdl, write_char, valid_buffer, complete_buffer);
143 snprintf(gen.prompt, sizeof(gen.prompt), "mainboard > ");
144 rdline_newline(&gen.rdl, gen.prompt);
147 c = uart_recv_nowait(CMDLINE_UART);
150 ret = rdline_char_in(&gen.rdl, c);
151 if (ret != 2 && ret != 0) {
152 buffer = rdline_get_buffer(&gen.rdl);
153 history = rdline_get_history_item(&gen.rdl, 0);
155 same = !memcmp(buffer, history, strlen(history)) &&
156 buffer[strlen(history)] == '\n';
160 if (strlen(buffer) > 1 && !same)
161 rdline_add_history(&gen.rdl, buffer);
162 rdline_newline(&gen.rdl, gen.prompt);