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>
38 #define FLUSH_LOGS_MS 1000 /* every second */
39 #define LOG_PER_SEC_MAX 10
41 extern const parse_ctx_t PROGMEM main_ctx[];
43 static struct callout flush_log_timer;
44 static uint8_t log_count;
46 int cmdline_dev_send(char c, FILE* f)
49 uart_send(CMDLINE_UART, c);
53 int cmdline_dev_recv(FILE* f)
58 c = uart_recv_nowait(CMDLINE_UART);
66 void cmdline_valid_buffer(const char *buf, uint8_t size)
69 PGM_P ctx = (PGM_P)main_ctx;
72 ret = parse(ctx, buf);
73 if (ret == PARSE_AMBIGUOUS)
74 printf_P(PSTR("Ambiguous command\r\n"));
75 else if (ret == PARSE_NOMATCH)
76 printf_P(PSTR("Command not found\r\n"));
77 else if (ret == PARSE_BAD_ARGS)
78 printf_P(PSTR("Bad arguments\r\n"));
82 complete_buffer(const char *buf, char *dstbuf, uint8_t dstsize,
85 PGM_P ctx = (PGM_P)main_ctx;
86 return complete(ctx, buf, state, dstbuf, dstsize);
90 void cmdline_write_char(char c)
92 cmdline_dev_send(c, NULL);
96 /* sending "pop" on cmdline uart resets the robot */
97 void emergency(char c)
101 if ((i == 0 && c == 'p') ||
102 (i == 1 && c == 'o') ||
103 (i == 2 && c == 'p'))
105 else if ( !(i == 1 && c == 'p') )
113 /* log function, configured dynamically */
114 void mylog(struct error * e, ...)
117 u16 stream_flags = stdout->flags;
125 if (log_count >= LOG_PER_SEC_MAX)
128 /* higher log value means lower criticity */
129 if (e->severity > ERROR_SEVERITY_ERROR) {
130 if (imuboard.log_level < e->severity)
133 for (i=0; i<NB_LOGS+1; i++)
134 if (imuboard.logs[i] == e->err_num)
145 /* prevent flush log to occur */
146 prio = callout_mgr_set_prio(&imuboard.intr_cm,
149 /* display the log */
151 printf_P(PSTR("%d.%.3d: "), (int)(ms/1000UL), (int)(ms%1000UL));
152 vfprintf_P(stdout, e->text, ap);
153 printf_P(PSTR("\r\n"));
157 stdout->flags = stream_flags;
159 callout_mgr_restore_prio(&imuboard.intr_cm, prio);
162 static void flush_logs_cb(struct callout_mgr *cm, struct callout *tim,
169 if (log_count == LOG_PER_SEC_MAX)
170 printf_P("some logs were dropped\n");
171 callout_reschedule(&imuboard.intr_cm, &flush_log_timer,
176 void cmdline_poll(void)
178 const char *history, *buffer;
179 int8_t ret, same = 0;
183 c = cmdline_dev_recv(NULL);
187 ret = rdline_char_in(&imuboard.rdl, c);
189 buffer = rdline_get_buffer(&imuboard.rdl);
190 history = rdline_get_history_item(&imuboard.rdl, 0);
192 same = !memcmp(buffer, history, strlen(history)) &&
193 buffer[strlen(history)] == '\n';
197 if (strlen(buffer) > 1 && !same)
198 rdline_add_history(&imuboard.rdl, buffer);
200 if (imuboard.rdl.status != RDLINE_STOPPED)
201 rdline_newline(&imuboard.rdl, imuboard.prompt);
206 void cmdline_init(void)
208 /* init command line */
209 rdline_init(&imuboard.rdl, cmdline_write_char, cmdline_valid_buffer,
211 snprintf_P(imuboard.prompt, sizeof(imuboard.prompt),
214 /* load a timer for flushing logs */
215 callout_init(&flush_log_timer, flush_logs_cb, NULL, LOW_PRIO);
216 callout_schedule(&imuboard.intr_cm, &flush_log_timer, FLUSH_LOGS_MS);