Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple arduino formatted printf
int aprintf(char *str, ...) {
int i, j, count = 0;
va_list argv;
va_start(argv, str);
for(i = 0, j = 0; str[i] != '\0'; i++) {
if (str[i] == '%') {
count++;
Serial.write(reinterpret_cast<const uint8_t*>(str+j), i-j);
switch (str[++i]) {
case 'd': Serial.print(va_arg(argv, int));
break;
case 'l': Serial.print(va_arg(argv, long));
break;
case 'f': Serial.print(va_arg(argv, double));
break;
case 'c': Serial.print((char) va_arg(argv, int));
break;
case 's': Serial.print(va_arg(argv, char *));
break;
case '%': Serial.print("%");
break;
default:;
};
j = i+1;
}
};
va_end(argv);
if(i > j) {
Serial.write(reinterpret_cast<const uint8_t*>(str+j), i-j);
}
return count;
}
@zekeApps

This comment has been minimized.

Copy link

zekeApps commented Aug 29, 2015

any dependency needed?

@VivekYadav7272

This comment has been minimized.

Copy link

VivekYadav7272 commented Mar 22, 2020

any dependency needed?

No.

@beageedog

This comment has been minimized.

Copy link

beageedog commented Mar 31, 2020

Been using it. Awesome. It should be part of the IDE!

Dave Aldrich

@EleotleCram

This comment has been minimized.

Copy link
Owner Author

EleotleCram commented Mar 31, 2020

Glad that is of help to people, that's why I put it up in the first place...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.