add climb fct
[aversive.git] / projects / microb2010 / tests / beacon_tsop / 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
34 #include <pid.h>
35
36 #include "main.h"
37 #include "cmdline.h"
38
39
40 /******** See in commands.c for the list of commands. */
41 extern parse_pgm_ctx_t main_ctx[];
42
43 void write_char(char c) 
44 {
45         uart_send(CMDLINE_UART, c);
46 }
47
48 void valid_buffer(const char *buf, __attribute__((unused)) uint8_t size) 
49 {
50         int8_t ret;
51
52         ret = parse(main_ctx, buf);
53         if (ret == PARSE_AMBIGUOUS)
54                 printf_P(PSTR("Ambiguous command\r\n"));
55         else if (ret == PARSE_NOMATCH)
56                 printf_P(PSTR("Command not found\r\n"));
57         else if (ret == PARSE_BAD_ARGS)
58                 printf_P(PSTR("Bad arguments\r\n"));
59 }
60
61 int8_t complete_buffer(const char *buf, char *dstbuf, uint8_t dstsize,
62                        int16_t *state)
63 {
64         return complete(main_ctx, buf, state, dstbuf, dstsize);
65 }
66
67 int cmdline_process(void)
68 {
69         const char *history, *buffer;
70         int8_t ret, same = 0;
71         int16_t c;
72         
73         while ( (c = uart_recv_nowait(CMDLINE_UART)) != -1) {
74
75                 ret = rdline_char_in(&beacon_tsop.rdl, c);
76                 if (ret != 2 && ret != 0) {
77                         buffer = rdline_get_buffer(&beacon_tsop.rdl);
78                         history = rdline_get_history_item(&beacon_tsop.rdl, 0);
79                         if (history) {
80                                 same = !memcmp(buffer, history, strlen(history)) &&
81                                         buffer[strlen(history)] == '\n';
82                         }
83                         else
84                                 same = 0;
85                         if (strlen(buffer) > 1 && !same)
86                                 rdline_add_history(&beacon_tsop.rdl, buffer);
87                         rdline_newline(&beacon_tsop.rdl, beacon_tsop.prompt);
88                 }
89         }
90
91         return 0;
92 }