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