mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-07-06 03:31:43 +00:00
Implements AutoCloseable for types, replacing free() (#1048)
This commit is contained in:
committed by
Peter Johnson
parent
a2ecb1027a
commit
cbaff52850
@@ -13,7 +13,7 @@ import java.util.function.Consumer;
|
||||
* An event listener. This calls back to a desigated callback function when
|
||||
* an event matching the specified mask is generated by the library.
|
||||
*/
|
||||
public class VideoListener {
|
||||
public class VideoListener implements AutoCloseable {
|
||||
/**
|
||||
* Create an event listener.
|
||||
* @param listener Listener function
|
||||
@@ -26,7 +26,13 @@ public class VideoListener {
|
||||
m_handle = CameraServerJNI.addListener(listener, eventMask, immediateNotify);
|
||||
}
|
||||
|
||||
public synchronized void free() {
|
||||
@Deprecated
|
||||
public void free() {
|
||||
close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
if (m_handle != 0) {
|
||||
CameraServerJNI.removeListener(m_handle);
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ package edu.wpi.cscore;
|
||||
* consist of multiple images (e.g. from a stereo or depth camera); these
|
||||
* are called channels.
|
||||
*/
|
||||
public class VideoSink {
|
||||
public class VideoSink implements AutoCloseable {
|
||||
public enum Kind {
|
||||
kUnknown(0), kMjpeg(2), kCv(4);
|
||||
private int value;
|
||||
@@ -38,7 +38,13 @@ public class VideoSink {
|
||||
m_handle = handle;
|
||||
}
|
||||
|
||||
public synchronized void free() {
|
||||
@Deprecated
|
||||
public void free() {
|
||||
close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
if (m_handle != 0) {
|
||||
CameraServerJNI.releaseSink(m_handle);
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ package edu.wpi.cscore;
|
||||
* consist of multiple images (e.g. from a stereo or depth camera); these
|
||||
* are called channels.
|
||||
*/
|
||||
public class VideoSource {
|
||||
public class VideoSource implements AutoCloseable {
|
||||
public enum Kind {
|
||||
kUnknown(0), kUsb(1), kHttp(2), kCv(4);
|
||||
private int value;
|
||||
@@ -39,7 +39,13 @@ public class VideoSource {
|
||||
m_handle = handle;
|
||||
}
|
||||
|
||||
public synchronized void free() {
|
||||
@Deprecated
|
||||
public void free() {
|
||||
close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
if (m_handle != 0) {
|
||||
CameraServerJNI.releaseSource(m_handle);
|
||||
}
|
||||
|
||||
@@ -7,9 +7,7 @@
|
||||
|
||||
package edu.wpi.first.wpilibj.sim;
|
||||
|
||||
import java.io.Closeable;
|
||||
|
||||
public class CallbackStore implements Closeable {
|
||||
public class CallbackStore implements AutoCloseable {
|
||||
interface CancelCallbackFunc {
|
||||
void cancel(int index, int uid);
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ import java.util.function.Consumer;
|
||||
* A reference must be kept to the NetworkTableInstance returned by this
|
||||
* function to keep it from being garbage collected.
|
||||
*/
|
||||
public final class NetworkTableInstance {
|
||||
public final class NetworkTableInstance implements AutoCloseable {
|
||||
/**
|
||||
* Client/server mode flag values (as returned by {@link #getNetworkMode()}).
|
||||
* This is a bitmask.
|
||||
@@ -59,10 +59,16 @@ public final class NetworkTableInstance {
|
||||
m_handle = handle;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void free() {
|
||||
close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys the instance (if created by {@link #create()}).
|
||||
*/
|
||||
public synchronized void free() {
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
if (m_owned && m_handle != 0) {
|
||||
NetworkTablesJNI.destroyInstance(m_handle);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ package edu.wpi.first.networktables;
|
||||
/**
|
||||
* NetworkTables Remote Procedure Call (Server Side).
|
||||
*/
|
||||
public final class RpcAnswer {
|
||||
public final class RpcAnswer implements AutoCloseable {
|
||||
/** Entry handle. */
|
||||
public final int entry;
|
||||
|
||||
@@ -46,10 +46,16 @@ public final class RpcAnswer {
|
||||
|
||||
static final byte[] emptyResponse = new byte[] {};
|
||||
|
||||
@Deprecated
|
||||
public void free() {
|
||||
close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Posts an empty response if one was not previously sent.
|
||||
*/
|
||||
public synchronized void free() {
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
if (call != 0) {
|
||||
postResponse(emptyResponse);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ package edu.wpi.first.networktables;
|
||||
/**
|
||||
* NetworkTables Remote Procedure Call.
|
||||
*/
|
||||
public final class RpcCall {
|
||||
public final class RpcCall implements AutoCloseable{
|
||||
/** Constructor.
|
||||
* This should generally only be used internally to NetworkTables.
|
||||
* @param entry Entry
|
||||
@@ -21,10 +21,16 @@ public final class RpcCall {
|
||||
m_call = call;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void free() {
|
||||
close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the result if no other action taken.
|
||||
*/
|
||||
public synchronized void free() {
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
if (m_call != 0) {
|
||||
cancelResult();
|
||||
}
|
||||
|
||||
@@ -26,8 +26,8 @@ public class ConnectionListenerTest extends TestCase {
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
clientInst.free();
|
||||
serverInst.free();
|
||||
clientInst.close();
|
||||
serverInst.close();
|
||||
}
|
||||
|
||||
private void connect() {
|
||||
|
||||
@@ -26,8 +26,8 @@ public class EntryListenerTest extends TestCase {
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
clientInst.free();
|
||||
serverInst.free();
|
||||
clientInst.close();
|
||||
serverInst.close();
|
||||
}
|
||||
|
||||
private void connect() {
|
||||
|
||||
@@ -21,7 +21,7 @@ public class LoggerTest extends TestCase {
|
||||
|
||||
@Override
|
||||
protected void tearDown() throws Exception {
|
||||
clientInst.free();
|
||||
clientInst.close();
|
||||
}
|
||||
|
||||
public void testLogger() {
|
||||
|
||||
@@ -93,9 +93,9 @@ public class ADXL345_I2C extends SensorBase implements Accelerometer, Sendable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
m_i2c.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
m_i2c.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -79,9 +79,9 @@ public class ADXL345_SPI extends SensorBase implements Accelerometer, Sendable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
m_spi.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
m_spi.close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -93,7 +93,7 @@ public class ADXL362 extends SensorBase implements Accelerometer, Sendable {
|
||||
transferBuffer.put(1, kPartIdRegister);
|
||||
m_spi.transaction(transferBuffer, transferBuffer, 3);
|
||||
if (transferBuffer.get(2) != (byte) 0xF2) {
|
||||
m_spi.free();
|
||||
m_spi.close();
|
||||
m_spi = null;
|
||||
DriverStation.reportError("could not find ADXL362 on SPI port " + port.value, false);
|
||||
return;
|
||||
@@ -112,10 +112,10 @@ public class ADXL362 extends SensorBase implements Accelerometer, Sendable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
if (m_spi != null) {
|
||||
m_spi.free();
|
||||
m_spi.close();
|
||||
m_spi = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ public class ADXRS450_Gyro extends GyroBase implements Gyro, PIDSource, Sendable
|
||||
|
||||
// Validate the part ID
|
||||
if ((readRegister(kPIDRegister) & 0xff00) != 0x5200) {
|
||||
m_spi.free();
|
||||
m_spi.close();
|
||||
m_spi = null;
|
||||
DriverStation.reportError("could not find ADXRS450 gyro on SPI port " + port.value,
|
||||
false);
|
||||
@@ -141,10 +141,10 @@ public class ADXRS450_Gyro extends GyroBase implements Gyro, PIDSource, Sendable
|
||||
* Delete (free) the spi port used for the gyro and stop accumulating.
|
||||
*/
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
if (m_spi != null) {
|
||||
m_spi.free();
|
||||
m_spi.close();
|
||||
m_spi = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,10 +67,10 @@ public class AnalogAccelerometer extends SensorBase implements PIDSource, Sendab
|
||||
* Delete the analog components used for the accelerometer.
|
||||
*/
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
if (m_analogChannel != null && m_allocatedChannel) {
|
||||
m_analogChannel.free();
|
||||
m_analogChannel.close();
|
||||
}
|
||||
m_analogChannel = null;
|
||||
}
|
||||
|
||||
@@ -119,10 +119,10 @@ public class AnalogGyro extends GyroBase implements Gyro, PIDSource, Sendable {
|
||||
* Delete (free) the accumulator and the analog components used for the gyro.
|
||||
*/
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
if (m_analog != null && m_channelAllocated) {
|
||||
m_analog.free();
|
||||
m_analog.close();
|
||||
}
|
||||
m_analog = null;
|
||||
AnalogGyroJNI.freeAnalogGyro(m_gyroHandle);
|
||||
|
||||
@@ -54,8 +54,8 @@ public class AnalogInput extends SensorBase implements PIDSource, Sendable {
|
||||
* Channel destructor.
|
||||
*/
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
AnalogJNI.freeAnalogInputPort(m_port);
|
||||
m_port = 0;
|
||||
m_channel = 0;
|
||||
|
||||
@@ -40,8 +40,8 @@ public class AnalogOutput extends SendableBase implements Sendable {
|
||||
* Channel destructor.
|
||||
*/
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
AnalogJNI.freeAnalogOutputPort(m_port);
|
||||
m_port = 0;
|
||||
m_channel = 0;
|
||||
|
||||
@@ -158,10 +158,10 @@ public class AnalogPotentiometer extends SensorBase implements Potentiometer, Se
|
||||
* Frees this resource.
|
||||
*/
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
if (m_initAnalogInput) {
|
||||
m_analogInput.free();
|
||||
m_analogInput.close();
|
||||
m_analogInput = null;
|
||||
m_initAnalogInput = false;
|
||||
}
|
||||
|
||||
@@ -78,12 +78,12 @@ public class AnalogTrigger extends SensorBase implements Sendable {
|
||||
* Release the resources used by this object.
|
||||
*/
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
AnalogJNI.cleanAnalogTrigger(m_port);
|
||||
m_port = 0;
|
||||
if (m_ownsAnalog && m_analogInput != null) {
|
||||
m_analogInput.free();
|
||||
m_analogInput.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -180,8 +180,8 @@ public class Counter extends SensorBase implements CounterBase, Sendable, PIDSou
|
||||
}
|
||||
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
setUpdateWhenEmpty(true);
|
||||
|
||||
clearUpSource();
|
||||
@@ -222,7 +222,7 @@ public class Counter extends SensorBase implements CounterBase, Sendable, PIDSou
|
||||
*/
|
||||
public void setUpSource(DigitalSource source) {
|
||||
if (m_upSource != null && m_allocatedUpSource) {
|
||||
m_upSource.free();
|
||||
m_upSource.close();
|
||||
m_allocatedUpSource = false;
|
||||
}
|
||||
m_upSource = source;
|
||||
@@ -263,7 +263,7 @@ public class Counter extends SensorBase implements CounterBase, Sendable, PIDSou
|
||||
*/
|
||||
public void clearUpSource() {
|
||||
if (m_upSource != null && m_allocatedUpSource) {
|
||||
m_upSource.free();
|
||||
m_upSource.close();
|
||||
m_allocatedUpSource = false;
|
||||
}
|
||||
m_upSource = null;
|
||||
@@ -292,7 +292,7 @@ public class Counter extends SensorBase implements CounterBase, Sendable, PIDSou
|
||||
requireNonNull(source, "The Digital Source given was null");
|
||||
|
||||
if (m_downSource != null && m_allocatedDownSource) {
|
||||
m_downSource.free();
|
||||
m_downSource.close();
|
||||
m_allocatedDownSource = false;
|
||||
}
|
||||
CounterJNI.setCounterDownSource(m_counter, source.getPortHandleForRouting(),
|
||||
@@ -332,7 +332,7 @@ public class Counter extends SensorBase implements CounterBase, Sendable, PIDSou
|
||||
*/
|
||||
public void clearDownSource() {
|
||||
if (m_downSource != null && m_allocatedDownSource) {
|
||||
m_downSource.free();
|
||||
m_downSource.close();
|
||||
m_allocatedDownSource = false;
|
||||
}
|
||||
m_downSource = null;
|
||||
|
||||
@@ -43,8 +43,8 @@ public class DigitalGlitchFilter extends SensorBase {
|
||||
/**
|
||||
* Free the resources used by this object.
|
||||
*/
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
if (m_channelIndex >= 0) {
|
||||
synchronized (m_mutex) {
|
||||
m_filterAllocated[m_channelIndex] = false;
|
||||
|
||||
@@ -40,8 +40,8 @@ public class DigitalInput extends DigitalSource implements Sendable {
|
||||
/**
|
||||
* Frees the resources for this output.
|
||||
*/
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
if (m_interrupt != 0) {
|
||||
cancelInterrupts();
|
||||
}
|
||||
|
||||
@@ -44,8 +44,8 @@ public class DigitalOutput extends SendableBase implements Sendable {
|
||||
* Free the resources associated with a digital output.
|
||||
*/
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
// disable the pwm only if we have allocated it
|
||||
if (m_pwmGenerator != invalidPwmGenerator) {
|
||||
disablePWM();
|
||||
|
||||
@@ -86,11 +86,10 @@ public class DoubleSolenoid extends SolenoidBase implements Sendable {
|
||||
* Destructor.
|
||||
*/
|
||||
@Override
|
||||
public synchronized void free() {
|
||||
super.free();
|
||||
public synchronized void close() {
|
||||
super.close();
|
||||
SolenoidJNI.freeSolenoidPort(m_forwardHandle);
|
||||
SolenoidJNI.freeSolenoidPort(m_reverseHandle);
|
||||
super.free();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -294,18 +294,18 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, Senda
|
||||
* Free the resources used by this object.
|
||||
*/
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
if (m_aSource != null && m_allocatedA) {
|
||||
m_aSource.free();
|
||||
m_aSource.close();
|
||||
m_allocatedA = false;
|
||||
}
|
||||
if (m_bSource != null && m_allocatedB) {
|
||||
m_bSource.free();
|
||||
m_bSource.close();
|
||||
m_allocatedB = false;
|
||||
}
|
||||
if (m_indexSource != null && m_allocatedI) {
|
||||
m_indexSource.free();
|
||||
m_indexSource.close();
|
||||
m_allocatedI = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ import static java.util.Objects.requireNonNull;
|
||||
* <p>This class is intended to be used by sensor (and other I2C device) drivers. It probably should
|
||||
* not be used directly.
|
||||
*/
|
||||
public class I2C {
|
||||
public class I2C implements AutoCloseable {
|
||||
public enum Port {
|
||||
kOnboard(0), kMXP(1);
|
||||
|
||||
@@ -52,10 +52,16 @@ public class I2C {
|
||||
HAL.report(tResourceType.kResourceType_I2C, deviceAddress);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void free() {
|
||||
close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
public void free() {
|
||||
@Override
|
||||
public void close() {
|
||||
I2CJNI.i2CClose(m_port);
|
||||
}
|
||||
|
||||
|
||||
@@ -48,8 +48,8 @@ public abstract class InterruptableSensorBase extends SensorBase {
|
||||
* Frees the resources for this output.
|
||||
*/
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
if (m_interrupt != 0) {
|
||||
cancelInterrupts();
|
||||
}
|
||||
|
||||
@@ -53,10 +53,10 @@ public class NidecBrushless extends SendableBase implements SpeedController, Mot
|
||||
* Free the resources used by this object.
|
||||
*/
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
m_dio.free();
|
||||
m_pwm.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
m_dio.close();
|
||||
m_pwm.close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -92,8 +92,8 @@ public class PIDController extends PIDBase implements Controller {
|
||||
* Free the PID object.
|
||||
*/
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
m_controlLoop.stop();
|
||||
m_thisMutex.lock();
|
||||
try {
|
||||
|
||||
@@ -71,8 +71,8 @@ public class PWM extends SendableBase implements Sendable {
|
||||
* <p>Free the resource associated with the PWM channel and set the value to 0.
|
||||
*/
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
if (m_handle == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -141,8 +141,8 @@ public class Relay extends SendableBase implements MotorSafety, Sendable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void free() {
|
||||
super.free();
|
||||
public void close() {
|
||||
super.close();
|
||||
freeRelay();
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ import edu.wpi.first.wpilibj.util.WPILibVersion;
|
||||
* run to completion before the OperatorControl code could start. In the future the Autonomous code
|
||||
* might be spawned as a task, then killed at the end of the Autonomous period.
|
||||
*/
|
||||
public abstract class RobotBase {
|
||||
public abstract class RobotBase implements AutoCloseable {
|
||||
/**
|
||||
* The ID of the main Java thread.
|
||||
*/
|
||||
@@ -93,10 +93,15 @@ public abstract class RobotBase {
|
||||
LiveWindow.setEnabled(false);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void free() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Free the resources for a RobotBase class.
|
||||
*/
|
||||
public void free() {
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,7 +25,7 @@ import static java.util.Objects.requireNonNull;
|
||||
* or {@link edu.wpi.first.wpilibj.drive.MecanumDrive} classes instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public class RobotDrive implements MotorSafety {
|
||||
public class RobotDrive implements MotorSafety, AutoCloseable {
|
||||
protected MotorSafetyHelper m_safetyHelper;
|
||||
|
||||
/**
|
||||
@@ -662,22 +662,28 @@ public class RobotDrive implements MotorSafety {
|
||||
m_maxOutput = maxOutput;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void free() {
|
||||
close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Free the speed controllers if they were allocated locally.
|
||||
*/
|
||||
public void free() {
|
||||
@Override
|
||||
public void close() {
|
||||
if (m_allocatedSpeedControllers) {
|
||||
if (m_frontLeftMotor != null) {
|
||||
((PWM) m_frontLeftMotor).free();
|
||||
((PWM) m_frontLeftMotor).close();
|
||||
}
|
||||
if (m_frontRightMotor != null) {
|
||||
((PWM) m_frontRightMotor).free();
|
||||
((PWM) m_frontRightMotor).close();
|
||||
}
|
||||
if (m_rearLeftMotor != null) {
|
||||
((PWM) m_rearLeftMotor).free();
|
||||
((PWM) m_rearLeftMotor).close();
|
||||
}
|
||||
if (m_rearRightMotor != null) {
|
||||
((PWM) m_rearRightMotor).free();
|
||||
((PWM) m_rearRightMotor).close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ import edu.wpi.first.wpilibj.hal.SPIJNI;
|
||||
/**
|
||||
* Represents a SPI bus port.
|
||||
*/
|
||||
public class SPI {
|
||||
public class SPI implements AutoCloseable {
|
||||
public enum Port {
|
||||
kOnboardCS0(0), kOnboardCS1(1), kOnboardCS2(2), kOnboardCS3(3), kMXP(4);
|
||||
|
||||
@@ -49,12 +49,19 @@ public class SPI {
|
||||
HAL.report(tResourceType.kResourceType_SPI, devices);
|
||||
}
|
||||
|
||||
|
||||
@Deprecated
|
||||
public void free() {
|
||||
close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Free the resources used by this object.
|
||||
*/
|
||||
public void free() {
|
||||
@Override
|
||||
public void close() {
|
||||
if (m_accum != null) {
|
||||
m_accum.free();
|
||||
m_accum.close();
|
||||
m_accum = null;
|
||||
}
|
||||
SPIJNI.spiClose(m_port);
|
||||
@@ -416,7 +423,7 @@ public class SPI {
|
||||
|
||||
private static final int kAccumulateDepth = 2048;
|
||||
|
||||
private static class Accumulator {
|
||||
private static class Accumulator implements AutoCloseable {
|
||||
Accumulator(int port, int xferSize, int validMask, int validValue, int dataShift,
|
||||
int dataSize, boolean isSigned, boolean bigEndian) {
|
||||
m_notifier = new Notifier(this::update);
|
||||
@@ -432,7 +439,8 @@ public class SPI {
|
||||
m_port = port;
|
||||
}
|
||||
|
||||
void free() {
|
||||
@Override
|
||||
public void close() {
|
||||
m_notifier.stop();
|
||||
}
|
||||
|
||||
@@ -570,7 +578,7 @@ public class SPI {
|
||||
*/
|
||||
public void freeAccumulator() {
|
||||
if (m_accum != null) {
|
||||
m_accum.free();
|
||||
m_accum.close();
|
||||
m_accum = null;
|
||||
}
|
||||
freeAuto();
|
||||
|
||||
@@ -13,7 +13,7 @@ import edu.wpi.first.wpilibj.livewindow.LiveWindow;
|
||||
* Base class for all sensors. Stores most recent status information as well as containing utility
|
||||
* functions for checking channels and error processing.
|
||||
*/
|
||||
public abstract class SendableBase implements Sendable {
|
||||
public abstract class SendableBase implements Sendable, AutoCloseable {
|
||||
private String m_name = "";
|
||||
private String m_subsystem = "Ungrouped";
|
||||
|
||||
@@ -35,10 +35,16 @@ public abstract class SendableBase implements Sendable {
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void free() {
|
||||
close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Free the resources used by this object.
|
||||
*/
|
||||
public void free() {
|
||||
@Override
|
||||
public void close() {
|
||||
LiveWindow.remove(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -197,7 +197,7 @@ public class SerialPort {
|
||||
/**
|
||||
* Destructor.
|
||||
*/
|
||||
public void free() {
|
||||
public void close() {
|
||||
SerialPortJNI.serialClose(m_port);
|
||||
}
|
||||
|
||||
|
||||
@@ -55,8 +55,8 @@ public class Solenoid extends SolenoidBase implements Sendable {
|
||||
* Destructor.
|
||||
*/
|
||||
@Override
|
||||
public synchronized void free() {
|
||||
super.free();
|
||||
public synchronized void close() {
|
||||
super.close();
|
||||
SolenoidJNI.freeSolenoidPort(m_solenoidHandle);
|
||||
m_solenoidHandle = 0;
|
||||
}
|
||||
|
||||
@@ -191,21 +191,21 @@ public class Ultrasonic extends SensorBase implements PIDSource, Sendable {
|
||||
* sensor).
|
||||
*/
|
||||
@Override
|
||||
public synchronized void free() {
|
||||
super.free();
|
||||
public synchronized void close() {
|
||||
super.close();
|
||||
final boolean wasAutomaticMode = m_automaticEnabled;
|
||||
setAutomaticMode(false);
|
||||
if (m_allocatedChannels) {
|
||||
if (m_pingChannel != null) {
|
||||
m_pingChannel.free();
|
||||
m_pingChannel.close();
|
||||
}
|
||||
if (m_echoChannel != null) {
|
||||
m_echoChannel.free();
|
||||
m_echoChannel.close();
|
||||
}
|
||||
}
|
||||
|
||||
if (m_counter != null) {
|
||||
m_counter.free();
|
||||
m_counter.close();
|
||||
m_counter = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ package edu.wpi.first.wpilibj.interfaces;
|
||||
/**
|
||||
* Interface for yaw rate gyros.
|
||||
*/
|
||||
public interface Gyro {
|
||||
public interface Gyro extends AutoCloseable {
|
||||
/**
|
||||
* Calibrate the gyro by running for a number of samples and computing the center value. Then use
|
||||
* the center value as the Accumulator center value for subsequent measurements. It's important to
|
||||
@@ -52,5 +52,12 @@ public interface Gyro {
|
||||
/**
|
||||
* Free the resources used by the gyro.
|
||||
*/
|
||||
@Deprecated
|
||||
void free();
|
||||
|
||||
/**
|
||||
* Free the resources used by the gyro.
|
||||
*/
|
||||
@Override
|
||||
void close();
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ public class PIDToleranceTest {
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
m_pid.free();
|
||||
m_pid.close();
|
||||
m_pid = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ public class AnalogCrossConnectTest extends AbstractInterruptTest {
|
||||
assertFalse("Analog trigger is in the window (2V, 3V)", trigger.getInWindow());
|
||||
assertFalse("Analog trigger is on", trigger.getTriggerState());
|
||||
|
||||
trigger.free();
|
||||
trigger.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -95,7 +95,7 @@ public class AnalogCrossConnectTest extends AbstractInterruptTest {
|
||||
assertTrue("Analog trigger is not in the window (2V, 3V)", trigger.getInWindow());
|
||||
assertFalse("Analog trigger is on", trigger.getTriggerState());
|
||||
|
||||
trigger.free();
|
||||
trigger.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -112,7 +112,7 @@ public class AnalogCrossConnectTest extends AbstractInterruptTest {
|
||||
assertFalse("Analog trigger is in the window (2V, 3V)", trigger.getInWindow());
|
||||
assertTrue("Analog trigger is not on", trigger.getTriggerState());
|
||||
|
||||
trigger.free();
|
||||
trigger.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -167,9 +167,9 @@ public class AnalogCrossConnectTest extends AbstractInterruptTest {
|
||||
@Override
|
||||
void freeInterruptableSensorBase() {
|
||||
m_interruptTriggerOutput.cancelInterrupts();
|
||||
m_interruptTriggerOutput.free();
|
||||
m_interruptTriggerOutput.close();
|
||||
m_interruptTriggerOutput = null;
|
||||
m_interruptTrigger.free();
|
||||
m_interruptTrigger.close();
|
||||
m_interruptTrigger = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ public class AnalogPotentiometerTest extends AbstractComsSetup {
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
m_potSource.reset();
|
||||
m_pot.free();
|
||||
m_pot.close();
|
||||
m_analogIO.teardown();
|
||||
}
|
||||
|
||||
|
||||
@@ -69,14 +69,14 @@ public class DigitalGlitchFilterTest extends AbstractComsSetup {
|
||||
filter3.remove(encoder5);
|
||||
filter3.remove(counter7);
|
||||
|
||||
input1.free();
|
||||
input2.free();
|
||||
input3.free();
|
||||
input4.free();
|
||||
encoder5.free();
|
||||
counter7.free();
|
||||
filter1.free();
|
||||
filter2.free();
|
||||
filter3.free();
|
||||
input1.close();
|
||||
input2.close();
|
||||
input3.close();
|
||||
input4.close();
|
||||
encoder5.close();
|
||||
counter7.close();
|
||||
filter1.close();
|
||||
filter2.close();
|
||||
filter3.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,7 +189,7 @@ public class MotorEncoderTest extends AbstractComsSetup {
|
||||
"PID loop did not reach setpoint within 10 seconds. The current error was" + pid
|
||||
.getError(), pid.onTarget());
|
||||
|
||||
pid.free();
|
||||
pid.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -210,7 +210,7 @@ public class MotorEncoderTest extends AbstractComsSetup {
|
||||
"PID loop did not reach setpoint within 10 seconds. The error was: " + pid.getError(),
|
||||
pid.onTarget());
|
||||
|
||||
pid.free();
|
||||
pid.close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -63,13 +63,13 @@ public class PCMTest extends AbstractComsSetup {
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownAfterClass() throws Exception {
|
||||
compressor.free();
|
||||
compressor.close();
|
||||
|
||||
fakePressureSwitch.free();
|
||||
fakeCompressor.free();
|
||||
fakePressureSwitch.close();
|
||||
fakeCompressor.close();
|
||||
|
||||
fakeSolenoid1.free();
|
||||
fakeSolenoid2.free();
|
||||
fakeSolenoid1.close();
|
||||
fakeSolenoid2.close();
|
||||
}
|
||||
|
||||
@Before
|
||||
@@ -145,8 +145,8 @@ public class PCMTest extends AbstractComsSetup {
|
||||
assertTrue("Solenoid #1 did not report on", solenoid1.get());
|
||||
assertTrue("Solenoid #2 did not report on", solenoid2.get());
|
||||
|
||||
solenoid1.free();
|
||||
solenoid2.free();
|
||||
solenoid1.close();
|
||||
solenoid2.close();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -177,7 +177,7 @@ public class PCMTest extends AbstractComsSetup {
|
||||
assertTrue("DoubleSolenoid did not report Reverse", solenoid.get() == DoubleSolenoid.Value
|
||||
.kReverse);
|
||||
|
||||
solenoid.free();
|
||||
solenoid.close();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,8 +265,8 @@ public class PCMTest extends AbstractComsSetup {
|
||||
assertFalse("Solenoid #1 did not report off", solenoid1.get());
|
||||
assertFalse("Solenoid #2 did not report off", solenoid2.get());
|
||||
|
||||
solenoid1.free();
|
||||
solenoid2.free();
|
||||
solenoid1.close();
|
||||
solenoid2.close();
|
||||
}
|
||||
|
||||
protected Logger getClassLogger() {
|
||||
|
||||
@@ -47,7 +47,7 @@ public class PDPTest extends AbstractComsSetup {
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownAfterClass() throws Exception {
|
||||
pdp.free();
|
||||
pdp.close();
|
||||
pdp = null;
|
||||
me.teardown();
|
||||
me = null;
|
||||
|
||||
@@ -116,7 +116,7 @@ public class PIDTest extends AbstractComsSetup {
|
||||
public void tearDown() throws Exception {
|
||||
logger.fine("Teardown: " + me.getType());
|
||||
m_controller.disable();
|
||||
m_controller.free();
|
||||
m_controller.close();
|
||||
m_controller = null;
|
||||
me.reset();
|
||||
}
|
||||
|
||||
@@ -64,8 +64,8 @@ public abstract class AnalogCrossConnectFixture implements ITestFixture {
|
||||
*/
|
||||
@Override
|
||||
public boolean teardown() {
|
||||
m_input.free();
|
||||
m_output.free();
|
||||
m_input.close();
|
||||
m_output.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -80,8 +80,8 @@ public class DIOCrossConnectFixture implements ITestFixture {
|
||||
public boolean teardown() {
|
||||
logger.log(Level.FINE, "Begining teardown");
|
||||
if (m_allocated) {
|
||||
m_input.free();
|
||||
m_output.free();
|
||||
m_input.close();
|
||||
m_output.close();
|
||||
m_allocated = false;
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -99,8 +99,8 @@ public class FakeCounterFixture implements ITestFixture {
|
||||
@Override
|
||||
public boolean teardown() {
|
||||
logger.log(Level.FINE, "Begining teardown");
|
||||
m_counter.free();
|
||||
m_source.free();
|
||||
m_counter.close();
|
||||
m_source.close();
|
||||
if (m_allocated) { // Only tear down the dio if this class allocated it
|
||||
m_dio.teardown();
|
||||
m_allocated = false;
|
||||
|
||||
@@ -101,9 +101,9 @@ public class FakeEncoderFixture implements ITestFixture {
|
||||
@Override
|
||||
public boolean teardown() {
|
||||
logger.fine("Begining teardown");
|
||||
m_source.free();
|
||||
m_source.close();
|
||||
logger.finer("Source freed " + m_sourcePort[0] + ", " + m_sourcePort[1]);
|
||||
m_encoder.free();
|
||||
m_encoder.close();
|
||||
logger.finer("Encoder freed " + m_encoderPort[0] + ", " + m_encoderPort[1]);
|
||||
if (m_allocated) {
|
||||
m_dio1.teardown();
|
||||
|
||||
@@ -181,37 +181,37 @@ public abstract class MotorEncoderFixture<T extends SpeedController> implements
|
||||
if (!m_tornDown) {
|
||||
boolean wasNull = false;
|
||||
if (m_motor instanceof PWM && m_motor != null) {
|
||||
((PWM) m_motor).free();
|
||||
((PWM) m_motor).close();
|
||||
m_motor = null;
|
||||
} else if (m_motor == null) {
|
||||
wasNull = true;
|
||||
}
|
||||
if (m_encoder != null) {
|
||||
m_encoder.free();
|
||||
m_encoder.close();
|
||||
m_encoder = null;
|
||||
} else {
|
||||
wasNull = true;
|
||||
}
|
||||
if (m_counters[0] != null) {
|
||||
m_counters[0].free();
|
||||
m_counters[0].close();
|
||||
m_counters[0] = null;
|
||||
} else {
|
||||
wasNull = true;
|
||||
}
|
||||
if (m_counters[1] != null) {
|
||||
m_counters[1].free();
|
||||
m_counters[1].close();
|
||||
m_counters[1] = null;
|
||||
} else {
|
||||
wasNull = true;
|
||||
}
|
||||
if (m_alphaSource != null) {
|
||||
m_alphaSource.free();
|
||||
m_alphaSource.close();
|
||||
m_alphaSource = null;
|
||||
} else {
|
||||
wasNull = true;
|
||||
}
|
||||
if (m_betaSource != null) {
|
||||
m_betaSource.free();
|
||||
m_betaSource.close();
|
||||
m_betaSource = null;
|
||||
} else {
|
||||
wasNull = true;
|
||||
|
||||
@@ -84,9 +84,9 @@ public abstract class RelayCrossConnectFixture implements ITestFixture {
|
||||
@Override
|
||||
public boolean teardown() {
|
||||
if (!m_freed) {
|
||||
m_relay.free();
|
||||
m_inputOne.free();
|
||||
m_inputTwo.free();
|
||||
m_relay.close();
|
||||
m_inputOne.close();
|
||||
m_inputTwo.close();
|
||||
m_freed = true;
|
||||
} else {
|
||||
throw new RuntimeException("You attempted to free the "
|
||||
|
||||
@@ -92,22 +92,22 @@ public abstract class TiltPanCameraFixture implements ITestFixture {
|
||||
}
|
||||
|
||||
public void freeGyro() {
|
||||
m_gyro.free();
|
||||
m_gyro.close();
|
||||
m_gyro = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean teardown() {
|
||||
m_tilt.free();
|
||||
m_tilt.close();
|
||||
m_tilt = null;
|
||||
m_pan.free();
|
||||
m_pan.close();
|
||||
m_pan = null;
|
||||
if (m_gyro != null) { //in case not freed during gyro tests
|
||||
m_gyro.free();
|
||||
m_gyro.close();
|
||||
m_gyro = null;
|
||||
}
|
||||
if (m_gyroParam != null) { //in case gyro tests failed before getting to this point
|
||||
m_gyroParam.free();
|
||||
m_gyroParam.close();
|
||||
m_gyroParam = null;
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -13,7 +13,7 @@ import edu.wpi.first.wpilibj.Timer;
|
||||
/**
|
||||
* Simulates an encoder for testing purposes.
|
||||
*/
|
||||
public class FakeCounterSource {
|
||||
public class FakeCounterSource implements AutoCloseable {
|
||||
private Thread m_task;
|
||||
private int m_count;
|
||||
private int m_milliSec;
|
||||
@@ -70,10 +70,10 @@ public class FakeCounterSource {
|
||||
/**
|
||||
* Destroy Object with minimum memory leak.
|
||||
*/
|
||||
public void free() {
|
||||
public void close() {
|
||||
m_task = null;
|
||||
if (m_allocated) {
|
||||
m_output.free();
|
||||
m_output.close();
|
||||
m_output = null;
|
||||
m_allocated = false;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import edu.wpi.first.wpilibj.Timer;
|
||||
/**
|
||||
* Emulates a quadrature encoder.
|
||||
*/
|
||||
public class FakeEncoderSource {
|
||||
public class FakeEncoderSource implements AutoCloseable {
|
||||
private Thread m_task;
|
||||
private int m_count;
|
||||
private int m_milliSec;
|
||||
@@ -93,11 +93,11 @@ public class FakeEncoderSource {
|
||||
/**
|
||||
* Frees the resource.
|
||||
*/
|
||||
public void free() {
|
||||
public void close() {
|
||||
m_task = null;
|
||||
if (m_allocatedOutputs) {
|
||||
m_outputA.free();
|
||||
m_outputB.free();
|
||||
m_outputA.close();
|
||||
m_outputB.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import edu.wpi.first.wpilibj.AnalogOutput;
|
||||
/**
|
||||
* A fake source to provide output to a {@link edu.wpi.first.wpilibj.interfaces.Potentiometer}.
|
||||
*/
|
||||
public class FakePotentiometerSource {
|
||||
public class FakePotentiometerSource implements AutoCloseable {
|
||||
private AnalogOutput m_output;
|
||||
private boolean m_initOutput;
|
||||
private double m_potMaxAngle;
|
||||
@@ -83,9 +83,9 @@ public class FakePotentiometerSource {
|
||||
/**
|
||||
* Frees the resouce.
|
||||
*/
|
||||
public void free() {
|
||||
public void close() {
|
||||
if (m_initOutput) {
|
||||
m_output.free();
|
||||
m_output.close();
|
||||
m_output = null;
|
||||
m_initOutput = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user