Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 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 | #include "frc/AsynchronousInterrupt.h" |
| 6 | |
| 7 | #include <frc/DigitalSource.h> |
| 8 | |
| 9 | using namespace frc; |
| 10 | |
| 11 | AsynchronousInterrupt::AsynchronousInterrupt( |
| 12 | DigitalSource& source, std::function<void(bool, bool)> callback) |
| 13 | : m_interrupt{source}, m_callback{std::move(callback)} {} |
| 14 | AsynchronousInterrupt::AsynchronousInterrupt( |
| 15 | DigitalSource* source, std::function<void(bool, bool)> callback) |
| 16 | : m_interrupt{source}, m_callback{std::move(callback)} {} |
| 17 | AsynchronousInterrupt::AsynchronousInterrupt( |
| 18 | std::shared_ptr<DigitalSource> source, |
| 19 | std::function<void(bool, bool)> callback) |
| 20 | : m_interrupt{source}, m_callback{std::move(callback)} {} |
| 21 | |
| 22 | AsynchronousInterrupt::~AsynchronousInterrupt() { |
| 23 | Disable(); |
| 24 | } |
| 25 | |
| 26 | void AsynchronousInterrupt::ThreadMain() { |
| 27 | while (m_keepRunning) { |
| 28 | auto result = m_interrupt.WaitForInterrupt(10_s, false); |
| 29 | if (!m_keepRunning) { |
| 30 | break; |
| 31 | } |
| 32 | if (result == SynchronousInterrupt::WaitResult::kTimeout) { |
| 33 | continue; |
| 34 | } |
| 35 | m_callback((result & SynchronousInterrupt::WaitResult::kRisingEdge) != 0, |
| 36 | (result & SynchronousInterrupt::WaitResult::kFallingEdge) != 0); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void AsynchronousInterrupt::Enable() { |
| 41 | if (m_keepRunning) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | m_keepRunning = true; |
| 46 | m_thread = std::thread([this] { this->ThreadMain(); }); |
| 47 | } |
| 48 | |
| 49 | void AsynchronousInterrupt::Disable() { |
| 50 | m_keepRunning = false; |
| 51 | m_interrupt.WakeupWaitingInterrupt(); |
| 52 | if (m_thread.joinable()) { |
| 53 | m_thread.join(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | void AsynchronousInterrupt::SetInterruptEdges(bool risingEdge, |
| 58 | bool fallingEdge) { |
| 59 | m_interrupt.SetInterruptEdges(risingEdge, fallingEdge); |
| 60 | } |
| 61 | |
| 62 | units::second_t AsynchronousInterrupt::GetRisingTimestamp() { |
| 63 | return m_interrupt.GetRisingTimestamp(); |
| 64 | } |
| 65 | units::second_t AsynchronousInterrupt::GetFallingTimestamp() { |
| 66 | return m_interrupt.GetFallingTimestamp(); |
| 67 | } |