国民技术N32G430开发笔记(13)- 实现一个轻量级的printf

实现一个轻量级的printf

1、在IAP升级中我们将App的代码固定在20K的大小,但是当使用printf函数时候发现,整个APP的代码会达到37K,远远超出我们20K的空间,所以决定不调用标准库的printf函数。
2、在Common中增加Log文件夹,创建log.c log.h文件,并在Makefile中增加对log.c的编译。

log.h 文件

#ifndef __LOG_H__
#define __LOG_H__


void max_print(char* fmt, ... );
#endif

log.c文件

#include <stdio.h>
#include <stdarg.h>
#include "log.h"
#include "main.h"

void printch(char ch)
{
    USART_Data_Send(USART1, (uint8_t)ch);
    while (USART_Flag_Status_Get(USART1, USART_FLAG_TXDE) == RESET);
}

void printstr(char* str)
{
    while(*str)
    {
        printch(*str++);
    }
}

void printhex(int hex)
{
    if(hex==0)
    {
        printstr("0x");
        return;
    }
    printhex(hex/16);
    if(hex < 10)
    {
        printch((char)(hex%16 + '0'));
    }
    else
    {
        printch((char)(hex%16 - 10 + 'a' ));
    }
}

void printbin(int bin)
{
    if(bin == 0)
    {
        printstr("0b");
        return;
    }
    printbin(bin/2);
    printch( (char)(bin%2 + '0'));
}

void printdec(int dec)
{
    if(dec==0)
    {
        return;
    }
    printdec(dec/10);
    printch( (char)(dec%10 + '0'));
}

void max_print(char* fmt, ... )
{
    double vargflt = 0;
    int  vargint = 0;
    char* vargpch = NULL;
    char vargch = 0;
    char* pfmt = NULL;
    va_list vp;
 
    va_start(vp, fmt);
    pfmt = fmt;
 
    while(*pfmt)
    {
        if(*pfmt == '%')
        {
            switch(*(++pfmt))
            {
                
                case 'c':
                    vargch = va_arg(vp, int); 
                    /*    va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
                        mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
                    printch(vargch);
                    break;
                case 'd':
                case 'i':
                    vargint = va_arg(vp, int);
                    printdec(vargint);
                    break;
                // case 'f':
                //     vargflt = va_arg(vp, double);
                //      /*    va_arg(ap, type), if type is narrow type (char, short, float) an error is given in strict ANSI
                //         mode, or a warning otherwise.In non-strict ANSI mode, 'type' is allowed to be any expression. */
                //     printflt(vargflt);
                //     break;
                case 's':
                    vargpch = va_arg(vp, char*);
                    printstr(vargpch);
                    break;
                case 'b':
                case 'B':
                    vargint = va_arg(vp, int);
                    printbin(vargint);
                    break;
                case 'x':
                case 'X':
                    vargint = va_arg(vp, int);
                    printhex(vargint);
                    break;
                case '%':
                    printch('%');
                    break;
                default:
                    break;
            }
            pfmt++;
        }
        else
        {
            printch(*pfmt++);
        }
    }
    va_end(vp);
}

3、在使用打印的地方掉用。
在这里插入图片描述
4、打印结果如图:

在这里插入图片描述

5、而编译后的代码仅仅只有7K,符合预期。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值