WPILibC++  2018.4.1-20180930034747-1211-g175c6c1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Notifier.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2018 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 <thread>
15 #include <utility>
16 
17 #include <hal/Notifier.h>
18 #include <wpi/mutex.h>
19 
20 #include "frc/ErrorBase.h"
21 
22 namespace frc {
23 
24 using TimerEventHandler = std::function<void()>;
25 
26 class Notifier : public ErrorBase {
27  public:
34  explicit Notifier(TimerEventHandler handler);
35 
36  template <typename Callable, typename Arg, typename... Args>
37  Notifier(Callable&& f, Arg&& arg, Args&&... args)
38  : Notifier(std::bind(std::forward<Callable>(f), std::forward<Arg>(arg),
39  std::forward<Args>(args)...)) {}
40 
44  virtual ~Notifier();
45 
46  Notifier(Notifier&& rhs);
47  Notifier& operator=(Notifier&& rhs);
48 
54  void SetHandler(TimerEventHandler handler);
55 
63  void StartSingle(double delay);
64 
75  void StartPeriodic(double period);
76 
86  void Stop();
87 
88  private:
94  void UpdateAlarm(uint64_t triggerTime);
95 
99  void UpdateAlarm();
100 
101  // The thread waiting on the HAL alarm
102  std::thread m_thread;
103 
104  // Held while updating process information
105  wpi::mutex m_processMutex;
106 
107  // HAL handle, atomic for proper destruction
108  std::atomic<HAL_NotifierHandle> m_notifier{0};
109 
110  // Address of the handler
111  TimerEventHandler m_handler;
112 
113  // The absolute expiration time
114  double m_expirationTime = 0;
115 
116  // The relative time (either periodic or single)
117  double m_period = 0;
118 
119  // True if this is a periodic event
120  bool m_periodic = false;
121 };
122 
123 } // namespace frc
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
virtual ~Notifier()
Free the resources for a timer event.
Definition: Notifier.h:26
void StartPeriodic(double period)
Register for periodic event notification.
void SetHandler(TimerEventHandler handler)
Change the handler function.
void Stop()
Stop timer events from occuring.
void StartSingle(double delay)
Register for single event notification.
Base class for most objects.
Definition: ErrorBase.h:74
Notifier(TimerEventHandler handler)
Create a Notifier for timer event notification.