mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-24 01:31:46 +00:00
Applies Google Styleguide to Java parts of the library (#23)
This was partially applied to simulation but simulation is a bit of a mess and has a lot of duplicated code.
This commit is contained in:
committed by
Peter Johnson
parent
64ab6e51fe
commit
a834fff7b2
@@ -18,27 +18,28 @@ import edu.wpi.first.wpilibj.tables.ITableListener;
|
||||
import edu.wpi.first.wpilibj.util.AllocationException;
|
||||
import edu.wpi.first.wpilibj.util.CheckedAllocationException;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
/**
|
||||
* Class for VEX Robotics Spike style relay outputs. Relays are intended to be
|
||||
* connected to Spikes or similar relays. The relay channels controls a pair of
|
||||
* pins that are either both off, one on, the other on, or both on. This
|
||||
* translates into two Spike outputs at 0v, one at 12v and one at 0v, one at 0v
|
||||
* and the other at 12v, or two Spike outputs at 12V. This allows off, full
|
||||
* forward, or full reverse control of motors without variable speed. It also
|
||||
* allows the two channels (forward and reverse) to be used independently for
|
||||
* something that does not care about voltage polarity (like a solenoid).
|
||||
* Class for VEX Robotics Spike style relay outputs. Relays are intended to be connected to Spikes
|
||||
* or similar relays. The relay channels controls a pair of pins that are either both off, one on,
|
||||
* the other on, or both on. This translates into two Spike outputs at 0v, one at 12v and one at 0v,
|
||||
* one at 0v and the other at 12v, or two Spike outputs at 12V. This allows off, full forward, or
|
||||
* full reverse control of motors without variable speed. It also allows the two channels (forward
|
||||
* and reverse) to be used independently for something that does not care about voltage polarity
|
||||
* (like a solenoid).
|
||||
*/
|
||||
public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable {
|
||||
private MotorSafetyHelper m_safetyHelper;
|
||||
|
||||
/**
|
||||
* This class represents errors in trying to set relay values contradictory to
|
||||
* the direction to which the relay is set.
|
||||
* This class represents errors in trying to set relay values contradictory to the direction to
|
||||
* which the relay is set.
|
||||
*/
|
||||
public class InvalidValueException extends RuntimeException {
|
||||
|
||||
/**
|
||||
* Create a new exception with the given message
|
||||
* Create a new exception with the given message.
|
||||
*
|
||||
* @param message the message to pass with the exception
|
||||
*/
|
||||
@@ -50,30 +51,31 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
|
||||
/**
|
||||
* The state to drive a Relay to.
|
||||
*/
|
||||
public static enum Value {
|
||||
public enum Value {
|
||||
/**
|
||||
* value: off
|
||||
* value: off.
|
||||
*/
|
||||
kOff(0),
|
||||
/**
|
||||
* value: on for relays with defined direction
|
||||
* value: on for relays with defined direction.
|
||||
*/
|
||||
kOn(1),
|
||||
/**
|
||||
* value: forward
|
||||
* value: forward.
|
||||
*/
|
||||
kForward(2),
|
||||
/**
|
||||
* value: reverse
|
||||
* value: reverse.
|
||||
*/
|
||||
kReverse(3);
|
||||
|
||||
/**
|
||||
* The integer value representing this enumeration
|
||||
* The integer value representing this enumeration.
|
||||
*/
|
||||
@SuppressWarnings("MemberName")
|
||||
public final int value;
|
||||
|
||||
private Value(int value) {
|
||||
Value(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
@@ -81,27 +83,28 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
|
||||
/**
|
||||
* The Direction(s) that a relay is configured to operate in.
|
||||
*/
|
||||
public static enum Direction {
|
||||
public enum Direction {
|
||||
/**
|
||||
* direction: both directions are valid
|
||||
* direction: both directions are valid.
|
||||
*/
|
||||
|
||||
kBoth(0),
|
||||
/**
|
||||
* direction: Only forward is valid
|
||||
* direction: Only forward is valid.
|
||||
*/
|
||||
kForward(1),
|
||||
/**
|
||||
* direction: only reverse is valid
|
||||
* direction: only reverse is valid.
|
||||
*/
|
||||
kReverse(2);
|
||||
|
||||
/**
|
||||
* The integer value representing this enumeration
|
||||
* The integer value representing this enumeration.
|
||||
*/
|
||||
@SuppressWarnings("MemberName")
|
||||
public final int value;
|
||||
|
||||
private Direction(int value) {
|
||||
Direction(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@@ -114,9 +117,9 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
|
||||
private static Resource relayChannels = new Resource(kRelayChannels * 2);
|
||||
|
||||
/**
|
||||
* Common relay initialization method. This code is common to all Relay
|
||||
* constructors and initializes the relay and reserves all resources that need
|
||||
* to be locked. Initially the relay is set to both lines at 0v.
|
||||
* Common relay initialization method. This code is common to all Relay constructors and
|
||||
* initializes the relay and reserves all resources that need to be locked. Initially the relay is
|
||||
* set to both lines at 0v.
|
||||
*/
|
||||
private void initRelay() {
|
||||
SensorBase.checkRelayChannel(m_channel);
|
||||
@@ -129,7 +132,7 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
|
||||
relayChannels.allocate(m_channel * 2 + 1);
|
||||
UsageReporting.report(tResourceType.kResourceType_Relay, m_channel + 128);
|
||||
}
|
||||
} catch (CheckedAllocationException e) {
|
||||
} catch (CheckedAllocationException ex) {
|
||||
throw new AllocationException("Relay channel " + m_channel + " is already allocated");
|
||||
}
|
||||
|
||||
@@ -144,14 +147,12 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
|
||||
/**
|
||||
* Relay constructor given a channel.
|
||||
*
|
||||
* @param channel The channel number for this relay (0 - 3).
|
||||
* @param channel The channel number for this relay (0 - 3).
|
||||
* @param direction The direction that the Relay object will control.
|
||||
*/
|
||||
public Relay(final int channel, Direction direction) {
|
||||
if (direction == null)
|
||||
throw new NullPointerException("Null Direction was given");
|
||||
m_channel = channel;
|
||||
m_direction = direction;
|
||||
m_direction = requireNonNull( direction, "Null Direction was given");
|
||||
initRelay();
|
||||
set(Value.kOff);
|
||||
}
|
||||
@@ -185,15 +186,13 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
|
||||
/**
|
||||
* Set the relay state.
|
||||
*
|
||||
* Valid values depend on which directions of the relay are controlled by the
|
||||
* object.
|
||||
* <p>Valid values depend on which directions of the relay are controlled by the object.
|
||||
*
|
||||
* When set to kBothDirections, the relay can be set to any of the four
|
||||
* states: 0v-0v, 12v-0v, 0v-12v, 12v-12v
|
||||
* <p>When set to kBothDirections, the relay can be set to any of the four states: 0v-0v, 12v-0v,
|
||||
* 0v-12v, 12v-12v
|
||||
*
|
||||
* When set to kForwardOnly or kReverseOnly, you can specify the constant for
|
||||
* the direction or you can simply specify kOff_val and kOn_val. Using only
|
||||
* kOff_val and kOn_val is recommended.
|
||||
* <p>When set to kForwardOnly or kReverseOnly, you can specify the constant for the direction or
|
||||
* you can simply specify kOff_val and kOn_val. Using only kOff_val and kOn_val is recommended.
|
||||
*
|
||||
* @param value The state to set the relay.
|
||||
*/
|
||||
@@ -216,8 +215,10 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
|
||||
}
|
||||
break;
|
||||
case kForward:
|
||||
if (m_direction == Direction.kReverse)
|
||||
throw new InvalidValueException("A relay configured for reverse cannot be set to forward");
|
||||
if (m_direction == Direction.kReverse) {
|
||||
throw new InvalidValueException("A relay configured for reverse cannot be set to "
|
||||
+ "forward");
|
||||
}
|
||||
if (m_direction == Direction.kBoth || m_direction == Direction.kForward) {
|
||||
RelayJNI.setRelayForward(m_port, true);
|
||||
}
|
||||
@@ -226,8 +227,10 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
|
||||
}
|
||||
break;
|
||||
case kReverse:
|
||||
if (m_direction == Direction.kForward)
|
||||
throw new InvalidValueException("A relay configured for forward cannot be set to reverse");
|
||||
if (m_direction == Direction.kForward) {
|
||||
throw new InvalidValueException("A relay configured for forward cannot be set to "
|
||||
+ "reverse");
|
||||
}
|
||||
if (m_direction == Direction.kBoth) {
|
||||
RelayJNI.setRelayForward(m_port, false);
|
||||
}
|
||||
@@ -241,11 +244,11 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Relay State
|
||||
* Get the Relay State.
|
||||
*
|
||||
* Gets the current state of the relay.
|
||||
* <p>Gets the current state of the relay.
|
||||
*
|
||||
* When set to kForwardOnly or kReverseOnly, value is returned as kOn/kOff not
|
||||
* <p>When set to kForwardOnly or kReverseOnly, value is returned as kOn/kOff not
|
||||
* kForward/kReverse (per the recommendation in Set)
|
||||
*
|
||||
* @return The current state of the relay as a Relay::Value
|
||||
@@ -319,18 +322,18 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Relay Direction
|
||||
* Set the Relay Direction.
|
||||
*
|
||||
* Changes which values the relay can be set to depending on which direction
|
||||
* is used
|
||||
* <p>Changes which values the relay can be set to depending on which direction is used
|
||||
*
|
||||
* Valid inputs are kBothDirections, kForwardOnly, and kReverseOnly
|
||||
* <p>Valid inputs are kBothDirections, kForwardOnly, and kReverseOnly
|
||||
*
|
||||
* @param direction The direction for the relay to operate in
|
||||
*/
|
||||
public void setDirection(Direction direction) {
|
||||
if (direction == null)
|
||||
if (direction == null) {
|
||||
throw new NullPointerException("Null Direction was given");
|
||||
}
|
||||
if (m_direction == direction) {
|
||||
return;
|
||||
}
|
||||
@@ -351,28 +354,19 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
|
||||
}
|
||||
|
||||
private ITable m_table;
|
||||
private ITableListener m_table_listener;
|
||||
private ITableListener m_tableListener;
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void initTable(ITable subtable) {
|
||||
m_table = subtable;
|
||||
updateTable();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public ITable getTable() {
|
||||
return m_table;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void updateTable() {
|
||||
if (m_table != null) {
|
||||
@@ -388,12 +382,9 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void startLiveWindowMode() {
|
||||
m_table_listener = new ITableListener() {
|
||||
m_tableListener = new ITableListener() {
|
||||
@Override
|
||||
public void valueChanged(ITable itable, String key, Object value, boolean bln) {
|
||||
String val = ((String) value);
|
||||
@@ -408,15 +399,12 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
|
||||
}
|
||||
}
|
||||
};
|
||||
m_table.addTableListener("Value", m_table_listener, true);
|
||||
m_table.addTableListener("Value", m_tableListener, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void stopLiveWindowMode() {
|
||||
// TODO: Broken, should only remove the listener from "Value" only.
|
||||
m_table.removeTableListener(m_table_listener);
|
||||
m_table.removeTableListener(m_tableListener);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user