From 128ae3935cf6b243882a7d29cc7564c893bf7b97 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 28 Mar 2020 00:15:21 -0400 Subject: [PATCH] Make Timer generic, add comments --- Firmware/MotorControl/endstop.hpp | 2 +- Firmware/MotorControl/timer.hpp | 15 ++++++++------- Firmware/Tests/test_timer.cpp | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Firmware/MotorControl/endstop.hpp b/Firmware/MotorControl/endstop.hpp index 02f116b7..7c7fa8b4 100644 --- a/Firmware/MotorControl/endstop.hpp +++ b/Firmware/MotorControl/endstop.hpp @@ -44,6 +44,6 @@ class Endstop { private: bool pin_state_ = false; float pos_when_pressed_ = 0.0f; - Timer debounceTimer_; + Timer debounceTimer_; }; #endif \ No newline at end of file diff --git a/Firmware/MotorControl/timer.hpp b/Firmware/MotorControl/timer.hpp index e1b6b0b0..a5e571cf 100644 --- a/Firmware/MotorControl/timer.hpp +++ b/Firmware/MotorControl/timer.hpp @@ -1,13 +1,14 @@ #pragma once #include +template class Timer { public: - void setTimeout(const float timeout) { + void setTimeout(const T timeout) { timeout_ = timeout; } - void setInterval(const float interval) { + void setInterval(const T interval) { interval_ = interval; } @@ -25,7 +26,7 @@ class Timer { } void reset() { - timer_ = 0.0f; + timer_ = static_cast(0); } bool expired() { @@ -33,8 +34,8 @@ class Timer { } private: - float timer_ = 0.0f; - float timeout_ = 0.0f; - float interval_ = 0.0f; - bool running_ = false; + T timer_ = static_cast(0); // Current state + T timeout_ = static_cast(0); // Time before + T interval_ = static_cast(0); // Amount to increment each time update() is called + bool running_ = false; // update() only increments if runing_ is true }; diff --git a/Firmware/Tests/test_timer.cpp b/Firmware/Tests/test_timer.cpp index bd34b7c6..7d65ccb2 100644 --- a/Firmware/Tests/test_timer.cpp +++ b/Firmware/Tests/test_timer.cpp @@ -3,7 +3,7 @@ #include "MotorControl/timer.hpp" TEST_CASE("Timer"){ - Timer myTimer; + Timer myTimer; myTimer.setTimeout(10); myTimer.setInterval(1); CHECK(!myTimer.expired());