Skip to content

Commit

Permalink
Merge pull request #22 from RugiSerl/master
Browse files Browse the repository at this point in the history
Added gyroscope support
  • Loading branch information
Bigfoot71 authored Nov 26, 2024
2 parents 27decb0 + 95687eb commit 8535f5c
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 29 deletions.
98 changes: 92 additions & 6 deletions app/src/main/cpp/deps/raymob/features.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,36 +41,36 @@ void Vibrate(float sec)
}
}

/* ACCELEROMETER */

void StartAccelerometerListening(void)

void StartSensorListening(void)
{
jobject featuresInstance = GetFeaturesInstance();

if (featuresInstance != NULL)
{
JNIEnv* env = AttachCurrentThread();
jclass featuresClass = (*env)->GetObjectClass(env, featuresInstance);
jmethodID method = (*env)->GetMethodID(env, featuresClass, "startAccelerometerListening", "()V");
jmethodID method = (*env)->GetMethodID(env, featuresClass, "startSensorListening", "()V");
(*env)->CallVoidMethod(env, featuresInstance, method);
DetachCurrentThread();
}
}

void StopAccelerometerListening(void)
void StopSensorListening(void)
{
jobject featuresInstance = GetFeaturesInstance();

if (featuresInstance != NULL)
{
JNIEnv* env = AttachCurrentThread();
jclass featuresClass = (*env)->GetObjectClass(env, featuresInstance);
jmethodID method = (*env)->GetMethodID(env, featuresClass, "stopAccelerometerListening", "()V");
jmethodID method = (*env)->GetMethodID(env, featuresClass, "stopSensorListening", "()V");
(*env)->CallVoidMethod(env, featuresInstance, method);
DetachCurrentThread();
}
}

/* ACCELEROMETER */
Vector3 GetAccelerometerAxis(void)
{
jobject featuresInstance = GetFeaturesInstance();
Expand Down Expand Up @@ -153,6 +153,92 @@ float GetAccelerometerZ(void)
return 0;
}

/* GYROSCOPE */

Vector3 GetGyroscopeAxis(void)
{
jobject featuresInstance = GetFeaturesInstance();

if (featuresInstance != NULL)
{
JNIEnv* env = AttachCurrentThread();

jclass featuresClass = (*env)->GetObjectClass(env, featuresInstance);

jmethodID methodX = (*env)->GetMethodID(env, featuresClass, "getGyroscopeX", "()F");
jmethodID methodY = (*env)->GetMethodID(env, featuresClass, "getGyroscopeY", "()F");
jmethodID methodZ = (*env)->GetMethodID(env, featuresClass, "getGyroscopeZ", "()F");

Vector3 value = {
(*env)->CallFloatMethod(env, featuresInstance, methodX),
(*env)->CallFloatMethod(env, featuresInstance, methodY),
(*env)->CallFloatMethod(env, featuresInstance, methodZ)
};

DetachCurrentThread();

return value;
}

return (Vector3) {0};
}

float GetGyroscopeX(void)
{
jobject featuresInstance = GetFeaturesInstance();

if (featuresInstance != NULL)
{
JNIEnv* env = AttachCurrentThread();
jclass featuresClass = (*env)->GetObjectClass(env, featuresInstance);
jmethodID method = (*env)->GetMethodID(env, featuresClass, "getGyroscopeX", "()F");
float value = (*env)->CallFloatMethod(env, featuresInstance, method);
DetachCurrentThread();

return value;
}

return 0;
}

float GetGyroscopeY(void)
{
jobject featuresInstance = GetFeaturesInstance();

if (featuresInstance != NULL)
{
JNIEnv* env = AttachCurrentThread();
jclass featuresClass = (*env)->GetObjectClass(env, featuresInstance);
jmethodID method = (*env)->GetMethodID(env, featuresClass, "getGyroscopeY", "()F");
float value = (*env)->CallFloatMethod(env, featuresInstance, method);
DetachCurrentThread();

return value;
}

return 0;
}

float GetGyroscopeZ(void)
{
jobject featuresInstance = GetFeaturesInstance();

if (featuresInstance != NULL)
{
JNIEnv* env = AttachCurrentThread();
jclass featuresClass = (*env)->GetObjectClass(env, featuresInstance);
jmethodID method = (*env)->GetMethodID(env, featuresClass, "getGyroscopeZ", "()F");
float value = (*env)->CallFloatMethod(env, featuresInstance, method);
DetachCurrentThread();

return value;
}

return 0;
}



/* SOFT KEYBOARD */

void ShowSoftKeyboard(void)
Expand Down
32 changes: 30 additions & 2 deletions app/src/main/cpp/deps/raymob/raymob.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ RMBAPI void Vibrate(float sec);
/**
* @brief Starts listening to accelerometer sensor data.
*/
RMBAPI void StartAccelerometerListening(void);
RMBAPI void StartSensorListening(void);

/**
* @brief Stops listening to accelerometer sensor data.
*/
RMBAPI void StopAccelerometerListening(void);
RMBAPI void StopSensorListening(void);

/**
* @brief Returns the current accelerometer axis vector.
Expand Down Expand Up @@ -155,6 +155,34 @@ RMBAPI float GetAccelerometerY(void);
*/
RMBAPI float GetAccelerometerZ(void);

/**
* @brief Returns the current gyroscope axis vector.
*
* @return Current gyroscope axis vector.
*/
RMBAPI Vector3 GetGyroscopeAxis(void);

/**
* @brief Returns the current value of the X-axis gyroscope.
*
* @return Current value of the X-axis gyroscope.
*/
RMBAPI float GetGyroscopeX(void);

