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.3 2009-04-24 19:30:42 zer0 Exp $
27 #include <aversive/error.h>
38 #include <control_system_manager.h>
39 #include <blocking_detection_manager.h>
45 /******** See in commands.c for the list of commands. */
46 extern parse_pgm_ctx_t main_ctx[];
48 static void write_char(char c)
50 uart_send(CMDLINE_UART, c);
54 valid_buffer(const char *buf, __attribute__((unused)) uint8_t size)
57 ret = parse(main_ctx, buf);
58 if (ret == PARSE_AMBIGUOUS)
59 printf_P(PSTR("Ambiguous command\r\n"));
60 else if (ret == PARSE_NOMATCH)
61 printf_P(PSTR("Command not found\r\n"));
62 else if (ret == PARSE_BAD_ARGS)
63 printf_P(PSTR("Bad arguments\r\n"));
67 complete_buffer(const char *buf, char *dstbuf, uint8_t dstsize,
70 return complete(main_ctx, buf, state, dstbuf, dstsize);
74 /* sending "pop" on cmdline uart resets the robot */
75 void emergency(char c)
79 if ((i == 0 && c == 'p') ||
80 (i == 1 && c == 'o') ||
83 else if ( !(i == 1 && c == 'p') )
89 /* log function, add a command to configure
91 void mylog(struct error * e, ...)
94 u16 stream_flags = stdout->flags;
98 if (e->severity > ERROR_SEVERITY_ERROR) {
99 if (gen.log_level < e->severity)
102 for (i=0; i<NB_LOGS+1; i++)
103 if (gen.logs[i] == e->err_num)
110 tv = time_get_time();
111 printf_P(PSTR("%ld.%.3ld: "), tv.s, (tv.us/1000UL));
112 vfprintf_P(stdout, e->text, ap);
113 printf_P(PSTR("\r\n"));
115 stdout->flags = stream_flags;
118 int cmdline_interact(void)
120 const char *history, *buffer;
121 int8_t ret, same = 0;
124 rdline_init(&gen.rdl, write_char, valid_buffer, complete_buffer);
125 snprintf(gen.prompt, sizeof(gen.prompt), "mechboard > ");
126 rdline_newline(&gen.rdl, gen.prompt);
129 c = uart_recv_nowait(CMDLINE_UART);
132 ret = rdline_char_in(&gen.rdl, c);
133 if (ret != 2 && ret != 0) {
134 buffer = rdline_get_buffer(&gen.rdl);
135 history = rdline_get_history_item(&gen.rdl, 0);
137 same = !memcmp(buffer, history, strlen(history)) &&
138 buffer[strlen(history)] == '\n';
142 if (strlen(buffer) > 1 && !same)
143 rdline_add_history(&gen.rdl, buffer);
144 rdline_newline(&gen.rdl, gen.prompt);