X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=CAN-binder%2Flow-can-binding%2Futils%2Ftimer.cpp;h=3d90a00756eeefdbad8764e6275be3686c3e4e24;hb=5d96c27e2679e37bd7354d3c618a4fc3111f137c;hp=3bc96a3a8b5714c2034b47cab90758341c3b45f9;hpb=f04542587423e3b26d76c25d1c84acd5dea180b8;p=apps%2Flow-level-can-service.git diff --git a/CAN-binder/low-can-binding/utils/timer.cpp b/CAN-binder/low-can-binding/utils/timer.cpp index 3bc96a3..3d90a00 100644 --- a/CAN-binder/low-can-binding/utils/timer.cpp +++ b/CAN-binder/low-can-binding/utils/timer.cpp @@ -17,6 +17,7 @@ #include #include +#include #include "timer.hpp" @@ -56,13 +57,26 @@ frequency_clock_t::frequency_clock_t() frequency_clock_t::frequency_clock_t(float frequency) : unit_{1000000}, frequency_{frequency}, last_tick_{0}, time_function_{nullptr} -{} +{ + if(frequency_ <= 0) + frequency_ = 1; +} /// @brief Return the period in ms given the frequency in hertz. /// @param[in] frequency - Frequency to convert, in hertz -float frequency_clock_t::frequency_to_period() +float frequency_clock_t::frequency_to_period() const { - return frequency_ == 0 ? 0 : 1 / frequency_ * unit_; + return frequency_ == 0 ? 0 : 1 / frequency_; +} + +const struct timeval frequency_clock_t::get_timeval_from_period() const +{ + struct timeval freq = {0, 0}; + float f; + freq.tv_usec = (long int)(std::modf(frequency_to_period(), &f) * unit_); + freq.tv_sec = (time_t)f; + + return freq; } bool frequency_clock_t::started() @@ -83,7 +97,7 @@ bool frequency_clock_t::elapsed(bool stagger) last_tick_ = get_time_function()() - (rand() % int(period)); // Make sure it ticks the the first call - elapsed_time = !started() ? period : get_time_function()() - last_tick_; + elapsed_time = !started() ? period : (float)get_time_function()() - (float)last_tick_; return frequency_ == 0 || elapsed_time >= period; }