I have a function that concatenates a print line, however I am having trouble getting some of the strings to format correctly.
Using Serial.print formats the output from keypad.getKey() correctly, however when I try to output the same format using printConcatLine() it does not give me the same output. I've tried casting it as both a char and an int...
void printConcatLine(const char* mask, ...) {
va_list params;
va_start(params, mask);
while(*mask != '\0') {
if (*mask == 'i') {
Serial.print(va_arg(params, int));
} else if(*mask == 'c') {
Serial.print(va_arg(params, const char *));
} else if (*mask == 'f' || *mask == 'd') {
Serial.print(va_arg(params, double));
}
++mask;
}
va_end(params);
Serial.println();
}
char btnPressed = keypad.getKey();
if (btnPressed) {
Serial.print("BtnPressed: ");
Serial.println(btnPressed);
// Output: "BtnPressed: 1"
printConcatLine("ci", "BtnPressed: ", btnPressed);
// Output: "BtnPressed: 49"
printConcatLine("cc", "BtnPressed: ", btnPressed);
// Output: "BtnPressed: "
}
I also tried changing:
va_arg(params, const char *)
to
va_arg(params, char)
which also did nothing.
How can I modify the call to va_arg() to properly output the same output as Serial.print()?
Serial.printf("...", ..., ..., ...)
function which is ideal for this. The formatting is handled by this code, which itself was lifted directly from RetroBSD.