X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;ds=sidebyside;f=CAN-binder%2Flow-can-binding%2Futils%2Ftimer.cpp;h=3d90a00756eeefdbad8764e6275be3686c3e4e24;hb=5d96c27e2679e37bd7354d3c618a4fc3111f137c;hp=a35a3e9f13170eb67998d9e869be9cbefe7bd1c9;hpb=f44a5b1549bc4c8a84d0dedf4a8b1e4220a34f42;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 a35a3e9..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" @@ -54,16 +55,28 @@ frequency_clock_t::frequency_clock_t() : unit_{1000000}, frequency_{10.0}, last_tick_{0}, time_function_{nullptr} {} - 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 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() @@ -84,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; }