summaryrefslogtreecommitdiffstats
path: root/src/utils/timer.cpp
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-03-10 02:02:00 +0100
committerRomain Forlot <romain.forlot@iot.bzh>2017-03-16 17:10:40 +0100
commit04f14d0017e94177c6afdf4f5b7df71f25fbc18e (patch)
treeb79ae20c8e30b7cf3f685eec97c0e13ef6d0e260 /src/utils/timer.cpp
parent7735f7d8603859431af40702f3e274c5e1d90e5c (diff)
Adding needed function to get some time handle on request
for diagnostic at first. Change-Id: I3ab966d6386bad52f68ebdbea723bb7507cfaf2a Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'src/utils/timer.cpp')
-rw-r--r--src/utils/timer.cpp43
1 files changed, 40 insertions, 3 deletions
diff --git a/src/utils/timer.cpp b/src/utils/timer.cpp
index 433fcb26..966178be 100644
--- a/src/utils/timer.cpp
+++ b/src/utils/timer.cpp
@@ -15,9 +15,13 @@
* limitations under the License.
*/
+#include <stdlib.h>
+
#include "timer.hpp"
-long long int systemTimeMs()
+#define MS_PER_SECOND 1000
+
+long long int system_time_ms()
{
struct timeb t_msec;
long long int timestamp_msec;
@@ -30,7 +34,40 @@ long long int systemTimeMs()
return timestamp_msec;
}
-
frequency_clock_t::frequency_clock_t()
: frequency_{0.0}, last_tick_{0}, time_function_{nullptr}
-{} \ No newline at end of file
+{}
+
+
+frequency_clock_t::frequency_clock_t(float frequency)
+ : frequency_{frequency}, last_tick_{0}, time_function_{nullptr}
+{}
+
+/// @brief Return the period in ms given the frequency in hertz.
+float frequency_clock_t::frequency_to_period(float frequency)
+{
+ return 1 / frequency * MS_PER_SECOND;
+}
+
+bool frequency_clock_t::started()
+{
+ return last_tick_ != 0;
+}
+
+time_function_t frequency_clock_t::get_time_function()
+{
+ return time_function_ != nullptr ? time_function_ : system_time_ms;
+}
+
+bool frequency_clock_t::elapsed(bool stagger)
+{
+ float period = frequency_to_period(frequency_);
+ float elapsed_time = 0;
+ if(!started() && stagger)
+ last_tick_ = get_time_function()() - (rand() % int(period));
+ else
+ // Make sure it ticks the the first call to conditionalTick(...)
+ elapsed_time = !started() ? period : get_time_function()() - last_tick_;
+
+ return frequency_ == 0 || elapsed_time >= period;
+}