Skip to content

Instantly share code, notes, and snippets.

@vildninja
Created January 11, 2020 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vildninja/86847cc6078910b1cb515e871ea9dd4d to your computer and use it in GitHub Desktop.
Save vildninja/86847cc6078910b1cb515e871ea9dd4d to your computer and use it in GitHub Desktop.
Templated comma separated println for Arduino Serial output: tprintln("Hello ", 101, " Arduino!");
// Created by Jannek @VildNinja
/*
Usage example: tprintln("Hello ", 101, " Arduino!");
Produces:
Serial.print("Hello ");
Serial.print(101);
Serial.print(" Arduino!");
Serial.println();
*/
// Specialize this!
template<typename T0>
void tprint(const T0& t0) {
Serial.print(t0);
}
template<typename T0, typename ...T>
void tprint(const T0& t0, const T&... t) {
tprint(t0);
tprint(t...);
}
// Call this!
template<typename ...T>
void tprintln(const T&... t) {
tprint(t...);
Serial.println();
}
// Specialization example for bool
template<>
void tprint<bool>(const bool& b) {
tprint(b ? F("true") : F("false"));
}
// Specialization example for custom type (vector with x and y)
//template<>
//void tprint<vec2>(const vec2& v) {
// tprint(v.x, '*', v.y);
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment