From 7163aa04a0e3fb0697246859e32889c49c95541c Mon Sep 17 00:00:00 2001 From: Edgar Bonet Date: Tue, 6 Nov 2018 10:42:10 +0100 Subject: [PATCH 1/5] Timer: simplify constructors The three constructors have been replaced by a single one with default arguments. Timer::Create(), now redundant, has been removed. --- Timer/Timer.cpp | 12 ------------ Timer/Timer.h | 5 +---- 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/Timer/Timer.cpp b/Timer/Timer.cpp index 091ff66..5c42de2 100644 --- a/Timer/Timer.cpp +++ b/Timer/Timer.cpp @@ -19,19 +19,7 @@ #include "Timer.h" -Timer::Timer(unsigned long int ms){ - Create(ms, NULL, false); -} - -Timer::Timer(unsigned long int ms, CallBackType callback){ - Create(ms, callback, false); -} - Timer::Timer(unsigned long int ms, CallBackType callback, bool isSingle){ - Create(ms, callback, isSingle); -} - -void Timer::Create(unsigned long int ms, CallBackType callback, bool isSingle){ setInterval(ms); setEnabled(false); setSingleShot(isSingle); diff --git a/Timer/Timer.h b/Timer/Timer.h index a0a3943..2c2c208 100644 --- a/Timer/Timer.h +++ b/Timer/Timer.h @@ -27,7 +27,6 @@ typedef void (*CallBackType)(); class Timer{ private: - void Create(unsigned long int ms, CallBackType callback, bool isSingle); unsigned long int msInterval; bool blEnabled; bool blSingleShot; @@ -37,9 +36,7 @@ class Timer{ unsigned long DiffTime;//used when I pause the Timer and need to resume public: - Timer(unsigned long int ms); - Timer(unsigned long int ms, CallBackType callback); - Timer(unsigned long int ms, CallBackType callback, bool isSingle); + Timer(unsigned long int ms, CallBackType callback = NULL, bool isSingle = false); ~Timer(); void setInterval(unsigned long int ms); From af418e76d6e5a0980e93f3ebcc849b1b04abe766 Mon Sep 17 00:00:00 2001 From: Edgar Bonet Date: Tue, 6 Nov 2018 10:49:11 +0100 Subject: [PATCH 2/5] Simplify Timer::setInterval() Since the argument has an unsigned type, it cannot be negative. --- Timer/Timer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Timer/Timer.cpp b/Timer/Timer.cpp index 5c42de2..b448244 100644 --- a/Timer/Timer.cpp +++ b/Timer/Timer.cpp @@ -28,7 +28,7 @@ Timer::Timer(unsigned long int ms, CallBackType callback, bool isSingle){ } void Timer::setInterval(unsigned long int ms){ - msInterval = (ms > 0) ? ms : 0; + msInterval = ms; } void Timer::setEnabled(bool Enabled){ From d93bd8878d653b1b9065b9c13c87271511339059 Mon Sep 17 00:00:00 2001 From: Edgar Bonet Date: Tue, 6 Nov 2018 10:54:28 +0100 Subject: [PATCH 3/5] Timer: remove redundant typecasts --- Timer/Timer.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Timer/Timer.cpp b/Timer/Timer.cpp index b448244..a457502 100644 --- a/Timer/Timer.cpp +++ b/Timer/Timer.cpp @@ -74,7 +74,7 @@ bool Timer::Tick(){ return false; if(LastTime > millis()*2)//millis restarted LastTime = 0; - if ((unsigned long int)(millis() - LastTime) >= msInterval) { + if (millis() - LastTime >= msInterval) { LastTime = millis(); if(isSingleShot()) setEnabled(false); @@ -89,14 +89,14 @@ unsigned long int Timer::getInterval(){ } unsigned long int Timer::getCurrentTime(){ - return (unsigned long int)(millis() - LastTime); + return millis() - LastTime; } unsigned long int Timer::getRemaining() { if (blEnabled) { - return msInterval - (unsigned long int)(millis() - LastTime); + return msInterval - (millis() - LastTime); } else { - return msInterval - (unsigned long int)(millis() - (millis() - DiffTime)); + return msInterval - (millis() - (millis() - DiffTime)); } } From 977e371452d798937c5bcc087e8b5a157e2572b8 Mon Sep 17 00:00:00 2001 From: Edgar Bonet Date: Tue, 6 Nov 2018 11:01:01 +0100 Subject: [PATCH 4/5] Simplify Timer::getRemaining() Since unsigned numbers obey the rules of modular arithmetics, the usual rules of commutativity and associativity of addition apply. --- Timer/Timer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Timer/Timer.cpp b/Timer/Timer.cpp index a457502..67b23e7 100644 --- a/Timer/Timer.cpp +++ b/Timer/Timer.cpp @@ -96,7 +96,7 @@ unsigned long int Timer::getRemaining() { if (blEnabled) { return msInterval - (millis() - LastTime); } else { - return msInterval - (millis() - (millis() - DiffTime)); + return msInterval - DiffTime; } } From 55c2d15b88544d449ca7859993f1d7914e8fee52 Mon Sep 17 00:00:00 2001 From: Edgar Bonet Date: Tue, 6 Nov 2018 11:09:16 +0100 Subject: [PATCH 5/5] Timer: fix millis() rollover bug There is no point in trying to detect a millis() rollover event as, per the rules of modular arithmetics, the subtraction millis() - LastTime is rollover safe. Fixes #4. --- Timer/Timer.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/Timer/Timer.cpp b/Timer/Timer.cpp index 67b23e7..6eeb0b2 100644 --- a/Timer/Timer.cpp +++ b/Timer/Timer.cpp @@ -72,8 +72,6 @@ void Timer::Update(){ bool Timer::Tick(){ if(!blEnabled) return false; - if(LastTime > millis()*2)//millis restarted - LastTime = 0; if (millis() - LastTime >= msInterval) { LastTime = millis(); if(isSingleShot())