WPILibC++  unspecified
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 <support/mutex.h>
19 
20 #include "ErrorBase.h"
21 
22 namespace frc {
23 
24 typedef std::function<void()> TimerEventHandler;
25 
26 class Notifier : public ErrorBase {
27  public:
28  explicit Notifier(TimerEventHandler handler);
29 
30  template <typename Callable, typename Arg, typename... Args>
31  Notifier(Callable&& f, Arg&& arg, Args&&... args)
32  : Notifier(std::bind(std::forward<Callable>(f), std::forward<Arg>(arg),
33  std::forward<Args>(args)...)) {}
34 
35  virtual ~Notifier();
36 
37  Notifier(const Notifier&) = delete;
38  Notifier& operator=(const Notifier&) = delete;
39 
40  void SetHandler(TimerEventHandler handler);
41  void StartSingle(double delay);
42  void StartPeriodic(double period);
43  void Stop();
44 
45  private:
46  // update the HAL alarm
47  void UpdateAlarm();
48  // the thread waiting on the HAL alarm
49  std::thread m_thread;
50  // held while updating process information
51  wpi::mutex m_processMutex;
52  // HAL handle, atomic for proper destruction
53  std::atomic<HAL_NotifierHandle> m_notifier{0};
54 
55  // Address of the handler
56  TimerEventHandler m_handler;
57 
58  // The absolute expiration time
59  double m_expirationTime = 0;
60 
61  // The relative time (either periodic or single)
62  double m_period = 0;
63 
64  // True if this is a periodic event
65  bool m_periodic = false;
66 };
67 
68 } // namespace frc
Definition: RobotController.cpp:14
void Stop()
Stop timer events from occuring.
Definition: Notifier.cpp:137
Definition: Notifier.h:26
void StartPeriodic(double period)
Register for periodic event notification.
Definition: Notifier.cpp:120
void StartSingle(double delay)
Register for single event notification.
Definition: Notifier.cpp:102
Base class for most objects.
Definition: ErrorBase.h:74
Notifier(TimerEventHandler handler)
Create a Notifier for timer event notification.
Definition: Notifier.cpp:24
void SetHandler(TimerEventHandler handler)
Change the handler function.
Definition: Notifier.cpp:90
virtual ~Notifier()
Free the resources for a timer event.
Definition: Notifier.cpp:59