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>
28 #include <aversive/queue.h>
39 extern const parse_ctx_t PROGMEM main_ctx[];
42 int cmdline_dev_send(char c, FILE* f)
45 uart_send(CMDLINE_UART, c);
49 int cmdline_dev_recv(FILE* f)
54 c = uart_recv_nowait(CMDLINE_UART);
62 int xbee_dev_send(char c, FILE* f)
65 uart_send(XBEE_UART, c);
69 int xbee_dev_recv(FILE* f)
74 c = uart_recv_nowait(XBEE_UART);
81 void cmdline_valid_buffer(const char *buf, uint8_t size)
84 PGM_P ctx = (PGM_P)main_ctx;
87 ret = parse(ctx, buf);
88 if (ret == PARSE_AMBIGUOUS)
89 printf_P(PSTR("Ambiguous command\r\n"));
90 else if (ret == PARSE_NOMATCH)
91 printf_P(PSTR("Command not found\r\n"));
92 else if (ret == PARSE_BAD_ARGS)
93 printf_P(PSTR("Bad arguments\r\n"));
97 complete_buffer(const char *buf, char *dstbuf, uint8_t dstsize,
100 PGM_P ctx = (PGM_P)main_ctx;
101 return complete(ctx, buf, state, dstbuf, dstsize);
105 void cmdline_write_char(char c)
107 cmdline_dev_send(c, NULL);
111 void cmdline_init(void)
113 rdline_init(&xbeeboard.rdl, cmdline_write_char, cmdline_valid_buffer, complete_buffer);
114 snprintf_P(xbeeboard.prompt, sizeof(xbeeboard.prompt), PSTR("mainboard > "));
118 /* sending "pop" on cmdline uart resets the robot */
119 void emergency(char c)
121 static uint8_t i = 0;
123 if ((i == 0 && c == 'p') ||
124 (i == 1 && c == 'o') ||
125 (i == 2 && c == 'p'))
127 else if ( !(i == 1 && c == 'p') )
135 /* log function, add a command to configure
137 void mylog(struct error * e, ...)
140 u16 stream_flags = stdout->flags;
146 if (e->severity > ERROR_SEVERITY_ERROR) {
147 if (xbeeboard.log_level < e->severity)
150 for (i=0; i<NB_LOGS+1; i++)
151 if (xbeeboard.logs[i] == e->err_num)
162 printf_P(PSTR("%d.%.3d: "), (int)(ms/1000UL), (int)(ms%1000UL));
164 vfprintf_P(stdout, e->text, ap);
165 printf_P(PSTR("\r\n"));
168 stdout->flags = stream_flags;
172 int cmdline_poll(void)
174 const char *history, *buffer;
175 int8_t ret, same = 0;
178 c = cmdline_dev_recv(NULL);
182 ret = rdline_char_in(&xbeeboard.rdl, c);
184 buffer = rdline_get_buffer(&xbeeboard.rdl);
185 history = rdline_get_history_item(&xbeeboard.rdl, 0);
187 same = !memcmp(buffer, history, strlen(history)) &&
188 buffer[strlen(history)] == '\n';
192 if (strlen(buffer) > 1 && !same)
193 rdline_add_history(&xbeeboard.rdl, buffer);
195 if (xbeeboard.rdl.status != RDLINE_STOPPED)
196 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);