#include <Arduino.h>
#include <tuple>
#include <utility>
/*
#include <iostream>
#include <string>
#include <sstream> */
namespace details {
template<typename type_t>
String format(const type_t& value) {
String os = String(typeid(type_t).name()) + ": " + String(value);
return os;
}
String format(const bool& value) {
return (String)((value) ? "bool:true" : "bool:false");
}
// add quotes to string
String format(const char* value) {
String os = "const char*:\"" + String(value) + "\"";
return os;
}
// recursively log all parameters
template<typename arg_t, typename... args_t>
inline void log_parameters(bool comma, const arg_t& arg, args_t... args) {
if (comma) Serial1.print(", ");
Serial1.print(format(arg));
if constexpr (sizeof...(args_t) > 0) // true is print separating comma at next call
log_parameters(true, std::forward<args_t>(args)...);
}
//-------------------------------------------------------------------------
template<typename... args_t>
inline void log(const char* function, args_t... args) {
Serial1.print("function call to : ");
Serial1.print(function);
Serial1.print("(");
// if there are any arguments to log do so
if constexpr (sizeof...(args_t) > 0) // false == do not print a comma on first call
log_parameters(false, std::forward<args_t>(args)...);
Serial1.println(");");
}
}
void my_function(const bool x, const int y, const char* str) {
details::log(__FUNCTION__, x, y, str);
}
void setup() {
Serial1.begin(9600);
Serial1.println("");
my_function(1, 42, "Hello world!");
}
void loop() { delay(1); }
/*
// Helper function to convert tuple elements to strings
template<typename Tuple, size_t... N>
void tupToArrImpl(const Tuple& t, String* arr, std::index_sequence<N...>) {
((arr[N] = String(std::get<N>(t))), ...);
}
template<typename... Args>
void tupToArr(const std::tuple<Args...>& t, String* arr) {
tupToArrImpl(t, arr, std::make_index_sequence<sizeof...(Args)>{});
}
void dumpParams(int x = 1, int y = 2) {
Serial1.println(__func__);
Serial1.println(__FUNCTION__);
Serial1.println(__PRETTY_FUNCTION__);
}
void someFunc(int a, int b) {
String prettyFunc = __PRETTY_FUNCTION__;
Serial1.println(prettyFunc);
}
void setup() {
Serial1.begin(9600);
someFunc(1,2);
dumpParams();
while (1) delay(1);
// Example tuple
auto myTuple = std::make_tuple(42, 3.14, "hello", true);
// Create array to hold strings
String strArray[4];
// Convert tuple to array
tupToArr(myTuple, strArray);
// Print results
for(int i = 0; i < 4; i++) {
Serial1.println(strArray[i]);
}
}
void loop() { delay(1); }
*/