Skip to content

Commit 8535f5c

Browse files
authored
Merge pull request #22 from RugiSerl/master
Added gyroscope support
2 parents 27decb0 + 95687eb commit 8535f5c

File tree

4 files changed

+179
-29
lines changed

4 files changed

+179
-29
lines changed

app/src/main/cpp/deps/raymob/features.c

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,36 @@ void Vibrate(float sec)
4141
}
4242
}
4343

44-
/* ACCELEROMETER */
4544

46-
void StartAccelerometerListening(void)
45+
46+
void StartSensorListening(void)
4747
{
4848
jobject featuresInstance = GetFeaturesInstance();
4949

5050
if (featuresInstance != NULL)
5151
{
5252
JNIEnv* env = AttachCurrentThread();
5353
jclass featuresClass = (*env)->GetObjectClass(env, featuresInstance);
54-
jmethodID method = (*env)->GetMethodID(env, featuresClass, "startAccelerometerListening", "()V");
54+
jmethodID method = (*env)->GetMethodID(env, featuresClass, "startSensorListening", "()V");
5555
(*env)->CallVoidMethod(env, featuresInstance, method);
5656
DetachCurrentThread();
5757
}
5858
}
5959

60-
void StopAccelerometerListening(void)
60+
void StopSensorListening(void)
6161
{
6262
jobject featuresInstance = GetFeaturesInstance();
6363

6464
if (featuresInstance != NULL)
6565
{
6666
JNIEnv* env = AttachCurrentThread();
6767
jclass featuresClass = (*env)->GetObjectClass(env, featuresInstance);
68-
jmethodID method = (*env)->GetMethodID(env, featuresClass, "stopAccelerometerListening", "()V");
68+
jmethodID method = (*env)->GetMethodID(env, featuresClass, "stopSensorListening", "()V");
6969
(*env)->CallVoidMethod(env, featuresInstance, method);
7070
DetachCurrentThread();
7171
}
7272
}
73-
73+
/* ACCELEROMETER */
7474
Vector3 GetAccelerometerAxis(void)
7575
{
7676
jobject featuresInstance = GetFeaturesInstance();
@@ -153,6 +153,92 @@ float GetAccelerometerZ(void)
153153
return 0;
154154
}
155155

156+
/* GYROSCOPE */
157+
158+
Vector3 GetGyroscopeAxis(void)
159+
{
160+
jobject featuresInstance = GetFeaturesInstance();
161+
162+
if (featuresInstance != NULL)
163+
{
164+
JNIEnv* env = AttachCurrentThread();
165+
166+
jclass featuresClass = (*env)->GetObjectClass(env, featuresInstance);
167+
168+
jmethodID methodX = (*env)->GetMethodID(env, featuresClass, "getGyroscopeX", "()F");
169+
jmethodID methodY = (*env)->GetMethodID(env, featuresClass, "getGyroscopeY", "()F");
170+
jmethodID methodZ = (*env)->GetMethodID(env, featuresClass, "getGyroscopeZ", "()F");
171+
172+
Vector3 value = {
173+
(*env)->CallFloatMethod(env, featuresInstance, methodX),
174+
(*env)->CallFloatMethod(env, featuresInstance, methodY),
175+
(*env)->CallFloatMethod(env, featuresInstance, methodZ)
176+
};
177+
178+
DetachCurrentThread();
179+
180+
return value;
181+
}
182+
183+
return (Vector3) {0};
184+
}
185+
186+
float GetGyroscopeX(void)
187+
{
188+
jobject featuresInstance = GetFeaturesInstance();
189+
190+
if (featuresInstance != NULL)
191+
{
192+
JNIEnv* env = AttachCurrentThread();
193+
jclass featuresClass = (*env)->GetObjectClass(env, featuresInstance);
194+
jmethodID method = (*env)->GetMethodID(env, featuresClass, "getGyroscopeX", "()F");
195+
float value = (*env)->CallFloatMethod(env, featuresInstance, method);
196+
DetachCurrentThread();
197+
198+
return value;
199+
}
200+
201+
return 0;
202+
}
203+
204+
float GetGyroscopeY(void)
205+
{
206+
jobject featuresInstance = GetFeaturesInstance();
207+
208+
if (featuresInstance != NULL)
209+
{
210+
JNIEnv* env = AttachCurrentThread();
211+
jclass featuresClass = (*env)->GetObjectClass(env, featuresInstance);
212+
jmethodID method = (*env)->GetMethodID(env, featuresClass, "getGyroscopeY", "()F");
213+
float value = (*env)->CallFloatMethod(env, featuresInstance, method);
214+
DetachCurrentThread();
215+
216+
return value;
217+
}
218+
219+
return 0;
220+
}
221+
222+
float GetGyroscopeZ(void)
223+
{
224+
jobject featuresInstance = GetFeaturesInstance();
225+
226+
if (featuresInstance != NULL)
227+
{
228+
JNIEnv* env = AttachCurrentThread();
229+
jclass featuresClass = (*env)->GetObjectClass(env, featuresInstance);
230+
jmethodID method = (*env)->GetMethodID(env, featuresClass, "getGyroscopeZ", "()F");
231+
float value = (*env)->CallFloatMethod(env, featuresInstance, method);
232+
DetachCurrentThread();
233+
234+
return value;
235+
}
236+
237+
return 0;
238+
}
239+
240+
241+
156242
/* SOFT KEYBOARD */
157243

