#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;
		}
	}
}
