avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Initilizing complex const arrays : syntax ?


From: David Kelly
Subject: Re: [avr-gcc-list] Initilizing complex const arrays : syntax ?
Date: Sun, 18 Sep 2005 21:24:28 -0500


On Sep 18, 2005, at 5:33 PM, Vincent Trouilliez wrote:

On Sun, 2005-09-18 at 16:54 -0500, David Kelly wrote:

...I don't use printf() so I haven't used printf_P()

But how do you do then ?
If you wrote better functions, please share ! :-)
Printf takes huge space, so if you have a light weight alternative...

Printf()'s bulk comes from the generic format string parser. Hardcode your format something like this:

#include <stdlib.h>    //  for utoi()
char buffer[10];    //  presumably this is something you can reuse

//
//    Convert unsigned x to base 10 and output "x.xx" via putchar()
//
void
dot_two( uint8_t x )
{
     // char buffer[4];    //  use global or auto
    int8_t i, n;
    char *cp;

    cp = utoi( x, buffer, 10 );
    n = strlen(cp);

    //  a for() loop is possibly much for just 3 characters
    for( i = 3 ; i ; i-- ) {
        if( n < i )
            putchar('0')
        else
            putchar(*cp++);
        if( i == 3 )    //  test for our decimal point location
            putchar('.');
    }
}

void
another_dot_two( uint8_t x )
{
    int8_t n;
    // char buffer[7];    //  needs to be bigger here

    strncpy_P( buffer, PGM_P("000"), 3 );    //  preload leading zeros
    utoi( x, &buffer[3], 10 );    //  convert after the zeros
    n = strlen(&buffer[3]);    // number of converted digits

    //  012345    index
    //  0001      buffer[] if n == 1
    //  00022     n == 2
    //  000333    n == 3
    putchar( buffer[n] );    //  see above
    putchar( '.' );
    putchar( buffer[n+1] );
    putchar( buffer[n+2] );
}

--
David Kelly N4HHE, address@hidden
========================================================================
Whom computers would destroy, they must first drive mad.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]