WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
Notifier.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008-2017. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <stdint.h>
11 
12 #include <atomic>
13 #include <functional>
14 #include <utility>
15 
16 #include "ErrorBase.h"
17 #include "HAL/Notifier.h"
18 #include "HAL/cpp/priority_mutex.h"
19 
20 namespace frc {
21 
22 typedef std::function<void()> TimerEventHandler;
23 
24 class Notifier : public ErrorBase {
25  public:
26  explicit Notifier(TimerEventHandler handler);
27 
28  template <typename Callable, typename Arg, typename... Args>
29  Notifier(Callable&& f, Arg&& arg, Args&&... args)
30  : Notifier(std::bind(std::forward<Callable>(f), std::forward<Arg>(arg),
31  std::forward<Args>(args)...)) {}
32 
33  virtual ~Notifier();
34 
35  Notifier(const Notifier&) = delete;
36  Notifier& operator=(const Notifier&) = delete;
37 
38  void StartSingle(double delay);
39  void StartPeriodic(double period);
40  void Stop();
41 
42  private:
43  // update the HAL alarm
44  void UpdateAlarm();
45  // HAL callback
46  static void Notify(uint64_t currentTimeInt, HAL_NotifierHandle handle);
47 
48  // used to constrain execution between destructors and callback
49  static hal::priority_mutex m_destructorMutex;
50  // held while updating process information
51  hal::priority_mutex m_processMutex;
52  // HAL handle, atomic for proper destruction
53  std::atomic<HAL_NotifierHandle> m_notifier{0};
54  // address of the handler
55  TimerEventHandler m_handler;
56  // the absolute expiration time
57  double m_expirationTime = 0;
58  // the relative time (either periodic or single)
59  double m_period = 0;
60  // true if this is a periodic event
61  bool m_periodic = false;
62 };
63 
64 } // namespace frc
void Stop()
Stop timer events from occuring.
Definition: Notifier.cpp:132
Definition: Notifier.h:24
void StartPeriodic(double period)
Register for periodic event notification.
Definition: Notifier.cpp:115
void StartSingle(double delay)
Register for single event notification.
Definition: Notifier.cpp:97
Base class for most objects.
Definition: ErrorBase.h:72
Notifier(TimerEventHandler handler)
Create a Notifier for timer event notification.
Definition: Notifier.cpp:25
Definition: priority_mutex.h:57
virtual ~Notifier()
Free the resources for a timer event.
Definition: Notifier.cpp:37