WPILibC++  2020.3.2
Notifier.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2019 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/Types.h>
18 #include <units/units.h>
19 #include <wpi/Twine.h>
20 #include <wpi/deprecated.h>
21 #include <wpi/mutex.h>
22 
23 #include "frc/ErrorBase.h"
24 
25 namespace frc {
26 
27 class Notifier : public ErrorBase {
28  public:
35  explicit Notifier(std::function<void()> handler);
36 
37  template <typename Callable, typename Arg, typename... Args>
38  Notifier(Callable&& f, Arg&& arg, Args&&... args)
39  : Notifier(std::bind(std::forward<Callable>(f), std::forward<Arg>(arg),
40  std::forward<Args>(args)...)) {}
41 
45  virtual ~Notifier();
46 
47  Notifier(Notifier&& rhs);
48  Notifier& operator=(Notifier&& rhs);
49 
55  void SetName(const wpi::Twine& name);
56 
62  void SetHandler(std::function<void()> handler);
63 
71  WPI_DEPRECATED("Use unit-safe StartSingle method instead.")
72  void StartSingle(double delay);
73 
81  void StartSingle(units::second_t delay);
82 
93  WPI_DEPRECATED("Use unit-safe StartPeriodic method instead.")
94  void StartPeriodic(double period);
95 
106  void StartPeriodic(units::second_t period);
107 
117  void Stop();
118 
119  private:
125  void UpdateAlarm(uint64_t triggerTime);
126 
130  void UpdateAlarm();
131 
132  // The thread waiting on the HAL alarm
133  std::thread m_thread;
134 
135  // Held while updating process information
136  wpi::mutex m_processMutex;
137 
138  // HAL handle, atomic for proper destruction
139  std::atomic<HAL_NotifierHandle> m_notifier{0};
140 
141  // Address of the handler
142  std::function<void()> m_handler;
143 
144  // The absolute expiration time
145  double m_expirationTime = 0;
146 
147  // The relative time (either periodic or single)
148  double m_period = 0;
149 
150  // True if this is a periodic event
151  bool m_periodic = false;
152 };
153 
154 } // namespace frc
frc::Notifier::StartPeriodic
void StartPeriodic(double period)
Register for periodic event notification.
frc::Notifier::SetHandler
void SetHandler(std::function< void()> handler)
Change the handler function.
frc::Notifier::Stop
void Stop()
Stop timer events from occuring.
frc::Notifier::~Notifier
virtual ~Notifier()
Free the resources for a timer event.
frc::Notifier::Notifier
Notifier(std::function< void()> handler)
Create a Notifier for timer event notification.
frc::Notifier::StartSingle
void StartSingle(double delay)
Register for single event notification.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
frc::Notifier::SetName
void SetName(const wpi::Twine &name)
Sets the name of the notifier.
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
wpi::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85
frc::Notifier
Definition: Notifier.h:27