mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
Changes DigitalSource API for HAL ease of use (#144)
This commit is contained in:
committed by
Peter Johnson
parent
7597e3c274
commit
4a3e3a6324
@@ -101,23 +101,23 @@ public class AnalogTriggerOutput extends DigitalSource {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getChannelForRouting() {
|
||||
return (m_trigger.m_index << 2) + m_outputType.m_value;
|
||||
public int getPortHandleForRouting() {
|
||||
return m_trigger.m_port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte getModuleForRouting() {
|
||||
return (byte) (m_trigger.m_index >> 2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getAnalogTriggerForRouting() {
|
||||
return true;
|
||||
public int getAnalogTriggerTypeForRouting() {
|
||||
return m_outputType.m_value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPortHandle() {
|
||||
return m_trigger.m_port;
|
||||
public int getChannel() {
|
||||
return m_trigger.m_index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAnalogTrigger() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -230,8 +230,8 @@ public class Counter extends SensorBase implements CounterBase, LiveWindowSendab
|
||||
m_allocatedUpSource = false;
|
||||
}
|
||||
m_upSource = source;
|
||||
CounterJNI.setCounterUpSource(m_counter, source.getChannelForRouting(),
|
||||
source.getAnalogTriggerForRouting());
|
||||
CounterJNI.setCounterUpSource(m_counter, source.getPortHandleForRouting(),
|
||||
source.getAnalogTriggerTypeForRouting());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -303,8 +303,8 @@ public class Counter extends SensorBase implements CounterBase, LiveWindowSendab
|
||||
m_downSource.free();
|
||||
m_allocatedDownSource = false;
|
||||
}
|
||||
CounterJNI.setCounterDownSource(m_counter, source.getChannelForRouting(),
|
||||
source.getAnalogTriggerForRouting());
|
||||
CounterJNI.setCounterDownSource(m_counter, source.getPortHandleForRouting(),
|
||||
source.getAnalogTriggerTypeForRouting());
|
||||
m_downSource = source;
|
||||
}
|
||||
|
||||
|
||||
@@ -54,12 +54,12 @@ public class DigitalGlitchFilter extends SensorBase {
|
||||
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.getAnalogTriggerForRouting()) {
|
||||
if (input.isAnalogTrigger()) {
|
||||
throw new IllegalStateException("Analog Triggers not supported for DigitalGlitchFilters");
|
||||
}
|
||||
DigitalGlitchFilterJNI.setFilterSelect(input.getPortHandle(), channelIndex);
|
||||
DigitalGlitchFilterJNI.setFilterSelect(input.getPortHandleForRouting(), channelIndex);
|
||||
|
||||
int selected = DigitalGlitchFilterJNI.getFilterSelect(input.getPortHandle());
|
||||
int selected = DigitalGlitchFilterJNI.getFilterSelect(input.getPortHandleForRouting());
|
||||
if (selected != channelIndex) {
|
||||
throw new IllegalStateException("DigitalGlitchFilterJNI.setFilterSelect("
|
||||
+ channelIndex + ") failed -> " + selected);
|
||||
|
||||
@@ -65,37 +65,28 @@ public class DigitalInput extends DigitalSource implements LiveWindowSendable {
|
||||
*
|
||||
* @return The GPIO channel number that this object represents.
|
||||
*/
|
||||
@Override
|
||||
public int getChannel() {
|
||||
return m_channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channel routing number.
|
||||
* Get the analog trigger type.
|
||||
*
|
||||
* @return channel routing number
|
||||
* @return false
|
||||
*/
|
||||
@Override
|
||||
public int getChannelForRouting() {
|
||||
return m_channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the module routing number.
|
||||
*
|
||||
* @return 0
|
||||
*/
|
||||
@Override
|
||||
public byte getModuleForRouting() {
|
||||
public int getAnalogTriggerTypeForRouting() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Is this an analog trigger.
|
||||
*
|
||||
* @return true if this is an analog trigger
|
||||
*/
|
||||
@Override
|
||||
public boolean getAnalogTriggerForRouting() {
|
||||
public boolean isAnalogTrigger() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -105,11 +96,10 @@ public class DigitalInput extends DigitalSource implements LiveWindowSendable {
|
||||
* @return The HAL Handle to the specified source.
|
||||
*/
|
||||
@Override
|
||||
public int getPortHandle() {
|
||||
public int getPortHandleForRouting() {
|
||||
return m_handle;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getSmartDashboardType() {
|
||||
return "Digital Input";
|
||||
|
||||
@@ -67,6 +67,7 @@ public class DigitalOutput extends DigitalSource implements LiveWindowSendable {
|
||||
/**
|
||||
* @return The GPIO channel number that this object represents.
|
||||
*/
|
||||
@Override
|
||||
public int getChannel() {
|
||||
return m_channel;
|
||||
}
|
||||
@@ -170,34 +171,24 @@ public class DigitalOutput extends DigitalSource implements LiveWindowSendable {
|
||||
}
|
||||
DIOJNI.setDigitalPWMDutyCycle(m_pwmGenerator, dutyCycle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the channel routing number.
|
||||
*
|
||||
* @return channel routing number
|
||||
*/
|
||||
@Override
|
||||
public int getChannelForRouting() {
|
||||
return m_channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the module routing number.
|
||||
* Get the analog trigger type.
|
||||
*
|
||||
* @return 0
|
||||
* @return false
|
||||
*/
|
||||
@Override
|
||||
public byte getModuleForRouting() {
|
||||
public int getAnalogTriggerTypeForRouting() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Is this an analog trigger.
|
||||
*
|
||||
* @return true if this is an analog trigger
|
||||
*/
|
||||
@Override
|
||||
public boolean getAnalogTriggerForRouting() {
|
||||
public boolean isAnalogTrigger() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -207,7 +198,7 @@ public class DigitalOutput extends DigitalSource implements LiveWindowSendable {
|
||||
* @return The HAL Handle to the specified source.
|
||||
*/
|
||||
@Override
|
||||
public int getPortHandle() {
|
||||
public int getPortHandleForRouting() {
|
||||
return m_handle;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,5 +14,7 @@ package edu.wpi.first.wpilibj;
|
||||
* source. The source can either be a digital input or analog trigger but not both.
|
||||
*/
|
||||
public abstract class DigitalSource extends InterruptableSensorBase {
|
||||
public abstract int getPortHandle();
|
||||
public abstract boolean isAnalogTrigger();
|
||||
|
||||
public abstract int getChannel();
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import edu.wpi.first.wpilibj.hal.EncoderJNI;
|
||||
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;
|
||||
|
||||
/**
|
||||
* Class to read quad encoders. Quadrature encoders are devices that count shaft rotation and can
|
||||
@@ -72,15 +73,14 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW
|
||||
* @param reverseDirection If true, counts down instead of up (this is all relative)
|
||||
*/
|
||||
private void initEncoder(boolean reverseDirection, final EncodingType type) {
|
||||
m_encoder = EncoderJNI.initializeEncoder(m_aSource.getModuleForRouting(),
|
||||
(byte)m_aSource.getChannelForRouting(), m_aSource.getAnalogTriggerForRouting(),
|
||||
m_bSource.getModuleForRouting(), (byte)m_bSource.getChannelForRouting(),
|
||||
m_bSource.getAnalogTriggerForRouting(), reverseDirection, type.value);
|
||||
m_encoder = EncoderJNI.initializeEncoder(m_aSource.getPortHandleForRouting(),
|
||||
m_aSource.getAnalogTriggerTypeForRouting(), m_bSource.getPortHandleForRouting(),
|
||||
m_bSource.getAnalogTriggerTypeForRouting(), reverseDirection, type.value);
|
||||
|
||||
m_pidSource = PIDSourceType.kDisplacement;
|
||||
|
||||
UsageReporting.report(tResourceType.kResourceType_Encoder, getFPGAIndex(), type.value);
|
||||
LiveWindow.addSensor("Encoder", m_aSource.getChannelForRouting(), this);
|
||||
LiveWindow.addSensor("Encoder", m_aSource.getChannel(), this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,7 +164,7 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW
|
||||
m_bSource = new DigitalInput(channelB);
|
||||
m_indexSource = new DigitalInput(indexChannel);
|
||||
initEncoder(reverseDirection, EncodingType.k4X);
|
||||
setIndexSource(indexChannel);
|
||||
setIndexSource(m_indexSource);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -564,7 +564,12 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW
|
||||
* @param type The state that will cause the encoder to reset
|
||||
*/
|
||||
public void setIndexSource(int channel, IndexingType type) {
|
||||
EncoderJNI.setEncoderIndexSource(m_encoder, channel, false, type.value);
|
||||
if (m_allocatedI) {
|
||||
throw new AllocationException("Digital Input for Indexing already allocated");
|
||||
}
|
||||
m_indexSource = new DigitalInput(channel);
|
||||
m_allocatedI = true;
|
||||
setIndexSource(m_indexSource, type);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -575,8 +580,8 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW
|
||||
* @param type The state that will cause the encoder to reset
|
||||
*/
|
||||
public void setIndexSource(DigitalSource source, IndexingType type) {
|
||||
EncoderJNI.setEncoderIndexSource(m_encoder, source.getChannelForRouting(),
|
||||
source.getAnalogTriggerForRouting(), type.value);
|
||||
EncoderJNI.setEncoderIndexSource(m_encoder, source.getPortHandleForRouting(),
|
||||
source.getAnalogTriggerTypeForRouting(), type.value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -61,21 +61,14 @@ public abstract class InterruptableSensorBase extends SensorBase {
|
||||
*
|
||||
* @return true if this is an analog trigger.
|
||||
*/
|
||||
abstract boolean getAnalogTriggerForRouting();
|
||||
public abstract int getAnalogTriggerTypeForRouting();
|
||||
|
||||
/**
|
||||
* The channel routing number.
|
||||
*
|
||||
* @return channel routing number
|
||||
*/
|
||||
abstract int getChannelForRouting();
|
||||
|
||||
/**
|
||||
* The modules routing number.
|
||||
*
|
||||
* @return module routing number
|
||||
*/
|
||||
abstract byte getModuleForRouting();
|
||||
public abstract int getPortHandleForRouting();
|
||||
|
||||
/**
|
||||
* Request one of the 8 interrupts asynchronously on this digital input.
|
||||
@@ -95,8 +88,8 @@ public abstract class InterruptableSensorBase extends SensorBase {
|
||||
|
||||
assert (m_interrupt != 0);
|
||||
|
||||
InterruptJNI.requestInterrupts(m_interrupt, getModuleForRouting(), getChannelForRouting(),
|
||||
getAnalogTriggerForRouting());
|
||||
InterruptJNI.requestInterrupts(m_interrupt, getPortHandleForRouting(),
|
||||
getAnalogTriggerTypeForRouting());
|
||||
setUpSourceEdge(true, false);
|
||||
InterruptJNI.attachInterruptHandler(m_interrupt, handler.m_function,
|
||||
handler.overridableParameter());
|
||||
@@ -116,8 +109,8 @@ public abstract class InterruptableSensorBase extends SensorBase {
|
||||
|
||||
assert (m_interrupt != 0);
|
||||
|
||||
InterruptJNI.requestInterrupts(m_interrupt, getModuleForRouting(), getChannelForRouting(),
|
||||
getAnalogTriggerForRouting());
|
||||
InterruptJNI.requestInterrupts(m_interrupt, getPortHandleForRouting(),
|
||||
getAnalogTriggerTypeForRouting());
|
||||
setUpSourceEdge(true, false);
|
||||
|
||||
}
|
||||
|
||||
@@ -16,16 +16,16 @@ public class CounterJNI extends JNIWrapper {
|
||||
|
||||
public static native void setCounterAverageSize(int counterHandle, int size);
|
||||
|
||||
public static native void setCounterUpSource(int counterHandle, int pin,
|
||||
boolean analogTrigger);
|
||||
public static native void setCounterUpSource(int counterHandle, int digitalSourceHandle,
|
||||
int analogTriggerType);
|
||||
|
||||
public static native void setCounterUpSourceEdge(int counterHandle, boolean risingEdge,
|
||||
boolean fallingEdge);
|
||||
|
||||
public static native void clearCounterUpSource(int counterHandle);
|
||||
|
||||
public static native void setCounterDownSource(int counterHandle, int pin,
|
||||
boolean analogTrigger);
|
||||
public static native void setCounterDownSource(int counterHandle, int digitalSourceHandle,
|
||||
int analogTriggerType);
|
||||
|
||||
public static native void setCounterDownSourceEdge(int counterHandle, boolean risingEdge,
|
||||
boolean fallingEdge);
|
||||
|
||||
@@ -11,10 +11,9 @@ import java.nio.IntBuffer;
|
||||
|
||||
public class EncoderJNI extends JNIWrapper {
|
||||
|
||||
public static native int initializeEncoder(byte portAModule, int portAPin,
|
||||
boolean portAAnalogTrigger, byte portBModule,
|
||||
int portBPin, boolean portBAnalogTrigger,
|
||||
boolean reverseDirection, int encodingType);
|
||||
public static native int initializeEncoder(int digitalSourceHandleA, int analogTriggerTypeA,
|
||||
int digitalSourceHandleB, int analogTriggerTypeB,
|
||||
boolean reverseDirection, int encodingType);
|
||||
|
||||
public static native void freeEncoder(int encoderHandle);
|
||||
|
||||
@@ -50,8 +49,8 @@ public class EncoderJNI extends JNIWrapper {
|
||||
|
||||
public static native int getEncoderSamplesToAverage(int encoderHandle);
|
||||
|
||||
public static native void setEncoderIndexSource(int encoderHandle, int pin,
|
||||
boolean analogTrigger, int indexingType);
|
||||
public static native void setEncoderIndexSource(int encoderHandle, int digitalSourceHandle,
|
||||
int analogTriggerType, int indexingType);
|
||||
|
||||
@SuppressWarnings("AbbreviationAsWordInName")
|
||||
public static native int getEncoderFPGAIndex(int encoderHandle);
|
||||
|
||||
@@ -29,8 +29,8 @@ public class InterruptJNI extends JNIWrapper {
|
||||
|
||||
public static native double readFallingTimestamp(int interruptHandle);
|
||||
|
||||
public static native void requestInterrupts(int interruptHandle, byte routingModule,
|
||||
int routingPin, boolean routingAnalogTrigger);
|
||||
public static native void requestInterrupts(int interruptHandle, int digitalSourceHandle,
|
||||
int analogTriggerType);
|
||||
|
||||
public static native void attachInterruptHandler(int interruptHandle,
|
||||
InterruptJNIHandlerFunction handler,
|
||||
|
||||
Reference in New Issue
Block a user