WPILibC++  2018.4.1-20180729040223-1137-g011f0ff
 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 typedef std::function<void()> TimerEventHandler;
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(const Notifier&) = delete;
47  Notifier& operator=(const Notifier&) = delete;
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:
92  void UpdateAlarm();
93 
94  // The thread waiting on the HAL alarm
95  std::thread m_thread;
96 
97  // Held while updating process information
98  wpi::mutex m_processMutex;
99 
100  // HAL handle, atomic for proper destruction
101  std::atomic<HAL_NotifierHandle> m_notifier{0};
102 
103  // Address of the handler
104  TimerEventHandler m_handler;
105 
106  // The absolute expiration time
107  double m_expirationTime = 0;
108 
109  // The relative time (either periodic or single)
110  double m_period = 0;
111 
112  // True if this is a periodic event
113  bool m_periodic = false;
114 };
115 
116 } // namespace frc
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.