/*
Code to run on 66s or 32s, despite platform differences, eg. time_t 4B on 32s, but 8B on 66s
Need printf format string with variable type length specifier,
to accomodate the same code on both platforms,
so this ("%010lld %06ld micros\n",time_t,long) // on esp8266
must be ("%010ld %06ld micros\n",time_t,long) // on esp32
Build user function to leverage std::string functions: find, replace
Needs to extend std::string.replace to 'replace all'
*/
#if defined (ARDUINO_ARCH_ESP8266)
#include <string> // support std::string on esp8266 (not req'd on esp32)
#include <ESP8266WiFi.h>
#endif
// https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string
// extend std::string.replace to 'replace all'
bool
replaceFirst (std::string &str, const std::string &from, const std::string &to, size_t start_pos = std::string::npos) {
if (start_pos == std::string::npos)
start_pos = str.find(from);
if (start_pos == std::string::npos) // not found
return false;
str.replace(start_pos, from.length(), to);
return true;
}
// replace all occurences
int
replace (std::string &str, const std::string &from, const std::string &to) {
size_t start_pos;
uint16_t replaced = 0;
while ((start_pos = str.find(from)) != std::string::npos) {
replaceFirst (str, from, to, start_pos);
replaced++;
}
return replaced;
}
std::string fmt_str ("%010lld %06ld 1st length specifier was for long long 64-bit var\n"); // var not literal so can modify if need be
time_t B4orB8 = 0x7ffffff; // max signed 4 byte, promoted if on esp8266 to 8 byte
byte tt; // sizeof time_t
long mikes = 0xfff;
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.println(F(__FILE__ " ... compiled " __TIME__ " " __DATE__));
// Serial.println(LONG_LONG_MAX);
tt = sizeof(time_t);
Serial.println("Our format string that, depending on platform, may need a different length type/conversion specifier:");
Serial.print(fmt_str.c_str());
Serial.println();
if (tt == 4) // we need to change length specifier
{
Serial.printf("Detected %hu byte time_t, typical of esp32, 'lld' for 32-bit vars need 'ld'\n", tt);
replace (fmt_str, "10lld", "10ld");
Serial.println("Our 'replaced' format string now with 'ld' specifier(s):");
Serial.print(fmt_str.c_str());
}
else if (tt == 8)
{
Serial.printf("format string not changed, 'lld' appropriate for %hu byte time_t, typical of esp8266\n", tt);
}
Serial.println();
Serial.println("And the formatted string:");
Serial.printf(fmt_str.c_str(), B4orB8, mikes);
Serial.println();
}
void loop()
{
delay(10); // this speeds up the simulation
}