I must be suffering brain fade as I cannot seem to make the passing of variable number of arguments to a variable number of argument function work.
Here is the troublesome code. My function I need to get working is SMP
Someone please point out what I've messed up with please
Alan
// function prototype, ultimately to go into a .h file
void SMP(char *sFmt, ...);
void setup() {
Serial.begin(19200);
// initialise the printf family
printf_begin();
}
void loop() {
SMP("Hi %s %s\n", "there ", "world");
delay(200);
}
// this is the troublesome function
// ... to pass variable number of arguments
void SMP(char *sFmt, ...)
{
char acTmp[128]; // place holder for sprintf output
va_list args; // args variable to hold the list of parameters
va_start(args, sFmt); // mandatory call to initilase args
// the following call should (hopefully) pass the variable
// arguments to the sprintf function which in turn handles variable arguments
sprintf(acTmp, sFmt, args);
// but it prints rubbish after the initial "Hi "
// the line below will make the first parameter, "there" print
// but not the second one, "world"
// I didn't expect this to be the right way to do it
// Just included as part of my testing
// sprintf(acTmp, sFmt, va_arg(args, char *));
// send it out to the serial port/monitor
printf(acTmp);
// mandatory tidy up
va_end(args);
return;
}
// Helper functions for printf family calls --------------------
int serial_putc(char c, FILE *)
{
Serial.write(c);
return c;
}
void printf_begin(void)
{
fdevopen(&serial_putc, 0);
return;
}