mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
[wpilib, hal] DigitalGlitchFilter: Fix sim crash and clean up construction (#6937)
Fixes error when >3 are constructed- in java, m_filterAllocated[index] would be evaluated before the bounds check and throw IndexOutOfBounds, in c++ a vague assertion error would be thrown. Makes DoAdd static in c++ Makes the error message when HAL_SetFilterSelect fails consistent with java
This commit is contained in:
@@ -7,6 +7,7 @@ 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;
|
||||
@@ -22,21 +23,12 @@ public class DigitalGlitchFilter implements Sendable, AutoCloseable {
|
||||
/** Configures the Digital Glitch Filter to its default settings. */
|
||||
@SuppressWarnings("this-escape")
|
||||
public DigitalGlitchFilter() {
|
||||
m_mutex.lock();
|
||||
try {
|
||||
int index = 0;
|
||||
while (m_filterAllocated[index] && index < m_filterAllocated.length) {
|
||||
index++;
|
||||
}
|
||||
if (index != m_filterAllocated.length) {
|
||||
m_channelIndex = index;
|
||||
m_filterAllocated[index] = true;
|
||||
HAL.report(tResourceType.kResourceType_DigitalGlitchFilter, m_channelIndex + 1, 0);
|
||||
SendableRegistry.addLW(this, "DigitalGlitchFilter", index);
|
||||
}
|
||||
} finally {
|
||||
m_mutex.unlock();
|
||||
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
|
||||
@@ -54,6 +46,26 @@ public class DigitalGlitchFilter implements Sendable, AutoCloseable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
@@ -174,7 +186,7 @@ public class DigitalGlitchFilter implements Sendable, AutoCloseable {
|
||||
@Override
|
||||
public void initSendable(SendableBuilder builder) {}
|
||||
|
||||
private int m_channelIndex = -1;
|
||||
private int m_channelIndex;
|
||||
private static final Lock m_mutex = new ReentrantLock(true);
|
||||
private static final boolean[] m_filterAllocated = new boolean[3];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user