这一版函数基于MSP430F169,%d %x %o %b的实现不再由自己编写函数,而是调用MSP430-GCC的标准库函数:
#include
char *itoa(int num, char *str, int radix);
send_fun函数指针,指向调用的UARTx的字节发送函数:
void uart_printf(send_fun fun, char *fmt, ...)
{
char *pnt = (char *)&fmt + sizeof(fmt);
char *str, buf[9];
int radix;
while (*fmt != '\0') {
if (*fmt != '%') {
fun(*fmt);
fmt += 1;
continue;
}
switch (*(fmt + 1)) {
case 'c':
fun(*((int *)pnt));
pnt += sizeof(int);
fmt += 2;
continue;
case 's':
str = (char *)*((int *)pnt);
while (*str != '\0')
fun(*str++);
pnt += sizeof(int);
fmt += 2;
continue;
case 'd':
radix = 10;
goto SEND_NUM;
case 'x':
radix = 16;
goto SEND_NUM;
case 'o':
radix = 8;
goto SEND_NUM;
case 'b':
radix = 2;
goto SEND_NUM;
SEND_NUM:
str = itoa(*(int *)pnt, buf, radix);
while (*str != '\0')
fun(*str++);
pnt += sizeof(int);
fmt += 2;
continue;
default:
break;
}
}
}
实际上,库stdio.h中也提供了printf的实现,直接调用它们就可以了:
int __attribute__((format (printf, 2, 3))) uprintf(int (*func)(int c), const char *fmt, ...);