|
|
View previous topic :: View next topic |
Author |
Message |
silelis
Joined: 12 Jun 2007 Posts: 68 Location: Poland, podlaskie district
|
Passing variable list of parameters to fprintf from own func |
Posted: Sat Jun 24, 2017 3:40 am |
|
|
Hi,
I want to write my own log function which will take as few flash as it is possible.
At the moment I have something like this:
Code: |
#define dbg_printf(fmt,...) fprintf(LOG_PORT,fmt,__VA_ARGS__)
#define dbg_return_carriage() fprintf_return_carriage()
void fprintf_return_carriage(void)
{
fprintf(LOG_PORT,"\r\n");
}
main()
{
dbg_printf("logc text");
dbg_return_carriage();
}
|
but it is flash space ineffective.
I want to write something like that:
Code: | void dbg_printf(fmt,...)
{
fprintf(LOG_PORT,fmt,__VA_ARGS__);
fprintf(LOG_PORT,"\r\n");
} |
but I don't know how to pass variables from dbg_printf to 1st fprintf?
Any help will be welcomed. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Sat Jun 24, 2017 10:31 am |
|
|
You can't.
All the standard CCS printf derivatives, work out what routines to use at compile time. They have to, since to have a printf, that can handle all the possible formats, would result in it being very much larger (huge...).
Now there has been code to handle some variables posted in the past, but such routines are very large compared to the standard implementation. You can create a 'half way house', by for example, creating three fixed printf routines to handle different numbers of variables, and then encapsulating these into three overloaded routines. So something like:
Code: |
void print_var(int8 val)
{
fprintf(STREAM,"%d",val);
}
void print_var(int8 val, int8 val2)
{
fprintf(STREAM,"%d %d",val, val2);
}
void print_var(int8 val, int8 val2, int8 val3)
{
fprintf(STREAM,"%d %d %d",val, val2, val3);
}
|
This obviously needs the correct stream name, and real formats, but you can then call
print_var(10);
and it'll use the first routine, while
print_var(10,20);
will use the second.
Full flexibility though would be bulky. I have in the past compiled part of a standard sprintf library. This came from stuff pointed to by some of the threads at sourceforge. However even quite a cut down version was enormous compared to the CCS routines.....
Have a look here for some limited sources:
<https://www.menie.org/georges/embedded/> |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|