Files
allwpilib/hal/src/main/native/cpp/jni/CounterJNI.cpp

371 lines
9.6 KiB
C++
Raw Normal View History

/*----------------------------------------------------------------------------*/
/* Copyright (c) 2016-2020 FIRST. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include <jni.h>
2014-01-06 09:27:51 -05:00
2018-05-13 17:09:56 -07:00
#include <cassert>
2014-01-06 09:27:51 -05:00
#include "HALUtil.h"
#include "edu_wpi_first_hal_CounterJNI.h"
#include "hal/Counter.h"
#include "hal/Errors.h"
2014-01-06 09:27:51 -05:00
using namespace hal;
extern "C" {
2014-01-06 09:27:51 -05:00
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: initializeCounter
2018-05-13 17:09:56 -07:00
* Signature: (ILjava/lang/Object;)I
2014-01-06 09:27:51 -05:00
*/
2016-07-01 00:29:08 -07:00
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_CounterJNI_initializeCounter
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint mode, jobject index)
{
jint* indexPtr = reinterpret_cast<jint*>(env->GetDirectBufferAddress(index));
int32_t status = 0;
2018-05-13 17:09:56 -07:00
auto counter = HAL_InitializeCounter(
(HAL_Counter_Mode)mode, reinterpret_cast<int32_t*>(indexPtr), &status);
CheckStatusForceThrow(env, status);
2016-07-01 00:29:08 -07:00
return (jint)counter;
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: freeCounter
2016-07-01 00:29:08 -07:00
* Signature: (I)V
2014-01-06 09:27:51 -05:00
*/
2018-05-13 17:09:56 -07:00
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_freeCounter
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
HAL_FreeCounter((HAL_CounterHandle)id, &status);
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: setCounterAverageSize
2016-07-01 00:29:08 -07:00
* Signature: (II)V
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_setCounterAverageSize
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id, jint value)
{
int32_t status = 0;
HAL_SetCounterAverageSize((HAL_CounterHandle)id, value, &status);
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
* Method: setCounterUpSource
* Signature: (III)V
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_setCounterUpSource
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id, jint digitalSourceHandle,
jint analogTriggerType)
{
int32_t status = 0;
2018-05-13 17:09:56 -07:00
HAL_SetCounterUpSource((HAL_CounterHandle)id, (HAL_Handle)digitalSourceHandle,
(HAL_AnalogTriggerType)analogTriggerType, &status);
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: setCounterUpSourceEdge
2016-07-01 00:29:08 -07:00
* Signature: (IZZ)V
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_setCounterUpSourceEdge
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id, jboolean valueRise, jboolean valueFall)
{
int32_t status = 0;
2018-05-13 17:09:56 -07:00
HAL_SetCounterUpSourceEdge((HAL_CounterHandle)id, valueRise, valueFall,
&status);
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: clearCounterUpSource
2016-07-01 00:29:08 -07:00
* Signature: (I)V
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_clearCounterUpSource
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
HAL_ClearCounterUpSource((HAL_CounterHandle)id, &status);
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
* Method: setCounterDownSource
2018-05-13 17:09:56 -07:00
* Signature: (III)V
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_setCounterDownSource
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id, jint digitalSourceHandle,
jint analogTriggerType)
{
int32_t status = 0;
2018-05-13 17:09:56 -07:00
HAL_SetCounterDownSource((HAL_CounterHandle)id,
(HAL_Handle)digitalSourceHandle,
(HAL_AnalogTriggerType)analogTriggerType, &status);
if (status == PARAMETER_OUT_OF_RANGE) {
ThrowIllegalArgumentException(env,
"Counter only supports DownSource in "
"TwoPulse and ExternalDirection modes.");
return;
}
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: setCounterDownSourceEdge
2016-07-01 00:29:08 -07:00
* Signature: (IZZ)V
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_setCounterDownSourceEdge
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id, jboolean valueRise, jboolean valueFall)
{
int32_t status = 0;
2018-05-13 17:09:56 -07:00
HAL_SetCounterDownSourceEdge((HAL_CounterHandle)id, valueRise, valueFall,
&status);
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: clearCounterDownSource
2016-07-01 00:29:08 -07:00
* Signature: (I)V
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_clearCounterDownSource
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
HAL_ClearCounterDownSource((HAL_CounterHandle)id, &status);
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: setCounterUpDownMode
2016-07-01 00:29:08 -07:00
* Signature: (I)V
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_setCounterUpDownMode
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
HAL_SetCounterUpDownMode((HAL_CounterHandle)id, &status);
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: setCounterExternalDirectionMode
2016-07-01 00:29:08 -07:00
* Signature: (I)V
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_setCounterExternalDirectionMode
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
HAL_SetCounterExternalDirectionMode((HAL_CounterHandle)id, &status);
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: setCounterSemiPeriodMode
2016-07-01 00:29:08 -07:00
* Signature: (IZ)V
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_setCounterSemiPeriodMode
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id, jboolean value)
{
int32_t status = 0;
HAL_SetCounterSemiPeriodMode((HAL_CounterHandle)id, value, &status);
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: setCounterPulseLengthMode
2016-07-01 00:29:08 -07:00
* Signature: (ID)V
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_setCounterPulseLengthMode
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id, jdouble value)
{
int32_t status = 0;
HAL_SetCounterPulseLengthMode((HAL_CounterHandle)id, value, &status);
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: getCounterSamplesToAverage
2016-07-01 00:29:08 -07:00
* Signature: (I)I
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_CounterJNI_getCounterSamplesToAverage
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
2018-05-13 17:09:56 -07:00
jint returnValue =
HAL_GetCounterSamplesToAverage((HAL_CounterHandle)id, &status);
CheckStatus(env, status);
return returnValue;
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: setCounterSamplesToAverage
2016-07-01 00:29:08 -07:00
* Signature: (II)V
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_setCounterSamplesToAverage
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id, jint value)
{
int32_t status = 0;
HAL_SetCounterSamplesToAverage((HAL_CounterHandle)id, value, &status);
if (status == PARAMETER_OUT_OF_RANGE) {
ThrowBoundaryException(env, value, 1, 127);
return;
}
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: resetCounter
2016-07-01 00:29:08 -07:00
* Signature: (I)V
2014-01-06 09:27:51 -05:00
*/
2018-05-13 17:09:56 -07:00
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_resetCounter
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
HAL_ResetCounter((HAL_CounterHandle)id, &status);
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: getCounter
2016-07-01 00:29:08 -07:00
* Signature: (I)I
2014-01-06 09:27:51 -05:00
*/
2018-05-13 17:09:56 -07:00
JNIEXPORT jint JNICALL
Java_edu_wpi_first_hal_CounterJNI_getCounter
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
jint returnValue = HAL_GetCounter((HAL_CounterHandle)id, &status);
CheckStatus(env, status);
return returnValue;
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: getCounterPeriod
2016-07-01 00:29:08 -07:00
* Signature: (I)D
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT jdouble JNICALL
Java_edu_wpi_first_hal_CounterJNI_getCounterPeriod
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
jdouble returnValue = HAL_GetCounterPeriod((HAL_CounterHandle)id, &status);
CheckStatus(env, status);
return returnValue;
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: setCounterMaxPeriod
2016-07-01 00:29:08 -07:00
* Signature: (ID)V
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_setCounterMaxPeriod
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id, jdouble value)
{
int32_t status = 0;
HAL_SetCounterMaxPeriod((HAL_CounterHandle)id, value, &status);
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: setCounterUpdateWhenEmpty
2016-07-01 00:29:08 -07:00
* Signature: (IZ)V
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_setCounterUpdateWhenEmpty
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id, jboolean value)
{
int32_t status = 0;
HAL_SetCounterUpdateWhenEmpty((HAL_CounterHandle)id, value, &status);
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: getCounterStopped
2016-07-01 00:29:08 -07:00
* Signature: (I)Z
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT jboolean JNICALL
Java_edu_wpi_first_hal_CounterJNI_getCounterStopped
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
jboolean returnValue = HAL_GetCounterStopped((HAL_CounterHandle)id, &status);
CheckStatus(env, status);
return returnValue;
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: getCounterDirection
2016-07-01 00:29:08 -07:00
* Signature: (I)Z
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT jboolean JNICALL
Java_edu_wpi_first_hal_CounterJNI_getCounterDirection
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id)
{
int32_t status = 0;
2018-05-13 17:09:56 -07:00
jboolean returnValue =
HAL_GetCounterDirection((HAL_CounterHandle)id, &status);
CheckStatus(env, status);
return returnValue;
2014-01-06 09:27:51 -05:00
}
/*
* Class: edu_wpi_first_hal_CounterJNI
2014-01-06 09:27:51 -05:00
* Method: setCounterReverseDirection
2016-07-01 00:29:08 -07:00
* Signature: (IZ)V
2014-01-06 09:27:51 -05:00
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_hal_CounterJNI_setCounterReverseDirection
2018-05-13 17:09:56 -07:00
(JNIEnv* env, jclass, jint id, jboolean value)
{
int32_t status = 0;
HAL_SetCounterReverseDirection((HAL_CounterHandle)id, value, &status);
CheckStatus(env, status);
2014-01-06 09:27:51 -05:00
}
} // extern "C"