Skip to content

Instantly share code, notes, and snippets.

@shevkoplyas
Forked from EleotleCram/aprintf
Last active August 29, 2015 14:10
Show Gist options
  • Save shevkoplyas/ec218af6a5881ae2f275 to your computer and use it in GitHub Desktop.
Save shevkoplyas/ec218af6a5881ae2f275 to your computer and use it in GitHub Desktop.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment