WPILibC++  unspecified
Notifier.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2017 FIRST. 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 <mutex>
15 #include <utility>
16 
17 #include <HAL/Notifier.h>
18 
19 #include "ErrorBase.h"
20 
21 namespace frc {
22 
23 typedef std::function<void()> TimerEventHandler;
24 
25 class Notifier : public ErrorBase {
26  public:
27  explicit Notifier(TimerEventHandler handler);
28 
29  template <typename Callable, typename Arg, typename... Args>
30  Notifier(Callable&& f, Arg&& arg, Args&&... args)
31  : Notifier(std::bind(std::forward<Callable>(f), std::forward<Arg>(arg),
32  std::forward<Args>(args)...)) {}
33 
34  virtual ~Notifier();
35 
36  Notifier(const Notifier&) = delete;
37  Notifier& operator=(const Notifier&) = delete;
38 
39  void StartSingle(double delay);
40  void StartPeriodic(double period);
41  void Stop();
42 
43  private:
44  // update the HAL alarm
45  void UpdateAlarm();
46  // HAL callback
47  static void Notify(uint64_t currentTimeInt, HAL_NotifierHandle handle);
48 
49  // used to constrain execution between destructors and callback
50  static std::mutex m_destructorMutex;
51  // held while updating process information
52  std::mutex m_processMutex;
53  // HAL handle, atomic for proper destruction
54  std::atomic<HAL_NotifierHandle> m_notifier{0};
55  // address of the handler
56  TimerEventHandler m_handler;
57  // the absolute expiration time
58  double m_expirationTime = 0;
59  // the relative time (either periodic or single)
60  double m_period = 0;
61  // true if this is a periodic event
62  bool m_periodic = false;
63 };
64 
65 } // namespace frc
Definition: Timer.cpp:18
void Stop()
Stop timer events from occuring.
Definition: Notifier.cpp:133
Definition: Notifier.h:25
void StartPeriodic(double period)
Register for periodic event notification.
Definition: Notifier.cpp:116
void StartSingle(double delay)
Register for single event notification.
Definition: Notifier.cpp:98
Base class for most objects.
Definition: ErrorBase.h:74
Notifier(TimerEventHandler handler)
Create a Notifier for timer event notification.
Definition: Notifier.cpp:26
virtual ~Notifier()
Free the resources for a timer event.
Definition: Notifier.cpp:38