Initial commit
This commit is contained in:
83
libs/SysTimer.h
Normal file
83
libs/SysTimer.h
Normal file
@@ -0,0 +1,83 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user