CDPTimer Class
The CDPTimer is a real-time timer for delay and time monitoring. More...
Header: | #include <OSAPI/Timer/Timer.h> |
Inherits: | CDP::StudioAPI::CDPNode |
Public Functions
CDPTimer(bool editableTimeout = false) | |
virtual void | Create(const char *shortName, CDPBaseObject *pParent) |
double | Delay() |
double | DeltaTime() |
virtual void | Destroy() |
double | Frequency() |
bool | IsRunning() |
double | LastTime() |
void | Reset(double delay) |
void | Reset() |
void | Restart() |
void | Set() |
void | SetModel(const std::string &model) |
void | SetTimeoutDelayCallback(std::function<void( CDP::StudioAPI::CDPVariantValue ) > callback) |
virtual void | Start() |
bool | State() |
double | TimeElapsed() |
bool | TimedOut() |
Reimplemented Public Functions
virtual void | FillNodeChildren(CDP::StudioAPI::NodeStream &stream) const override |
virtual const std::string | GetNodeName() const override |
virtual std::string | GetNodeTypeName() const override |
- 26 public functions inherited from CDP::StudioAPI::CDPNode
- 22 public functions inherited from CDP::StudioAPI::ICDPNode
Static Public Members
int64_t | GetCountsPrSec(void) |
int | Init() |
void | SetCountsPrSec(int64_t ticks) |
void | SetZeroTime(double timeOffset) |
double | Time() |
Protected Variables
CDPSetting<int64_t> | m_currentTimeCnt |
CDPSetting<std::string> | m_model |
CDPSetting<int64_t> | m_previousTimeCnt |
CDPSetting<bool> | m_running |
CDPSetting<int64_t> | m_startTimeCnt |
CDPSetting<double> | m_timeElapsed |
CDPSetting<bool> | m_timedout |
CDPSetting<double> | m_timeoutDelay |
CDPSetting<int64_t> | m_timeoutDelayCnt |
CDPSetting<int64_t> | m_timeoutTimeCnt |
Static Protected Members
int64_t | SecondsToTimerCounts(double seconds) |
double | TimerCountsToSeconds(int64_t counts) |
int64_t | l_timerCountsPrSec |
int64_t | zeroTime |
Additional Inherited Members
- 1 protected function inherited from CDP::StudioAPI::CDPNode
Detailed Description
The CDPTimer is a real-time timer for delay and time monitoring.
If the timer is used in a CDPComponent based class, the timer and it's values may show up in CDP Studio under the component's Timers section if Create()
is called. In component's Create()
, call myTimer.Create("MyTimer",this)
, and in component's Destroy()
, call myTimer.Destroy()
.
Use as delay timer
- Init using Reset(double delay).
- Call Start()(or Restart()) to enable timeout checking.
- Call TimedOut() to check if time has expired.
- Call Restart() to reset and start new timeout check when time has expired.
Example
Declare and start the timer
CDPTimer myTimer; // declaration myTimer.Reset( 10.0 ); // Set timeout value in seconds myTimer.Start(); // Will start timing from now
In some Process() method check if timer is timed out
if (myTimer.TimedOut()) { myTimer.Restart(); // Start new timing cycle CDPMessage("This message will be written every 10s.\n") }
Use as stopwatch timer
- Call Start() to Start the timer.
- Read time elapsed since Start() was called, with TimeElapsed().
- Call TimedOut() to start timing. Timer initialises automatically on first run.
- Call TimedOut() again to stop timing.
- Read time in s between two last TimedOut() calls, with DeltaTime().
Example
Declare and start the timer
CDPTimer myTimer;
myTimer.Start();
Measure the time between two TimedOut() calls
myTimer.TimedOut() SomeFunctionWeWantToMesureDurationOf(); myTimer.TimedOut(); double duration = myTimer.DeltaTime(); CDPMessage("SomeFunctionWeWantToMesureDurationOf() took %f seconds.\n", duration);
See also CDPParameterTimer, CDPTimerMs, CDPParameterTimerMs, OSAPIPeriodicTimer, OSAPIOneShotTimer, CDPTimerCounting, CDPRampTimer, and CDPComponent::RunInComponentThread.
Member Function Documentation
CDPTimer::CDPTimer(bool editableTimeout = false)
Constructs a CDPTimer
[virtual]
void CDPTimer::Create(const char *shortName, CDPBaseObject *pParent)
Stores shortName, calls parent's RegisterCDPNode()
and sets up callback when m_timeoutDelay
changes.
double CDPTimer::Delay()
Gets the delay in seconds set by Reset(double delay)
double CDPTimer::DeltaTime()
Reads time in seconds between last two TimedOut() calls.
[virtual]
void CDPTimer::Destroy()
Calls parent's UnRegisterCDPNode()
.
[override virtual]
void CDPTimer::FillNodeChildren(CDP::StudioAPI::NodeStream &stream) const
Reimplemented from CDPNode::FillNodeChildren().
Writes its CDPSettings to stream.
double CDPTimer::Frequency()
Reads frequency in Hz of time interval between last two TimedOut() calls.
[static]
int64_t CDPTimer::GetCountsPrSec(void)
[override virtual]
const std::string CDPTimer::GetNodeName() const
Reimplemented from ICDPNode::GetNodeName().
Returns m_timerName
[override virtual]
std::string CDPTimer::GetNodeTypeName() const
Reimplemented from ICDPNode::GetNodeTypeName().
Returns "CDPTimer"
[static]
int CDPTimer::Init()
Initializes CDPTimer::zeroTime
to current time if it is 0. Returns 0 if initialization failed.
bool CDPTimer::IsRunning()
Returns true
if timer is running.
double CDPTimer::LastTime()
Gets time in seconds between last TimedOut() called and the first CDPTimer initialized.
void CDPTimer::Reset(double delay)
Resets and sets delay timeout in seconds.
This method automatically initializes the timer (calls Init()) if necessary. Remember to call Start() to enable the timeout timer after a call to Reset().
void CDPTimer::Reset()
Resets the timeout timer flag and stop the timer (m_timedout
and m_running
are set to false).
This method automatically initializes the timer (calls Init()) if necessary. Remember to call Start() to enable the timeout timer after a call to Reset().
void CDPTimer::Restart()
Resets and starts the timer.
Call this method after each TimedOut() with timeout to enable next timeout checking. Call this method to Reset and Start the timer (equals calling Reset() and Start())
[static protected]
int64_t CDPTimer::SecondsToTimerCounts(double seconds)
Multiplies seconds with CDPTimer::l_timerCountsPrSec.
void CDPTimer::Set()
Sets m_timedout to true
.
[static]
void CDPTimer::SetCountsPrSec(int64_t ticks)
Sets ticks to the CDPTimer::l_timerCountsPrSec
.
void CDPTimer::SetModel(const std::string &model)
void CDPTimer::SetTimeoutDelayCallback(std::function<void( CDP::StudioAPI::CDPVariantValue ) > callback)
Sets up callback when m_timeoutDelay
changes.
[static]
void CDPTimer::SetZeroTime(double timeOffset)
Sets new zero time by muliplying CDPTimer::l_timerCountsPrSec
with timeOffset.
[virtual]
void CDPTimer::Start()
Starts timer.
bool CDPTimer::State()
Returns true
if timer is timed out, as calculated in TimedOut().
[static]
double CDPTimer::Time()
Gets current time in seconds since first CDPTimer initialized.
double CDPTimer::TimeElapsed()
Gets time in seconds from the last Start() (or Restart()) was called.
bool CDPTimer::TimedOut()
Returns true
if timer has timed out. Updates delta time, returned by DeltaTime().
Remember to Restart() the timer after reaching timeout delay time. It is not necessary to call TimedOut() periodically at an accurate time interval, the timer keeps track of real time internally.
See also DeltaTime() and Time().
[static protected]
double CDPTimer::TimerCountsToSeconds(int64_t counts)
Divides counts with CDPTimer::l_timerCountsPrSec.
Member Variable Documentation
int64_t CDPTimer::l_timerCountsPrSec
This variable holds system counts / sec.
CDPSetting<int64_t> CDPTimer::m_currentTimeCnt
This variable holds current time [counts]. Updated by TimedOut().
CDPSetting<std::string> CDPTimer::m_model
This variable holds timer object's modelname ('CDPTimer').
CDPSetting<int64_t> CDPTimer::m_previousTimeCnt
This variable holds previous time [counts]. Updated by TimedOut().
CDPSetting<bool> CDPTimer::m_running
This variable holds set to true when timer is running.
CDPSetting<int64_t> CDPTimer::m_startTimeCnt
This variable holds time when Start() was called [counts].
CDPSetting<double> CDPTimer::m_timeElapsed
This variable holds time elapsed [s] since Start()/Restart(), as calculated by TimedOut() function.
CDPSetting<bool> CDPTimer::m_timedout
This variable holds set to true when timer has timed out.
CDPSetting<double> CDPTimer::m_timeoutDelay
This variable holds timeout delay [s]. Set in Reset(double delay) and returned in Delay(). Has higher precision than 'm_timeoutDelayCnt'.
CDPSetting<int64_t> CDPTimer::m_timeoutDelayCnt
This variable holds timeout delay [counts].
CDPSetting<int64_t> CDPTimer::m_timeoutTimeCnt
This variable holds timeout absolute time [counts]. Sum of m_startTimeCnt and m_timeoutDelayCnt, set in Start().
int64_t CDPTimer::zeroTime
This variable holds system zero time [counts].
Get started with CDP Studio today
Let us help you take your great ideas and turn them into the products your customer will love.