3ae4d3061721655ec3e247eebb79c35f15054045
[aversive.git] / projects / microb2010 / mainboard / cmdline.c
1 /*
2  *  Copyright Droids Corporation
3  *  Olivier Matz <zer0@droids-corp.org>
4  *
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.
9  *
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.
14  *
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
18  *
19  *  Revision : $Id: cmdline.c,v 1.7 2009-11-08 17:24:33 zer0 Exp $
20  *
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <aversive.h>
27 #include <aversive/error.h>
28
29 #include <hostsim.h>
30
31 #include <parse.h>
32 #include <rdline.h>
33 #include <ax12.h>
34 #include <uart.h>
35 #include <pwm_ng.h>
36 #include <clock_time.h>
37
38 #include <pid.h>
39 #include <quadramp.h>
40 #include <control_system_manager.h>
41 #include <trajectory_manager.h>
42 #include <vect_base.h>
43 #include <lines.h>
44 #include <polygon.h>
45 #include <obstacle_avoidance.h>
46 #include <blocking_detection_manager.h>
47 #include <robot_system.h>
48 #include <position_manager.h>
49
50 #include "main.h"
51 #include "cmdline.h"
52 #include "strat_base.h"
53
54
55 /******** See in commands.c for the list of commands. */
56 extern parse_pgm_ctx_t main_ctx[];
57
58 static void write_char(char c)
59 {
60         uart_send(CMDLINE_UART, c);
61 }
62
63 static void
64 valid_buffer(const char *buf, uint8_t size)
65 {
66         int8_t ret;
67
68         /* reset CTRL-C for trajectory interruption each time we
69          * receive a new command */
70         interrupt_traj_reset();
71
72         ret = parse(main_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"));
79 }
80
81 static int8_t
82 complete_buffer(const char *buf, char *dstbuf, uint8_t dstsize,
83                 int16_t *state)
84 {
85         return complete(main_ctx, buf, state, dstbuf, dstsize);
86 }
87
88
89 /* sending "pop" on cmdline uart resets the robot */
90 void emergency(char c)
91 {
92         static uint8_t i = 0;
93
94         /* interrupt traj here */
95         if (c == '\003')
96                 interrupt_traj();
97
98         if ((i == 0 && c == 'p') ||
99             (i == 1 && c == 'o') ||
100             (i == 2 && c == 'p'))
101                 i++;
102         else if ( !(i == 1 && c == 'p') )
103                 i = 0;
104         if (i == 3) {
105 #ifdef HOST_VERSION
106                 hostsim_exit();
107 #endif
108                 reset();
109         }
110 }
111
112 /* log function, add a command to configure
113  * it dynamically */
114 void mylog(struct error * e, ...)
115 {
116         va_list ap;
117 #ifndef HOST_VERSION
118         u16 stream_flags = stdout->flags;
119 #endif
120         uint8_t i;
121         time_h tv;
122
123         if (e->severity > ERROR_SEVERITY_ERROR) {
124                 if (gen.log_level < e->severity)
125                         return;
126
127                 for (i=0; i<NB_LOGS+1; i++)
128                         if (gen.logs[i] == e->err_num)
129                                 break;
130                 if (i == NB_LOGS+1)
131                         return;
132         }
133
134         va_start(ap, e);
135         tv = time_get_time();
136         printf_P(PSTR("%d.%.3d: "), (int)tv.s, (int)(tv.us/1000UL));
137
138         printf_P(PSTR("(%d,%d,%d) "),
139                  position_get_x_s16(&mainboard.pos),
140                  position_get_y_s16(&mainboard.pos),
141                  position_get_a_deg_s16(&mainboard.pos));
142
143         vfprintf_P(stdout, e->text, ap);
144         printf_P(PSTR("\r\n"));
145         va_end(ap);
146 #ifndef HOST_VERSION
147         stdout->flags = stream_flags;
148 #endif
149 }
150
151 int cmdline_interact(void)
152 {
153         const char *history, *buffer;
154         int8_t ret, same = 0;
155         int16_t c;
156
157         rdline_init(&gen.rdl, write_char, valid_buffer, complete_buffer);
158         snprintf(gen.prompt, sizeof(gen.prompt), "mainboard > ");
159         rdline_newline(&gen.rdl, gen.prompt);
160
161         while (1) {
162                 c = uart_recv_nowait(CMDLINE_UART);
163                 if (c == -1)
164                         continue;
165                 ret = rdline_char_in(&gen.rdl, c);
166                 if (ret != 2 && ret != 0) {
167                         buffer = rdline_get_buffer(&gen.rdl);
168                         history = rdline_get_history_item(&gen.rdl, 0);
169                         if (history) {
170                                 same = !memcmp(buffer, history, strlen(history)) &&
171                                         buffer[strlen(history)] == '\n';
172                         }
173                         else
174                                 same = 0;
175                         if (strlen(buffer) > 1 && !same)
176                                 rdline_add_history(&gen.rdl, buffer);
177                         rdline_newline(&gen.rdl, gen.prompt);
178                 }
179         }
180
181         return 0;
182 }