config: update test config files to current aversive config.in
[aversive.git] / modules / ihm / parse / test / commands.c
1 #include <math.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 #include <aversive/pgmspace.h>
6 #include <aversive/wait.h>
7
8 #include <parse.h>
9 #include <parse_num.h>
10 #include <parse_string.h>
11
12
13
14 /**********************************************************/
15 /* operations on float */
16
17 /* this structure is filled when cmd_float is parsed successfully */
18 struct cmd_float_result {
19         float a;
20         fixed_string_t op;
21         float b;
22 };
23
24 /* function called when cmd_float is parsed successfully */
25 static void cmd_float_parsed(void * parsed_result, void * data)
26 {
27         struct cmd_float_result * cmd = (struct cmd_float_result *) parsed_result;
28         float res=0.;
29
30         switch(cmd->op[0]) {
31         case '+': res = cmd->a + cmd->b; break;
32         case '-': res = cmd->a - cmd->b; break;
33         case '*': res = cmd->a * cmd->b; break;
34         case '/': res = cmd->a / cmd->b; break;
35         default: break;
36         }
37         printf_P(PSTR("%f\n"), res);
38 }
39
40 parse_pgm_token_num_t cmd_float_a = TOKEN_NUM_INITIALIZER(struct cmd_float_result, a, FLOAT);
41 prog_char str_float_op[] = "+#-#*#/";
42 parse_pgm_token_string_t cmd_float_op = TOKEN_STRING_INITIALIZER(struct cmd_float_result, op, str_float_op);
43 parse_pgm_token_num_t cmd_float_b = TOKEN_NUM_INITIALIZER(struct cmd_float_result, b, FLOAT);
44
45 prog_char help_float[] = "Operation on float (ex: '2 + 5.4')";
46 parse_pgm_inst_t cmd_float = {
47         .f = cmd_float_parsed,  /* function to call */
48         .data = NULL,      /* 2nd arg of func */
49         .help_str = help_float,
50         .tokens = {        /* token list, NULL terminated */
51                 (prog_void *)&cmd_float_a, 
52                 (prog_void *)&cmd_float_op, 
53                 (prog_void *)&cmd_float_b, 
54                 NULL,
55         },
56 };
57
58
59 /**********************************************************/
60 /* operations on trigo */
61
62 /* this structure is filled when cmd_trigo is parsed successfully */
63 struct cmd_trigo_result {
64         fixed_string_t op;
65         float a;
66 };
67
68 /* function called when cmd_trigo is parsed successfully */
69 static void cmd_trigo_parsed(void * parsed_result, void * data)
70 {
71         struct cmd_trigo_result * cmd = (struct cmd_trigo_result *) parsed_result;
72         float res=0.;
73         
74         if (!strcmp_P(cmd->op, PSTR("sin"))) {
75                 res = sin(cmd->a);
76         }
77
78         else if (!strcmp_P(cmd->op, PSTR("cos"))) {
79                 res = cos(cmd->a);
80         }
81
82         else if (!strcmp_P(cmd->op, PSTR("tan"))) {
83                 res = tan(cmd->a);
84         }
85
86         printf_P(PSTR("%f\n"), res);
87 }
88
89 prog_char str_trigo_op[] = "sin#tan#cos";
90 parse_pgm_token_string_t cmd_trigo_op = TOKEN_STRING_INITIALIZER(struct cmd_trigo_result, op, str_trigo_op);
91 parse_pgm_token_num_t cmd_trigo_a = TOKEN_NUM_INITIALIZER(struct cmd_trigo_result, a, FLOAT);
92
93 prog_char help_trigo[] = "Trigonometric operations (ex: 'sin 2.03')";
94 parse_pgm_inst_t cmd_trigo = {
95         .f = cmd_trigo_parsed,  /* function to call */
96         .data = NULL,      /* 2nd arg of func */
97         .help_str = help_trigo,
98         .tokens = {        /* token list, NULL terminated */
99                 (prog_void *)&cmd_trigo_op, 
100                 (prog_void *)&cmd_trigo_a, 
101                 NULL,
102         },
103 };
104
105
106 /**********************************************************/
107 /* Help */
108
109 /* this structure is filled when cmd_help is parsed successfully */
110 struct cmd_help_result {
111         fixed_string_t arg0;
112 };
113
114 /* function called when cmd_help is parsed successfully */
115 static void cmd_help_parsed(void * parsed_result, void * data)
116 {
117         printf_P(PSTR("== Simple calculator program ==\n"
118                       "You can do simple operations on floats, like '1 + 3'\n"
119                       "or '4.4 * 2.' (space is important).\n"
120                       "Some trigonometric operations are available, like\n"
121                       "'sin 4.5'.\n"));
122 }
123
124 prog_char str_help_arg0[] = "help";
125 parse_pgm_token_string_t cmd_help_arg0 = TOKEN_STRING_INITIALIZER(struct cmd_help_result, arg0, str_help_arg0);
126
127 prog_char help_help[] = "Display help";
128 parse_pgm_inst_t cmd_help = {
129         .f = cmd_help_parsed,  /* function to call */
130         .data = NULL,      /* 2nd arg of func */
131         .help_str = help_help,
132         .tokens = {        /* token list, NULL terminated */
133                 (prog_void *)&cmd_help_arg0, 
134                 NULL,
135         },
136 };
137
138
139
140
141 /**********************************************************/
142 /**********************************************************/
143 /****** CONTEXT (list of instruction) */
144
145 /* in progmem */
146 parse_pgm_ctx_t main_ctx[] = {
147         (parse_pgm_inst_t *)&cmd_float, 
148         (parse_pgm_inst_t *)&cmd_trigo, 
149         (parse_pgm_inst_t *)&cmd_help, 
150         NULL,
151 };
152