打印
[MM32软件]

MMF103 中 printf.c 对Printf 的实现

[复制链接]
519|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
stm32jy|  楼主 | 2020-3-30 20:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

此例程将pad字符放入输出缓冲区
static void padding( const int l_flag, params_t *par)
{
    int i;

    if (par->do_padding && l_flag && (par->len < par->num1))
        for (i = par->len; i < par->num1; i++)
            outbyte( par->pad_character);
}


使用特权

评论回复
沙发
stm32jy|  楼主 | 2020-3-30 20:50 | 只看该作者
此例程按照填充和定位标志的指示将字符串移动到输出缓冲区
static void outs( charptr lp, params_t *par)
{
    /* pad on left if needed                         */
    par->len = strlen( lp);
    padding( !(par->left_flag), par);

    /* Move string to the buffer                     */
    while (*lp && (par->num2)--)
        outbyte( *lp++);

    /* Pad on right if needed                        */
    /* CR 439175 - elided next stmt. Seemed bogus.   */
    /* par->len = strlen( lp);                       */
    padding( par->left_flag, par);
}


使用特权

评论回复
板凳
stm32jy|  楼主 | 2020-3-30 20:51 | 只看该作者
此例程按照填充和定位标志的指示将数字移动到输出缓冲区
static void outnum( const long n, const long base, params_t *par)
{
    charptr cp;
    int negative;
    char outbuf[32];
    const char digits[] = "0123456789ABCDEF";
    unsigned long num;

    /* Check if number is negative                   */
    if (base == 10 && n < 0L)
    {
        negative = 1;
        num = -(n);
    }
    else
    {
        num = (n);
        negative = 0;
    }

    /* Build number (backwards) in outbuf            */
    cp = outbuf;
    do
    {
        *cp++ = digits[(int)(num % base)];
    }
    while ((num /= base) > 0);
    if (negative)
        *cp++ = '-';
    *cp-- = 0;

    /* Move the converted number to the buffer and   */
    /* add in the padding where needed.              */
    par->len = strlen(outbuf);
    padding( !(par->left_flag), par);
    while (cp >= outbuf)
        outbyte( *cp--);
    padding( par->left_flag, par);
}


使用特权

评论回复
地板
stm32jy|  楼主 | 2020-3-30 20:52 | 只看该作者
从格式字符串中获取一个数字
static int getnum( charptr* linep)
{
    int n;
    charptr cp;

    n = 0;
    cp = *linep;
    while (isdigit(*cp))
        n = n * 10 + ((*cp++) - '0');
    *linep = cp;
    return(n);
}


使用特权

评论回复
5
stm32jy|  楼主 | 2020-3-30 20:53 | 只看该作者
主角登场 串口printf打印函数
void uart_printf( const charptr ctrl1, ...)
{

    int long_flag;
    int dot_flag;

    params_t par;

    char ch;
    va_list argp;
    charptr ctrl = ctrl1;

    va_start( argp, ctrl1);

    for ( ; *ctrl; ctrl++)
    {

        /* move format string chars to buffer until a  */
        /* format control is found.                    */
        if (*ctrl != '%')
        {
            outbyte(*ctrl);
            continue;
        }

        /* initialize all the flags for this format.   */
        dot_flag   = long_flag = par.left_flag = par.do_padding = 0;
        par.pad_character = ' ';
        par.num2 = 32767;

try_next:
        ch = *(++ctrl);

        if (isdigit(ch))
        {
            if (dot_flag)
                par.num2 = getnum(&ctrl);
            else
            {
                if (ch == '0')
                    par.pad_character = '0';

                par.num1 = getnum(&ctrl);
                par.do_padding = 1;
            }
            ctrl--;
            goto try_next;
        }

        switch (tolower(ch))
        {
            case '%':
                outbyte( '%');
                continue;

            case '-':
                par.left_flag = 1;
                break;

            case '.':
                dot_flag = 1;
                break;

            case 'l':
                long_flag = 1;
                break;

            case 'd':
                if (long_flag || ch == 'D')
                {
                    outnum( va_arg(argp, long), 10L, &par);
                    continue;
                }
                else
                {
                    outnum( va_arg(argp, int), 10L, &par);
                    continue;
                }
            case 'x':
                outnum((long)va_arg(argp, int), 16L, &par);
                continue;

            case 's':
                outs( va_arg( argp, charptr), &par);
                continue;

            case 'c':
                outbyte( va_arg( argp, int));
                continue;

            case '\\':
                switch (*ctrl)
                {
                    case 'a':
                        outbyte( 0x07);
                        break;
                    case 'h':
                        outbyte( 0x08);
                        break;
                    case 'r':
                        outbyte( 0x0D);
                        break;
                    case 'n':
                        outbyte( 0x0D);
                        outbyte( 0x0A);
                        break;
                    default:
                        outbyte( *ctrl);
                        break;
                }
                ctrl++;
                break;

            default:
                continue;
        }
        goto try_next;
    }
    va_end( argp);
}


使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

44

主题

1118

帖子

3

粉丝