Files
Geely/libs/SysTimer.h

84 lines
1.4 KiB
C++

#ifndef SYSTIMER_H
#define SYSTIMER_H
#include <cstdint>
#include "CanFwInterface.h"
// миллисекундный таймер. Макс. таймаут - 2**32 мс.
class TimerMs {
public:
typedef uint32_t T_Timer;
TimerMs()
{ Restart(); }
virtual ~TimerMs()
{ }
virtual inline T_Timer Restart(void)
{
auto prev = _tmr;
_tmr = CntValue();
return _tmr - prev;
}
inline T_Timer Value(void) const
{ return CntValue() - _tmr; }
inline T_Timer Rest(const T_Timer timeout) const
{
T_Timer elapsed = Value();
if (elapsed < timeout)
return timeout - elapsed;
return 0;
}
inline bool CheckTimeout(const T_Timer timeout) const
{ return Value() > timeout; }
private:
T_Timer _tmr;
static inline T_Timer CntValue(void)
{ return CoreFunc->GetTickMs(); }
};
class TimerMsOnce : public TimerMs
{
public:
TimerMsOnce() : TimerMs()
{ started = true; }
virtual ~TimerMsOnce()
{ }
virtual inline T_Timer Restart(void)
{
auto elapsed = TimerMs::Restart();
started = true;
return elapsed;
}
inline bool IsStarted(void) const
{ return started; }
inline void Stop(void)
{ started = false; }
virtual inline bool CheckTimeout(T_Timer timeout)
{
if (started)
{
if (Value() <= timeout)
return false;
Stop();
}
return true;
}
private:
bool started;
};
#endif // SYSTIMER_H