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: Vincent Trouilliez
Subject: Re: [avr-gcc-list] Initilizing complex const arrays : syntax ?
Date: Mon, 19 Sep 2005 05:17:11 +0200

> > 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] );
> }


Thanks David, your functions look a lot more elegant and clean to me
than the ugly printf hack for this task. The code is very interesting, I
will reuse it at will in my project, if the license allows ;-). However
gcc throws bags of errors when trying to compile your functions, so I
will have to get them working first ! ;-P


--
Vince


avr-gcc -O -g -Wall -ffreestanding -mmcu=atmega32 -c main.c
In file included from main.c:9:
ui.h:20: warning: type defaults to `int' in declaration of `dot_two'
ui.h:22: warning: type defaults to `int' in declaration of
`another_dot_two'
avr-gcc -O -g -Wall -ffreestanding -mmcu=atmega32 -c lcd.c
avr-gcc -O -g -Wall -ffreestanding -mmcu=atmega32 -c ui.c
In file included from ui.c:1:
ui.h:20: warning: type defaults to `int' in declaration of `dot_two'
ui.h:22: warning: type defaults to `int' in declaration of
`another_dot_two'
ui.c:39: error: conflicting types for 'dot_two'
ui.h:20: error: previous declaration of 'dot_two' was here
ui.c:39: error: conflicting types for 'dot_two'
ui.h:20: error: previous declaration of 'dot_two' was here
ui.c: In function `dot_two':
ui.c:44: warning: implicit declaration of function `utoi'
ui.c:44: warning: assignment makes pointer from integer without a cast
ui.c:45: warning: implicit declaration of function `strlen'
ui.c:51: error: parse error before "else"
ui.c: At top level:
ui.c:60: error: conflicting types for 'another_dot_two'
ui.h:22: error: previous declaration of 'another_dot_two' was here
ui.c:60: error: conflicting types for 'another_dot_two'
ui.h:22: error: previous declaration of 'another_dot_two' was here
ui.c: In function `another_dot_two':
ui.c:64: error: parse error before "const"
make: *** [ui.o] Error 1






reply via email to

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