From 77191f2fe97a7910386aa87405bf22747a8c394d Mon Sep 17 00:00:00 2001 From: Hadinger Date: Tue, 14 Jan 2020 16:41:47 +0100 Subject: [PATCH] Simplified TimeDifference (saves 16 bytes) --- tasmota/support.ino | 35 ++++------------------------------- 1 file changed, 4 insertions(+), 31 deletions(-) diff --git a/tasmota/support.ino b/tasmota/support.ino index 011fa8ca4..a9a631fd1 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -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(diff); // Difference is a positive value. - } else { - // prev has overflow, return a negative difference value - signed_diff = static_cast((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(diff); - signed_diff = -1 * signed_diff; - } else { - // next has overflow, return a positive difference value - signed_diff = static_cast((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);