WPILibC++ 2024.3.2
AsynchronousInterrupt.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
8
9#include <atomic>
10#include <functional>
11#include <memory>
12#include <thread>
13#include <utility>
14
15#include <units/time.h>
16
17namespace frc {
18/**
19 * Class for handling asynchronous interrupts using a callback thread.
20 *
21 * <p> By default, interrupts will occur on rising edge. Callbacks are disabled
22 * by default, and Enable() must be called before they will occur.
23 *
24 * <p> Both rising and falling edges can be indicated in one callback if both a
25 * rising and falling edge occurred since the previous callback.
26 *
27 * <p>Synchronous (blocking) interrupts are handled by the SynchronousInterrupt
28 * class.
29 */
31 public:
32 /**
33 * Construct an Asynchronous Interrupt from a Digital Source.
34 *
35 * <p> At construction, the interrupt will trigger on the rising edge.
36 *
37 * <p> The first bool in the callback indicates the rising edge triggered the
38 * interrupt, the second bool is falling edge.
39 *
40 * @param source the DigitalSource the interrupts are triggered from
41 * @param callback the callback function to call when the interrupt is
42 * triggered
43 */
45 std::function<void(bool, bool)> callback);
46
47 /**
48 * Construct an Asynchronous Interrupt from a Digital Source.
49 *
50 * <p> At construction, the interrupt will trigger on the rising edge.
51 *
52 * <p> The first bool in the callback indicates the rising edge triggered the
53 * interrupt, the second bool is falling edge.
54 *
55 * @param source the DigitalSource the interrupts are triggered from
56 * @param callback the callback function to call when the interrupt is
57 * triggered
58 */
60 std::function<void(bool, bool)> callback);
61
62 /**
63 * Construct an Asynchronous Interrupt from a Digital Source.
64 *
65 * <p> At construction, the interrupt will trigger on the rising edge.
66 *
67 * <p> The first bool in the callback indicates the rising edge triggered the
68 * interrupt, the second bool is falling edge.
69 *
70 * @param source the DigitalSource the interrupts are triggered from
71 * @param callback the callback function to call when the interrupt is
72 * triggered
73 */
74 AsynchronousInterrupt(std::shared_ptr<DigitalSource> source,
75 std::function<void(bool, bool)> callback);
76
77 /**
78 * Construct an Asynchronous Interrupt from a Digital Source.
79 *
80 * <p> At construction, the interrupt will trigger on the rising edge.
81 *
82 * @param source the DigitalSource the interrupts are triggered from
83 * @param f the callback function to call when the interrupt is triggered
84 * @param arg the first argument, interrupt was triggered on rising edge
85 * @param args the remaining arguments, interrupt was triggered on falling
86 * edge
87 */
88 template <typename Callable, typename Arg, typename... Args>
90 Args&&... args)
92 source, std::bind(std::forward<Callable>(f), std::forward<Arg>(arg),
93 std::forward<Args>(args)...)) {}
94
95 /**
96 * Construct an Asynchronous Interrupt from a Digital Source.
97 *
98 * <p> At construction, the interrupt will trigger on the rising edge.
99 *
100 * @param source the DigitalSource the interrupts are triggered from
101 * @param f the callback function to call when the interrupt is triggered
102 * @param arg the first argument, interrupt was triggered on rising edge
103 * @param args the remaining arguments, interrupt was triggered on falling
104 * edge
105 */
106 template <typename Callable, typename Arg, typename... Args>
108 Args&&... args)
110 source, std::bind(std::forward<Callable>(f), std::forward<Arg>(arg),
111 std::forward<Args>(args)...)) {}
112
113 /**
114 * Construct an Asynchronous Interrupt from a Digital Source.
115 *
116 * <p> At construction, the interrupt will trigger on the rising edge.
117 *
118 * @param source the DigitalSource the interrupts are triggered from
119 * @param f the callback function to call when the interrupt is triggered
120 * @param arg the first argument, interrupt was triggered on rising edge
121 * @param args the remaining arguments, interrupt was triggered on falling
122 * edge
123 */
124 template <typename Callable, typename Arg, typename... Args>
125 AsynchronousInterrupt(std::shared_ptr<DigitalSource> source, Callable&& f,
126 Arg&& arg, Args&&... args)
128 source, std::bind(std::forward<Callable>(f), std::forward<Arg>(arg),
129 std::forward<Args>(args)...)) {}
130
132
133 /**
134 * Enables interrupt callbacks. Before this, callbacks will not occur. Does
135 * nothing if already enabled.
136 */
137 void Enable();
138
139 /**
140 * Disables interrupt callbacks. Does nothing if already disabled.
141 */
142 void Disable();
143
144 /**
145 * Set which edges to trigger the interrupt on.
146 *
147 * @param risingEdge %Trigger on rising edge
148 * @param fallingEdge %Trigger on falling edge
149 */
150 void SetInterruptEdges(bool risingEdge, bool fallingEdge);
151
152 /**
153 * Get the timestamp of the last rising edge.
154 *
155 * <p>This function does not require the interrupt to be enabled to work.
156 *
157 * <p>This only works if rising edge was configured using SetInterruptEdges.
158 * @return the timestamp in seconds relative to GetFPGATime
159 */
160 units::second_t GetRisingTimestamp();
161
162 /**
163 * Get the timestamp of the last falling edge.
164 *
165 * <p>This function does not require the interrupt to be enabled to work.
166 *
167 * <p>This only works if falling edge was configured using SetInterruptEdges.
168 * @return the timestamp in seconds relative to GetFPGATime
169 */
170 units::second_t GetFallingTimestamp();
171
172 private:
173 void ThreadMain();
174
175 std::atomic_bool m_keepRunning{false};
176 std::thread m_thread;
177 SynchronousInterrupt m_interrupt;
178 std::function<void(bool, bool)> m_callback;
179};
180} // namespace frc
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation source
Definition: ThirdPartyNotices.txt:111
Class for handling asynchronous interrupts using a callback thread.
Definition: AsynchronousInterrupt.h:30
AsynchronousInterrupt(DigitalSource &source, std::function< void(bool, bool)> callback)
Construct an Asynchronous Interrupt from a Digital Source.
AsynchronousInterrupt(DigitalSource *source, std::function< void(bool, bool)> callback)
Construct an Asynchronous Interrupt from a Digital Source.
AsynchronousInterrupt(std::shared_ptr< DigitalSource > source, std::function< void(bool, bool)> callback)
Construct an Asynchronous Interrupt from a Digital Source.
void Disable()
Disables interrupt callbacks.
AsynchronousInterrupt(DigitalSource *source, Callable &&f, Arg &&arg, Args &&... args)
Construct an Asynchronous Interrupt from a Digital Source.
Definition: AsynchronousInterrupt.h:107
AsynchronousInterrupt(DigitalSource &source, Callable &&f, Arg &&arg, Args &&... args)
Construct an Asynchronous Interrupt from a Digital Source.
Definition: AsynchronousInterrupt.h:89
units::second_t GetRisingTimestamp()
Get the timestamp of the last rising edge.
void Enable()
Enables interrupt callbacks.
void SetInterruptEdges(bool risingEdge, bool fallingEdge)
Set which edges to trigger the interrupt on.
AsynchronousInterrupt(std::shared_ptr< DigitalSource > source, Callable &&f, Arg &&arg, Args &&... args)
Construct an Asynchronous Interrupt from a Digital Source.
Definition: AsynchronousInterrupt.h:125
units::second_t GetFallingTimestamp()
Get the timestamp of the last falling edge.
DigitalSource Interface.
Definition: DigitalSource.h:22
Class for handling synchronous (blocking) interrupts.
Definition: SynchronousInterrupt.h:22
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
\rst Returns a named argument to be used in a formatting function.
Definition: core.h:1841
Definition: AprilTagPoseEstimator.h:15
Definition: array.h:89