beacon_tsop
[aversive.git] / projects / microb2010 / tests / beacon_tsop / commands.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.c,v 1.7 2009-04-24 19:30:41 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 <pid.h>
31 #include <pwm_ng.h>
32 #include <parse.h>
33 #include <rdline.h>
34 #include <parse_string.h>
35 #include <vect_base.h>
36 #include <lines.h>
37 #include <circles.h>
38
39 #include "trigo.h"
40 #include "main.h"
41
42 /**********************************************************/
43 /* Reset */
44
45 /* this structure is filled when cmd_reset is parsed successfully */
46 struct cmd_reset_result {
47         fixed_string_t arg0;
48 };
49
50 /* function called when cmd_reset is parsed successfully */
51 static void cmd_reset_parsed(__attribute__((unused)) void *parsed_result,
52                              __attribute__((unused)) void *data)
53 {
54         reset();
55 }
56
57 prog_char str_reset_arg0[] = "reset";
58 parse_pgm_token_string_t cmd_reset_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_reset_result, arg0, str_reset_arg0);
59
60 prog_char help_reset[] = "Reset the board";
61 parse_pgm_inst_t cmd_reset = {
62         .f = cmd_reset_parsed,  /* function to call */
63         .data = NULL,      /* 2nd arg of func */
64         .help_str = help_reset,
65         .tokens = {        /* token list, NULL terminated */
66                 (prog_void *)&cmd_reset_arg0, 
67                 NULL,
68         },
69 };
70
71 /**********************************************************/
72 /* Debug_Frame */
73
74 /* this structure is filled when cmd_debug_frame is parsed successfully */
75 struct cmd_debug_frame_result {
76         fixed_string_t arg0;
77         fixed_string_t arg1;
78 };
79
80 /* function called when cmd_debug_frame is parsed successfully */
81 static void cmd_debug_frame_parsed(void *parsed_result,
82                                    __attribute__((unused)) void *data)
83 {
84         struct cmd_debug_frame_result *res = parsed_result;
85         if (!strcmp_P(res->arg1, PSTR("on")))
86                 beacon_tsop.debug_frame = 1;
87         else
88                 beacon_tsop.debug_frame = 0;
89 }
90
91 prog_char str_debug_frame_arg0[] = "debug_frame";
92 parse_pgm_token_string_t cmd_debug_frame_arg0 =
93         TOKEN_STRING_INITIALIZER(struct cmd_debug_frame_result,
94                                  arg0, str_debug_frame_arg0);
95 prog_char str_debug_frame_arg1[] = "on#off";
96 parse_pgm_token_string_t cmd_debug_frame_arg1 =
97         TOKEN_STRING_INITIALIZER(struct cmd_debug_frame_result,
98                                  arg1, str_debug_frame_arg1);
99
100 prog_char help_debug_frame[] = "Enable frame debug [debug_frame on|off]";
101 parse_pgm_inst_t cmd_debug_frame = {
102         .f = cmd_debug_frame_parsed,  /* function to call */
103         .data = NULL,      /* 2nd arg of func */
104         .help_str = help_debug_frame,
105         .tokens = {        /* token list, NULL terminated */
106                 (prog_void *)&cmd_debug_frame_arg0, 
107                 (prog_void *)&cmd_debug_frame_arg1, 
108                 NULL,
109         },
110 };
111
112 /**********************************************************/
113 /* Debug_Speed */
114
115 /* this structure is filled when cmd_debug_speed is parsed successfully */
116 struct cmd_debug_speed_result {
117         fixed_string_t arg0;
118         fixed_string_t arg1;
119 };
120
121 /* function called when cmd_debug_speed is parsed successfully */
122 static void cmd_debug_speed_parsed(void *parsed_result,
123                                    __attribute__((unused)) void *data)
124 {
125         struct cmd_debug_speed_result *res = parsed_result;
126         if (!strcmp_P(res->arg1, PSTR("on")))
127                 beacon_tsop.debug_speed = 1;
128         else
129                 beacon_tsop.debug_speed = 0;
130 }
131
132 prog_char str_debug_speed_arg0[] = "debug_speed";
133 parse_pgm_token_string_t cmd_debug_speed_arg0 =
134         TOKEN_STRING_INITIALIZER(struct cmd_debug_speed_result,
135                                  arg0, str_debug_speed_arg0);
136 prog_char str_debug_speed_arg1[] = "on#off";
137 parse_pgm_token_string_t cmd_debug_speed_arg1 =
138         TOKEN_STRING_INITIALIZER(struct cmd_debug_speed_result,
139                                  arg1, str_debug_speed_arg1);
140
141 prog_char help_debug_speed[] = "Enable speed debug [debug_speed on|off]";
142 parse_pgm_inst_t cmd_debug_speed = {
143         .f = cmd_debug_speed_parsed,  /* function to call */
144         .data = NULL,      /* 2nd arg of func */
145         .help_str = help_debug_speed,
146         .tokens = {        /* token list, NULL terminated */
147                 (prog_void *)&cmd_debug_speed_arg0, 
148                 (prog_void *)&cmd_debug_speed_arg1, 
149                 NULL,
150         },
151 };
152
153 /**********************************************************/
154 /* Test */
155
156 /* this structure is filled when cmd_test is parsed successfully */
157 struct cmd_test_result {
158         fixed_string_t arg0;
159 };
160
161 /* function called when cmd_test is parsed successfully */
162 static void cmd_test_parsed(__attribute__((unused)) void *parsed_result,
163                              __attribute__((unused)) void *data)
164 {
165         point_t pos;
166         double a01 = 1.65;
167         double a12 = 2.12;
168         double a20 = 2.53;
169         uint16_t time1, time2;
170
171         cli();
172         time1 = TCNT3;
173         sei();
174
175         if (angles_to_posxy(&pos, a01, a12, a20) < 0)
176                 printf_P(PSTR("error\n"));
177
178         cli();
179         time2 = TCNT3;
180         sei();
181
182         printf_P(PSTR("%2.2f %2.2f (t=%d)\n"), pos.x, pos.y, time2-time1);
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 the board";
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 };
198
199 /**********************************************************/
200
201 /* in progmem */
202 parse_pgm_ctx_t main_ctx[] = {
203
204         /* commands_gen.c */
205         (parse_pgm_inst_t *)&cmd_reset,
206         (parse_pgm_inst_t *)&cmd_debug_frame,
207         (parse_pgm_inst_t *)&cmd_debug_speed,
208         (parse_pgm_inst_t *)&cmd_test,
209         NULL,
210 };