mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-26 01:51:41 +00:00
Switches AnalogInputs and AnalogTriggers to Handles (#117)
Results in a breaking change to AnalogTrigger int constructor. If a user wants multiple AnalogTriggers, they must use the AnalogInput constructor.
This commit is contained in:
committed by
Peter Johnson
parent
77a1af44c4
commit
e8e052712e
@@ -17,7 +17,6 @@ import edu.wpi.first.wpilibj.livewindow.LiveWindow;
|
||||
import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
|
||||
import edu.wpi.first.wpilibj.tables.ITable;
|
||||
import edu.wpi.first.wpilibj.util.AllocationException;
|
||||
import edu.wpi.first.wpilibj.util.CheckedAllocationException;
|
||||
|
||||
/**
|
||||
* Analog channel class.
|
||||
@@ -34,8 +33,7 @@ import edu.wpi.first.wpilibj.util.CheckedAllocationException;
|
||||
public class AnalogInput extends SensorBase implements PIDSource, LiveWindowSendable {
|
||||
|
||||
private static final int kAccumulatorSlot = 1;
|
||||
private static Resource channels = new Resource(kAnalogInputChannels);
|
||||
private long m_port;
|
||||
int m_port; // explicit no modifier, private and package accessable.
|
||||
private int m_channel;
|
||||
private static final int[] kAccumulatorChannels = {0, 1};
|
||||
private long m_accumulatorOffset;
|
||||
@@ -53,11 +51,6 @@ public class AnalogInput extends SensorBase implements PIDSource, LiveWindowSend
|
||||
throw new AllocationException("Analog input channel " + m_channel
|
||||
+ " cannot be allocated. Channel is not present.");
|
||||
}
|
||||
try {
|
||||
channels.allocate(channel);
|
||||
} catch (CheckedAllocationException ex) {
|
||||
throw new AllocationException("Analog input channel " + m_channel + " is already allocated");
|
||||
}
|
||||
|
||||
final int portHandle = AnalogJNI.getPort((byte) channel);
|
||||
m_port = AnalogJNI.initializeAnalogInputPort(portHandle);
|
||||
@@ -72,7 +65,6 @@ public class AnalogInput extends SensorBase implements PIDSource, LiveWindowSend
|
||||
public void free() {
|
||||
AnalogJNI.freeAnalogInputPort(m_port);
|
||||
m_port = 0;
|
||||
channels.free(m_channel);
|
||||
m_channel = 0;
|
||||
m_accumulatorOffset = 0;
|
||||
}
|
||||
|
||||
@@ -42,8 +42,10 @@ public class AnalogTrigger {
|
||||
/**
|
||||
* Where the analog trigger is attached.
|
||||
*/
|
||||
protected long m_port;
|
||||
protected int m_port;
|
||||
protected int m_index;
|
||||
protected AnalogInput m_analogInput = null;
|
||||
protected boolean m_ownsAnalog = false;
|
||||
|
||||
/**
|
||||
* Constructor for an analog trigger given a channel number.
|
||||
@@ -51,15 +53,8 @@ public class AnalogTrigger {
|
||||
* @param channel the port to use for the analog trigger
|
||||
*/
|
||||
public AnalogTrigger(final int channel) {
|
||||
final int portHandle = AnalogJNI.getPort((byte) channel);
|
||||
ByteBuffer index = ByteBuffer.allocateDirect(4);
|
||||
index.order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
m_port =
|
||||
AnalogJNI.initializeAnalogTrigger(portHandle, index.asIntBuffer());
|
||||
m_index = index.asIntBuffer().get(0);
|
||||
|
||||
UsageReporting.report(tResourceType.kResourceType_AnalogTrigger, channel);
|
||||
this(new AnalogInput(channel));
|
||||
m_ownsAnalog = true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,7 +64,15 @@ public class AnalogTrigger {
|
||||
* @param channel the AnalogInput to use for the analog trigger
|
||||
*/
|
||||
public AnalogTrigger(AnalogInput channel) {
|
||||
this(requireNonNull(channel, "The Analog Input given was null").getChannel());
|
||||
m_analogInput = channel;
|
||||
ByteBuffer index = ByteBuffer.allocateDirect(4);
|
||||
index.order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
m_port =
|
||||
AnalogJNI.initializeAnalogTrigger(channel.m_port, index.asIntBuffer());
|
||||
m_index = index.asIntBuffer().get(0);
|
||||
|
||||
UsageReporting.report(tResourceType.kResourceType_AnalogTrigger, channel.getChannel());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -78,6 +81,9 @@ public class AnalogTrigger {
|
||||
public void free() {
|
||||
AnalogJNI.cleanAnalogTrigger(m_port);
|
||||
m_port = 0;
|
||||
if (m_ownsAnalog && m_analogInput != null) {
|
||||
m_analogInput.free();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,9 +33,9 @@ public class AnalogJNI extends JNIWrapper {
|
||||
int kFallingPulse = 3;
|
||||
}
|
||||
|
||||
public static native long initializeAnalogInputPort(int halPortHandle);
|
||||
public static native int initializeAnalogInputPort(int halPortHandle);
|
||||
|
||||
public static native void freeAnalogInputPort(long portPointer);
|
||||
public static native void freeAnalogInputPort(int portHandle);
|
||||
|
||||
public static native int initializeAnalogOutputPort(int halPortHandle);
|
||||
|
||||
@@ -55,64 +55,64 @@ public class AnalogJNI extends JNIWrapper {
|
||||
|
||||
public static native double getAnalogSampleRate();
|
||||
|
||||
public static native void setAnalogAverageBits(long analogPortPointer, int bits);
|
||||
public static native void setAnalogAverageBits(int analogPortHandle, int bits);
|
||||
|
||||
public static native int getAnalogAverageBits(long analogPortPointer);
|
||||
public static native int getAnalogAverageBits(int analogPortHandle);
|
||||
|
||||
public static native void setAnalogOversampleBits(long analogPortPointer, int bits);
|
||||
public static native void setAnalogOversampleBits(int analogPortHandle, int bits);
|
||||
|
||||
public static native int getAnalogOversampleBits(long analogPortPointer);
|
||||
public static native int getAnalogOversampleBits(int analogPortHandle);
|
||||
|
||||
public static native short getAnalogValue(long analogPortPointer);
|
||||
public static native short getAnalogValue(int analogPortHandle);
|
||||
|
||||
public static native int getAnalogAverageValue(long analogPortPointer);
|
||||
public static native int getAnalogAverageValue(int analogPortHandle);
|
||||
|
||||
public static native int getAnalogVoltsToValue(long analogPortPointer, double voltage);
|
||||
public static native int getAnalogVoltsToValue(int analogPortHandle, double voltage);
|
||||
|
||||
public static native double getAnalogVoltage(long analogPortPointer);
|
||||
public static native double getAnalogVoltage(int analogPortHandle);
|
||||
|
||||
public static native double getAnalogAverageVoltage(long analogPortPointer);
|
||||
public static native double getAnalogAverageVoltage(int analogPortHandle);
|
||||
|
||||
public static native int getAnalogLSBWeight(long analogPortPointer);
|
||||
public static native int getAnalogLSBWeight(int analogPortHandle);
|
||||
|
||||
public static native int getAnalogOffset(long analogPortPointer);
|
||||
public static native int getAnalogOffset(int analogPortHandle);
|
||||
|
||||
public static native boolean isAccumulatorChannel(long analogPortPointer);
|
||||
public static native boolean isAccumulatorChannel(int analogPortHandle);
|
||||
|
||||
public static native void initAccumulator(long analogPortPointer);
|
||||
public static native void initAccumulator(int analogPortHandle);
|
||||
|
||||
public static native void resetAccumulator(long analogPortPointer);
|
||||
public static native void resetAccumulator(int analogPortHandle);
|
||||
|
||||
public static native void setAccumulatorCenter(long analogPortPointer, int center);
|
||||
public static native void setAccumulatorCenter(int analogPortHandle, int center);
|
||||
|
||||
public static native void setAccumulatorDeadband(long analogPortPointer, int deadband);
|
||||
public static native void setAccumulatorDeadband(int analogPortHandle, int deadband);
|
||||
|
||||
public static native long getAccumulatorValue(long analogPortPointer);
|
||||
public static native long getAccumulatorValue(int analogPortHandle);
|
||||
|
||||
public static native int getAccumulatorCount(long analogPortPointer);
|
||||
public static native int getAccumulatorCount(int analogPortHandle);
|
||||
|
||||
public static native void getAccumulatorOutput(long analogPortPointer, LongBuffer value,
|
||||
public static native void getAccumulatorOutput(int analogPortHandle, LongBuffer value,
|
||||
IntBuffer count);
|
||||
|
||||
public static native long initializeAnalogTrigger(int halPortHandle, IntBuffer index);
|
||||
public static native int initializeAnalogTrigger(int analogInputHandle, IntBuffer index);
|
||||
|
||||
public static native void cleanAnalogTrigger(long analogTriggerPointer);
|
||||
public static native void cleanAnalogTrigger(int analogTriggerHandle);
|
||||
|
||||
public static native void setAnalogTriggerLimitsRaw(long analogTriggerPointer, int lower,
|
||||
public static native void setAnalogTriggerLimitsRaw(int analogTriggerHandle, int lower,
|
||||
int upper);
|
||||
|
||||
public static native void setAnalogTriggerLimitsVoltage(long analogTriggerPointer,
|
||||
public static native void setAnalogTriggerLimitsVoltage(int analogTriggerHandle,
|
||||
double lower, double upper);
|
||||
|
||||
public static native void setAnalogTriggerAveraged(long analogTriggerPointer,
|
||||
public static native void setAnalogTriggerAveraged(int analogTriggerHandle,
|
||||
boolean useAveragedValue);
|
||||
|
||||
public static native void setAnalogTriggerFiltered(long analogTriggerPointer,
|
||||
public static native void setAnalogTriggerFiltered(int analogTriggerHandle,
|
||||
boolean useFilteredValue);
|
||||
|
||||
public static native boolean getAnalogTriggerInWindow(long analogTriggerPointer);
|
||||
public static native boolean getAnalogTriggerInWindow(int analogTriggerHandle);
|
||||
|
||||
public static native boolean getAnalogTriggerTriggerState(long analogTriggerPointer);
|
||||
public static native boolean getAnalogTriggerTriggerState(int analogTriggerHandle);
|
||||
|
||||
public static native boolean getAnalogTriggerOutput(long analogTriggerPointer, int type);
|
||||
public static native boolean getAnalogTriggerOutput(int analogTriggerHandle, int type);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user