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>
33 #include <clock_time.h>
41 #include "DualVirtualSerial.h"
45 extern parse_pgm_ctx_t main_ctx[];
47 int cmdline_dev_send(char c, FILE* f)
50 CDC_Device_SendByte(&VirtualSerial1_CDC_Interface, (uint8_t)c);
52 uart_send(CMDLINE_UART, c);
57 int cmdline_dev_recv(FILE* f)
62 c = CDC_Device_ReceiveByte(&VirtualSerial1_CDC_Interface);
64 c = uart_recv_nowait(CMDLINE_UART);
73 int xbee_dev_send(char c, FILE* f)
76 CDC_Device_SendByte(&VirtualSerial2_CDC_Interface, (uint8_t)c);
78 uart_send(XBEE_UART, c);
83 int xbee_dev_recv(FILE* f)
89 c = CDC_Device_ReceiveByte(&VirtualSerial2_CDC_Interface);
91 c = uart_recv_nowait(XBEE_UART);
100 valid_buffer(const char *buf, uint8_t size)
104 ret = parse(main_ctx, buf);
105 if (ret == PARSE_AMBIGUOUS)
106 printf_P(PSTR("Ambiguous command\r\n"));
107 else if (ret == PARSE_NOMATCH)
108 printf_P(PSTR("Command not found\r\n"));
109 else if (ret == PARSE_BAD_ARGS)
110 printf_P(PSTR("Bad arguments\r\n"));
114 complete_buffer(const char *buf, char *dstbuf, uint8_t dstsize,
117 return complete(main_ctx, buf, state, dstbuf, dstsize);
121 static void write_char(char c)
123 cmdline_dev_send(c, NULL);
127 void cmdline_init(void)
129 rdline_init(&xbeeboard.rdl, write_char, valid_buffer, complete_buffer);
130 snprintf_P(xbeeboard.prompt, sizeof(xbeeboard.prompt), PSTR("mainboard > "));
134 /* sending "pop" on cmdline uart resets the robot */
135 void emergency(char c)
137 static uint8_t i = 0;
139 if ((i == 0 && c == 'p') ||
140 (i == 1 && c == 'o') ||
141 (i == 2 && c == 'p'))
143 else if ( !(i == 1 && c == 'p') )
149 /* log function, add a command to configure
151 void mylog(struct error * e, ...)
155 u16 stream_flags = stdout->flags;
160 if (e->severity > ERROR_SEVERITY_ERROR) {
161 if (xbeeboard.log_level < e->severity)
164 for (i=0; i<NB_LOGS+1; i++)
165 if (xbeeboard.logs[i] == e->err_num)
172 tv = time_get_time();
173 printf_P(PSTR("%d.%.3d: "), (int)tv.s, (int)(tv.us/1000UL));
175 vfprintf_P(stdout, e->text, ap);
176 printf_P(PSTR("\r\n"));
179 stdout->flags = stream_flags;
183 int cmdline_poll(void)
185 const char *history, *buffer;
186 int8_t ret, same = 0;
189 c = cmdline_dev_recv(NULL);
193 ret = rdline_char_in(&xbeeboard.rdl, c);
195 buffer = rdline_get_buffer(&xbeeboard.rdl);
196 history = rdline_get_history_item(&xbeeboard.rdl, 0);
198 same = !memcmp(buffer, history, strlen(history)) &&
199 buffer[strlen(history)] == '\n';
203 if (strlen(buffer) > 1 && !same)
204 rdline_add_history(&xbeeboard.rdl, buffer);
206 if (xbeeboard.rdl.status != RDLINE_STOPPED)
207 rdline_newline(&xbeeboard.rdl, xbeeboard.prompt);