原创 再来一版简易的printf函数实现

2015-4-20 18:22 1814 18 18 分类: MCU/ 嵌入式 文集: C语言

以前发过两版简易的串口printf函数实现,最近学习了一段时间Linux的库文件,回过头又有不同的理解。
这一版函数基于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, ...);
 

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
18
关闭 站长推荐上一条 /3 下一条