Switches relays to handles (#119)

This commit is contained in:
Thad House
2016-06-29 18:58:14 -07:00
committed by Peter Johnson
parent e8e052712e
commit 9b2af0d090
10 changed files with 275 additions and 235 deletions

View File

@@ -27,68 +27,75 @@ extern "C" {
/*
* Class: edu_wpi_first_wpilibj_hal_RelayJNI
* Method: setRelayForward
* Signature: (JZ)V
* Method: initializeRelayPort
* Signature: (IZ)I;
*/
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_RelayJNI_setRelayForward(
JNIEnv* env, jclass, jlong id, jboolean value) {
RELAYJNI_LOG(logDEBUG) << "Calling RELAYJNI setRelayForward";
RELAYJNI_LOG(logDEBUG) << "Port Ptr = " << (void*)id;
JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_RelayJNI_initializeRelayPort(
JNIEnv* env, jclass, jint id, jboolean fwd) {
RELAYJNI_LOG(logDEBUG) << "Calling RELAYJNI initializeRelayPort";
RELAYJNI_LOG(logDEBUG) << "Port Handle = " << (HalPortHandle)id;
RELAYJNI_LOG(logDEBUG) << "Forward = " << (jint)fwd;
int32_t status = 0;
HalRelayHandle handle = initializeRelayPort((HalPortHandle)id, (uint8_t) fwd, &status);
RELAYJNI_LOG(logDEBUG) << "Status = " << status;
RELAYJNI_LOG(logDEBUG) << "Relay Handle = " << handle;
return (jint) handle;
}
/*
* Class: edu_wpi_first_wpilibj_hal_RelayJNI
* Method: freeRelayPort
* Signature: (I)V;
*/
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_RelayJNI_freeRelayPort(
JNIEnv *env, jclass, jint id) {
RELAYJNI_LOG(logDEBUG) << "Calling RELAYJNI freeRelayPort";
RELAYJNI_LOG(logDEBUG) << "Port Handle = " << (HalRelayHandle)id;
freeRelayPort((HalRelayHandle)id);
}
/*
* Class: edu_wpi_first_wpilibj_hal_RelayJNI
* Method: checkRelayChannel
* Signature: (I)Z;
*/
JNIEXPORT jboolean JNICALL Java_edu_wpi_first_wpilibj_hal_RelayJNI_checkRelayChannel(
JNIEnv *env, jclass, jint channel) {
RELAYJNI_LOG(logDEBUG) << "Calling RELAYJNI checkRelayChannel";
RELAYJNI_LOG(logDEBUG) << "Channel = " << channel;
return (jboolean)checkRelayChannel((uint8_t) channel);
}
/*
* Class: edu_wpi_first_wpilibj_hal_RelayJNI
* Method: setRelay
* Signature: (IZ)V
*/
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_RelayJNI_setRelay(
JNIEnv* env, jclass, jint id, jboolean value) {
RELAYJNI_LOG(logDEBUG) << "Calling RELAYJNI setRelay";
RELAYJNI_LOG(logDEBUG) << "Port Handle = " << (HalRelayHandle)id;
RELAYJNI_LOG(logDEBUG) << "Flag = " << (jint)value;
int32_t status = 0;
setRelayForward((void*)id, value, &status);
setRelay((HalRelayHandle)id, value, &status);
RELAYJNI_LOG(logDEBUG) << "Status = " << status;
CheckStatus(env, status);
}
/*
* Class: edu_wpi_first_wpilibj_hal_RelayJNI
* Method: setRelayReverse
* Signature: (JZ)V
*/
JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_RelayJNI_setRelayReverse(
JNIEnv* env, jclass, jlong id, jboolean value) {
RELAYJNI_LOG(logDEBUG) << "Calling RELAYJNI setRelayReverse";
RELAYJNI_LOG(logDEBUG) << "Port Ptr = " << (void*)id;
RELAYJNI_LOG(logDEBUG) << "Flag = " << (jint)value;
int32_t status = 0;
setRelayReverse((void*)id, value, &status);
RELAYJNI_LOG(logDEBUG) << "Status = " << status;
CheckStatus(env, status);
}
/*
* Class: edu_wpi_first_wpilibj_hal_RelayJNI
* Method: getRelayForward
* Signature: (J)Z
* Method: getRelay
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL
Java_edu_wpi_first_wpilibj_hal_RelayJNI_getRelayForward(
JNIEnv* env, jclass, jlong id) {
RELAYJNI_LOG(logDEBUG) << "Calling RELAYJNI getRelayForward";
RELAYJNI_LOG(logDEBUG) << "Port Ptr = " << (void*)id;
Java_edu_wpi_first_wpilibj_hal_RelayJNI_getRelay(
JNIEnv* env, jclass, jint id) {
RELAYJNI_LOG(logDEBUG) << "Calling RELAYJNI getRelay";
RELAYJNI_LOG(logDEBUG) << "Port Handle = " << (HalRelayHandle)id;
int32_t status = 0;
jboolean returnValue = getRelayForward((void*)id, &status);
jboolean returnValue = getRelay((HalRelayHandle)id, &status);
RELAYJNI_LOG(logDEBUG) << "Status = " << status;
RELAYJNI_LOG(logDEBUG) << "getRelayForwardResult = " << (jint)returnValue;
CheckStatus(env, status);
return returnValue;
}
/*
* Class: edu_wpi_first_wpilibj_hal_RelayJNI
* Method: getRelayReverse
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL
Java_edu_wpi_first_wpilibj_hal_RelayJNI_getRelayReverse(
JNIEnv* env, jclass, jlong id) {
RELAYJNI_LOG(logDEBUG) << "Calling RELAYJNI getRelayReverse";
RELAYJNI_LOG(logDEBUG) << "Port Ptr = " << (void*)id;
int32_t status = 0;
jboolean returnValue = getRelayReverse((void*)id, &status);
RELAYJNI_LOG(logDEBUG) << "Status = " << status;
RELAYJNI_LOG(logDEBUG) << "getRelayReverseResult = " << (jint)returnValue;
RELAYJNI_LOG(logDEBUG) << "getRelayResult = " << (jint)returnValue;
CheckStatus(env, status);
return returnValue;
}

View File

@@ -16,7 +16,6 @@ import edu.wpi.first.wpilibj.livewindow.LiveWindowSendable;
import edu.wpi.first.wpilibj.tables.ITable;
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;
@@ -111,10 +110,12 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
}
private final int m_channel;
private int m_forwardHandle = 0;
private int m_reverseHandle = 0;
private long m_port;
private Direction m_direction;
private static Resource relayChannels = new Resource(kRelayChannels * 2);
/**
* Common relay initialization method. This code is common to all Relay constructors and
@@ -122,21 +123,18 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
* set to both lines at 0v.
*/
private void initRelay() {
SensorBase.checkRelayChannel(m_channel);
try {
if (m_direction == Direction.kBoth || m_direction == Direction.kForward) {
relayChannels.allocate(m_channel * 2);
UsageReporting.report(tResourceType.kResourceType_Relay, m_channel);
}
if (m_direction == Direction.kBoth || m_direction == Direction.kReverse) {
relayChannels.allocate(m_channel * 2 + 1);
UsageReporting.report(tResourceType.kResourceType_Relay, m_channel + 128);
}
} catch (CheckedAllocationException ex) {
throw new AllocationException("Relay channel " + m_channel + " is already allocated");
if (!RelayJNI.checkRelayChannel(m_channel)) {
throw new IndexOutOfBoundsException("Requested relay channel number is out of range.");
}
int portHandle = RelayJNI.getPort((byte)m_channel);
if (m_direction == Direction.kBoth || m_direction == Direction.kForward) {
m_forwardHandle = RelayJNI.initializeRelayPort(portHandle, true);
UsageReporting.report(tResourceType.kResourceType_Relay, m_channel);
}
if (m_direction == Direction.kBoth || m_direction == Direction.kReverse) {
m_reverseHandle = RelayJNI.initializeRelayPort(portHandle, false);
UsageReporting.report(tResourceType.kResourceType_Relay, m_channel + 128);
}
m_port = DIOJNI.initializeDigitalPort(DIOJNI.getPort((byte) m_channel));
m_safetyHelper = new MotorSafetyHelper(this);
m_safetyHelper.setSafetyEnabled(false);
@@ -168,19 +166,22 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
@Override
public void free() {
if (m_direction == Direction.kBoth || m_direction == Direction.kForward) {
relayChannels.free(m_channel * 2);
try {
RelayJNI.setRelay(m_forwardHandle, false);
} catch (RuntimeException ex) {
// do nothing. Ignore
}
if (m_direction == Direction.kBoth || m_direction == Direction.kReverse) {
relayChannels.free(m_channel * 2 + 1);
try {
RelayJNI.setRelay(m_reverseHandle, false);
} catch (RuntimeException ex) {
// do nothing. Ignore
}
RelayJNI.setRelayForward(m_port, false);
RelayJNI.setRelayReverse(m_port, false);
DIOJNI.freeDIO(m_port);
DIOJNI.freeDigitalPort(m_port);
m_port = 0;
RelayJNI.freeRelayPort(m_forwardHandle);
RelayJNI.freeRelayPort(m_reverseHandle);
m_forwardHandle = 0;
m_reverseHandle = 0;
}
/**
@@ -200,18 +201,18 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
switch (value) {
case kOff:
if (m_direction == Direction.kBoth || m_direction == Direction.kForward) {
RelayJNI.setRelayForward(m_port, false);
RelayJNI.setRelay(m_forwardHandle, false);
}
if (m_direction == Direction.kBoth || m_direction == Direction.kReverse) {
RelayJNI.setRelayReverse(m_port, false);
RelayJNI.setRelay(m_reverseHandle, false);
}
break;
case kOn:
if (m_direction == Direction.kBoth || m_direction == Direction.kForward) {
RelayJNI.setRelayForward(m_port, true);
RelayJNI.setRelay(m_forwardHandle, true);
}
if (m_direction == Direction.kBoth || m_direction == Direction.kReverse) {
RelayJNI.setRelayReverse(m_port, true);
RelayJNI.setRelay(m_reverseHandle, true);
}
break;
case kForward:
@@ -220,10 +221,10 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
+ "forward");
}
if (m_direction == Direction.kBoth || m_direction == Direction.kForward) {
RelayJNI.setRelayForward(m_port, true);
RelayJNI.setRelay(m_forwardHandle, true);
}
if (m_direction == Direction.kBoth) {
RelayJNI.setRelayReverse(m_port, false);
RelayJNI.setRelay(m_reverseHandle, false);
}
break;
case kReverse:
@@ -232,10 +233,10 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
+ "reverse");
}
if (m_direction == Direction.kBoth) {
RelayJNI.setRelayForward(m_port, false);
RelayJNI.setRelay(m_forwardHandle, false);
}
if (m_direction == Direction.kBoth || m_direction == Direction.kReverse) {
RelayJNI.setRelayReverse(m_port, true);
RelayJNI.setRelay(m_reverseHandle, true);
}
break;
default:
@@ -254,8 +255,8 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
* @return The current state of the relay as a Relay::Value
*/
public Value get() {
if (RelayJNI.getRelayForward(m_port)) {
if (RelayJNI.getRelayReverse(m_port)) {
if (RelayJNI.getRelay(m_forwardHandle)) {
if (RelayJNI.getRelay(m_reverseHandle)) {
return Value.kOn;
} else {
if (m_direction == Direction.kForward) {
@@ -265,7 +266,7 @@ public class Relay extends SensorBase implements MotorSafety, LiveWindowSendable
}
}
} else {
if (RelayJNI.getRelayReverse(m_port)) {
if (RelayJNI.getRelay(m_reverseHandle)) {
if (m_direction == Direction.kReverse) {
return Value.kOn;
} else {

View File

@@ -8,11 +8,13 @@
package edu.wpi.first.wpilibj.hal;
public class RelayJNI extends DIOJNI {
public static native void setRelayForward(long digitalPortPointer, boolean on);
public static native int initializeRelayPort(int halPortHandle, boolean forward);
public static native void freeRelayPort(int relayPortHandle);
public static native boolean checkRelayChannel(int pin);
public static native void setRelay(int relayPortHandle, boolean on);
public static native void setRelayReverse(long digitalPortPointer, boolean on);
public static native boolean getRelayForward(long digitalPortPointer);
public static native boolean getRelayReverse(long digitalPortPointer);
public static native boolean getRelay(int relayPortHandle);
}