WPILibC++  2020.2.2-33-g65eab93
InterruptableSensorBase.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 <functional>
11 #include <memory>
12 
13 #include <hal/Interrupts.h>
14 
15 #include "frc/AnalogTriggerType.h"
16 #include "frc/ErrorBase.h"
17 
18 namespace frc {
19 
21  public:
22  enum WaitResult {
23  kTimeout = 0x0,
24  kRisingEdge = 0x1,
25  kFallingEdge = 0x100,
26  kBoth = 0x101,
27  };
28 
34  using InterruptEventHandler = std::function<void(WaitResult)>;
35 
36  InterruptableSensorBase() = default;
37 
41  virtual ~InterruptableSensorBase();
42 
44  InterruptableSensorBase& operator=(InterruptableSensorBase&&) = default;
45 
46  virtual HAL_Handle GetPortHandleForRouting() const = 0;
47  virtual AnalogTriggerType GetAnalogTriggerTypeForRouting() const = 0;
48 
57  virtual void RequestInterrupts(HAL_InterruptHandlerFunction handler,
58  void* param);
59 
68  virtual void RequestInterrupts(InterruptEventHandler handler);
69 
77  virtual void RequestInterrupts();
78 
84  virtual void CancelInterrupts();
85 
98  virtual WaitResult WaitForInterrupt(double timeout,
99  bool ignorePrevious = true);
100 
108  virtual void EnableInterrupts();
109 
113  virtual void DisableInterrupts();
114 
124  virtual double ReadRisingTimestamp();
125 
135  virtual double ReadFallingTimestamp();
136 
143  virtual void SetUpSourceEdge(bool risingEdge, bool fallingEdge);
144 
145  protected:
146  hal::Handle<HAL_InterruptHandle> m_interrupt;
147  std::unique_ptr<InterruptEventHandler> m_interruptHandler{nullptr};
148 
149  void AllocateInterrupts(bool watcher);
150 };
151 
152 } // namespace frc
frc::InterruptableSensorBase::InterruptEventHandler
std::function< void(WaitResult)> InterruptEventHandler
Handler for interrupts.
Definition: InterruptableSensorBase.h:34
frc::InterruptableSensorBase::WaitForInterrupt
virtual WaitResult WaitForInterrupt(double timeout, bool ignorePrevious=true)
In synchronous mode, wait for the defined interrupt to occur.
frc::InterruptableSensorBase
Definition: InterruptableSensorBase.h:20
frc::InterruptableSensorBase::DisableInterrupts
virtual void DisableInterrupts()
Disable Interrupts without without deallocating structures.
frc::InterruptableSensorBase::ReadFallingTimestamp
virtual double ReadFallingTimestamp()
Return the timestamp for the falling interrupt that occurred most recently.
frc::InterruptableSensorBase::RequestInterrupts
virtual void RequestInterrupts()
Request one of the 8 interrupts synchronously on this digital input.
frc::InterruptableSensorBase::~InterruptableSensorBase
virtual ~InterruptableSensorBase()
Free the resources for an interrupt event.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
frc::InterruptableSensorBase::ReadRisingTimestamp
virtual double ReadRisingTimestamp()
Return the timestamp for the rising interrupt that occurred most recently.
frc::InterruptableSensorBase::EnableInterrupts
virtual void EnableInterrupts()
Enable interrupts to occur on this input.
frc::InterruptableSensorBase::CancelInterrupts
virtual void CancelInterrupts()
Cancel interrupts on this device.
frc
WPILib FRC namespace.
Definition: AnalogOutSim.h:16
frc::InterruptableSensorBase::SetUpSourceEdge
virtual void SetUpSourceEdge(bool risingEdge, bool fallingEdge)
Set which edge to trigger interrupts on.