From e2b6beb28abef4f9ee2ee2b8b6640d8f28fb70bb Mon Sep 17 00:00:00 2001 From: Thad House Date: Thu, 23 Jan 2025 21:44:18 -0800 Subject: [PATCH] [hal, wpilib] Remove DigitalGlitchFilter (#7725) --- .../wpi/first/hal/DigitalGlitchFilterJNI.java | 65 ------ .../native/cpp/jni/DigitalGlitchFilterJNI.cpp | 79 ------- hal/src/main/native/include/hal/DIO.h | 54 ----- hal/src/main/native/sim/DIO.cpp | 28 --- hal/src/main/native/systemcore/DIO.cpp | 21 -- .../main/native/cpp/DigitalGlitchFilter.cpp | 138 ------------- .../native/include/frc/DigitalGlitchFilter.h | 138 ------------- .../main/native/include/frc/DigitalInput.h | 4 - wpilibc/src/main/native/include/frc/Encoder.h | 3 - .../first/wpilibj/DigitalGlitchFilter.java | 192 ------------------ 10 files changed, 722 deletions(-) delete mode 100644 hal/src/main/java/edu/wpi/first/hal/DigitalGlitchFilterJNI.java delete mode 100644 hal/src/main/native/cpp/jni/DigitalGlitchFilterJNI.cpp delete mode 100644 wpilibc/src/main/native/cpp/DigitalGlitchFilter.cpp delete mode 100644 wpilibc/src/main/native/include/frc/DigitalGlitchFilter.h delete mode 100644 wpilibj/src/main/java/edu/wpi/first/wpilibj/DigitalGlitchFilter.java diff --git a/hal/src/main/java/edu/wpi/first/hal/DigitalGlitchFilterJNI.java b/hal/src/main/java/edu/wpi/first/hal/DigitalGlitchFilterJNI.java deleted file mode 100644 index ececa130c5..0000000000 --- a/hal/src/main/java/edu/wpi/first/hal/DigitalGlitchFilterJNI.java +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) FIRST and other WPILib contributors. -// Open Source Software; you can modify and/or share it under the terms of -// the WPILib BSD license file in the root directory of this project. - -package edu.wpi.first.hal; - -/** - * Digital Glitch Filter JNI functions. - * - * @see "hal/DIO.h" - */ -public class DigitalGlitchFilterJNI extends JNIWrapper { - /** - * Writes the filter index from the FPGA. - * - *

Set the filter index used to filter out short pulses. - * - * @param digitalPortHandle the digital port handle - * @param filterIndex the filter index (Must be in the range 0 - 3, where 0 means "none" and 1 - 3 - * means filter # filterIndex - 1) - * @see "HAL_SetFilterSelect" - */ - public static native void setFilterSelect(int digitalPortHandle, int filterIndex); - - /** - * Reads the filter index from the FPGA. - * - *

Gets the filter index used to filter out short pulses. - * - * @param digitalPortHandle the digital port handle - * @return the filter index (Must be in the range 0 - 3, where 0 means "none" and 1 - 3 means - * filter # filterIndex - 1) - * @see "HAL_GetFilterSelect" - */ - public static native int getFilterSelect(int digitalPortHandle); - - /** - * Sets the filter period for the specified filter index. - * - *

Sets the filter period in FPGA cycles. Even though there are 2 different filter index - * domains (MXP vs HDR), ignore that distinction for now since it complicates the interface. That - * can be changed later. - * - * @param filterIndex the filter index, 0 - 2 - * @param fpgaCycles the number of cycles that the signal must not transition to be counted as a - * transition. - * @see "HAL_SetFilterPeriod" - */ - public static native void setFilterPeriod(int filterIndex, int fpgaCycles); - - /** - * Gets the filter period for the specified filter index. - * - *

Gets the filter period in FPGA cycles. Even though there are 2 different filter index - * domains (MXP vs HDR), ignore that distinction for now since it complicates the interface. - * - * @param filterIndex the filter index, 0 - 2 - * @return The number of FPGA cycles of the filter period. - * @see "HAL_GetFilterPeriod" - */ - public static native int getFilterPeriod(int filterIndex); - - /** Utility class. */ - private DigitalGlitchFilterJNI() {} -} diff --git a/hal/src/main/native/cpp/jni/DigitalGlitchFilterJNI.cpp b/hal/src/main/native/cpp/jni/DigitalGlitchFilterJNI.cpp deleted file mode 100644 index 762f7fbe35..0000000000 --- a/hal/src/main/native/cpp/jni/DigitalGlitchFilterJNI.cpp +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright (c) FIRST and other WPILib contributors. -// Open Source Software; you can modify and/or share it under the terms of -// the WPILib BSD license file in the root directory of this project. - -#include - -#include "HALUtil.h" -#include "edu_wpi_first_hal_DigitalGlitchFilterJNI.h" -#include "hal/DIO.h" - -using namespace hal; - -extern "C" { - -/* - * Class: edu_wpi_first_hal_DigitalGlitchFilterJNI - * Method: setFilterSelect - * Signature: (II)V - */ -JNIEXPORT void JNICALL -Java_edu_wpi_first_hal_DigitalGlitchFilterJNI_setFilterSelect - (JNIEnv* env, jclass, jint id, jint filter_index) -{ - int32_t status = 0; - - HAL_SetFilterSelect(static_cast(id), filter_index, - &status); - CheckStatus(env, status); -} - -/* - * Class: edu_wpi_first_hal_DigitalGlitchFilterJNI - * Method: getFilterSelect - * Signature: (I)I - */ -JNIEXPORT jint JNICALL -Java_edu_wpi_first_hal_DigitalGlitchFilterJNI_getFilterSelect - (JNIEnv* env, jclass, jint id) -{ - int32_t status = 0; - - jint result = - HAL_GetFilterSelect(static_cast(id), &status); - CheckStatus(env, status); - return result; -} - -/* - * Class: edu_wpi_first_hal_DigitalGlitchFilterJNI - * Method: setFilterPeriod - * Signature: (II)V - */ -JNIEXPORT void JNICALL -Java_edu_wpi_first_hal_DigitalGlitchFilterJNI_setFilterPeriod - (JNIEnv* env, jclass, jint filter_index, jint fpga_cycles) -{ - int32_t status = 0; - - HAL_SetFilterPeriod(filter_index, fpga_cycles, &status); - CheckStatus(env, status); -} - -/* - * Class: edu_wpi_first_hal_DigitalGlitchFilterJNI - * Method: getFilterPeriod - * Signature: (I)I - */ -JNIEXPORT jint JNICALL -Java_edu_wpi_first_hal_DigitalGlitchFilterJNI_getFilterPeriod - (JNIEnv* env, jclass, jint filter_index) -{ - int32_t status = 0; - - jint result = HAL_GetFilterPeriod(filter_index, &status); - CheckStatus(env, status); - return result; -} - -} // extern "C" diff --git a/hal/src/main/native/include/hal/DIO.h b/hal/src/main/native/include/hal/DIO.h index b81044cde8..f62e7451ad 100644 --- a/hal/src/main/native/include/hal/DIO.h +++ b/hal/src/main/native/include/hal/DIO.h @@ -194,60 +194,6 @@ HAL_Bool HAL_IsPulsing(HAL_DigitalHandle dioPortHandle, int32_t* status); * @return true if a pulse on some line is in progress */ HAL_Bool HAL_IsAnyPulsing(int32_t* status); - -/** - * Writes the filter index from the FPGA. - * - * Set the filter index used to filter out short pulses. - * - * @param[in] dioPortHandle the digital port handle - * @param[in] filterIndex the filter index (Must be in the range 0 - 3, where - * 0 means "none" and 1 - 3 means filter # filterIndex - * - 1) - * @param[out] status Error status variable. 0 on success. - */ -void HAL_SetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t filterIndex, - int32_t* status); - -/** - * Reads the filter index from the FPGA. - * - * Gets the filter index used to filter out short pulses. - * - * @param[in] dioPortHandle the digital port handle - * @param[out] status Error status variable. 0 on success. - * @return filterIndex the filter index (Must be in the range 0 - 3, where 0 - * means "none" and 1 - 3 means filter # filterIndex - 1) - */ -int32_t HAL_GetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t* status); - -/** - * Sets the filter period for the specified filter index. - * - * Sets the filter period in FPGA cycles. Even though there are 2 different - * filter index domains (MXP vs HDR), ignore that distinction for now since it - * complicates the interface. That can be changed later. - * - * @param[in] filterIndex the filter index, 0 - 2 - * @param[in] value the number of cycles that the signal must not - * transition to be counted as a transition. - * @param[out] status Error status variable. 0 on success. - */ -void HAL_SetFilterPeriod(int32_t filterIndex, int64_t value, int32_t* status); - -/** - * Gets the filter period for the specified filter index. - * - * Gets the filter period in FPGA cycles. Even though there are 2 different - * filter index domains (MXP vs HDR), ignore that distinction for now since it - * complicates the interface. Set status to NiFpga_Status_SoftwareFault if the - * filter values mismatch. - * - * @param[in] filterIndex the filter index, 0 - 2 - * @param[out] status Error status variable. 0 on success. - * @return The number of FPGA cycles of the filter period. - */ -int64_t HAL_GetFilterPeriod(int32_t filterIndex, int32_t* status); #ifdef __cplusplus } // extern "C" #endif diff --git a/hal/src/main/native/sim/DIO.cpp b/hal/src/main/native/sim/DIO.cpp index 1148b75d60..7f87f80fad 100644 --- a/hal/src/main/native/sim/DIO.cpp +++ b/hal/src/main/native/sim/DIO.cpp @@ -269,32 +269,4 @@ HAL_Bool HAL_IsPulsing(HAL_DigitalHandle dioPortHandle, int32_t* status) { HAL_Bool HAL_IsAnyPulsing(int32_t* status) { return false; // TODO(Thad) Figure this out } - -void HAL_SetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t filterIndex, - int32_t* status) { - auto port = digitalChannelHandles->Get(dioPortHandle, HAL_HandleEnum::DIO); - if (port == nullptr) { - *status = HAL_HANDLE_ERROR; - return; - } - // mimics athena HAL - port->filterIndex = filterIndex % 4; -} - -int32_t HAL_GetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t* status) { - auto port = digitalChannelHandles->Get(dioPortHandle, HAL_HandleEnum::DIO); - if (port == nullptr) { - *status = HAL_HANDLE_ERROR; - return 0; - } - return port->filterIndex; -} - -void HAL_SetFilterPeriod(int32_t filterIndex, int64_t value, int32_t* status) { - // TODO(Thad) figure this out -} - -int64_t HAL_GetFilterPeriod(int32_t filterIndex, int32_t* status) { - return 0; // TODO(Thad) figure this out -} } // extern "C" diff --git a/hal/src/main/native/systemcore/DIO.cpp b/hal/src/main/native/systemcore/DIO.cpp index 46eb679cbb..64ba27bd37 100644 --- a/hal/src/main/native/systemcore/DIO.cpp +++ b/hal/src/main/native/systemcore/DIO.cpp @@ -201,25 +201,4 @@ HAL_Bool HAL_IsAnyPulsing(int32_t* status) { return false; } -void HAL_SetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t filterIndex, - int32_t* status) { - *status = HAL_HANDLE_ERROR; - return; -} - -int32_t HAL_GetFilterSelect(HAL_DigitalHandle dioPortHandle, int32_t* status) { - *status = HAL_HANDLE_ERROR; - return 0; -} - -void HAL_SetFilterPeriod(int32_t filterIndex, int64_t value, int32_t* status) { - *status = HAL_HANDLE_ERROR; - return; -} - -int64_t HAL_GetFilterPeriod(int32_t filterIndex, int32_t* status) { - *status = HAL_HANDLE_ERROR; - return 0; -} - } // extern "C" diff --git a/wpilibc/src/main/native/cpp/DigitalGlitchFilter.cpp b/wpilibc/src/main/native/cpp/DigitalGlitchFilter.cpp deleted file mode 100644 index 44bcabe463..0000000000 --- a/wpilibc/src/main/native/cpp/DigitalGlitchFilter.cpp +++ /dev/null @@ -1,138 +0,0 @@ -// Copyright (c) FIRST and other WPILib contributors. -// Open Source Software; you can modify and/or share it under the terms of -// the WPILib BSD license file in the root directory of this project. - -#include "frc/DigitalGlitchFilter.h" - -#include -#include -#include - -#include -#include -#include -#include - -#include "frc/Counter.h" -#include "frc/Encoder.h" -#include "frc/Errors.h" -#include "frc/SensorUtil.h" - -using namespace frc; - -std::array DigitalGlitchFilter::m_filterAllocated = { - {false, false, false}}; -wpi::mutex DigitalGlitchFilter::m_mutex; - -DigitalGlitchFilter::DigitalGlitchFilter() { - m_channelIndex = AllocateFilterIndex(); - if (m_channelIndex < 0) { - throw FRC_MakeError(err::NoAvailableResources, - "No filters available to allocate."); - } - HAL_Report(HALUsageReporting::kResourceType_DigitalGlitchFilter, - m_channelIndex + 1); - wpi::SendableRegistry::AddLW(this, "DigitalGlitchFilter", m_channelIndex); -} - -DigitalGlitchFilter::~DigitalGlitchFilter() { - if (m_channelIndex >= 0) { - std::scoped_lock lock(m_mutex); - m_filterAllocated[m_channelIndex] = false; - } -} - -int DigitalGlitchFilter::AllocateFilterIndex() { - std::scoped_lock lock{m_mutex}; - auto filter = - std::find(m_filterAllocated.begin(), m_filterAllocated.end(), false); - - if (filter == m_filterAllocated.end()) { - return -1; - } - *filter = true; - return std::distance(m_filterAllocated.begin(), filter); -} - -void DigitalGlitchFilter::Add(DigitalSource* input) { - DoAdd(input, m_channelIndex + 1); -} - -void DigitalGlitchFilter::DoAdd(DigitalSource* input, int requestedIndex) { - // Some sources from Counters and Encoders are null. By pushing the check - // here, we catch the issue more generally. - if (input) { - // We don't support GlitchFilters on AnalogTriggers. - if (input->IsAnalogTrigger()) { - throw FRC_MakeError( - -1, "Analog Triggers not supported for DigitalGlitchFilters"); - } - int32_t status = 0; - HAL_SetFilterSelect(input->GetPortHandleForRouting(), requestedIndex, - &status); - FRC_CheckErrorStatus(status, "requested index {}", requestedIndex); - - // Validate that we set it correctly. - int actualIndex = - HAL_GetFilterSelect(input->GetPortHandleForRouting(), &status); - FRC_CheckErrorStatus(status, "requested index {}", requestedIndex); - FRC_AssertMessage(actualIndex == requestedIndex, - "HAL_SetFilterSelect({}) failed -> {}", requestedIndex, - actualIndex); - } -} - -void DigitalGlitchFilter::Add(Encoder* input) { - Add(input->m_aSource.get()); - Add(input->m_bSource.get()); -} - -void DigitalGlitchFilter::Add(Counter* input) { - Add(input->m_upSource.get()); - Add(input->m_downSource.get()); -} - -void DigitalGlitchFilter::Remove(DigitalSource* input) { - DoAdd(input, 0); -} - -void DigitalGlitchFilter::Remove(Encoder* input) { - Remove(input->m_aSource.get()); - Remove(input->m_bSource.get()); -} - -void DigitalGlitchFilter::Remove(Counter* input) { - Remove(input->m_upSource.get()); - Remove(input->m_downSource.get()); -} - -void DigitalGlitchFilter::SetPeriodCycles(int fpgaCycles) { - int32_t status = 0; - HAL_SetFilterPeriod(m_channelIndex, fpgaCycles, &status); - FRC_CheckErrorStatus(status, "Channel {}", m_channelIndex); -} - -void DigitalGlitchFilter::SetPeriodNanoSeconds(uint64_t nanoseconds) { - int32_t status = 0; - int fpgaCycles = - nanoseconds * HAL_GetSystemClockTicksPerMicrosecond() / 4 / 1000; - HAL_SetFilterPeriod(m_channelIndex, fpgaCycles, &status); - FRC_CheckErrorStatus(status, "Channel {}", m_channelIndex); -} - -int DigitalGlitchFilter::GetPeriodCycles() { - int32_t status = 0; - int fpgaCycles = HAL_GetFilterPeriod(m_channelIndex, &status); - FRC_CheckErrorStatus(status, "Channel {}", m_channelIndex); - return fpgaCycles; -} - -uint64_t DigitalGlitchFilter::GetPeriodNanoSeconds() { - int32_t status = 0; - int fpgaCycles = HAL_GetFilterPeriod(m_channelIndex, &status); - FRC_CheckErrorStatus(status, "Channel {}", m_channelIndex); - return static_cast(fpgaCycles) * 1000L / - static_cast(HAL_GetSystemClockTicksPerMicrosecond() / 4); -} - -void DigitalGlitchFilter::InitSendable(wpi::SendableBuilder&) {} diff --git a/wpilibc/src/main/native/include/frc/DigitalGlitchFilter.h b/wpilibc/src/main/native/include/frc/DigitalGlitchFilter.h deleted file mode 100644 index 05ddded313..0000000000 --- a/wpilibc/src/main/native/include/frc/DigitalGlitchFilter.h +++ /dev/null @@ -1,138 +0,0 @@ -// Copyright (c) FIRST and other WPILib contributors. -// Open Source Software; you can modify and/or share it under the terms of -// the WPILib BSD license file in the root directory of this project. - -#pragma once - -#include - -#include - -#include -#include -#include - -#include "frc/DigitalSource.h" - -namespace frc { - -class Encoder; -class Counter; - -/** - * Class to enable glitch filtering on a set of digital inputs. - * - * This class will manage adding and removing digital inputs from a FPGA glitch - * filter. The filter lets the user configure the time that an input must remain - * high or low before it is classified as high or low. - */ -class DigitalGlitchFilter : public wpi::Sendable, - public wpi::SendableHelper { - public: - DigitalGlitchFilter(); - ~DigitalGlitchFilter() override; - - DigitalGlitchFilter(DigitalGlitchFilter&&) = default; - DigitalGlitchFilter& operator=(DigitalGlitchFilter&&) = default; - - /** - * Assigns the DigitalSource to this glitch filter. - * - * @param input The DigitalSource to add. - */ - void Add(DigitalSource* input); - - /** - * Assigns the Encoder to this glitch filter. - * - * @param input The Encoder to add. - */ - void Add(Encoder* input); - - /** - * Assigns the Counter to this glitch filter. - * - * @param input The Counter to add. - */ - void Add(Counter* input); - - /** - * Removes a digital input from this filter. - * - * Removes the DigitalSource from this glitch filter and re-assigns it to - * the default filter. - * - * @param input The DigitalSource to remove. - */ - void Remove(DigitalSource* input); - - /** - * Removes an encoder from this filter. - * - * Removes the Encoder from this glitch filter and re-assigns it to - * the default filter. - * - * @param input The Encoder to remove. - */ - void Remove(Encoder* input); - - /** - * Removes a counter from this filter. - * - * Removes the Counter from this glitch filter and re-assigns it to - * the default filter. - * - * @param input The Counter to remove. - */ - void Remove(Counter* input); - - /** - * Sets the number of cycles that the input must not change state for. - * - * @param fpgaCycles The number of FPGA cycles. - */ - void SetPeriodCycles(int fpgaCycles); - - /** - * Sets the number of nanoseconds that the input must not change state for. - * - * @param nanoseconds The number of nanoseconds. - */ - void SetPeriodNanoSeconds(uint64_t nanoseconds); - - /** - * Gets the number of cycles that the input must not change state for. - * - * @return The number of cycles. - */ - int GetPeriodCycles(); - - /** - * Gets the number of nanoseconds that the input must not change state for. - * - * @return The number of nanoseconds. - */ - uint64_t GetPeriodNanoSeconds(); - - void InitSendable(wpi::SendableBuilder& builder) override; - - private: - int m_channelIndex; - - // Sets the filter for the input to be the requested index. A value of 0 - // disables the filter, and the filter value must be between 1 and 3, - // inclusive. - static void DoAdd(DigitalSource* input, int requested_index); - - /** - * Allocates the next available filter index, or -1 if there are no filters - * available. - * @return the filter index - */ - static int AllocateFilterIndex(); - - static wpi::mutex m_mutex; - static std::array m_filterAllocated; -}; - -} // namespace frc diff --git a/wpilibc/src/main/native/include/frc/DigitalInput.h b/wpilibc/src/main/native/include/frc/DigitalInput.h index e850c56ae2..b53453855d 100644 --- a/wpilibc/src/main/native/include/frc/DigitalInput.h +++ b/wpilibc/src/main/native/include/frc/DigitalInput.h @@ -12,8 +12,6 @@ namespace frc { -class DigitalGlitchFilter; - /** * Class to read a digital input. * @@ -81,8 +79,6 @@ class DigitalInput : public DigitalSource, private: int m_channel; hal::Handle m_handle; - - friend class DigitalGlitchFilter; }; } // namespace frc diff --git a/wpilibc/src/main/native/include/frc/Encoder.h b/wpilibc/src/main/native/include/frc/Encoder.h index 82d7d3409d..f05c33cc04 100644 --- a/wpilibc/src/main/native/include/frc/Encoder.h +++ b/wpilibc/src/main/native/include/frc/Encoder.h @@ -17,7 +17,6 @@ namespace frc { class DigitalSource; -class DigitalGlitchFilter; /** * Class to read quad encoders. @@ -375,8 +374,6 @@ class Encoder : public CounterBase, std::shared_ptr m_bSource; // The B phase of the quad encoder std::shared_ptr m_indexSource = nullptr; hal::Handle m_encoder; - - friend class DigitalGlitchFilter; }; } // namespace frc diff --git a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DigitalGlitchFilter.java b/wpilibj/src/main/java/edu/wpi/first/wpilibj/DigitalGlitchFilter.java deleted file mode 100644 index 9d3086e952..0000000000 --- a/wpilibj/src/main/java/edu/wpi/first/wpilibj/DigitalGlitchFilter.java +++ /dev/null @@ -1,192 +0,0 @@ -// Copyright (c) FIRST and other WPILib contributors. -// Open Source Software; you can modify and/or share it under the terms of -// the WPILib BSD license file in the root directory of this project. - -package edu.wpi.first.wpilibj; - -import edu.wpi.first.hal.DigitalGlitchFilterJNI; -import edu.wpi.first.hal.FRCNetComm.tResourceType; -import edu.wpi.first.hal.HAL; -import edu.wpi.first.hal.util.AllocationException; -import edu.wpi.first.util.sendable.Sendable; -import edu.wpi.first.util.sendable.SendableBuilder; -import edu.wpi.first.util.sendable.SendableRegistry; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantLock; - -/** - * Class to enable glitch filtering on a set of digital inputs. This class will manage adding and - * removing digital inputs from an FPGA glitch filter. The filter lets the user configure the time - * that an input must remain high or low before it is classified as high or low. - */ -public class DigitalGlitchFilter implements Sendable, AutoCloseable { - /** Configures the Digital Glitch Filter to its default settings. */ - @SuppressWarnings("this-escape") - public DigitalGlitchFilter() { - m_channelIndex = allocateFilterIndex(); - if (m_channelIndex < 0) { - throw new AllocationException("No filters available to allocate."); - } - HAL.report(tResourceType.kResourceType_DigitalGlitchFilter, m_channelIndex + 1, 0); - SendableRegistry.addLW(this, "DigitalGlitchFilter", m_channelIndex); - } - - @Override - public void close() { - SendableRegistry.remove(this); - if (m_channelIndex >= 0) { - m_mutex.lock(); - try { - m_filterAllocated[m_channelIndex] = false; - } finally { - m_mutex.unlock(); - } - - m_channelIndex = -1; - } - } - - /** - * Allocates the next available filter index, or -1 if there are no filters available. - * - * @return the filter index - */ - private static int allocateFilterIndex() { - m_mutex.lock(); - try { - for (int index = 0; index < m_filterAllocated.length; index++) { - if (!m_filterAllocated[index]) { - m_filterAllocated[index] = true; - return index; - } - } - } finally { - m_mutex.unlock(); - } - return -1; - } - - private static void setFilter(DigitalSource input, int channelIndex) { - if (input != null) { // Counter might have just one input - // analog triggers are not supported for DigitalGlitchFilters - if (input.isAnalogTrigger()) { - throw new IllegalStateException("Analog Triggers not supported for DigitalGlitchFilters"); - } - DigitalGlitchFilterJNI.setFilterSelect(input.getPortHandleForRouting(), channelIndex); - - int selected = DigitalGlitchFilterJNI.getFilterSelect(input.getPortHandleForRouting()); - if (selected != channelIndex) { - throw new IllegalStateException( - "DigitalGlitchFilterJNI.setFilterSelect(" + channelIndex + ") failed -> " + selected); - } - } - } - - /** - * Assigns the DigitalSource to this glitch filter. - * - * @param input The DigitalSource to add. - */ - public void add(DigitalSource input) { - setFilter(input, m_channelIndex + 1); - } - - /** - * Assigns the Encoder to this glitch filter. - * - * @param input The Encoder to add. - */ - public void add(Encoder input) { - add(input.m_aSource); - add(input.m_bSource); - } - - /** - * Assigns the Counter to this glitch filter. - * - * @param input The Counter to add. - */ - public void add(Counter input) { - add(input.m_upSource); - add(input.m_downSource); - } - - /** - * Removes this filter from the given digital input. - * - * @param input The DigitalSource to stop filtering. - */ - public void remove(DigitalSource input) { - setFilter(input, 0); - } - - /** - * Removes this filter from the given Encoder. - * - * @param input the Encoder to stop filtering. - */ - public void remove(Encoder input) { - remove(input.m_aSource); - remove(input.m_bSource); - } - - /** - * Removes this filter from the given Counter. - * - * @param input The Counter to stop filtering. - */ - public void remove(Counter input) { - remove(input.m_upSource); - remove(input.m_downSource); - } - - /** - * Sets the number of FPGA cycles that the input must hold steady to pass through this glitch - * filter. - * - * @param fpgaCycles The number of FPGA cycles. - */ - public void setPeriodCycles(int fpgaCycles) { - DigitalGlitchFilterJNI.setFilterPeriod(m_channelIndex, fpgaCycles); - } - - /** - * Sets the number of nanoseconds that the input must hold steady to pass through this glitch - * filter. - * - * @param nanoseconds The number of nanoseconds. - */ - public void setPeriodNanoSeconds(long nanoseconds) { - int fpgaCycles = (int) (nanoseconds * SensorUtil.kSystemClockTicksPerMicrosecond / 4 / 1000); - setPeriodCycles(fpgaCycles); - } - - /** - * Gets the number of FPGA cycles that the input must hold steady to pass through this glitch - * filter. - * - * @return The number of cycles. - */ - public int getPeriodCycles() { - return DigitalGlitchFilterJNI.getFilterPeriod(m_channelIndex); - } - - /** - * Gets the number of nanoseconds that the input must hold steady to pass through this glitch - * filter. - * - * @return The number of nanoseconds. - */ - public long getPeriodNanoSeconds() { - int fpgaCycles = getPeriodCycles(); - - return fpgaCycles * 1000L / (SensorUtil.kSystemClockTicksPerMicrosecond / 4); - } - - @Override - public void initSendable(SendableBuilder builder) {} - - private int m_channelIndex; - private static final Lock m_mutex = new ReentrantLock(true); - private static final boolean[] m_filterAllocated = new boolean[3]; -}