Switches PWM and DIO to Handles (#120)

This commit is contained in:
Thad House
2016-06-30 21:39:09 -07:00
committed by Peter Johnson
parent 9b2af0d090
commit 3593ecb17e
37 changed files with 656 additions and 514 deletions

View File

@@ -80,10 +80,15 @@ public class AnalogTriggerOutput extends DigitalSource {
UsageReporting.report(tResourceType.kResourceType_AnalogTriggerOutput, trigger.getIndex(),
outputType.m_value);
}
@Override
/**
* Frees the resources for this output.
*/
public void free() {
if (m_interrupt != 0) {
cancelInterrupts();
}
}
/**
@@ -109,6 +114,11 @@ public class AnalogTriggerOutput extends DigitalSource {
public boolean getAnalogTriggerForRouting() {
return true;
}
@Override
public int getPortHandle() {
return m_trigger.m_port;
}
/**
* Defines the state in which the AnalogTrigger triggers.

View File

@@ -53,9 +53,13 @@ public class DigitalGlitchFilter extends SensorBase {
private static void setFilter(DigitalSource input, int channelIndex) {
if (input != null) { // Counter might have just one input
DigitalGlitchFilterJNI.setFilterSelect(input.m_port, channelIndex);
// analog triggers are not supported for DigitalGlitchFilters
if (input.getAnalogTriggerForRouting()) {
throw new IllegalStateException("Analog Triggers not supported for DigitalGlitchFilters");
}
DigitalGlitchFilterJNI.setFilterSelect(input.getPortHandle(), channelIndex);
int selected = DigitalGlitchFilterJNI.getFilterSelect(input.m_port);
int selected = DigitalGlitchFilterJNI.getFilterSelect(input.getPortHandle());
if (selected != channelIndex) {
throw new IllegalStateException("DigitalGlitchFilterJNI.setFilterSelect("
+ channelIndex + ") failed -> " + selected);

View File

@@ -21,18 +21,34 @@ import edu.wpi.first.wpilibj.tables.ITable;
* for devices like switches etc. that aren't implemented anywhere else.
*/
public class DigitalInput extends DigitalSource implements LiveWindowSendable {
private int m_channel = 0;
private int m_handle = 0;
/**
* Create an instance of a Digital Input class. Creates a digital input given a channel.
*
* @param channel the DIO channel for the digital input 0-9 are on-board, 10-25 are on the MXP
*/
public DigitalInput(int channel) {
initDigitalPort(channel, true);
checkDigitalChannel(channel);
m_channel = channel;
m_handle = DIOJNI.initializeDIOPort(DIOJNI.getPort((byte)channel), true);
LiveWindow.addSensor("DigitalInput", channel, this);
UsageReporting.report(tResourceType.kResourceType_DigitalInput, channel);
}
/**
* Frees the resources for this output.
*/
public void free() {
if (m_interrupt != 0) {
cancelInterrupts();
}
DIOJNI.freeDIOPort(m_handle);
}
/**
* Get the value from a digital input channel. Retrieve the value of a single digital input
@@ -41,7 +57,7 @@ public class DigitalInput extends DigitalSource implements LiveWindowSendable {
* @return the status of the digital input
*/
public boolean get() {
return DIOJNI.getDIO(super.m_port);
return DIOJNI.getDIO(m_handle);
}
/**
@@ -50,13 +66,48 @@ public class DigitalInput extends DigitalSource implements LiveWindowSendable {
* @return The GPIO channel number that this object represents.
*/
public int getChannel() {
return super.m_channel;
return m_channel;
}
/**
* Get the channel routing number.
*
* @return channel routing number
*/
@Override
public int getChannelForRouting() {
return m_channel;
}
/**
* Get the module routing number.
*
* @return 0
*/
@Override
public byte getModuleForRouting() {
return 0;
}
/**
* Is this an analog trigger.
*
* @return true if this is an analog trigger
*/
@Override
public boolean getAnalogTriggerForRouting() {
return false;
}
/**
* Get the HAL Port Handle.
*
* @return The HAL Handle to the specified source.
*/
@Override
public int getPortHandle() {
return m_handle;
}
@Override

View File

@@ -24,6 +24,9 @@ public class DigitalOutput extends DigitalSource implements LiveWindowSendable {
private static final long invalidPwmGenerator = 0xffffffff;
private long m_pwmGenerator = invalidPwmGenerator;
private int m_channel = 0;
private int m_handle = 0;
/**
* Create an instance of a digital output. Create an instance of a digital output given a
@@ -33,7 +36,10 @@ public class DigitalOutput extends DigitalSource implements LiveWindowSendable {
* the MXP
*/
public DigitalOutput(int channel) {
initDigitalPort(channel, false);
checkDigitalChannel(channel);
m_channel = channel;
m_handle = DIOJNI.initializeDIOPort(DIOJNI.getPort((byte)channel), false);
UsageReporting.report(tResourceType.kResourceType_DigitalOutput, channel);
}
@@ -47,7 +53,8 @@ public class DigitalOutput extends DigitalSource implements LiveWindowSendable {
if (m_pwmGenerator != invalidPwmGenerator) {
disablePWM();
}
super.free();
DIOJNI.freeDIOPort(m_handle);
m_handle = 0;
}
/**
@@ -56,14 +63,14 @@ public class DigitalOutput extends DigitalSource implements LiveWindowSendable {
* @param value true is on, off is false
*/
public void set(boolean value) {
DIOJNI.setDIO(super.m_port, (short) (value ? 1 : 0));
DIOJNI.setDIO(m_handle, (short) (value ? 1 : 0));
}
/**
* @return The GPIO channel number that this object represents.
*/
public int getChannel() {
return super.m_channel;
return m_channel;
}
/**
@@ -74,7 +81,7 @@ public class DigitalOutput extends DigitalSource implements LiveWindowSendable {
* @param pulseLength The length of the pulse.
*/
public void pulse(final int channel, final float pulseLength) {
DIOJNI.pulse(super.m_port, pulseLength);
DIOJNI.pulse(m_handle, pulseLength);
}
/**
@@ -89,7 +96,7 @@ public class DigitalOutput extends DigitalSource implements LiveWindowSendable {
(float) (pulseLength / 1.0e9 * (DIOJNI.getLoopTiming() * 25));
System.err
.println("You should use the float version of pulse for portability. This is deprecated");
DIOJNI.pulse(super.m_port, convertedPulse);
DIOJNI.pulse(m_handle, convertedPulse);
}
/**
@@ -98,7 +105,7 @@ public class DigitalOutput extends DigitalSource implements LiveWindowSendable {
* @return true if pulsing
*/
public boolean isPulsing() {
return DIOJNI.isPulsing(super.m_port);
return DIOJNI.isPulsing(m_handle);
}
/**
@@ -165,6 +172,46 @@ public class DigitalOutput extends DigitalSource implements LiveWindowSendable {
}
PWMJNI.setPWMDutyCycle(m_pwmGenerator, dutyCycle);
}
/**
* Get the channel routing number.
*
* @return channel routing number
*/
@Override
public int getChannelForRouting() {
return m_channel;
}
/**
* Get the module routing number.
*
* @return 0
*/
@Override
public byte getModuleForRouting() {
return 0;
}
/**
* Is this an analog trigger.
*
* @return true if this is an analog trigger
*/
@Override
public boolean getAnalogTriggerForRouting() {
return false;
}
/**
* Get the HAL Port Handle.
*
* @return The HAL Handle to the specified source.
*/
@Override
public int getPortHandle() {
return m_handle;
}
/*
* Live Window code, only does anything if live window is activated.

View File

@@ -7,10 +7,6 @@
package edu.wpi.first.wpilibj;
import edu.wpi.first.wpilibj.hal.DIOJNI;
import edu.wpi.first.wpilibj.util.AllocationException;
import edu.wpi.first.wpilibj.util.CheckedAllocationException;
/**
* DigitalSource Interface. The DigitalSource represents all the possible inputs for a counter or a
* quadrature encoder. The source may be either a digital input or an analog input. If the caller
@@ -18,64 +14,5 @@ import edu.wpi.first.wpilibj.util.CheckedAllocationException;
* source. The source can either be a digital input or analog trigger but not both.
*/
public abstract class DigitalSource extends InterruptableSensorBase {
protected static Resource channels = new Resource(kDigitalChannels);
protected long m_port;
protected int m_channel;
protected void initDigitalPort(int channel, boolean input) {
m_channel = channel;
checkDigitalChannel(m_channel);
try {
channels.allocate(m_channel);
} catch (CheckedAllocationException ex) {
throw new AllocationException("Digital input " + m_channel + " is already allocated");
}
int portHandle = DIOJNI.getPort((byte) channel);
m_port = DIOJNI.initializeDigitalPort(portHandle);
DIOJNI.allocateDIO(m_port, input);
}
@Override
public void free() {
channels.free(m_channel);
DIOJNI.freeDIO(m_port);
DIOJNI.freeDigitalPort(m_port);
m_port = 0;
m_channel = 0;
}
/**
* Get the channel routing number.
*
* @return channel routing number
*/
@Override
public int getChannelForRouting() {
return m_channel;
}
/**
* Get the module routing number.
*
* @return 0
*/
@Override
public byte getModuleForRouting() {
return 0;
}
/**
* Is this an analog trigger.
*
* @return true if this is an analog trigger
*/
@Override
public boolean getAnalogTriggerForRouting() {
return false;
}
public abstract int getPortHandle();
}

View File

@@ -61,7 +61,7 @@ public class PWM extends SensorBase implements LiveWindowSendable {
}
private int m_channel;
private long m_port;
private int m_handle;
/**
* kDefaultPwmPeriod is in ms.
@@ -106,13 +106,9 @@ public class PWM extends SensorBase implements LiveWindowSendable {
checkPWMChannel(channel);
m_channel = channel;
m_port = DIOJNI.initializeDigitalPort(DIOJNI.getPort((byte) channel));
m_handle = PWMJNI.initializePWMPort(DIOJNI.getPort((byte) channel));
if (!PWMJNI.allocatePWMChannel(m_port)) {
throw new AllocationException("PWM channel " + channel + " is already allocated");
}
PWMJNI.setPWM(m_port, (short) 0);
PWMJNI.setPWM(m_handle, (short) 0);
m_eliminateDeadband = false;
@@ -125,14 +121,12 @@ public class PWM extends SensorBase implements LiveWindowSendable {
* <p>Free the resource associated with the PWM channel and set the value to 0.
*/
public void free() {
if (m_port == 0) {
if (m_handle == 0) {
return;
}
PWMJNI.setPWM(m_port, (short) 0);
PWMJNI.freePWMChannel(m_port);
PWMJNI.freeDIO(m_port);
DIOJNI.freeDigitalPort(m_port);
m_port = 0;
PWMJNI.setPWM(m_handle, (short) 0);
PWMJNI.freePWMPort(m_handle);
m_handle = 0;
}
/**
@@ -320,7 +314,7 @@ public class PWM extends SensorBase implements LiveWindowSendable {
* @param value Raw PWM value. Range 0 - 255.
*/
public void setRaw(int value) {
PWMJNI.setPWM(m_port, (short) value);
PWMJNI.setPWM(m_handle, (short) value);
}
/**
@@ -331,7 +325,7 @@ public class PWM extends SensorBase implements LiveWindowSendable {
* @return Raw PWM control value. Range: 0 - 255.
*/
public int getRaw() {
return PWMJNI.getPWM(m_port);
return PWMJNI.getPWM(m_handle);
}
/**
@@ -343,15 +337,15 @@ public class PWM extends SensorBase implements LiveWindowSendable {
switch (mult.value) {
case PeriodMultiplier.k4X_val:
// Squelch 3 out of 4 outputs
PWMJNI.setPWMPeriodScale(m_port, 3);
PWMJNI.setPWMPeriodScale(m_handle, 3);
break;
case PeriodMultiplier.k2X_val:
// Squelch 1 out of 2 outputs
PWMJNI.setPWMPeriodScale(m_port, 1);
PWMJNI.setPWMPeriodScale(m_handle, 1);
break;
case PeriodMultiplier.k1X_val:
// Don't squelch any outputs
PWMJNI.setPWMPeriodScale(m_port, 0);
PWMJNI.setPWMPeriodScale(m_handle, 0);
break;
default:
// Cannot hit this, limited by PeriodMultiplier enum
@@ -359,7 +353,7 @@ public class PWM extends SensorBase implements LiveWindowSendable {
}
protected void setZeroLatch() {
PWMJNI.latchPWMZero(m_port);
PWMJNI.latchPWMZero(m_handle);
}
private int getMaxPositivePwm() {

View File

@@ -83,7 +83,7 @@ public class Ultrasonic extends SensorBase implements PIDSource, LiveWindowSenda
return;
}
if (ultrasonic.isEnabled()) {
ultrasonic.m_pingChannel.pulse(m_pingChannel.m_channel, (float) kPingTime); // do
ultrasonic.m_pingChannel.pulse(m_pingChannel.getChannel(), (float) kPingTime); // do
// the
// ping
}
@@ -288,15 +288,8 @@ public class Ultrasonic extends SensorBase implements PIDSource, LiveWindowSenda
setAutomaticMode(false); // turn off automatic round robin if pinging
// single sensor
m_counter.reset(); // reset the counter to zero (invalid data now)
m_pingChannel.pulse(m_pingChannel.m_channel, (float) kPingTime); // do
// the
// ping
// to
// start
// getting
// a
// single
// range
// do the ping to start getting a single range
m_pingChannel.pulse(m_pingChannel.getChannel(), (float) kPingTime);
}
/**

View File

@@ -9,23 +9,19 @@ package edu.wpi.first.wpilibj.hal;
@SuppressWarnings("AbbreviationAsWordInName")
public class DIOJNI extends JNIWrapper {
public static native long initializeDigitalPort(int halPortHandle);
public static native int initializeDIOPort(int halPortHandle, boolean input);
public static native void freeDigitalPort(long portPointer);
public static native void freeDIOPort(int dioPortHandle);
public static native boolean allocateDIO(long digitalPortPointer, boolean input);
public static native void setDIO(int dioPortHandle, short value);
public static native void freeDIO(long digitalPortPointer);
public static native boolean getDIO(int dioPortHandle);
public static native void setDIO(long digitalPortPointer, short value);
public static native boolean getDIODirection(int dioPortHandle);
public static native boolean getDIO(long digitalPortPointer);
public static native void pulse(int dioPortHandle, double pulseLength);
public static native boolean getDIODirection(long digitalPortPointer);
public static native void pulse(long digitalPortPointer, double pulseLength);
public static native boolean isPulsing(long digitalPortPointer);
public static native boolean isPulsing(int dioPortHandle);
public static native boolean isAnyPulsing();

View File

@@ -8,9 +8,9 @@
package edu.wpi.first.wpilibj.hal;
public class DigitalGlitchFilterJNI extends JNIWrapper {
public static native void setFilterSelect(long digitalPortPointer, int filterIndex);
public static native void setFilterSelect(int digitalPortHandle, int filterIndex);
public static native int getFilterSelect(long digitalPortPointer);
public static native int getFilterSelect(int digitalPortHandle);
public static native void setFilterPeriod(int filterIndex, int fpgaCycles);

View File

@@ -9,17 +9,17 @@ package edu.wpi.first.wpilibj.hal;
@SuppressWarnings("AbbreviationAsWordInName")
public class PWMJNI extends DIOJNI {
public static native boolean allocatePWMChannel(long digitalPortPointer);
public static native int initializePWMPort(int halPortHandle);
public static native void freePWMChannel(long digitalPortPointer);
public static native void freePWMPort(int pwmPortHandle);
public static native void setPWM(long digitalPortPointer, short value);
public static native void setPWM(int pwmPortHandle, short value);
public static native short getPWM(long digitalPortPointer);
public static native short getPWM(int pwmPortHandle);
public static native void latchPWMZero(long digitalPortPointer);
public static native void latchPWMZero(int pwmPortHandle);
public static native void setPWMPeriodScale(long digitalPortPointer, int squelchMask);
public static native void setPWMPeriodScale(int pwmPortHandle, int squelchMask);
public static native long allocatePWM();