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=8c6215b025e958334fd01ad64ec8aaa202a0016c;hpb=c59a57338a488cc451482f1180fe672e7d5c9179;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 8c6215b..3d90a00 100644 --- a/CAN-binder/low-can-binding/utils/timer.cpp +++ b/CAN-binder/low-can-binding/utils/timer.cpp @@ -57,20 +57,23 @@ 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_, &f); + freq.tv_usec = (long int)(std::modf(frequency_to_period(), &f) * unit_); freq.tv_sec = (time_t)f; return freq; @@ -94,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; }