blob: c52463b2c3d47879032ab87331770656d8ac2773 [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
Austin Schuhf6b94632019-02-02 22:11:27 -080049 // Returns the timestamp for the most recent rising interrupt.
50 virtual hal::fpga_clock::time_point ReadRisingTimestamp();
51 // Returns the timestamp for the most recent falling interrupt.
52 virtual hal::fpga_clock::time_point ReadFallingTimestamp();
53
54 // Configures which edges to interrupt on.
55 //
56 // The default is on rising edges only.
Parker Schuhd3b7a8872018-02-19 16:42:27 -080057 virtual void SetUpSourceEdge(bool risingEdge, bool fallingEdge);
58
59 protected:
60 HAL_InterruptHandle m_interrupt = HAL_kInvalidHandle;
Austin Schuh9950f682021-11-06 15:27:58 -070061 void AllocateInterrupts();
Parker Schuhd3b7a8872018-02-19 16:42:27 -080062};
63
64} // namespace frc