#include "uart_console.h" #include "debug.h" typedef struct uart_config { char *name; void (*fun)(u8, void *); } uart_config; #define NAME_SIZE 20 #define PAR_SIZE 100 typedef struct uart_console { char fun_name[NAME_SIZE]; int par_int[PAR_SIZE]; float par_float[PAR_SIZE]; uart_config *cfg; // private u8 pos; u8 step; u8 par_num; u8 fun_ord; u8 dot : 1; // �� u8 nega : 1; // ���� } uart_console; /*_______________________________________Driver___________________________________________________________*/ static u8 str_cmp(char *char1, char *char2, u8 len) { for (int i = 0; i < NAME_SIZE; i++) { if ((*char1 == *char2) && *char1 == '\0') { return 1; } if (*char1 != *char2) { return 0; } char1++; char2++; } return 1; } /** function comment * @description: * @param {uart_console} *tis * @return {*} */ u8 uart_console_name_analysis(uart_console *tis) { uart_config *cfg = tis->cfg; u8 ord = 0; while (!(str_cmp(cfg[ord].name, "end", 3))) { if (str_cmp(cfg[ord].name, tis->fun_name, tis->pos)) { return ord; } ord++; } return 255; } extern uart_console console; /** function comment * @description: * @return {*} */ void uart_console_put_char(u8 data) { uart_console *tis = &console; if (data == '@') { tis->step = 0; tis->pos = 0; return; } switch (tis->step) { case 0: if (data == '(') { tis->fun_ord = uart_console_name_analysis(tis); // Printf("name is %d\n", tis->fun_ord); if (tis->fun_ord != 255) { tis->nega = 0; tis->step++; tis->pos = 0; tis->par_num = 0; tis->dot = 0; tis->par_int[0] = 0; // Printf("name is %d\n", tis->fun_ord); } else { tis->step = 0; tis->pos = 0; } } else { tis->fun_name[tis->pos] = data; tis->pos++; tis->fun_name[tis->pos] = '\0'; if (tis->pos == NAME_SIZE) { Printf("error: uart console buffer overflow\n"); tis->pos = 0; } } break; case 1: if (data == ')') { tis->cfg[tis->fun_ord].fun(tis->par_num, tis); tis->step = 0; return; } else { tis->step++; } case 2: if (data == ')') { while (tis->par_float[tis->pos] > 1) { tis->par_float[tis->pos] = tis->par_float[tis->pos] / 10.0; } tis->par_float[tis->pos] = (float)tis->par_int[tis->pos] + tis->par_float[tis->pos]; if (tis->nega == 1) { Printf("nega\n"); tis->par_float[tis->pos] = tis->par_float[tis->pos] * (-1.0); tis->par_int[tis->pos] = tis->par_int[tis->pos] * (-1); } tis->par_num++; tis->cfg[tis->fun_ord].fun(tis->par_num, tis); tis->step = 0; return; } else if (data == '.') { tis->dot = 1; return; } else if (data == '-') { tis->nega = 1; return; } else if (data == ',') { while (tis->par_float[tis->pos] > 1) { tis->par_float[tis->pos] = tis->par_float[tis->pos] / 10.0; } tis->par_float[tis->pos] = (float)tis->par_int[tis->pos] + tis->par_float[tis->pos]; if (tis->nega == 1) { Printf("nega\n"); tis->par_float[tis->pos] = tis->par_float[tis->pos] * (-1.0); tis->par_int[tis->pos] = tis->par_int[tis->pos] * (-1); } tis->nega = 0; tis->dot = 0; tis->pos++; tis->par_num++; tis->par_float[tis->pos] = 0.0; tis->par_int[tis->pos] = 0; if (tis->par_num == PAR_SIZE) { Printf("error : uart console par overflow\n"); tis->par_num = 0; } return; } else if (data < '0' || data > '9') { Printf("error: uart console illegal parameter\n"); return; } if (!tis->dot) { tis->par_int[tis->pos] = tis->par_int[tis->pos] * 10 + data - '0'; // Printf("%d\n",tis->par_int[tis->pos]); } else { tis->par_float[tis->pos] = tis->par_float[tis->pos] * 10 + data - '0'; } default: break; } } /*__________________________________________________________________________________________________*/ /*__________________________________________user________________________________________________________*/ static void test_cmd(u8 par_num, void *par) { uart_console *tis = (uart_console *)par; Printf("test cmd %d\n", par_num); for (int i = 0; i < par_num; i++) { Printf(" %d,%f\n", tis->par_int[i], tis->par_float[i]); } } static void pwm_set(u8 par_num, void *par) { uart_console *tis = (uart_console *)par; extern void duty_set(unsigned char ch, unsigned char _duty); duty_set((unsigned char)tis->par_int[0], (unsigned char)tis->par_int[1]); } static void test_ds18b20(u8 par_num, void *par){ uart_console *tis = (uart_console *)par; #include "ds18b20.h" #include "debug.h" float temp = ds18b20_read_temp(0); Printf("temp = %f\n", temp); ds18b20_start_convert(0); // ds18b20_rst(0); // ds18b20_write_data(0, 0xcc); } static uart_config cfg[] = { {"test", test_cmd}, {"pwm", pwm_set}, {"ds18b20",test_ds18b20}, {"end", NULL}, }; uart_console console = { .step = 0, .cfg = cfg, }; // int main() // { // u8 data[] = "@test123(12,3214,2183,0.232434,-12321)"; // for (int i = 0; i < sizeof(data); i++) // { // uart_console_put_char(data[i]); // } // }