158244
void ShowSoftKeyboard(void)

app/src/main/cpp/deps/raymob/raymob.h

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ RMBAPI void Vibrate(float sec);
120120
/**
121121
* @brief Starts listening to accelerometer sensor data.
122122
*/
123-
RMBAPI void StartAccelerometerListening(void);
123+
RMBAPI void StartSensorListening(void);
124124

125125
/**
126126
* @brief Stops listening to accelerometer sensor data.
127127
*/
128-
RMBAPI void StopAccelerometerListening(void);
128+
RMBAPI void StopSensorListening(void);
129129

130130
/**
131131
* @brief Returns the current accelerometer axis vector.
@@ -155,6 +155,34 @@ RMBAPI float GetAccelerometerY(void);
155155
*/
156156
RMBAPI float GetAccelerometerZ(void);
157157

158+
/**
159+
* @brief Returns the current gyroscope axis vector.
160+
*
161+
* @return Current gyroscope axis vector.
162+
*/
163+
RMBAPI Vector3 GetGyroscopeAxis(void);
164+
165+
/**
166+
* @brief Returns the current value of the X-axis gyroscope.
167+
*
168+
* @return Current value of the X-axis gyroscope.
169+
*/
170+
RMBAPI float GetGyroscopeX(void);
171+
172+
/**
173+
* @brief Returns the current value of the Y-axis gyroscope.
174+
*
175+
* @return Current value of the Y-axis gyroscope.
176+
*/
177+
RMBAPI float GetGyroscopeY(void);
178+
179+
/**
180+
* @brief Returns the current value of the Z-axis gyroscope.
181+
*
182+
* @return Current value of the Z-axis gyroscope.
183+
*/
184+
RMBAPI float GetGyroscopeZ(void);
185+
158186
/**
159187
* @brief Displays the soft keyboard on the screen.
160188
*/

app/src/main/java/com/raylib/features/Accelerometer.java renamed to app/src/main/java/com/raylib/features/Sensor.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,30 @@
2525
package com.raylib.features;
2626

2727
import android.content.Context;
28-
import android.hardware.Sensor;
2928
import android.hardware.SensorEvent;
3029
import android.hardware.SensorManager;
3130

