Metronome.cpp
Go to the documentation of this file.
1#include "Metronome.h"
2
4
5namespace armarx::core::time
6{
7
8 Metronome::Metronome(const Frequency& targetFrequency, ClockType clockType, NextTickMode mode) :
9 _clock{clockType}, _targetPeriod{targetFrequency.toCycleDuration()}
10 {
11 reset(mode);
12 }
13
14 Metronome::Metronome(const Duration& targetPeriod, ClockType clockType) :
15 _clock{clockType}, _targetPeriod{targetPeriod}
16 {
17 reset();
18 }
19
20 Metronome::Metronome(const Duration& targetPeriod, const Clock& clock) :
21 _clock{clock}, _targetPeriod(targetPeriod)
22 {
23 reset();
24 }
25
28 {
29 const Duration waitTime = _clock.waitUntil(_nextCheckpoint);
30 _nextCheckpoint += _targetPeriod;
31 return waitTime;
32 }
33
34 void
36 {
37 _nextCheckpoint =
38 mode == NextTickMode::wait_for_next_tick ? _clock.now() + _targetPeriod : _clock.now();
39 }
40
41} // namespace armarx::core::time
Clock to get date/time from a specific clock type or wait for certain durations or until certain date...
Definition Clock.h:26
Represents a duration.
Definition Duration.h:17
Represents a frequency.
Definition Frequency.h:17
Duration waitForNextTick() const
Wait and block until the target period is met.
Definition Metronome.cpp:27
NextTickMode
Modes that influence how the reset method should function for the next waitForNextTick() method call.
Definition Metronome.h:65
@ wait_for_next_tick
If waitForNextTicK() is called shortly after reset() (or any constructor), the metronome will wait fo...
Definition Metronome.h:70
Metronome(const Frequency &targetFrequency, ClockType clockType=ClockType::Virtual, NextTickMode mode=NextTickMode::wait_for_next_tick)
Definition Metronome.cpp:8
void reset(NextTickMode mode=NextTickMode::wait_for_next_tick) const
Resets the rate limiter so that the next targetted time point will be in the current time plus the ta...
Definition Metronome.cpp:35
ClockType
Describes the type of clock.
Definition ClockType.h:10