/**
* @brief Returns the current value of the Y-axis gyroscope.
*
* @return Current value of the Y-axis gyroscope.
*/
RMBAPI float GetGyroscopeY(void);

/**
* @brief Returns the current value of the Z-axis gyroscope.
*
* @return Current value of the Z-axis gyroscope.
*/
RMBAPI float GetGyroscopeZ(void);

/**
* @brief Displays the soft keyboard on the screen.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,30 @@
package com.raylib.features;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;

public class Accelerometer implements android.hardware.SensorEventListener {
public class Sensor implements android.hardware.SensorEventListener {

private SensorManager sensorManager;
private Sensor accelerometer;
private android.hardware.Sensor accelerometer;
private android.hardware.Sensor gyroscope;
private float[] accelerometerValues = new float[3]; // To store the axis values (x, y, z)
private float[] gyroscopeValues = new float[3]; // To store the axis values (x, y, z)

public Accelerometer(Context context) {
public Sensor(Context context) {
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
if (sensorManager != null) {
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
accelerometer = sensorManager.getDefaultSensor(android.hardware.Sensor.TYPE_ACCELEROMETER);
gyroscope = sensorManager.getDefaultSensor(android.hardware.Sensor.TYPE_GYROSCOPE);
}
}

// Method to start listening to the accelerometer
public void startListening() {
if (accelerometer != null) {
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
sensorManager.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_GAME);
}
}

Expand All @@ -55,31 +58,50 @@ public void stopListening() {
}

// Method to get the value of the X-axis
public float getX() {
public float getAccelerometerX() {
return accelerometerValues[0];
}

// Method to get the value of the Y-axis
public float getY() {
public float getAccelerometerY() {
return accelerometerValues[1];
}

// Method to get the value of the Z-axis
public float getZ() {
public float getAccelerometerZ() {
return accelerometerValues[2];
}

// Method to get the value of the X-axis
public float getGyroscopeX() {
return gyroscopeValues[0];
}

// Method to get the value of the Y-axis
public float getGyroscopeY() {
return gyroscopeValues[1];
}

// Method to get the value of the Z-axis
public float getGyroscopeZ() {
return gyroscopeValues[2];
}

@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
if (event.sensor.getType() == android.hardware.Sensor.TYPE_ACCELEROMETER) {
accelerometerValues[0] = event.values[0]; // X-axis
accelerometerValues[1] = event.values[1]; // Y-axis
accelerometerValues[2] = event.values[2]; // Z-axis
} else if (event.sensor.getType() == android.hardware.Sensor.TYPE_GYROSCOPE) {
gyroscopeValues[0] = event.values[0]; // X-axis
gyroscopeValues[1] = event.values[1]; // Y-axis
gyroscopeValues[2] = event.values[2]; // Z-axis
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
public void onAccuracyChanged(android.hardware.Sensor sensor, int accuracy) {
// Do nothing in case of sensor accuracy change
}
}
34 changes: 24 additions & 10 deletions app/src/main/java/com/raylib/raymob/Features.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public class Features {
/* FEATURES INSTANCES */

private final Optional<Vibration> vibrator;
private final Optional<Accelerometer> accelerometer;
private final Optional<Sensor> sensor;
private final Optional<SoftKeyboard> softKeyboard;
private final Optional<DisplayManager> display;

/* CONSTRUCTOR */

public Features(Context context) {
vibrator = BuildConfig.FEATURE_VIBRATION ? Optional.of(new Vibration(context)) : Optional.empty();
accelerometer = BuildConfig.FEATURE_ACCELEROMETER ? Optional.of(new Accelerometer(context)) : Optional.empty();
sensor = BuildConfig.FEATURE_ACCELEROMETER ? Optional.of(new Sensor(context)) : Optional.empty();
softKeyboard = BuildConfig.FEATURE_SOFT_KEYBOARD ? Optional.of(new SoftKeyboard(context)) : Optional.empty();
display = BuildConfig.FEATURE_DISPLAY ? Optional.of(new DisplayManager(context)) : Optional.empty();
}
Expand All @@ -52,26 +52,40 @@ public void vibrate(float seconds) {
vibrator.ifPresent(v -> v.vibrate(seconds));
}

/* ACCELEROMETER */
/* SENSOR */

public void startAccelerometerListening() {
accelerometer.ifPresent(Accelerometer::startListening);
public void startSensorListening() {
sensor.ifPresent(Sensor::startListening);
}

public void stopAccelerometerListening() {
accelerometer.ifPresent(Accelerometer::stopListening);
public void stopSensorListening() {
sensor.ifPresent(Sensor::stopListening);
}

/* ACCELEROMETER */
public float getAccelerometerX() {
return accelerometer.map(Accelerometer::getX).orElse(0f);
return sensor.map(Sensor::getAccelerometerX).orElse(0f);
}

public float getAccelerometerY() {
return accelerometer.map(Accelerometer::getY).orElse(0f);
return sensor.map(Sensor::getAccelerometerY).orElse(0f);
}

public float getAccelerometerZ() {
return accelerometer.map(Accelerometer::getZ).orElse(0f);
return sensor.map(Sensor::getAccelerometerZ).orElse(0f);
}

/* GYROSCOPE */
public float getGyroscopeX() {
return sensor.map(Sensor::getGyroscopeX).orElse(0f);
}

public float getGyroscopeY() {
return sensor.map(Sensor::getGyroscopeY).orElse(0f);
}

public float getGyroscopeZ() {
return sensor.map(Sensor::getGyroscopeZ).orElse(0f);
}

/* SOFT KEYBOARD */
Expand Down

0 comments on commit 8535f5c

Please # to comment.