32-
public class Accelerometer implements android.hardware.SensorEventListener {
31+
public class Sensor implements android.hardware.SensorEventListener {
3332

3433
private SensorManager sensorManager;
35-
private Sensor accelerometer;
34+
private android.hardware.Sensor accelerometer;
35+
private android.hardware.Sensor gyroscope;
3636
private float[] accelerometerValues = new float[3]; // To store the axis values (x, y, z)
37+
private float[] gyroscopeValues = new float[3]; // To store the axis values (x, y, z)
3738

38-
public Accelerometer(Context context) {
39+
public Sensor(Context context) {
3940
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
4041
if (sensorManager != null) {
41-
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
42+
accelerometer = sensorManager.getDefaultSensor(android.hardware.Sensor.TYPE_ACCELEROMETER);
43+
gyroscope = sensorManager.getDefaultSensor(android.hardware.Sensor.TYPE_GYROSCOPE);
4244
}
4345
}
4446

4547
// Method to start listening to the accelerometer
4648
public void startListening() {
4749
if (accelerometer != null) {
48-
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
50+
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
51+
sensorManager.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_GAME);
4952
}
5053
}
5154

@@ -55,31 +58,50 @@ public void stopListening() {
5558
}
5659

5760
// Method to get the value of the X-axis
58-
public float getX() {
61+
public float getAccelerometerX() {
5962
return accelerometerValues[0];
6063
}
6164

6265
// Method to get the value of the Y-axis
63-
public float getY() {
66+
public float getAccelerometerY() {
6467
return accelerometerValues[1];
6568
}
6669

6770
// Method to get the value of the Z-axis
68-
public float getZ() {
71+
public float getAccelerometerZ() {
6972
return accelerometerValues[2];
7073
}
7174

75+
// Method to get the value of the X-axis
76+
public float getGyroscopeX() {
77+
return gyroscopeValues[0];
78+
}
79+
80+
// Method to get the value of the Y-axis
81+
public float getGyroscopeY() {
82+
return gyroscopeValues[1];
83+
}
84+
85+
// Method to get the value of the Z-axis
86+
public float getGyroscopeZ() {
87+
return gyroscopeValues[2];
88+
}
89+
7290
@Override
7391
public void onSensorChanged(SensorEvent event) {
74-
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
92+
if (event.sensor.getType() == android.hardware.Sensor.TYPE_ACCELEROMETER) {
7593
accelerometerValues[0] = event.values[0]; // X-axis
7694
accelerometerValues[1] = event.values[1]; // Y-axis
7795
accelerometerValues[2] = event.values[2]; // Z-axis
96+
} else if (event.sensor.getType() == android.hardware.Sensor.TYPE_GYROSCOPE) {
97+
gyroscopeValues[0] = event.values[0]; // X-axis
98+
gyroscopeValues[1] = event.values[1]; // Y-axis
99+
gyroscopeValues[2] = event.values[2]; // Z-axis
78100
}
79101
}
80102

81103
@Override
82-
public void onAccuracyChanged(Sensor sensor, int accuracy) {
104+
public void onAccuracyChanged(android.hardware.Sensor sensor, int accuracy) {
83105
// Do nothing in case of sensor accuracy change
84106
}
85107
}

app/src/main/java/com/raylib/raymob/Features.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ public class Features {
3333
/* FEATURES INSTANCES */
3434

3535
private final Optional<Vibration> vibrator;
36-
private final Optional<Accelerometer> accelerometer;
36+
private final Optional<Sensor> sensor;
3737
private final Optional<SoftKeyboard> softKeyboard;
3838
private final Optional<DisplayManager> display;
3939

4040
/* CONSTRUCTOR */
4141

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

55-
/* ACCELEROMETER */
55+
/* SENSOR */
5656

57-
public void startAccelerometerListening() {
58-
accelerometer.ifPresent(Accelerometer::startListening);
57+
public void startSensorListening() {
58+
sensor.ifPresent(Sensor::startListening);
5959
}
6060

61-
public void stopAccelerometerListening() {
62-
accelerometer.ifPresent(Accelerometer::stopListening);
61+
public void stopSensorListening() {
62+
sensor.ifPresent(Sensor::stopListening);
6363
}
6464

65+
/* ACCELEROMETER */
6566
public float getAccelerometerX() {
66-
return accelerometer.map(Accelerometer::getX).orElse(0f);
67+
return sensor.map(Sensor::getAccelerometerX).orElse(0f);
6768
}
6869

6970
public float getAccelerometerY() {
70-
return accelerometer.map(Accelerometer::getY).orElse(0f);
71+
return sensor.map(Sensor::getAccelerometerY).orElse(0f);
7172
}
7273

7374
public float getAccelerometerZ() {
74-
return accelerometer.map(Accelerometer::getZ).orElse(0f);
75+
return sensor.map(Sensor::getAccelerometerZ).orElse(0f);
76+
}
77+
78+
/* GYROSCOPE */
79+
public float getGyroscopeX() {
80+
return sensor.map(Sensor::getGyroscopeX).orElse(0f);
81+
}
82+
83+
public float getGyroscopeY() {
84+
return sensor.map(Sensor::getGyroscopeY).orElse(0f);
85+
}
86+
87+
public float getGyroscopeZ() {
88+
return sensor.map(Sensor::getGyroscopeZ).orElse(0f);
7589
}
7690

7791
/* SOFT KEYBOARD */

0 commit comments

Comments
 (0)