#include "command.h" uint8 command_buffer[COMMAND_BUFFER_SIZE]; // 命令缓冲区 static uint8 command_buffer_index = 0; // 当前命令索引 static vuint8 is_busy_command = 0; /************************************************************ * 名称 :getIntFromStr() * 功能 :从字符串中获取整数 * 输入 :void * 输出 :int * 说明 :从字符串中获取整数,自动移动指针,遇到特殊字符停止 ************************************************************/ int16 getIntFromStr() { int16 result = 0; int8 is_negative = 1; // 是否为负数 bit has_get = 0; while(command_buffer[command_buffer_index]) { if(command_buffer[command_buffer_index] == ' ' || command_buffer[command_buffer_index] == ';' || command_buffer[command_buffer_index] == '\r' || command_buffer[command_buffer_index] == '\n' || command_buffer[command_buffer_index] == '\0') { if(has_get) { break; } command_buffer_index++; continue; } else if(command_buffer[command_buffer_index] == '-') { is_negative = -1; } else if(command_buffer[command_buffer_index] >= '0' && command_buffer[command_buffer_index] <= '9') { has_get = 1; result = result*10 + command_buffer[command_buffer_index] - '0'; } else if(command_buffer[command_buffer_index] == '.') { command_buffer_index++; while(command_buffer[command_buffer_index] >= '0' && command_buffer[command_buffer_index] <= '9')command_buffer_index++; } else { result = 0; break; } command_buffer_index++; } return result * is_negative; // 返回结果 } /************************************************************ * 名称 :startWith() * 功能 :判断字符串是否以某个字符串开头 * 输入 :uint8 *str,uint8 *cmd,uint8 offset * 输出 uint8 * 说明 :判断字符串是否以某个字符串开头 ************************************************************/ uint8 startWith(uint8 *str, uint8 *cmd,uint8 offset,uint8 *counter) { uint8 cou,k; k = offset; for(cou=0;*cmd;cou++) { if(*(str+cou+k) != *cmd) { (*counter) -= cou; return 0; } cmd++; (*counter) ++; } return 1; } /************************************************************ * 名称 :parseCommand() * 功能 :解析命令 * 输入 :无 * 输出 :无 * 说明 :解析命令 ************************************************************/ void parseCommand() { uint8 cmd_state = 1; // 命令状态 if(!is_busy_command) return; // 如果没有忙状态,直接返回 command_buffer[command_buffer_index] = '\0'; command_buffer_index = 0; if(startWith(command_buffer,"POW",0,&command_buffer_index)) { // command_buffer_index += 2; if(command_buffer[command_buffer_index] == ' ') { // int16 power_index = 1000; // power_index = getIntFromStr(); // if(startWith(command_buffer," ON",command_buffer_index,&command_buffer_index)) // { // } // else if(startWith(command_buffer," OFF",command_buffer_index,&command_buffer_index)) // { // } // if(command_buffer[command_buffer_index] == ';') // { // cmd_state = 0; // 命令成功 // } } } if(cmd_state) { printf("Error!\r\n"); } else { printf("OK!\r\n"); } command_buffer_index = 0; is_busy_command = 0; } void command_uart_callback(uint8 dat) { if(is_busy_command) return; command_buffer[command_buffer_index++] = dat; if(command_buffer[command_buffer_index-1] == ';') { is_busy_command = 1; // 设置忙状态,防止中断嵌套 } if(command_buffer_index >= COMMAND_BUFFER_SIZE) { command_buffer_index = 0; // 防止缓冲区溢出 } }