66 lines
1.4 KiB
C++
66 lines
1.4 KiB
C++
#include "utils.h"
|
|
|
|
#include <Arduino.h>
|
|
|
|
namespace utils
|
|
{
|
|
unsigned long IRAM_ATTR elapsed(unsigned long from, unsigned long to)
|
|
{
|
|
if(to >= from)
|
|
{
|
|
return to - from;
|
|
}
|
|
else
|
|
{
|
|
// if the counter overflowed, this computes the real duration
|
|
// of course it won't work if the counter made a "full turn" or more
|
|
const unsigned long biggestValue = (unsigned long)-1;
|
|
return (biggestValue - from) + to + 1;
|
|
}
|
|
}
|
|
|
|
uint32_t elapsed(uint32_t from, uint32_t to)
|
|
{
|
|
if(to >= from)
|
|
{
|
|
return to - from;
|
|
}
|
|
else
|
|
{
|
|
// if the counter overflowed, this computes the real duration
|
|
// of course it won't work if the counter made a "full turn" or more
|
|
const uint32_t biggestValue = (uint32_t)-1;
|
|
return (biggestValue - from) + to + 1;
|
|
}
|
|
}
|
|
|
|
void replaceString(char* str, char* searchStr, char* replacementStr)
|
|
{
|
|
size_t searchStrLen = strlen(searchStr);
|
|
size_t replacementStrLen = strlen(replacementStr);
|
|
|
|
char* newPos = str;
|
|
char* nextSearchPos = str;
|
|
while(true)
|
|
{
|
|
char* p = strstr(nextSearchPos, searchStr);
|
|
if(p == nullptr) p = nextSearchPos + strlen(nextSearchPos);
|
|
|
|
size_t l = p - nextSearchPos;
|
|
if(newPos != nextSearchPos)
|
|
{
|
|
memcpy(newPos, nextSearchPos, l);
|
|
}
|
|
newPos += l;
|
|
|
|
if(*p == 0)
|
|
break;
|
|
|
|
memcpy(newPos, replacementStr, replacementStrLen);
|
|
newPos += replacementStrLen;
|
|
|
|
nextSearchPos = p + searchStrLen;
|
|
}
|
|
}
|
|
}
|