Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
| 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 Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 12 | #include "frc971/wpilib/ahal/AnalogTriggerType.h" |
| 13 | #include "frc971/wpilib/ahal/SensorBase.h" |
Austin Schuh | f6b9463 | 2019-02-02 22:11:27 -0800 | [diff] [blame] | 14 | #include "hal/Interrupts.h" |
| 15 | #include "hal/cpp/fpga_clock.h" |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 16 | |
| 17 | namespace frc { |
| 18 | |
| 19 | class 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 Schuh | f6b9463 | 2019-02-02 22:11:27 -0800 | [diff] [blame] | 30 | |
Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 31 | virtual HAL_Handle GetPortHandleForRouting() const = 0; |
| 32 | virtual AnalogTriggerType GetAnalogTriggerTypeForRouting() const = 0; |
Austin Schuh | f6b9463 | 2019-02-02 22:11:27 -0800 | [diff] [blame] | 33 | |
| 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 Schuh | d3b7a887 | 2018-02-19 16:42:27 -0800 | [diff] [blame] | 60 | 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 |