2 * Copyright Droids Corporation (2009)
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.
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.
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
18 * Revision : $Id: commands_ballboard.c,v 1.2 2009-04-24 19:30:42 zer0 Exp $
20 * Olivier MATZ <zer0@droids-corp.org>
26 #include <aversive/pgmspace.h>
27 #include <aversive/wait.h>
28 #include <aversive/error.h>
33 #include <clock_time.h>
38 #include <control_system_manager.h>
39 #include <blocking_detection_manager.h>
43 #include <parse_string.h>
44 #include <parse_num.h>
49 #include "../common/i2c_commands.h"
50 #include "i2c_protocol.h"
53 struct cmd_event_result {
60 /* function called when cmd_event is parsed successfully */
61 static void cmd_event_parsed(void *parsed_result, void *data)
65 struct cmd_event_result * res = parsed_result;
67 if (!strcmp_P(res->arg1, PSTR("all"))) {
69 if (!strcmp_P(res->arg2, PSTR("on")))
70 ballboard.flags |= bit;
71 else if (!strcmp_P(res->arg2, PSTR("off")))
72 ballboard.flags &= bit;
74 printf_P(PSTR("encoders is %s\r\n"),
75 (DO_ENCODERS & ballboard.flags) ? "on":"off");
76 printf_P(PSTR("cs is %s\r\n"),
77 (DO_CS & ballboard.flags) ? "on":"off");
78 printf_P(PSTR("bd is %s\r\n"),
79 (DO_BD & ballboard.flags) ? "on":"off");
80 printf_P(PSTR("power is %s\r\n"),
81 (DO_POWER & ballboard.flags) ? "on":"off");
82 printf_P(PSTR("errblock is %s\r\n"),
83 (DO_ERRBLOCKING & ballboard.flags) ? "on":"off");
88 if (!strcmp_P(res->arg1, PSTR("encoders")))
90 else if (!strcmp_P(res->arg1, PSTR("cs"))) {
93 else if (!strcmp_P(res->arg1, PSTR("bd")))
95 else if (!strcmp_P(res->arg1, PSTR("power")))
97 else if (!strcmp_P(res->arg1, PSTR("errblock")))
101 if (!strcmp_P(res->arg2, PSTR("on")))
102 ballboard.flags |= bit;
103 else if (!strcmp_P(res->arg2, PSTR("off"))) {
104 if (!strcmp_P(res->arg1, PSTR("cs"))) {
105 pwm_ng_set(ROLLER_PWM, 0);
106 pwm_ng_set(FORKTRANS_PWM, 0);
107 pwm_ng_set(FORKROT_PWM, 0);
109 ballboard.flags &= (~bit);
111 printf_P(PSTR("%s is %s\r\n"), res->arg1,
112 (bit & ballboard.flags) ? "on":"off");
115 prog_char str_event_arg0[] = "event";
116 parse_pgm_token_string_t cmd_event_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_event_result, arg0, str_event_arg0);
117 prog_char str_event_arg1[] = "all#encoders#cs#bd#power#errblock";
118 parse_pgm_token_string_t cmd_event_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_event_result, arg1, str_event_arg1);
119 prog_char str_event_arg2[] = "on#off#show";
120 parse_pgm_token_string_t cmd_event_arg2 = TOKEN_STRING_INITIALIZER(struct cmd_event_result, arg2, str_event_arg2);
122 prog_char help_event[] = "Enable/disable events";
123 parse_pgm_inst_t cmd_event = {
124 .f = cmd_event_parsed, /* function to call */
125 .data = NULL, /* 2nd arg of func */
126 .help_str = help_event,
127 .tokens = { /* token list, NULL terminated */
128 (prog_void *)&cmd_event_arg0,
129 (prog_void *)&cmd_event_arg1,
130 (prog_void *)&cmd_event_arg2,
135 /**********************************************************/
138 /* this structure is filled when cmd_color is parsed successfully */
139 struct cmd_color_result {
141 fixed_string_t color;
144 /* function called when cmd_color is parsed successfully */
145 static void cmd_color_parsed(void *parsed_result, void *data)
147 struct cmd_color_result *res = (struct cmd_color_result *) parsed_result;
148 if (!strcmp_P(res->color, PSTR("yellow"))) {
149 ballboard.our_color = I2C_COLOR_YELLOW;
151 else if (!strcmp_P(res->color, PSTR("blue"))) {
152 ballboard.our_color = I2C_COLOR_BLUE;
154 printf_P(PSTR("Done\r\n"));
157 prog_char str_color_arg0[] = "color";
158 parse_pgm_token_string_t cmd_color_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_color_result, arg0, str_color_arg0);
159 prog_char str_color_color[] = "blue#yellow";
160 parse_pgm_token_string_t cmd_color_color = TOKEN_STRING_INITIALIZER(struct cmd_color_result, color, str_color_color);
162 prog_char help_color[] = "Set our color";
163 parse_pgm_inst_t cmd_color = {
164 .f = cmd_color_parsed, /* function to call */
165 .data = NULL, /* 2nd arg of func */
166 .help_str = help_color,
167 .tokens = { /* token list, NULL terminated */
168 (prog_void *)&cmd_color_arg0,
169 (prog_void *)&cmd_color_color,
175 /**********************************************************/
178 /* this structure is filled when cmd_state1 is parsed successfully */
179 struct cmd_state1_result {
184 /* function called when cmd_state1 is parsed successfully */
185 static void cmd_state1_parsed(void *parsed_result,
186 __attribute__((unused)) void *data)
188 struct cmd_state1_result *res = parsed_result;
190 if (!strcmp_P(res->arg1, PSTR("init")))
192 else if (!strcmp_P(res->arg1, PSTR("off")))
193 state_set_mode(I2C_BALLBOARD_MODE_OFF);
194 else if (!strcmp_P(res->arg1, PSTR("eject")))
195 state_set_mode(I2C_BALLBOARD_MODE_EJECT);
196 else if (!strcmp_P(res->arg1, PSTR("harvest")))
197 state_set_mode(I2C_BALLBOARD_MODE_HARVEST);
198 else if (!strcmp_P(res->arg1, PSTR("prepare")))
199 state_set_mode(I2C_BALLBOARD_MODE_PREP_FORK);
200 else if (!strcmp_P(res->arg1, PSTR("take")))
201 state_set_mode(I2C_BALLBOARD_MODE_TAKE_FORK);
206 prog_char str_state1_arg0[] = "ballboard";
207 parse_pgm_token_string_t cmd_state1_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state1_result, arg0, str_state1_arg0);
208 prog_char str_state1_arg1[] = "init#eject#harvest#off#prepare#take";
209 parse_pgm_token_string_t cmd_state1_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_state1_result, arg1, str_state1_arg1);
211 prog_char help_state1[] = "set ballboard mode";
212 parse_pgm_inst_t cmd_state1 = {
213 .f = cmd_state1_parsed, /* function to call */
214 .data = NULL, /* 2nd arg of func */
215 .help_str = help_state1,
216 .tokens = { /* token list, NULL terminated */
217 (prog_void *)&cmd_state1_arg0,
218 (prog_void *)&cmd_state1_arg1,
223 /**********************************************************/
226 /* this structure is filled when cmd_state2 is parsed successfully */
227 struct cmd_state2_result {
233 /* function called when cmd_state2 is parsed successfully */
234 static void cmd_state2_parsed(void *parsed_result,
235 __attribute__((unused)) void *data)
239 prog_char str_state2_arg0[] = "ballboard";
240 parse_pgm_token_string_t cmd_state2_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state2_result, arg0, str_state2_arg0);
241 prog_char str_state2_arg1[] = "xxx";
242 parse_pgm_token_string_t cmd_state2_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_state2_result, arg1, str_state2_arg1);
243 prog_char str_state2_arg2[] = "left#right";
244 parse_pgm_token_string_t cmd_state2_arg2 = TOKEN_STRING_INITIALIZER(struct cmd_state2_result, arg2, str_state2_arg2);
246 prog_char help_state2[] = "set ballboard mode";
247 parse_pgm_inst_t cmd_state2 = {
248 .f = cmd_state2_parsed, /* function to call */
249 .data = NULL, /* 2nd arg of func */
250 .help_str = help_state2,
251 .tokens = { /* token list, NULL terminated */
252 (prog_void *)&cmd_state2_arg0,
253 (prog_void *)&cmd_state2_arg1,
254 (prog_void *)&cmd_state2_arg2,
259 /**********************************************************/
262 /* this structure is filled when cmd_state3 is parsed successfully */
263 struct cmd_state3_result {
269 /* function called when cmd_state3 is parsed successfully */
270 static void cmd_state3_parsed(void *parsed_result,
271 __attribute__((unused)) void *data)
273 struct cmd_state3_result *res = parsed_result;
275 if (!strcmp_P(res->arg1, PSTR("xxx"))) {
276 /* xxx = res->arg2 */
278 else if (!strcmp_P(res->arg1, PSTR("yyy"))) {
283 prog_char str_state3_arg0[] = "ballboard";
284 parse_pgm_token_string_t cmd_state3_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state3_result, arg0, str_state3_arg0);
285 prog_char str_state3_arg1[] = "xxx";
286 parse_pgm_token_string_t cmd_state3_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_state3_result, arg1, str_state3_arg1);
287 parse_pgm_token_num_t cmd_state3_arg2 = TOKEN_NUM_INITIALIZER(struct cmd_state3_result, arg2, UINT8);
289 prog_char help_state3[] = "set ballboard mode";
290 parse_pgm_inst_t cmd_state3 = {
291 .f = cmd_state3_parsed, /* function to call */
292 .data = NULL, /* 2nd arg of func */
293 .help_str = help_state3,
294 .tokens = { /* token list, NULL terminated */
295 (prog_void *)&cmd_state3_arg0,
296 (prog_void *)&cmd_state3_arg1,
297 (prog_void *)&cmd_state3_arg2,
302 /**********************************************************/
305 /* this structure is filled when cmd_state_machine is parsed successfully */
306 struct cmd_state_machine_result {
310 /* function called when cmd_state_machine is parsed successfully */
311 static void cmd_state_machine_parsed(__attribute__((unused)) void *parsed_result,
312 __attribute__((unused)) void *data)
317 prog_char str_state_machine_arg0[] = "state_machine";
318 parse_pgm_token_string_t cmd_state_machine_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state_machine_result, arg0, str_state_machine_arg0);
320 prog_char help_state_machine[] = "launch state machine";
321 parse_pgm_inst_t cmd_state_machine = {
322 .f = cmd_state_machine_parsed, /* function to call */
323 .data = NULL, /* 2nd arg of func */
324 .help_str = help_state_machine,
325 .tokens = { /* token list, NULL terminated */
326 (prog_void *)&cmd_state_machine_arg0,
331 /**********************************************************/
334 /* this structure is filled when cmd_state_debug is parsed successfully */
335 struct cmd_state_debug_result {
340 /* function called when cmd_state_debug is parsed successfully */
341 static void cmd_state_debug_parsed(void *parsed_result,
342 __attribute__((unused)) void *data)
344 struct cmd_state_debug_result *res = parsed_result;
345 state_debug = res->on;
348 prog_char str_state_debug_arg0[] = "state_debug";
349 parse_pgm_token_string_t cmd_state_debug_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state_debug_result, arg0, str_state_debug_arg0);
350 parse_pgm_token_num_t cmd_state_debug_on = TOKEN_NUM_INITIALIZER(struct cmd_state_debug_result, on, UINT8);
352 prog_char help_state_debug[] = "Set debug for state machine";
353 parse_pgm_inst_t cmd_state_debug = {
354 .f = cmd_state_debug_parsed, /* function to call */
355 .data = NULL, /* 2nd arg of func */
356 .help_str = help_state_debug,
357 .tokens = { /* token list, NULL terminated */
358 (prog_void *)&cmd_state_debug_arg0,
359 (prog_void *)&cmd_state_debug_on,
364 /**********************************************************/
367 /* this structure is filled when cmd_fork is parsed successfully */
368 struct cmd_fork_result {
373 /* function called when cmd_fork is parsed successfully */
374 static void cmd_fork_parsed(void *parsed_result,
375 __attribute__((unused)) void *data)
377 struct cmd_fork_result *res = parsed_result;
378 if (!strcmp_P(res->arg1, PSTR("deploy")))
380 else if (!strcmp_P(res->arg1, PSTR("pack")))
382 else if (!strcmp_P(res->arg1, PSTR("middle")))
386 prog_char str_fork_arg0[] = "fork";
387 parse_pgm_token_string_t cmd_fork_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_fork_result, arg0, str_fork_arg0);
388 prog_char str_fork_arg1[] = "deploy#pack#middle";
389 parse_pgm_token_string_t cmd_fork_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_fork_result, arg1, str_fork_arg1);
391 prog_char help_fork[] = "move fork";
392 parse_pgm_inst_t cmd_fork = {
393 .f = cmd_fork_parsed, /* function to call */
394 .data = NULL, /* 2nd arg of func */
395 .help_str = help_fork,
396 .tokens = { /* token list, NULL terminated */
397 (prog_void *)&cmd_fork_arg0,
398 (prog_void *)&cmd_fork_arg1,
403 /**********************************************************/
406 /* this structure is filled when cmd_roller is parsed successfully */
407 struct cmd_roller_result {
412 /* function called when cmd_roller is parsed successfully */
413 static void cmd_roller_parsed(void *parsed_result,
414 __attribute__((unused)) void *data)
416 struct cmd_roller_result *res = parsed_result;
417 if (!strcmp_P(res->arg1, PSTR("on")))
419 else if (!strcmp_P(res->arg1, PSTR("off")))
421 else if (!strcmp_P(res->arg1, PSTR("reverse")))
425 prog_char str_roller_arg0[] = "roller";
426 parse_pgm_token_string_t cmd_roller_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_roller_result, arg0, str_roller_arg0);
427 prog_char str_roller_arg1[] = "on#off#reverse";
428 parse_pgm_token_string_t cmd_roller_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_roller_result, arg1, str_roller_arg1);
430 prog_char help_roller[] = "Servo door function";
431 parse_pgm_inst_t cmd_roller = {
432 .f = cmd_roller_parsed, /* function to call */
433 .data = NULL, /* 2nd arg of func */
434 .help_str = help_roller,
435 .tokens = { /* token list, NULL terminated */
436 (prog_void *)&cmd_roller_arg0,
437 (prog_void *)&cmd_roller_arg1,
442 /**********************************************************/
445 /* this structure is filled when cmd_test is parsed successfully */
446 struct cmd_test_result {
450 /* function called when cmd_test is parsed successfully */
451 static void cmd_test_parsed(void *parsed_result, void *data)
453 //struct cmd_test_result *res = parsed_result;
456 prog_char str_test_arg0[] = "test";
457 parse_pgm_token_string_t cmd_test_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_test_result, arg0, str_test_arg0);
459 prog_char help_test[] = "Test function";
460 parse_pgm_inst_t cmd_test = {
461 .f = cmd_test_parsed, /* function to call */
462 .data = NULL, /* 2nd arg of func */
463 .help_str = help_test,
464 .tokens = { /* token list, NULL terminated */
465 (prog_void *)&cmd_test_arg0,