diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/Encoder.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/Encoder.java index 18e78b126f..edbc3c0c29 100644 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/Encoder.java +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/Encoder.java @@ -33,6 +33,9 @@ import edu.wpi.first.wpilibj.util.BoundaryException; * to be zeroed before use. */ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveWindowSendable { + public enum IndexingType { + kResetWhileHigh, kResetWhileLow, kResetOnFallingEdge, kResetOnRisingEdge + } /** * The a source @@ -727,6 +730,59 @@ public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveW } } + /** + * Set the index source for the encoder. When this source rises, the encoder count automatically resets. + * + * @param channel A DIO channel to set as the encoder index + * @param type The state that will cause the encoder to reset + */ + public void setIndexSource(int channel, IndexingType type) { + ByteBuffer status = ByteBuffer.allocateDirect(4); + status.order(ByteOrder.LITTLE_ENDIAN); + + boolean activeHigh = (type == IndexingType.kResetWhileHigh) || (type == IndexingType.kResetOnRisingEdge); + boolean edgeSensitive = (type == IndexingType.kResetOnFallingEdge) || (type == IndexingType.kResetOnRisingEdge); + + EncoderJNI.setEncoderIndexSource(m_encoder, channel, false, activeHigh, edgeSensitive, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); + } + + /** + * Set the index source for the encoder. When this source is activated, the encoder count automatically resets. + * + * @param channel A DIO channel to set as the encoder index + */ + public void setIndexSource(int channel) { + this.setIndexSource(channel, IndexingType.kResetOnRisingEdge); + } + + /** + * Set the index source for the encoder. When this source rises, the encoder count automatically resets. + * + * @param source A digital source to set as the encoder index + * @param type The state that will cause the encoder to reset + */ + public void setIndexSource(DigitalSource source, IndexingType type) { + ByteBuffer status = ByteBuffer.allocateDirect(4); + status.order(ByteOrder.LITTLE_ENDIAN); + + boolean activeHigh = (type == IndexingType.kResetWhileHigh) || (type == IndexingType.kResetOnRisingEdge); + boolean edgeSensitive = (type == IndexingType.kResetOnFallingEdge) || (type == IndexingType.kResetOnRisingEdge); + + EncoderJNI.setEncoderIndexSource(m_encoder, source.getChannelForRouting(), source.getAnalogTriggerForRouting(), + activeHigh, edgeSensitive, status.asIntBuffer()); + HALUtil.checkStatus(status.asIntBuffer()); + } + + /** + * Set the index source for the encoder. When this source is activated, the encoder count automatically resets. + * + * @param source A digital source to set as the encoder index + */ + public void setIndexSource(DigitalSource source) { + this.setIndexSource(source, IndexingType.kResetOnRisingEdge); + } + /* * Live Window code, only does anything if live window is activated. */ diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/hal/EncoderJNI.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/hal/EncoderJNI.java index be23668ce0..1c59301f20 100644 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/hal/EncoderJNI.java +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/hal/EncoderJNI.java @@ -15,4 +15,5 @@ public class EncoderJNI extends JNIWrapper { public static native void setEncoderReverseDirection(ByteBuffer encoder_pointer, byte reverseDirection, IntBuffer status); public static native void setEncoderSamplesToAverage(ByteBuffer encoder_pointer, int samplesToAverage, IntBuffer status); public static native int getEncoderSamplesToAverage(ByteBuffer encoder_pointer, IntBuffer status); + public static native void setEncoderIndexSource(ByteBuffer digital_port, int pin, boolean analogTrigger, boolean activeHigh, boolean edgeSensitive, IntBuffer status); } diff --git a/wpilibj/wpilibJavaJNI/lib/EncoderJNI.cpp b/wpilibj/wpilibJavaJNI/lib/EncoderJNI.cpp index 05c2b073ad..773bf0882a 100644 --- a/wpilibj/wpilibJavaJNI/lib/EncoderJNI.cpp +++ b/wpilibj/wpilibJavaJNI/lib/EncoderJNI.cpp @@ -7,7 +7,7 @@ #include "HAL/Digital.hpp" // set the logging level -TLogLevel encoderJNILogLevel = logWARNING; +TLogLevel encoderJNILogLevel = logDEBUG; #define ENCODERJNI_LOG(level) \ if (level > encoderJNILogLevel) ; \ @@ -223,3 +223,24 @@ JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_hal_EncoderJNI_getEncoderSampl ENCODERJNI_LOG(logDEBUG) << "getEncoderSamplesToAverageResult = " << returnValue; return returnValue; } + +/* + * Class: edu_wpi_first_wpilibj_hal_EncoderJNI + * Method: setEncoderIndexSource + * Signature: (Ljava/nio/ByteBuffer;IZZZLjava/nio/IntBuffer;)V + */ +JNIEXPORT void JNICALL Java_edu_wpi_first_wpilibj_hal_EncoderJNI_setEncoderIndexSource + (JNIEnv * env, jclass, jobject id, jint pin, jboolean analogTrigger, jboolean activeHigh, jboolean edgeSensitive, jobject status) +{ + ENCODERJNI_LOG(logDEBUG) << "Calling ENCODERJNI setEncoderIndexSource"; + void ** javaId = (void**)env->GetDirectBufferAddress(id); + ENCODERJNI_LOG(logDEBUG) << "Encoder Ptr = " << *javaId; + jint * statusPtr = (jint*)env->GetDirectBufferAddress(status); + ENCODERJNI_LOG(logDEBUG) << "Pin = " << pin; + ENCODERJNI_LOG(logDEBUG) << "Analog Trigger = " << (analogTrigger?"true":"false"); + ENCODERJNI_LOG(logDEBUG) << "Active High = " << (activeHigh?"true":"false"); + ENCODERJNI_LOG(logDEBUG) << "Edge Sensitive = " << (edgeSensitive?"true":"false"); + ENCODERJNI_LOG(logDEBUG) << "Status Ptr = " << statusPtr; + setEncoderIndexSource(*javaId, pin, analogTrigger, activeHigh, edgeSensitive, statusPtr); + ENCODERJNI_LOG(logDEBUG) << "Status = " << *statusPtr; +}