第一,去掉了原来使用的goto语句,因为C语言中除了错误处理之外,不建议使用goto语句;
第二,fmt和pnt的含义更加明确,它们始终指向下一个需要处理的字符和变参;
第三,整理了程序结构,使它更加清晰。
void uart_printf(USART_TypeDef *USARTx, char *fmt, ...)
{
char *pnt = (char *)&fmt + sizeof(fmt);
int len;
while (*fmt != '\0') {
if (*fmt == '%') {
if (*(fmt + 1) == 'c') {
uart_send_byte(USARTx, *((int *)pnt));
pnt += sizeof(int);
fmt += 2;
continue;
}
if (*(fmt + 1) == 's') {
uart_send_string(USARTx, (char *)*((int *)pnt));
pnt += sizeof(int);
fmt += 2;
continue;
}
if (check_ascii_case(*(fmt + 1)) == CASE_NUM) {
len = get_num_from_ascii(*(fmt + 1));
if (*(fmt + 2) == 'd')
uart_send_number(USARTx, *((int *)pnt), len, 10);
else if (*(fmt + 2) == 'x')
uart_send_number(USARTx, *((int *)pnt), len, 16);
else if (*(fmt + 2) == 'o')
uart_send_number(USARTx, *((int *)pnt), len, 8);
else if (*(fmt + 2) == 'b')
uart_send_number(USARTx, *((int *)pnt), len, 2);
pnt += sizeof(int);
fmt += 3;
continue;
}
}
uart_send_byte(USARTx, *fmt);
fmt += 1;
}
}
使用时:
uart_printf(USART1, "Hello, %s! Nice to meet you. \r\n", "Marianna");
uart_printf(USART1, "I have %2d apples and %1d oranges.\r\n", 3, 9);