Skip to content

Instantly share code, notes, and snippets.

@EleotleCram
Last active April 16, 2023 05:14
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save EleotleCram/eb586037e2976a8d9884 to your computer and use it in GitHub Desktop.
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
Copy link

any dependency needed?

@VivekYadav7272
Copy link

any dependency needed?

No.

@beageedog
Copy link

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

Dave Aldrich

@EleotleCram
Copy link
Author

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