blob: 59ddc9ee376d2aa7111a96bfc7d47d083d6cb5f3 [file] [log] [blame]
Parker Schuhd3b7a8872018-02-19 16:42:27 -08001/*----------------------------------------------------------------------------*/
2/* Copyright (c) FIRST 2008-2017. 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 <memory>
11
Parker Schuhd3b7a8872018-02-19 16:42:27 -080012#include "frc971/wpilib/ahal/AnalogTriggerType.h"
13#include "frc971/wpilib/ahal/SensorBase.h"
Austin Schuhf6b94632019-02-02 22:11:27 -080014#include "hal/Interrupts.h"
15#include "hal/cpp/fpga_clock.h"
Parker Schuhd3b7a8872018-02-19 16:42:27 -080016
17namespace frc {
18
19class InterruptableSensorBase {
20 public:
21 enum WaitResult {
22 kTimeout = 0x0,
23 kRisingEdge = 0x1,
24 kFallingEdge = 0x100,
25 kBoth = 0x101,
26 };
27
28 InterruptableSensorBase();
29 virtual ~InterruptableSensorBase() = default;
Austin Schuhf6b94632019-02-02 22:11:27 -080030
Parker Schuhd3b7a8872018-02-19 16:42:27 -080031 virtual HAL_Handle GetPortHandleForRouting() const = 0;
32 virtual AnalogTriggerType GetAnalogTriggerTypeForRouting() const = 0;
Austin Schuhf6b94632019-02-02 22:11:27 -080033
34 // Requests interrupts in synchronous mode. This means you should call
35 // WaitForInterrupt to receive interrupts.
36 virtual void RequestInterrupts();
37
38 // Prevents any more interrupts from occuring.
39 virtual void CancelInterrupts();
40
41 // Waits for an interrupt or timeout to occur.
42 //
43 // Must be synchronized with all other operations on the input.
44 //
45 // timeout is in seconds.
46 virtual WaitResult WaitForInterrupt(double timeout,
47 bool ignorePrevious = true);
48
49 // Enables interrupts to occur based on the current configuration.
50 virtual void EnableInterrupts();
51
52 // Returns the timestamp for the most recent rising interrupt.
53 virtual hal::fpga_clock::time_point ReadRisingTimestamp();
54 // Returns the timestamp for the most recent falling interrupt.
55 virtual hal::fpga_clock::time_point ReadFallingTimestamp();
56
57 // Configures which edges to interrupt on.
58 //
59 // The default is on rising edges only.
Parker Schuhd3b7a8872018-02-19 16:42:27 -080060 virtual void SetUpSourceEdge(bool risingEdge, bool fallingEdge);
61
62 protected:
63 HAL_InterruptHandle m_interrupt = HAL_kInvalidHandle;
64 void AllocateInterrupts(bool watcher);
65};
66
67} // namespace frc