kickstand
[aversive.git] / projects / microb2010 / cobboard / commands_cobboard.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_cobboard.c,v 1.6 2009-11-08 17:25:00 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 <clock_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 "../common/i2c_commands.h"
47 #include "main.h"
48 #include "sensor.h"
49 #include "cmdline.h"
50 #include "state.h"
51 #include "i2c_protocol.h"
52 #include "actuator.h"
53 #include "spickle.h"
54 #include "shovel.h"
55
56 struct cmd_event_result {
57         fixed_string_t arg0;
58         fixed_string_t arg1;
59         fixed_string_t arg2;
60 };
61
62
63 /* function called when cmd_event is parsed successfully */
64 static void cmd_event_parsed(void *parsed_result, __attribute__((unused)) void *data)
65 {
66         u08 bit=0;
67
68         struct cmd_event_result * res = parsed_result;
69
70         if (!strcmp_P(res->arg1, PSTR("all"))) {
71                 bit = 0xFF;
72                 if (!strcmp_P(res->arg2, PSTR("on")))
73                         cobboard.flags |= bit;
74                 else if (!strcmp_P(res->arg2, PSTR("off")))
75                         cobboard.flags &= bit;
76                 else { /* show */
77                         printf_P(PSTR("encoders is %s\r\n"),
78                                  (DO_ENCODERS & cobboard.flags) ? "on":"off");
79                         printf_P(PSTR("cs is %s\r\n"),
80                                  (DO_CS & cobboard.flags) ? "on":"off");
81                         printf_P(PSTR("bd is %s\r\n"),
82                                  (DO_BD & cobboard.flags) ? "on":"off");
83                         printf_P(PSTR("power is %s\r\n"),
84                                  (DO_POWER & cobboard.flags) ? "on":"off");
85                         printf_P(PSTR("errblock is %s\r\n"),
86                                  (DO_ERRBLOCKING & cobboard.flags) ? "on":"off");
87                 }
88                 return;
89         }
90
91         if (!strcmp_P(res->arg1, PSTR("encoders")))
92                 bit = DO_ENCODERS;
93         else if (!strcmp_P(res->arg1, PSTR("cs"))) {
94                 bit = DO_CS;
95         }
96         else if (!strcmp_P(res->arg1, PSTR("bd")))
97                 bit = DO_BD;
98         else if (!strcmp_P(res->arg1, PSTR("power")))
99                 bit = DO_POWER;
100         else if (!strcmp_P(res->arg1, PSTR("errblock")))
101                 bit = DO_ERRBLOCKING;
102
103
104         if (!strcmp_P(res->arg2, PSTR("on")))
105                 cobboard.flags |= bit;
106         else if (!strcmp_P(res->arg2, PSTR("off"))) {
107                 if (!strcmp_P(res->arg1, PSTR("cs"))) {
108                         pwm_ng_set(LEFT_SPICKLE_PWM, 0);
109                         pwm_ng_set(RIGHT_SPICKLE_PWM, 0);
110                         pwm_ng_set(SHOVEL_PWM, 0);
111                 }
112                 cobboard.flags &= (~bit);
113         }
114         printf_P(PSTR("%s is %s\r\n"), res->arg1,
115                       (bit & cobboard.flags) ? "on":"off");
116 }
117
118 prog_char str_event_arg0[] = "event";
119 parse_pgm_token_string_t cmd_event_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_event_result, arg0, str_event_arg0);
120 prog_char str_event_arg1[] = "all#encoders#cs#bd#power#errblock";
121 parse_pgm_token_string_t cmd_event_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_event_result, arg1, str_event_arg1);
122 prog_char str_event_arg2[] = "on#off#show";
123 parse_pgm_token_string_t cmd_event_arg2 = TOKEN_STRING_INITIALIZER(struct cmd_event_result, arg2, str_event_arg2);
124
125 prog_char help_event[] = "Enable/disable events";
126 parse_pgm_inst_t cmd_event = {
127         .f = cmd_event_parsed,  /* function to call */
128         .data = NULL,      /* 2nd arg of func */
129         .help_str = help_event,
130         .tokens = {        /* token list, NULL terminated */
131                 (prog_void *)&cmd_event_arg0,
132                 (prog_void *)&cmd_event_arg1,
133                 (prog_void *)&cmd_event_arg2,
134                 NULL,
135         },
136 };
137
138 /**********************************************************/
139 /* Color */
140
141 /* this structure is filled when cmd_color is parsed successfully */
142 struct cmd_color_result {
143         fixed_string_t arg0;
144         fixed_string_t color;
145 };
146
147 /* function called when cmd_color is parsed successfully */
148 static void cmd_color_parsed(void *parsed_result, __attribute__((unused)) void *data)
149 {
150         struct cmd_color_result *res = (struct cmd_color_result *) parsed_result;
151         if (!strcmp_P(res->color, PSTR("yellow"))) {
152                 cobboard.our_color = I2C_COLOR_YELLOW;
153         }
154         else if (!strcmp_P(res->color, PSTR("blue"))) {
155                 cobboard.our_color = I2C_COLOR_BLUE;
156         }
157         printf_P(PSTR("Done\r\n"));
158 }
159
160 prog_char str_color_arg0[] = "color";
161 parse_pgm_token_string_t cmd_color_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_color_result, arg0, str_color_arg0);
162 prog_char str_color_color[] = "blue#yellow";
163 parse_pgm_token_string_t cmd_color_color = TOKEN_STRING_INITIALIZER(struct cmd_color_result, color, str_color_color);
164
165 prog_char help_color[] = "Set our color";
166 parse_pgm_inst_t cmd_color = {
167         .f = cmd_color_parsed,  /* function to call */
168         .data = NULL,      /* 2nd arg of func */
169         .help_str = help_color,
170         .tokens = {        /* token list, NULL terminated */
171                 (prog_void *)&cmd_color_arg0,
172                 (prog_void *)&cmd_color_color,
173                 NULL,
174         },
175 };
176
177 /**********************************************************/
178 /* State1 */
179
180 /* this structure is filled when cmd_state1 is parsed successfully */
181 struct cmd_state1_result {
182         fixed_string_t arg0;
183         fixed_string_t arg1;
184 };
185
186 /* function called when cmd_state1 is parsed successfully */
187 static void cmd_state1_parsed(void *parsed_result,
188                               __attribute__((unused)) void *data)
189 {
190         struct cmd_state1_result *res = parsed_result;
191
192         if (!strcmp_P(res->arg1, PSTR("init")))
193                 state_init();
194         else if (!strcmp_P(res->arg1, PSTR("eject")))
195                 state_set_mode(I2C_COBBOARD_MODE_EJECT);
196         else if (!strcmp_P(res->arg1, PSTR("kickstand")))
197                 state_set_mode(I2C_COBBOARD_MODE_KICKSTAND);
198         else if (!strcmp_P(res->arg1, PSTR("ignore_i2c")))
199                 state_set_i2c_ignore(1);
200         else if (!strcmp_P(res->arg1, PSTR("care_i2c")))
201                 state_set_i2c_ignore(0);
202
203         /* other commands */
204 }
205
206 prog_char str_state1_arg0[] = "cobboard";
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#ignore_i2c#care_i2c#kickstand";
209 parse_pgm_token_string_t cmd_state1_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_state1_result, arg1, str_state1_arg1);
210
211 prog_char help_state1[] = "set cobboard 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,
219                 NULL,
220         },
221 };
222
223 /**********************************************************/
224 /* State2 */
225
226 /* this structure is filled when cmd_state2 is parsed successfully */
227 struct cmd_state2_result {
228         fixed_string_t arg0;
229         fixed_string_t arg1;
230         fixed_string_t arg2;
231 };
232
233 /* function called when cmd_state2 is parsed successfully */
234 static void cmd_state2_parsed(void *parsed_result,
235                               __attribute__((unused)) void *data)
236 {
237         struct cmd_state2_result *res = parsed_result;
238         uint8_t side;
239
240         if (!strcmp_P(res->arg2, PSTR("left")))
241                 side = I2C_LEFT_SIDE;
242         else
243                 side = I2C_RIGHT_SIDE;
244
245         if (!strcmp_P(res->arg1, PSTR("pack"))) {
246                 state_set_mode(I2C_COBBOARD_MODE_HARVEST);
247                 state_set_spickle(side, 0);
248         }
249         else if (!strcmp_P(res->arg1, PSTR("weak_pack"))) {
250                 state_set_mode(I2C_COBBOARD_MODE_HARVEST);
251                 state_set_spickle(side, I2C_COBBOARD_SPK_WEAK);
252         }
253         else if (!strcmp_P(res->arg1, PSTR("deploy"))) {
254                 state_set_mode(I2C_COBBOARD_MODE_HARVEST);
255                 state_set_spickle(side, I2C_COBBOARD_SPK_DEPLOY);
256         }
257         else if (!strcmp_P(res->arg1, PSTR("harvest"))) {
258                 state_set_mode(I2C_COBBOARD_MODE_HARVEST);
259                 state_set_spickle(side, I2C_COBBOARD_SPK_DEPLOY |
260                                   I2C_COBBOARD_SPK_AUTOHARVEST);
261         }
262         else if (!strcmp_P(res->arg1, PSTR("deploy_nomove"))) {
263                 state_set_mode(I2C_COBBOARD_MODE_HARVEST);
264                 state_set_spickle(side, I2C_COBBOARD_SPK_DEPLOY |
265                                   I2C_COBBOARD_SPK_NO_MOVE);
266         }
267         else if (!strcmp_P(res->arg1, PSTR("harvest_nomove"))) {
268                 state_set_mode(I2C_COBBOARD_MODE_HARVEST);
269                 state_set_spickle(side, I2C_COBBOARD_SPK_DEPLOY |
270                                   I2C_COBBOARD_SPK_AUTOHARVEST |
271                                   I2C_COBBOARD_SPK_NO_MOVE);
272         }
273 }
274
275 prog_char str_state2_arg0[] = "cobboard";
276 parse_pgm_token_string_t cmd_state2_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state2_result, arg0, str_state2_arg0);
277 prog_char str_state2_arg1[] = "harvest#deploy#pack#weak_pack#harvest_nomove#deploy_nomove";
278 parse_pgm_token_string_t cmd_state2_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_state2_result, arg1, str_state2_arg1);
279 prog_char str_state2_arg2[] = "left#right";
280 parse_pgm_token_string_t cmd_state2_arg2 = TOKEN_STRING_INITIALIZER(struct cmd_state2_result, arg2, str_state2_arg2);
281
282 prog_char help_state2[] = "set cobboard mode";
283 parse_pgm_inst_t cmd_state2 = {
284         .f = cmd_state2_parsed,  /* function to call */
285         .data = NULL,      /* 2nd arg of func */
286         .help_str = help_state2,
287         .tokens = {        /* token list, NULL terminated */
288                 (prog_void *)&cmd_state2_arg0,
289                 (prog_void *)&cmd_state2_arg1,
290                 (prog_void *)&cmd_state2_arg2,
291                 NULL,
292         },
293 };
294
295 /**********************************************************/
296 /* State3 */
297
298 /* this structure is filled when cmd_state3 is parsed successfully */
299 struct cmd_state3_result {
300         fixed_string_t arg0;
301         fixed_string_t arg1;
302         uint8_t arg2;
303 };
304
305 /* function called when cmd_state3 is parsed successfully */
306 static void cmd_state3_parsed(void *parsed_result,
307                               __attribute__((unused)) void *data)
308 {
309         struct cmd_state3_result *res = parsed_result;
310
311         if (!strcmp_P(res->arg1, PSTR("xxx"))) {
312                 /* xxx = res->arg2 */
313         }
314         else if (!strcmp_P(res->arg1, PSTR("yyy"))) {
315         }
316         state_set_mode(0);
317 }
318
319 prog_char str_state3_arg0[] = "cobboard";
320 parse_pgm_token_string_t cmd_state3_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state3_result, arg0, str_state3_arg0);
321 prog_char str_state3_arg1[] = "xxx";
322 parse_pgm_token_string_t cmd_state3_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_state3_result, arg1, str_state3_arg1);
323 parse_pgm_token_num_t cmd_state3_arg2 = TOKEN_NUM_INITIALIZER(struct cmd_state3_result, arg2, UINT8);
324
325 prog_char help_state3[] = "set cobboard mode";
326 parse_pgm_inst_t cmd_state3 = {
327         .f = cmd_state3_parsed,  /* function to call */
328         .data = NULL,      /* 2nd arg of func */
329         .help_str = help_state3,
330         .tokens = {        /* token list, NULL terminated */
331                 (prog_void *)&cmd_state3_arg0,
332                 (prog_void *)&cmd_state3_arg1,
333                 (prog_void *)&cmd_state3_arg2,
334                 NULL,
335         },
336 };
337
338 /**********************************************************/
339 /* State_Machine */
340
341 /* this structure is filled when cmd_state_machine is parsed successfully */
342 struct cmd_state_machine_result {
343         fixed_string_t arg0;
344 };
345
346 /* function called when cmd_state_machine is parsed successfully */
347 static void cmd_state_machine_parsed(__attribute__((unused)) void *parsed_result,
348                                      __attribute__((unused)) void *data)
349 {
350         state_machine();
351 }
352
353 prog_char str_state_machine_arg0[] = "state_machine";
354 parse_pgm_token_string_t cmd_state_machine_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state_machine_result, arg0, str_state_machine_arg0);
355
356 prog_char help_state_machine[] = "launch state machine";
357 parse_pgm_inst_t cmd_state_machine = {
358         .f = cmd_state_machine_parsed,  /* function to call */
359         .data = NULL,      /* 2nd arg of func */
360         .help_str = help_state_machine,
361         .tokens = {        /* token list, NULL terminated */
362                 (prog_void *)&cmd_state_machine_arg0,
363                 NULL,
364         },
365 };
366
367 /**********************************************************/
368 /* State_Debug */
369
370 /* this structure is filled when cmd_state_debug is parsed successfully */
371 struct cmd_state_debug_result {
372         fixed_string_t arg0;
373         uint8_t on;
374 };
375
376 /* function called when cmd_state_debug is parsed successfully */
377 static void cmd_state_debug_parsed(void *parsed_result,
378                                    __attribute__((unused)) void *data)
379 {
380         struct cmd_state_debug_result *res = parsed_result;
381         state_debug = res->on;
382 }
383
384 prog_char str_state_debug_arg0[] = "state_debug";
385 parse_pgm_token_string_t cmd_state_debug_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_state_debug_result, arg0, str_state_debug_arg0);
386 parse_pgm_token_num_t cmd_state_debug_on = TOKEN_NUM_INITIALIZER(struct cmd_state_debug_result, on, UINT8);
387
388 prog_char help_state_debug[] = "Set debug for state machine";
389 parse_pgm_inst_t cmd_state_debug = {
390         .f = cmd_state_debug_parsed,  /* function to call */
391         .data = NULL,      /* 2nd arg of func */
392         .help_str = help_state_debug,
393         .tokens = {        /* token list, NULL terminated */
394                 (prog_void *)&cmd_state_debug_arg0,
395                 (prog_void *)&cmd_state_debug_on,
396                 NULL,
397         },
398 };
399
400 /**********************************************************/
401 /* Servo_Door */
402
403 /* this structure is filled when cmd_servo_door is parsed successfully */
404 struct cmd_servo_door_result {
405         fixed_string_t arg0;
406         fixed_string_t arg1;
407 };
408
409 /* function called when cmd_servo_door is parsed successfully */
410 static void cmd_servo_door_parsed(void *parsed_result,
411                                     __attribute__((unused)) void *data)
412 {
413         struct cmd_servo_door_result *res = parsed_result;
414         if (!strcmp_P(res->arg1, PSTR("open")))
415                 servo_door_open();
416         else if (!strcmp_P(res->arg1, PSTR("closed")))
417                 servo_door_close();
418         else if (!strcmp_P(res->arg1, PSTR("block")))
419                 servo_door_close();
420 }
421
422 prog_char str_servo_door_arg0[] = "door";
423 parse_pgm_token_string_t cmd_servo_door_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_servo_door_result, arg0, str_servo_door_arg0);
424 prog_char str_servo_door_arg1[] = "open#closed#block";
425 parse_pgm_token_string_t cmd_servo_door_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_servo_door_result, arg1, str_servo_door_arg1);
426
427 prog_char help_servo_door[] = "Servo door function";
428 parse_pgm_inst_t cmd_servo_door = {
429         .f = cmd_servo_door_parsed,  /* function to call */
430         .data = NULL,      /* 2nd arg of func */
431         .help_str = help_servo_door,
432         .tokens = {        /* token list, NULL terminated */
433                 (prog_void *)&cmd_servo_door_arg0,
434                 (prog_void *)&cmd_servo_door_arg1,
435                 NULL,
436         },
437 };
438
439 /**********************************************************/
440 /* cobroller */
441
442 /* this structure is filled when cmd_cobroller is parsed successfully */
443 struct cmd_cobroller_result {
444         fixed_string_t arg0;
445         fixed_string_t arg1;
446         fixed_string_t arg2;
447 };
448
449 /* function called when cmd_cobroller is parsed successfully */
450 static void cmd_cobroller_parsed(void *parsed_result,
451                                     __attribute__((unused)) void *data)
452 {
453         struct cmd_cobroller_result *res = parsed_result;
454         uint8_t side;
455
456         if (!strcmp_P(res->arg1, PSTR("left")))
457                 side = I2C_LEFT_SIDE;
458         else
459                 side = I2C_RIGHT_SIDE;
460
461         if (!strcmp_P(res->arg2, PSTR("on")))
462                 cobroller_on(side);
463         else if (!strcmp_P(res->arg2, PSTR("off")))
464                 cobroller_off(side);
465 }
466
467 prog_char str_cobroller_arg0[] = "cobroller";
468 parse_pgm_token_string_t cmd_cobroller_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_cobroller_result, arg0, str_cobroller_arg0);
469 prog_char str_cobroller_arg1[] = "left#right";
470 parse_pgm_token_string_t cmd_cobroller_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_cobroller_result, arg1, str_cobroller_arg1);
471 prog_char str_cobroller_arg2[] = "on#off";
472 parse_pgm_token_string_t cmd_cobroller_arg2 = TOKEN_STRING_INITIALIZER(struct cmd_cobroller_result, arg2, str_cobroller_arg2);
473
474 prog_char help_cobroller[] = "Servo door function";
475 parse_pgm_inst_t cmd_cobroller = {
476         .f = cmd_cobroller_parsed,  /* function to call */
477         .data = NULL,      /* 2nd arg of func */
478         .help_str = help_cobroller,
479         .tokens = {        /* token list, NULL terminated */
480                 (prog_void *)&cmd_cobroller_arg0,
481                 (prog_void *)&cmd_cobroller_arg1,
482                 (prog_void *)&cmd_cobroller_arg2,
483                 NULL,
484         },
485 };
486
487 /**********************************************************/
488 /* shovel */
489
490 /* this structure is filled when cmd_shovel is parsed successfully */
491 struct cmd_shovel_result {
492         fixed_string_t arg0;
493         fixed_string_t arg1;
494 };
495
496 /* function called when cmd_shovel is parsed successfully */
497 static void cmd_shovel_parsed(void *parsed_result,
498                               __attribute__((unused)) void *data)
499 {
500         struct cmd_shovel_result *res = parsed_result;
501         if (!strcmp_P(res->arg1, PSTR("down")))
502                 shovel_down();
503         else if (!strcmp_P(res->arg1, PSTR("up")))
504                 shovel_up();
505         else if (!strcmp_P(res->arg1, PSTR("mid")))
506                 shovel_mid();
507 }
508
509 prog_char str_shovel_arg0[] = "shovel";
510 parse_pgm_token_string_t cmd_shovel_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_shovel_result, arg0, str_shovel_arg0);
511 prog_char str_shovel_arg1[] = "down#up#mid";
512 parse_pgm_token_string_t cmd_shovel_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_shovel_result, arg1, str_shovel_arg1);
513
514 prog_char help_shovel[] = "Servo shovel function";
515 parse_pgm_inst_t cmd_shovel = {
516         .f = cmd_shovel_parsed,  /* function to call */
517         .data = NULL,      /* 2nd arg of func */
518         .help_str = help_shovel,
519         .tokens = {        /* token list, NULL terminated */
520                 (prog_void *)&cmd_shovel_arg0,
521                 (prog_void *)&cmd_shovel_arg1,
522                 NULL,
523         },
524 };
525
526 /**********************************************************/
527 /* Servo_Carry */
528
529 /* this structure is filled when cmd_servo_carry is parsed successfully */
530 struct cmd_servo_carry_result {
531         fixed_string_t arg0;
532         fixed_string_t arg1;
533 };
534
535 /* function called when cmd_servo_carry is parsed successfully */
536 static void cmd_servo_carry_parsed(void *parsed_result,
537                                     __attribute__((unused)) void *data)
538 {
539         struct cmd_servo_carry_result *res = parsed_result;
540         if (!strcmp_P(res->arg1, PSTR("open")))
541                 servo_carry_open();
542         else if (!strcmp_P(res->arg1, PSTR("closed")))
543                 servo_carry_close();
544 }
545
546 prog_char str_servo_carry_arg0[] = "carry";
547 parse_pgm_token_string_t cmd_servo_carry_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_servo_carry_result, arg0, str_servo_carry_arg0);
548 prog_char str_servo_carry_arg1[] = "open#closed";
549 parse_pgm_token_string_t cmd_servo_carry_arg1 = TOKEN_STRING_INITIALIZER(struct cmd_servo_carry_result, arg1, str_servo_carry_arg1);
550
551 prog_char help_servo_carry[] = "Servo carry function";
552 parse_pgm_inst_t cmd_servo_carry = {
553         .f = cmd_servo_carry_parsed,  /* function to call */
554         .data = NULL,      /* 2nd arg of func */
555         .help_str = help_servo_carry,
556         .tokens = {        /* token list, NULL terminated */
557                 (prog_void *)&cmd_servo_carry_arg0,
558                 (prog_void *)&cmd_servo_carry_arg1,
559                 NULL,
560         },
561 };
562
563 /**********************************************************/
564 /* Spickle tests */
565
566 /* this structure is filled when cmd_spickle is parsed successfully */
567 struct cmd_spickle_result {
568         fixed_string_t arg0;
569         fixed_string_t arg1;
570         fixed_string_t arg2;
571 };
572
573 /* function called when cmd_spickle is parsed successfully */
574 static void cmd_spickle_parsed(void * parsed_result,
575                                __attribute__((unused)) void *data)
576 {
577         struct cmd_spickle_result * res = parsed_result;
578         uint8_t side;
579
580         if (!strcmp_P(res->arg1, PSTR("left")))
581                 side = I2C_LEFT_SIDE;
582         else
583                 side = I2C_RIGHT_SIDE;
584
585         if (!strcmp_P(res->arg2, PSTR("deploy"))) {
586                 spickle_deploy(side);
587         }
588         else if (!strcmp_P(res->arg2, PSTR("pack"))) {
589                 spickle_pack(side);
590         }
591         else if (!strcmp_P(res->arg2, PSTR("mid"))) {
592                 spickle_mid(side);
593         }
594         printf_P(PSTR("done\r\n"));
595 }
596
597 prog_char str_spickle_arg0[] = "spickle";
598 parse_pgm_token_string_t cmd_spickle_arg0 =
599         TOKEN_STRING_INITIALIZER(struct cmd_spickle_result, arg0, str_spickle_arg0);
600 prog_char str_spickle_arg1[] = "left#right";
601 parse_pgm_token_string_t cmd_spickle_arg1 =
602         TOKEN_STRING_INITIALIZER(struct cmd_spickle_result, arg1, str_spickle_arg1);
603 prog_char str_spickle_arg2[] = "deploy#pack#mid";
604 parse_pgm_token_string_t cmd_spickle_arg2 =
605         TOKEN_STRING_INITIALIZER(struct cmd_spickle_result, arg2, str_spickle_arg2);
606
607 prog_char help_spickle[] = "move spickle";
608 parse_pgm_inst_t cmd_spickle = {
609         .f = cmd_spickle_parsed,  /* function to call */
610         .data = NULL,      /* 2nd arg of func */
611         .help_str = help_spickle,
612         .tokens = {        /* token list, NULL terminated */
613                 (prog_void *)&cmd_spickle_arg0,
614                 (prog_void *)&cmd_spickle_arg1,
615                 (prog_void *)&cmd_spickle_arg2,
616                 NULL,
617         },
618 };
619
620 /**********************************************************/
621 /* Set Spickle Params */
622
623 /* this structure is filled when cmd_spickle_params is parsed successfully */
624 struct cmd_spickle_params_result {
625         fixed_string_t arg0;
626         fixed_string_t arg1;
627         fixed_string_t arg2;
628         int32_t arg3;
629         int32_t arg4;
630         int32_t arg5;
631 };
632
633 /* function called when cmd_spickle_params is parsed successfully */
634 static void cmd_spickle_params_parsed(void *parsed_result,
635                                       __attribute__((unused)) void *data)
636 {
637         struct cmd_spickle_params_result * res = parsed_result;
638         uint8_t side;
639
640         if (!strcmp_P(res->arg1, PSTR("show"))) {
641                 spickle_dump_params();
642                 return;
643         }
644
645         if (!strcmp_P(res->arg1, PSTR("left")))
646                 side = I2C_LEFT_SIDE;
647         else
648                 side = I2C_RIGHT_SIDE;
649
650         if (!strcmp_P(res->arg2, PSTR("pos")))
651                 spickle_set_pos(side, res->arg3, res->arg4, res->arg5);
652 }
653
654 prog_char str_spickle_params_arg0[] = "spickle_params";
655 parse_pgm_token_string_t cmd_spickle_params_arg0 =
656         TOKEN_STRING_INITIALIZER(struct cmd_spickle_params_result, arg0, str_spickle_params_arg0);
657 prog_char str_spickle_params_arg1[] = "left#right";
658 parse_pgm_token_string_t cmd_spickle_params_arg1 =
659         TOKEN_STRING_INITIALIZER(struct cmd_spickle_params_result, arg1, str_spickle_params_arg1);
660 prog_char str_spickle_params_arg2[] = "pos";
661 parse_pgm_token_string_t cmd_spickle_params_arg2 =
662         TOKEN_STRING_INITIALIZER(struct cmd_spickle_params_result, arg2, str_spickle_params_arg2);
663 parse_pgm_token_num_t cmd_spickle_params_arg3 =
664         TOKEN_NUM_INITIALIZER(struct cmd_spickle_params_result, arg3, INT32);
665 parse_pgm_token_num_t cmd_spickle_params_arg4 =
666         TOKEN_NUM_INITIALIZER(struct cmd_spickle_params_result, arg4, INT32);
667 parse_pgm_token_num_t cmd_spickle_params_arg5 =
668         TOKEN_NUM_INITIALIZER(struct cmd_spickle_params_result, arg5, INT32);
669
670 prog_char help_spickle_params[] = "Set spickle pos values: left|right pos INTPACK INTMID INTDEPL";
671 parse_pgm_inst_t cmd_spickle_params = {
672         .f = cmd_spickle_params_parsed,  /* function to call */
673         .data = NULL,      /* 2nd arg of func */
674         .help_str = help_spickle_params,
675         .tokens = {        /* token list, NULL terminated */
676                 (prog_void *)&cmd_spickle_params_arg0,
677                 (prog_void *)&cmd_spickle_params_arg1,
678                 (prog_void *)&cmd_spickle_params_arg2,
679                 (prog_void *)&cmd_spickle_params_arg3,
680                 (prog_void *)&cmd_spickle_params_arg4,
681                 (prog_void *)&cmd_spickle_params_arg5,
682                 NULL,
683         },
684 };
685
686 prog_char str_spickle_params_arg1_show[] = "show";
687 parse_pgm_token_string_t cmd_spickle_params_arg1_show =
688         TOKEN_STRING_INITIALIZER(struct cmd_spickle_params_result, arg1, str_spickle_params_arg1_show);
689
690 prog_char help_spickle_params_show[] = "show spickle params";
691 parse_pgm_inst_t cmd_spickle_params_show = {
692         .f = cmd_spickle_params_parsed,  /* function to call */
693         .data = NULL,      /* 2nd arg of func */
694         .help_str = help_spickle_params_show,
695         .tokens = {        /* token list, NULL terminated */
696                 (prog_void *)&cmd_spickle_params_arg0,
697                 (prog_void *)&cmd_spickle_params_arg1_show,
698                 NULL,
699         },
700 };
701
702 /**********************************************************/
703 /* Set Spickle Params */
704
705 /* this structure is filled when cmd_spickle_params2 is parsed successfully */
706 struct cmd_spickle_params2_result {
707         fixed_string_t arg0;
708         fixed_string_t arg1;
709         int32_t arg2;
710         int32_t arg3;
711 };
712
713 /* function called when cmd_spickle_params2 is parsed successfully */
714 static void cmd_spickle_params2_parsed(void *parsed_result,
715                                       __attribute__((unused)) void *data)
716 {
717         struct cmd_spickle_params2_result * res = parsed_result;
718
719         if (!strcmp_P(res->arg1, PSTR("wcoef"))) {
720                 spickle_set_wcoefs(res->arg2, res->arg3);
721         }
722         else if (!strcmp_P(res->arg1, PSTR("scoef"))) {
723                 spickle_set_scoefs(res->arg2, res->arg3);
724         }
725
726         /* else show */
727         spickle_dump_params();
728 }
729
730 prog_char str_spickle_params2_arg0[] = "spickle_params2";
731 parse_pgm_token_string_t cmd_spickle_params2_arg0 =
732         TOKEN_STRING_INITIALIZER(struct cmd_spickle_params2_result, arg0, str_spickle_params2_arg0);
733 prog_char str_spickle_params2_arg1[] = "wcoef#scoef";
734 parse_pgm_token_string_t cmd_spickle_params2_arg1 =
735         TOKEN_STRING_INITIALIZER(struct cmd_spickle_params2_result, arg1, str_spickle_params2_arg1);
736 parse_pgm_token_num_t cmd_spickle_params2_arg2 =
737         TOKEN_NUM_INITIALIZER(struct cmd_spickle_params2_result, arg2, INT32);
738 parse_pgm_token_num_t cmd_spickle_params2_arg3 =
739         TOKEN_NUM_INITIALIZER(struct cmd_spickle_params2_result, arg3, INT32);
740
741 prog_char help_spickle_params2[] = "Set spickle_params2 values";
742 parse_pgm_inst_t cmd_spickle_params2 = {
743         .f = cmd_spickle_params2_parsed,  /* function to call */
744         .data = NULL,      /* 2nd arg of func */
745         .help_str = help_spickle_params2,
746         .tokens = {        /* token list, NULL terminated */
747                 (prog_void *)&cmd_spickle_params2_arg0,
748                 (prog_void *)&cmd_spickle_params2_arg1,
749                 (prog_void *)&cmd_spickle_params2_arg2,
750                 (prog_void *)&cmd_spickle_params2_arg3,
751                 NULL,
752         },
753 };
754
755 prog_char str_spickle_params2_arg1_show[] = "show";
756 parse_pgm_token_string_t cmd_spickle_params2_arg1_show =
757         TOKEN_STRING_INITIALIZER(struct cmd_spickle_params2_result, arg1, str_spickle_params2_arg1_show);
758
759 prog_char help_spickle_params2_show[] = "show spickle params";
760 parse_pgm_inst_t cmd_spickle_params2_show = {
761         .f = cmd_spickle_params2_parsed,  /* function to call */
762         .data = NULL,      /* 2nd arg of func */
763         .help_str = help_spickle_params2_show,
764         .tokens = {        /* token list, NULL terminated */
765                 (prog_void *)&cmd_spickle_params2_arg0,
766                 (prog_void *)&cmd_spickle_params2_arg1_show,
767                 NULL,
768         },
769 };
770
771 /**********************************************************/
772 /* Set Shovel Params */
773
774 /* this structure is filled when cmd_shovel_current is parsed successfully */
775 struct cmd_shovel_current_result {
776         fixed_string_t arg0;
777         fixed_string_t arg1;
778         int32_t arg2;
779         int32_t arg3;
780 };
781
782 /* function called when cmd_shovel_current is parsed successfully */
783 static void cmd_shovel_current_parsed(void *parsed_result,
784                                       __attribute__((unused)) void *data)
785 {
786         struct cmd_shovel_current_result * res = parsed_result;
787         uint8_t enable;
788         int32_t k1, k2;
789
790         if (!strcmp_P(res->arg1, PSTR("set")))
791                 shovel_set_current_limit_coefs(res->arg2, res->arg3);
792         else if (!strcmp_P(res->arg1, PSTR("on")))
793                 shovel_current_limit_enable(1);
794         else if (!strcmp_P(res->arg1, PSTR("off")))
795                 shovel_current_limit_enable(0);
796
797         /* else show */
798         enable = shovel_get_current_limit_coefs(&k1, &k2);
799         printf_P(PSTR("enabled=%d k1=%"PRIi32" k2=%"PRIi32"\r\n"),
800                  enable, k1, k2);
801 }
802
803 prog_char str_shovel_current_arg0[] = "shovel_current";
804 parse_pgm_token_string_t cmd_shovel_current_arg0 =
805         TOKEN_STRING_INITIALIZER(struct cmd_shovel_current_result, arg0, str_shovel_current_arg0);
806 prog_char str_shovel_current_arg1[] = "set";
807 parse_pgm_token_string_t cmd_shovel_current_arg1 =
808         TOKEN_STRING_INITIALIZER(struct cmd_shovel_current_result, arg1, str_shovel_current_arg1);
809 parse_pgm_token_num_t cmd_shovel_current_arg2 =
810         TOKEN_NUM_INITIALIZER(struct cmd_shovel_current_result, arg2, INT32);
811 parse_pgm_token_num_t cmd_shovel_current_arg3 =
812         TOKEN_NUM_INITIALIZER(struct cmd_shovel_current_result, arg3, INT32);
813
814 prog_char help_shovel_current[] = "Set shovel_current values";
815 parse_pgm_inst_t cmd_shovel_current = {
816         .f = cmd_shovel_current_parsed,  /* function to call */
817         .data = NULL,      /* 2nd arg of func */
818         .help_str = help_shovel_current,
819         .tokens = {        /* token list, NULL terminated */
820                 (prog_void *)&cmd_shovel_current_arg0,
821                 (prog_void *)&cmd_shovel_current_arg1,
822                 (prog_void *)&cmd_shovel_current_arg2,
823                 (prog_void *)&cmd_shovel_current_arg3,
824                 NULL,
825         },
826 };
827
828 prog_char str_shovel_current_arg1_show[] = "show#on#off";
829 parse_pgm_token_string_t cmd_shovel_current_arg1_show =
830         TOKEN_STRING_INITIALIZER(struct cmd_shovel_current_result, arg1, str_shovel_current_arg1_show);
831
832 prog_char help_shovel_current_show[] = "show shovel params";
833 parse_pgm_inst_t cmd_shovel_current_show = {
834         .f = cmd_shovel_current_parsed,  /* function to call */
835         .data = NULL,      /* 2nd arg of func */
836         .help_str = help_shovel_current_show,
837         .tokens = {        /* token list, NULL terminated */
838                 (prog_void *)&cmd_shovel_current_arg0,
839                 (prog_void *)&cmd_shovel_current_arg1_show,
840                 NULL,
841         },
842 };
843
844 /**********************************************************/
845 /* Test */
846
847 /* this structure is filled when cmd_test is parsed successfully */
848 struct cmd_test_result {
849         fixed_string_t arg0;
850 };
851
852 /* function called when cmd_test is parsed successfully */
853 static void cmd_test_parsed(__attribute__((unused)) void *parsed_result,
854                             __attribute__((unused)) void *data)
855 {
856 }
857
858 prog_char str_test_arg0[] = "test";
859 parse_pgm_token_string_t cmd_test_arg0 =
860         TOKEN_STRING_INITIALIZER(struct cmd_test_result, arg0, str_test_arg0);
861
862 prog_char help_test[] = "Test function";
863 parse_pgm_inst_t cmd_test = {
864         .f = cmd_test_parsed,  /* function to call */
865         .data = NULL,      /* 2nd arg of func */
866         .help_str = help_test,
867         .tokens = {        /* token list, NULL terminated */
868                 (prog_void *)&cmd_test_arg0,
869                 NULL,
870         },
871 };