Last active
April 16, 2023 05:14
-
-
Save EleotleCram/eb586037e2976a8d9884 to your computer and use it in GitHub Desktop.
Simple arduino formatted printf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Been using it. Awesome. It should be part of the IDE!
Dave Aldrich
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
No.