add unified compile target
[avr_work.git] / lline / text_cmd.c
blob61fcb80a0b47e9c48ae9bf69443119078d689b92
2 #include "common.h"
4 #include <stdlib.h>
6 #include "motor.h"
7 #include "spi_io.h"
8 #include "adc.h"
10 void print_adc_get_i(uint8_t channel_i) {
11 spi_puts("ADC ");
12 spi_puth(channel_i);
13 spi_putchar(' ');
14 spi_putchar(':');
15 spi_puth2(adc_get_i(channel_i));
16 spi_putchar('\n');
19 typedef struct text_cmd_s {
20 const char text;
21 const uint8_t param_ct;
22 union {
23 // void (* const func0 )(void);
24 void (* const func1 )(uint8_t,...);
25 // void (* const func2 )(...);
26 } func;
27 } text_cmd_t;
29 #define CMD(c,param,_func) \
30 { .text = (c), .param_ct = (param), .func = {.func1=(_func)} }
32 text_cmd_t text_cmd_list[] = {
33 CMD('m',2,motor_set),
34 CMD('a',1,print_adc_get_i)
36 #define TEXT_CMD_LIST_SZ CT(text_cmd_list)
39 typedef enum input_state_e {INP_WAIT, INP_ATT, INP_CMD_FOUND, INP_CMP_PARAM} input_state_t;
41 void process_rx(void) {
42 input_state_t state = INP_WAIT;
43 uint8_t cmd_num;
44 uint8_t param_ct = 0;
46 if (spi_io_rx_nl>0) {
47 spi_io_rx_nl--;
48 int c = spi_getchar();
49 do {
50 switch(state) {
51 default:
52 state = INP_WAIT;
53 case INP_WAIT:
54 if (c == '!')
55 state = INP_ATT;
56 break;
58 case INP_ATT:
59 for(uint8_t i=0;i<TEXT_CMD_LIST_SZ;i++) {
60 if(text_cmd_list[i].text==c) {
61 state = INP_CMD_FOUND;
62 cmd_num = i;
63 goto TEXT_SCAN_SUCC;
66 //invalid command.
67 state = INP_WAIT;
69 TEXT_SCAN_SUCC:
70 break;
72 case INP_CMD_FOUND:
73 // we just found a command (on the last character).
74 if (text_cmd_list[cmd_num].param_ct==param_ct) {
77 break;
79 c = spi_getchar();
80 } while (c!=EOF);
81 // parse new input