add USART command

This commit is contained in:
小冯Sir 2024-05-03 03:30:49 +08:00
parent 72eff9a4c8
commit 57c9a54615
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,18 @@
/**
******************************************************************************
* @ : command.h
* @ : Sir
* @brief : None
* @ : None
* @ : 24-5-3
******************************************************************************
*/
#ifndef FIRMWARE_CORE_APPS_INC_COMMAND_H_
#define FIRMWARE_CORE_APPS_INC_COMMAND_H_
#include "main.h"
#include "string.h"
void command_Process(uint8_t *str,uint16_t size);
#endif //FIRMWARE_CORE_APPS_INC_COMMAND_H_

View File

@ -0,0 +1,64 @@
/**
******************************************************************************
* @ : command.c
* @ : Sir
* @brief : None
* @ : None
* @ : 24-5-3
******************************************************************************
*/
#include "command.h"
#include "ctype.h"
uint8_t flag = 0;
uint8_t parsedate(const char *date_str, int *year, int *month, int *day) {
if (date_str == NULL || year == NULL || month == NULL || day == NULL) {
return -1;
}
if (sscanf(date_str, "%4d-%2d-%2d", year, month, day) != 3) {
return -3;
}
if (*month < 1 || *month > 12) {
return -4;
}
if (*day < 1 || *day > 31) {
return -5;
}
return 0;
}
uint8_t parseTime(const char *str, int *hour, int *minute, int *second) {
if(sscanf(str,"%d:%d:%d",hour,minute,second) == 3){
return 1;
}
return 0;
}
void command_Process(uint8_t *str, uint16_t size) {
int year, month, day;
int hour, minute, second;
int result;
if (strstr(str, "setdate")) flag = 1;
if (strstr(str, "settime")) flag = 2;
if (flag == 1) {
printf("请以下日期格式输入:\r\n");
printf("example: 2024-05-03\r\n");
result = parsedate(str, &year, &month, &day);
if (result == 0) {
printf("Year: %d, Month: %d, Day: %d\r\n", year, month, day);
} else {
printf("Error parsing date: %d\n", result);
}
}
if (flag == 2) {
printf("请以一下时间格式输入:\r\n");
printf("example: 02:44:30\r\n");
result = parseTime(str,&hour,&minute,&second);
if(result == 1){
printf("Hours: %d, Minutes: %d, Seconds: %d\n", hour, minute, second);
}
else{
printf("Error parsing time: %d\n",result);
}
}
printf("%s", str);
}