WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
Notifier.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008-2016. 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 <functional>
11 
12 #include "ErrorBase.h"
13 #include "HAL/cpp/priority_mutex.h"
14 
15 typedef std::function<void()> TimerEventHandler;
16 
17 class Notifier : public ErrorBase {
18  public:
19  explicit Notifier(TimerEventHandler handler);
20 
21  template <typename Callable, typename Arg, typename... Args>
22  Notifier(Callable&& f, Arg&& arg, Args&&... args)
23  : Notifier(std::bind(std::forward<Callable>(f),
24  std::forward<Arg>(arg),
25  std::forward<Args>(args)...)) {}
26 
27  virtual ~Notifier();
28 
29  Notifier(const Notifier&) = delete;
30  Notifier& operator=(const Notifier&) = delete;
31 
32  void StartSingle(double delay);
33  void StartPeriodic(double period);
34  void Stop();
35 
36  private:
37  // update the HAL alarm
38  void UpdateAlarm();
39  // HAL callback
40  static void Notify(uint64_t currentTimeInt, void *param);
41 
42  // held while updating process information
43  priority_mutex m_processMutex;
44  // HAL handle
45  void *m_notifier;
46  // address of the handler
47  TimerEventHandler m_handler;
48  // the absolute expiration time
49  double m_expirationTime = 0;
50  // the relative time (either periodic or single)
51  double m_period = 0;
52  // true if this is a periodic event
53  bool m_periodic = false;
54 
55  // held by interrupt manager task while handler call is in progress
56  priority_mutex m_handlerMutex;
57 };
void StartPeriodic(double period)
Register for periodic event notification.
Definition: Notifier.cpp:94
virtual ~Notifier()
Free the resources for a timer event.
Definition: Notifier.cpp:31
Definition: Notifier.h:17
Base class for most objects.
Definition: ErrorBase.h:66
void StartSingle(double delay)
Register for single event notification.
Definition: Notifier.cpp:78
void Stop()
Stop timer events from occuring.
Definition: Notifier.cpp:109
Definition: priority_mutex.h:53
Notifier(TimerEventHandler handler)
Create a Notifier for timer event notification.
Definition: Notifier.cpp:19