This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

customization of putchar function

I have made the new function putchar for redirect the output to LCD already. But by default, I also want to use this as output to serial port on the same program.
I do not understand how to use both output function in parallel or other way to switch between each other.
Please tell me if possible.
With my thanks.

Parents
  • "So I can not switch between output function (e.g. lcd_putchar and putchar) which I can do in PIC-C(C compiler for PIC Microcontroller) very flexible as:
    - printf("......",.....);//default serial port
    - printf(lcd_putc,"......",.....);//redirect to LCD"


    char (* putchar_fp)(char c);
    
    char lcd_putchar(char c)
    {
        /* Your LCD output handler goes here */
    }
    
    char ser_putchar(char c)
    {
        /* Body of Keil's putchar goes here */
    }
    
    char putchar(char c)
    {
        /* Body of Keil's putchar moved to ser_putchar()
         * and replaced with...
         */
        return putchar_fp(c);
    }
    
    int my_printf(char (* which_putchar_fp)(char), const char *format, ...)
    {
        int     ret_val;
        va_list ap;
    
        putchar_fp = which_putchar_fp;
    
        va_start(ap, format);
        rtn = vprintf(format, ap);
        va_end(ap);
    
        return ret_val;
    }
    
    int main(void)
    {
        my_printf(lcd_putchar, "Hello world on %s\n", "LCD");
        my_printf(ser_putchar, "Hello world on %s\n", "SER");
    }

    You get the idea.

Reply
  • "So I can not switch between output function (e.g. lcd_putchar and putchar) which I can do in PIC-C(C compiler for PIC Microcontroller) very flexible as:
    - printf("......",.....);//default serial port
    - printf(lcd_putc,"......",.....);//redirect to LCD"


    char (* putchar_fp)(char c);
    
    char lcd_putchar(char c)
    {
        /* Your LCD output handler goes here */
    }
    
    char ser_putchar(char c)
    {
        /* Body of Keil's putchar goes here */
    }
    
    char putchar(char c)
    {
        /* Body of Keil's putchar moved to ser_putchar()
         * and replaced with...
         */
        return putchar_fp(c);
    }
    
    int my_printf(char (* which_putchar_fp)(char), const char *format, ...)
    {
        int     ret_val;
        va_list ap;
    
        putchar_fp = which_putchar_fp;
    
        va_start(ap, format);
        rtn = vprintf(format, ap);
        va_end(ap);
    
        return ret_val;
    }
    
    int main(void)
    {
        my_printf(lcd_putchar, "Hello world on %s\n", "LCD");
        my_printf(ser_putchar, "Hello world on %s\n", "SER");
    }

    You get the idea.

Children