Simplified TimeDifference (saves 16 bytes)

This commit is contained in:
Hadinger 2020-01-14 16:41:47 +01:00
parent ceef563414
commit 77191f2fe9
1 changed files with 4 additions and 31 deletions

View File

@ -1244,45 +1244,18 @@ void TemplateJson(void)
* Sleep aware time scheduler functions borrowed from ESPEasy
\*********************************************************************************************/
long TimeDifference(unsigned long prev, unsigned long next)
{
// Return the time difference as a signed value, taking into account the timers may overflow.
// Returned timediff is between -24.9 days and +24.9 days.
// Returned value is positive when "next" is after "prev"
long signed_diff = 0;
// To cast a value to a signed long, the difference may not exceed half 0xffffffffUL (= 4294967294)
const unsigned long half_max_unsigned_long = 2147483647u; // = 2^31 -1
if (next >= prev) {
const unsigned long diff = next - prev;
if (diff <= half_max_unsigned_long) { // Normal situation, just return the difference.
signed_diff = static_cast<long>(diff); // Difference is a positive value.
} else {
// prev has overflow, return a negative difference value
signed_diff = static_cast<long>((0xffffffffUL - next) + prev + 1u);
signed_diff = -1 * signed_diff;
}
} else {
// next < prev
const unsigned long diff = prev - next;
if (diff <= half_max_unsigned_long) { // Normal situation, return a negative difference value
signed_diff = static_cast<long>(diff);
signed_diff = -1 * signed_diff;
} else {
// next has overflow, return a positive difference value
signed_diff = static_cast<long>((0xffffffffUL - prev) + next + 1u);
}
}
return signed_diff;
inline int32_t TimeDifference(uint32_t prev, uint32_t next) {
return ((int32_t) (next - prev));
}
long TimePassedSince(unsigned long timestamp)
int32_t TimePassedSince(uint32_t timestamp)
{
// Compute the number of milliSeconds passed since timestamp given.
// Note: value can be negative if the timestamp has not yet been reached.
return TimeDifference(timestamp, millis());
}
bool TimeReached(unsigned long timer)
bool TimeReached(uint32_t timer)
{
// Check if a certain timeout has been reached.
const long passed = TimePassedSince(timer);