restore first circle algo + hostsim work
[aversive.git] / projects / microb2010 / sensorboard / commands_sensorboard.c
1 /*
2  *  Copyright Droids Corporation (2009)
3  * 
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: commands_sensorboard.c,v 1.2 2009-04-24 19:30:42 zer0 Exp $
19  *
20  *  Olivier MATZ <zer0@droids-corp.org> 
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25
26 #include <aversive/pgmspace.h>
27 #include <aversive/wait.h>
28 #include <aversive/error.h>
29
30 #include <ax12.h>
31 #include <uart.h>
32 #include <pwm_ng.h>
33 #include <time.h>
34 #include <spi.h>
35
36 #include <pid.h>
37 #include <quadramp.h>
38 #include <control_system_manager.h>
39 #include <blocking_detection_manager.h>
40
41 #include <rdline.h>
42 #include <parse.h>
43 #include <parse_string.h>
44 #include <parse_num.h>
45
46 #include "main.h"
47 #include "cmdline.h"
48 #include "../common/i2c_commands.h"
49 #include "i2c_protocol.h"
50 #include "actuator.h"
51
52 struct cmd_event_result {
53         fixed_string_t arg0;
54         fixed_string_t arg1;
55         fixed_string_t arg2;
56 };
57
58
59 /* function called when cmd_event is parsed successfully */
60 static void cmd_event_parsed(void *parsed_result, void *data)
61 {
62         u08 bit=0;
63
64         struct cmd_event_result * res = parsed_result;
65         
66         if (!strcmp_P(res->arg1, PSTR("all"))) {
67                 bit = DO_ENCODERS | DO_CS | DO_BD | DO_POWER;
68                 if (!strcmp_P(res->arg2, PSTR("on")))
69                         sensorboard.flags |= bit;
70                 else if (!strcmp_P(res->arg2, PSTR("off")))
71                         sensorboard.flags &= bit;
72                 else { /* show */
73                         printf_P(PSTR("encoders is %s\r\n"), 
74                                  (DO_ENCODERS & sensorboard.flags) ? "on":"off");
75                         printf_P(PSTR("cs is %s\r\n"), 
76                                  (DO_CS & sensorboard.flags) ? "on":"off");
77                         printf_P(PSTR("bd is %s\r\n"), 
78                                  (DO_BD & sensorboard.flags) ? "on":"off");
79                         printf_P(PSTR("power is %s\r\n"), 
80                                  (DO_POWER & sensorboard.flags) ? "on":"off");
81                 }
82                 return;
83         }
84
85         if (!strcmp_P(res->arg1, PSTR("encoders")))
86                 bit = DO_ENCODERS;
87         else if (!strcmp_P(res->arg1, PSTR("cs"))) {
88                 //strat_hardstop();
89                 bit = DO_CS;
90         }
91         else if (!strcmp_P(res->arg1, PSTR("bd")))
92                 bit = DO_BD;
93         else if (!strcmp_P(res->arg1, PSTR("power")))
94                 bit = DO_POWER;
95
96
97         if (!strcmp_P(res->arg2, PSTR("on")))
98                 sensorboard.flags |= bit;
99         else if (!strcmp_P(res->arg2, PSTR("off"))) {
100                 if (!strcmp_P(res->arg1, PSTR("cs"))) {
101                         pwm_ng_set(BEACON_PWM, 0);
102                         pwm_ng_set(SCANNER_PWM, 0);
103                 }
104                 sensorboard.flags &= (~bit);
105         }
106         printf_P(PSTR("%s is %s\r\n"), res->arg1, 
107                       (bit & sensorboard.flags) ? "on":"off");
108 }
109
110 prog_char str_event_arg0[] = "event";
111 parse_pgm_token_string_t cmd_event_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_event_result, arg0, str_event_arg0);
112 prog_char str_event_arg1[] = "all#encoders#cs#bd#power";
113 parse_pgm_token_string_t cmd_event_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_event_result, arg1, str_event_arg1);
114 prog_char str_event_arg2[] = "on#off#show";
115 parse_pgm_token_string_t cmd_event_arg2 = TOKEN_STRING_INITIALIZER(struct cmd_event_result, arg2, str_event_arg2);
116
117 prog_char help_event[] = "Enable/disable events";
118 parse_pgm_inst_t cmd_event = {
119         .f = cmd_event_parsed,  /* function to call */
120         .data = NULL,      /* 2nd arg of func */
121         .help_str = help_event,
122         .tokens = {        /* token list, NULL terminated */
123                 (prog_void *)&cmd_event_arg0, 
124                 (prog_void *)&cmd_event_arg1, 
125                 (prog_void *)&cmd_event_arg2, 
126                 NULL,
127         },
128 };
129
130 /**********************************************************/
131 /* Color */
132
133 /* this structure is filled when cmd_color is parsed successfully */
134 struct cmd_color_result {
135         fixed_string_t arg0;
136         fixed_string_t color;
137 };
138
139 /* function called when cmd_color is parsed successfully */
140 static void cmd_color_parsed(void *parsed_result, void *data)
141 {
142         struct cmd_color_result *res = (struct cmd_color_result *) parsed_result;
143         if (!strcmp_P(res->color, PSTR("red"))) {
144                 sensorboard.our_color = I2C_COLOR_RED;
145         }
146         else if (!strcmp_P(res->color, PSTR("green"))) {
147                 sensorboard.our_color = I2C_COLOR_GREEN;
148         }
149         printf_P(PSTR("Done\r\n"));
150 }
151
152 prog_char str_color_arg0[] = "color";
153 parse_pgm_token_string_t cmd_color_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_color_result, arg0, str_color_arg0);
154 prog_char str_color_color[] = "green#red";
155 parse_pgm_token_string_t cmd_color_color = TOKEN_STRING_INITIALIZER(struct cmd_color_result, color, str_color_color);
156
157 prog_char help_color[] = "Set our color";
158 parse_pgm_inst_t cmd_color = {
159         .f = cmd_color_parsed,  /* function to call */
160         .data = NULL,      /* 2nd arg of func */
161         .help_str = help_color,
162         .tokens = {        /* token list, NULL terminated */
163                 (prog_void *)&cmd_color_arg0, 
164                 (prog_void *)&cmd_color_color, 
165                 NULL,
166         },
167 };
168
169
170 /**********************************************************/
171 /* Test */
172
173 /* this structure is filled when cmd_test is parsed successfully */
174 struct cmd_test_result {
175         fixed_string_t arg0;
176 };
177
178 /* function called when cmd_test is parsed successfully */
179 static void cmd_test_parsed(void *parsed_result, void *data)
180 {
181         //struct cmd_test_result *res = parsed_result;
182         
183 }
184
185 prog_char str_test_arg0[] = "test";
186 parse_pgm_token_string_t cmd_test_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_test_result, arg0, str_test_arg0);
187
188 prog_char help_test[] = "Test function";
189 parse_pgm_inst_t cmd_test = {
190         .f = cmd_test_parsed,  /* function to call */
191         .data = NULL,      /* 2nd arg of func */
192         .help_str = help_test,
193         .tokens = {        /* token list, NULL terminated */
194                 (prog_void *)&cmd_test_arg0, 
195                 NULL,
196         },
197 };