timer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #ifndef UAVCAN_NODE_TIMER_HPP_INCLUDED
6 #define UAVCAN_NODE_TIMER_HPP_INCLUDED
7 
8 #include <uavcan/std.hpp>
9 #include <uavcan/error.hpp>
10 #include <uavcan/build_config.hpp>
15 
16 #if !defined(UAVCAN_CPP11) || !defined(UAVCAN_CPP_VERSION)
17 # error UAVCAN_CPP_VERSION
18 #endif
19 
20 #if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
21 # include <functional>
22 #endif
23 
24 namespace uavcan
25 {
26 
28 
33 {
36 
37  TimerEvent(MonotonicTime arg_scheduled_time, MonotonicTime arg_real_time)
38  : scheduled_time(arg_scheduled_time)
39  , real_time(arg_real_time)
40  { }
41 };
42 
47 {
49 
50  virtual void handleDeadline(MonotonicTime current);
51 
52 public:
57 
58  explicit TimerBase(INode& node)
59  : DeadlineHandler(node.getScheduler())
60  , period_(MonotonicDuration::getInfinite())
61  { }
62 
69  void startOneShotWithDeadline(MonotonicTime deadline);
70  void startOneShotWithDelay(MonotonicDuration delay);
71  void startPeriodic(MonotonicDuration period);
72 
77  MonotonicDuration getPeriod() const { return period_; }
78 
82  virtual void handleTimerEvent(const TimerEvent& event) = 0;
83 };
84 
95 template <typename Callback_>
97 {
98 public:
99  typedef Callback_ Callback;
100 
101 private:
102  Callback callback_;
103 
104  virtual void handleTimerEvent(const TimerEvent& event)
105  {
106  if (coerceOrFallback<bool>(callback_, true))
107  {
108  callback_(event);
109  }
110  else
111  {
112  handleFatalError("Invalid timer callback");
113  }
114  }
115 
116 public:
118  : TimerBase(node)
119  , callback_()
120  { }
121 
122  TimerEventForwarder(INode& node, const Callback& callback)
123  : TimerBase(node)
124  , callback_(callback)
125  { }
126 
131  const Callback& getCallback() const { return callback_; }
132  void setCallback(const Callback& callback) { callback_ = callback; }
133 };
134 
135 
136 #if UAVCAN_CPP_VERSION >= UAVCAN_CPP11
137 
143 
144 #endif
145 
146 }
147 
148 #endif // UAVCAN_NODE_TIMER_HPP_INCLUDED
const Callback & getCallback() const
Definition: timer.hpp:131
TimerBase(INode &node)
Definition: timer.hpp:58
TimerEventForwarder(INode &node)
Definition: timer.hpp:117
MonotonicTime scheduled_time
Time when the timer callback was expected to be invoked.
Definition: timer.hpp:34
UAVCAN_EXPORT void handleFatalError(const char *msg)
Definition: uc_error.cpp:20
Scheduler & getScheduler() const
Definition: scheduler.hpp:42
void setCallback(const Callback &callback)
Definition: timer.hpp:132
TimerEvent(MonotonicTime arg_scheduled_time, MonotonicTime arg_real_time)
Definition: timer.hpp:37
TimerEventForwarder< std::function< void(const TimerEvent &event)> > Timer
Definition: timer.hpp:142
virtual void handleTimerEvent(const TimerEvent &event)
Definition: timer.hpp:104
MonotonicTime getDeadline() const
Definition: scheduler.hpp:41
MonotonicTime real_time
True time when the timer callback was invoked.
Definition: timer.hpp:35
MonotonicDuration period_
Definition: timer.hpp:48
TimerEventForwarder(INode &node, const Callback &callback)
Definition: timer.hpp:122
MonotonicDuration getPeriod() const
Definition: timer.hpp:77


uavcan_communicator
Author(s):
autogenerated on Wed Jan 11 2023 03:59:40