Files
lockstep/app/src/main/cpp/jni_stepdetector.cpp

52 lines
1.6 KiB
C++
Raw Normal View History

2026-03-04 01:31:35 +01:00
//
// Created by david on 03.03.2026.
//
#include <jni.h>
2026-05-19 22:58:28 +02:00
#include "step_detector.h"
2026-03-04 01:31:35 +01:00
#include <new>
#include <vector>
extern "C" {
jint throwIllegalArgumentException(JNIEnv *env, const char *message)
{
jclass exClass;
const char *className = "java/lang/IllegalArgumentException";
exClass = env->FindClass(className);
if (exClass == nullptr) {
return -1;
}
return env->ThrowNew(exClass, message);
}
JNIEXPORT jlong JNICALL
Java_at_lockstep_filter_StepDetector_native_1create(
JNIEnv *env,
jclass /*unused*/, jlong engineHandle) {
auto *listener = reinterpret_cast<StepListener *>(engineHandle);
// We use std::nothrow so `new` returns a nullptr if the engine creation fails
2026-05-19 22:58:28 +02:00
auto *detector = new(std::nothrow) StepDetector(60.0 /* FPS */, listener); // TODO
2026-03-04 01:31:35 +01:00
return reinterpret_cast<jlong>(detector);
}
JNIEXPORT void JNICALL
Java_at_lockstep_filter_StepDetector_native_1delete(
JNIEnv *env,
jclass /*unused*/, jlong handle) {
delete reinterpret_cast<StepDetector *>(handle);
}
JNIEXPORT void JNICALL
Java_at_lockstep_filter_StepDetector_native_1filter(
JNIEnv *env,
jclass /*unused*/, jlong handle, jlong timestamp, jfloatArray values) {
if(values == nullptr) throwIllegalArgumentException(env, "values == null");
float* nativeValues = (float *)env->GetFloatArrayElements(values, 0);
jsize length = env->GetArrayLength(values);
std::vector<float> vecValues(nativeValues, nativeValues + length);
auto *detector = reinterpret_cast<StepDetector *>(handle);
2026-05-20 00:34:19 +02:00
detector->filter((double) timestamp, vecValues);
2026-03-04 01:31:35 +01:00
}
}