Skip to content

Commit 03ac30e

Browse files
authored
Merge pull request #36 from KalebKE/v2.2
v2.2
2 parents 79f326f + e89ffa2 commit 03ac30e

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

fsensor/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ android {
1515
minSdkVersion 14
1616
targetSdkVersion 29
1717
versionCode 9
18-
versionName "2.0"
18+
versionName "2.2"
1919

2020
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
2121

fsensor/src/main/java/com/kircherelectronics/fsensor/sensor/acceleration/AccelerationSensor.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public class AccelerationSensor implements FSensor {
4040

4141
private final SensorSubject sensorSubject;
4242

43+
private int sensorType = Sensor.TYPE_ACCELEROMETER;
44+
4345
public AccelerationSensor(Context context) {
4446
this.sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
4547
this.listener = new SimpleSensorListener();
@@ -69,6 +71,18 @@ public void unregister(SensorSubject.SensorObserver sensorObserver) {
6971
sensorSubject.unregister(sensorObserver);
7072
}
7173

74+
/**
75+
* Set the gyroscope sensor type.
76+
* @param sensorType must be Sensor.TYPE_GYROSCOPE or Sensor.TYPE_GYROSCOPE_UNCALIBRATED
77+
*/
78+
public void setSensorType(int sensorType) {
79+
if(sensorType != Sensor.TYPE_ACCELEROMETER && sensorType != Sensor.TYPE_ACCELEROMETER_UNCALIBRATED && sensorType != Sensor.TYPE_LINEAR_ACCELERATION) {
80+
throw new IllegalStateException("Sensor Type must be Sensor.TYPE_ACCELEROMETER or Sensor.TYPE_ACCELEROMETER_UNCALIBRATED or sensorType != Sensor.TYPE_LINEAR_ACCELERATION");
81+
}
82+
83+
this.sensorType = sensorType;
84+
}
85+
7286
/**
7387
* Set the sensor frequency.
7488
* @param sensorDelay Must be SensorManager.SENSOR_DELAY_FASTEST, SensorManager.SENSOR_DELAY_GAME, SensorManager.SENSOR_DELAY_NORMAL or SensorManager.SENSOR_DELAY_UI
@@ -112,7 +126,7 @@ private void processAcceleration(float[] acceleration) {
112126
private void registerSensors(int sensorDelay) {
113127
// Register for sensor updates.
114128
sensorManager.registerListener(listener, sensorManager
115-
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
129+
.getDefaultSensor(sensorType),
116130
sensorDelay);
117131
}
118132

@@ -130,7 +144,7 @@ private class SimpleSensorListener implements SensorEventListener {
130144

131145
@Override
132146
public void onSensorChanged(SensorEvent event) {
133-
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
147+
if (event.sensor.getType() == sensorType) {
134148

135149
processAcceleration(event.values);
136150
setOutput(acceleration);

fsensor/src/main/java/com/kircherelectronics/fsensor/util/gravity/GravityUtil.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,24 @@ public static float[] getGravityFromOrientation(float[] orientation) {
4040

4141
float[] components = new float[3];
4242

43+
float pitch = orientation[1];
44+
float roll = orientation[2];
45+
4346
// Find the gravity component of the X-axis
4447
// = g*-cos(pitch)*sin(roll);
4548
components[0] = (float) -(gravity[0]
46-
* -Math.cos(orientation[0]) * Math
47-
.sin(orientation[1]));
49+
* -Math.cos(pitch) * Math
50+
.sin(roll));
4851

4952
// Find the gravity component of the Y-axis
5053
// = g*-sin(pitch);
5154
components[1] = (float) (gravity[1] * -Math
52-
.sin(orientation[0]));
55+
.sin(pitch));
5356

5457
// Find the gravity component of the Z-axis
5558
// = g*cos(pitch)*cos(roll);
5659
components[2] = (float) (gravity[2]
57-
* Math.cos(orientation[0]) * Math.cos(orientation[1]));
60+
* Math.cos(pitch) * Math.cos(roll));
5861

5962
return components;
6063
}

0 commit comments

Comments
 (0)