Last active
January 11, 2024 16:37
-
-
Save asheeshr/9004783 to your computer and use it in GitHub Desktop.
A printf function for serial communication from Arduino boards
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
/* | |
This code should be pasted within the files where this function is needed. | |
This function will not create any code conflicts. | |
The function call is similar to printf: ardprintf("Test %d %s", 25, "string"); | |
To print the '%' character, use '%%' | |
This code was first posted on http://arduino.stackexchange.com/a/201 | |
*/ | |
#ifndef ARDPRINTF | |
#define ARDPRINTF | |
#define ARDBUFFER 16 //Buffer for storing intermediate strings. Performance may vary depending on size. | |
#include <stdarg.h> | |
#include <Arduino.h> //To allow function to run from any file in a project | |
int ardprintf(char *str, ...) //Variadic Function | |
{ | |
int i, count=0, j=0, flag=0; | |
char temp[ARDBUFFER+1]; | |
for(i=0; str[i]!='\0';i++) if(str[i]=='%') count++; //Evaluate number of arguments required to be printed | |
va_list argv; | |
va_start(argv, count); | |
for(i=0,j=0; str[i]!='\0';i++) //Iterate over formatting string | |
{ | |
if(str[i]=='%') | |
{ | |
//Clear buffer | |
temp[j] = '\0'; | |
Serial.print(temp); | |
j=0; | |
temp[0] = '\0'; | |
//Process argument | |
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; | |
default: ; | |
}; | |
} | |
else | |
{ | |
//Add to buffer | |
temp[j] = str[i]; | |
j = (j+1)%ARDBUFFER; | |
if(j==0) //If buffer is full, empty buffer. | |
{ | |
temp[ARDBUFFER] = '\0'; | |
Serial.print(temp); | |
temp[0]='\0'; | |
} | |
} | |
}; | |
Serial.println(); //Print trailing newline | |
return count + 1; //Return number of arguments detected | |
} | |
#undef ARDBUFFER | |
#endif |
Hi, It is very useful function!
I have one remark. When I invoked it like this:
ardprintf("Test %d %s other text", 25, "string");
"other text" was not shown"
Adding this code in line 65 resolve it:
temp[j] = '\0';
Serial.print(temp);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just a note that the 'clear buffer' on line 57 does not work!