Embedded in printf

In embedded in, often you need to use printf debugging program

The default output device is a display standard library functions to achieve the serial port or LCD, you have to re-function associated with the output device definition in the standard library function call.

printf output to the serial port, the output of which is necessary to point serial fputc (redirect)

Therefore, we need to redirect printf realize related functions. Sometimes, we want to achieve their own printf function, may wish to write your own

#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>

#define BUFSIZE  1024
char myprintf_buf[BUFSIZE];


void Debug_printf(const char* fmt, ...)
{
    va_list args;
    int n;

    va_start(args, fmt);
    n = vsnprintf(myprintf_buf, BUFSIZE, fmt, args);
    va_end(args);
    int i = 0;
    for(i = 0; i < n; i++)
    {
       HAL_UART_Transmit(&huart2, (uint8_t *)&myprintf_buf[i], 1, 0xFFFF ); // different platforms, according to the modification function serial output 
    } 
} 

int main ( void ) 
{ 
    Debug_printf ( " Test% D \ R & lt \ n- " , 100 );
     return  0 ; 
}

 

Method Two:

To realize his simple printf function

#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

static unsigned long m_pow(int x,int y)
{
    unsigned long sum = 1;
    while(y--)
    {
        sum *= x;
    }
    return sum;
}

//打印字符​
void m_putchar(const char ch)
{
    HAL_UART_Transmit(&huart2, (uint8_t *)ch, 1, 0xFFFF);
}

//打印字符串
void m_putstr(const char *str)
{
    while(*str)
    {
        m_putchar(*str++);
    }
}

int printint(const int dec)
{
    int count = 0, ret = 0;
    int r_val = dec;
    char ch;
    while(r_val)
    {
        count++;
        r_val /= 10;
    }
    ret = count;
    r_val = dec;
    while(count)
    {
        ch = r_val / m_pow(10, count - 1);
        r_val %= m_pow(10, count - 1);
        m_putchar(ch + '0');
        count--;
    }
    return ret;
}

int printfloat(const float flt)
{
    int countint = 0,countflt = 0;
    int tmpint = (int)flt;
    int tmpflt = (int)(100000 * (flt - tmpint));
    if(tmpflt % 10 >= 5)
    {
        tmpflt = tmpflt / 10 + 1;
    }
    else
    {
        tmpflt = tmpflt / 10;
    }
    countint = printint(tmpint);
    m_putchar('.');
    countflt = printint(tmpflt);

    return countint + 1 + countflt;
}

int m_printf(const char *str,...)
{
    va_list ap;
    int val,r_val;
    float val_float;
    char count,ch;
    char *s = NULL;
    int res = 0;

    va_start(ap, str);
    while('\0' != *str)
    {
        switch(*str)
        {
        case '%':
            str++;
            switch(*str)
            {
            case 'd':
                val = va_arg(ap, int);
                count = printint(val);
                res += count;
                break;
            case 'x':
                val = va_arg(ap, int);
                r_val = val;
                count = 0;
                while(r_val)
                {
                    count++;
                    r_val /= 16;
                }
                res += count;
                r_val = val;
                while(count)
                {
                    ch = r_val / m_pow(16, count - 1);
                    r_val %= m_pow(16, count - 1);
                    if(ch <= 9)
                        m_putchar(ch + '0');
                    else
                        m_putchar(ch - 10 + 'a');
                    count--;
                }
                break;
            case 'f':
                val_float = va_arg(ap, double);
                count = printfloat(val_float);
                res += count;
                break;
            case 's':
                s = va_arg(ap, char*);
                m_putstr(s);
                res += strlen(s);
                break;
            case 'c':
                m_putchar((char)va_arg(ap, int));
                res += 1;
                break;
            default:
                ;
            }
            case '\n':
                m_putchar('\n');
                res += 1;
                break;
            case '\r':
                m_putchar(*str);
                res += 1;
                break;
            default:
                m_putchar(*str);
                res += 1;
        }
        str++;
    }
    va_end(ap);
    return res;
}



int main(void)
{
    char buf[64];
    m_printf("======== begin test =========\n");
    snprintf(buf, sizeof(buf), "%s %f", "hello world", 20.043);
    m_printf("%s\n", buf);

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/birdBull/p/10939597.html