5 #include <aversive/pgmspace.h>
6 #include <aversive/wait.h>
10 #include <parse_string.h>
14 /**********************************************************/
15 /* operations on float */
17 /* this structure is filled when cmd_float is parsed successfully */
18 struct cmd_float_result {
24 /* function called when cmd_float is parsed successfully */
25 static void cmd_float_parsed(void * parsed_result, void * data)
27 struct cmd_float_result * cmd = (struct cmd_float_result *) parsed_result;
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;
37 printf_P(PSTR("%f\n"), res);
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);
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,
59 /**********************************************************/
60 /* operations on trigo */
62 /* this structure is filled when cmd_trigo is parsed successfully */
63 struct cmd_trigo_result {
68 /* function called when cmd_trigo is parsed successfully */
69 static void cmd_trigo_parsed(void * parsed_result, void * data)
71 struct cmd_trigo_result * cmd = (struct cmd_trigo_result *) parsed_result;
74 if (!strcmp_P(cmd->op, PSTR("sin"))) {
78 else if (!strcmp_P(cmd->op, PSTR("cos"))) {
82 else if (!strcmp_P(cmd->op, PSTR("tan"))) {
86 printf_P(PSTR("%f\n"), res);
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);
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,
106 /**********************************************************/
109 /* this structure is filled when cmd_help is parsed successfully */
110 struct cmd_help_result {
114 /* function called when cmd_help is parsed successfully */
115 static void cmd_help_parsed(void * parsed_result, void * data)
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"
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);
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,
141 /**********************************************************/
142 /**********************************************************/
143 /****** CONTEXT (list of instruction) */
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,