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