diff --git a/fsensor/build.gradle b/fsensor/build.gradle index b5c70f2..1c91ccd 100644 --- a/fsensor/build.gradle +++ b/fsensor/build.gradle @@ -15,7 +15,7 @@ android { minSdkVersion 14 targetSdkVersion 29 versionCode 9 - versionName "2.0" + versionName "2.2" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/fsensor/src/main/java/com/kircherelectronics/fsensor/sensor/acceleration/AccelerationSensor.java b/fsensor/src/main/java/com/kircherelectronics/fsensor/sensor/acceleration/AccelerationSensor.java index 7381950..b3152e4 100644 --- a/fsensor/src/main/java/com/kircherelectronics/fsensor/sensor/acceleration/AccelerationSensor.java +++ b/fsensor/src/main/java/com/kircherelectronics/fsensor/sensor/acceleration/AccelerationSensor.java @@ -40,6 +40,8 @@ public class AccelerationSensor implements FSensor { private final SensorSubject sensorSubject; + private int sensorType = Sensor.TYPE_ACCELEROMETER; + public AccelerationSensor(Context context) { this.sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); this.listener = new SimpleSensorListener(); @@ -69,6 +71,18 @@ public void unregister(SensorSubject.SensorObserver sensorObserver) { sensorSubject.unregister(sensorObserver); } + /** + * Set the gyroscope sensor type. + * @param sensorType must be Sensor.TYPE_GYROSCOPE or Sensor.TYPE_GYROSCOPE_UNCALIBRATED + */ + public void setSensorType(int sensorType) { + if(sensorType != Sensor.TYPE_ACCELEROMETER && sensorType != Sensor.TYPE_ACCELEROMETER_UNCALIBRATED && sensorType != Sensor.TYPE_LINEAR_ACCELERATION) { + throw new IllegalStateException("Sensor Type must be Sensor.TYPE_ACCELEROMETER or Sensor.TYPE_ACCELEROMETER_UNCALIBRATED or sensorType != Sensor.TYPE_LINEAR_ACCELERATION"); + } + + this.sensorType = sensorType; + } + /** * Set the sensor frequency. * @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) { private void registerSensors(int sensorDelay) { // Register for sensor updates. sensorManager.registerListener(listener, sensorManager - .getDefaultSensor(Sensor.TYPE_ACCELEROMETER), + .getDefaultSensor(sensorType), sensorDelay); } @@ -130,7 +144,7 @@ private class SimpleSensorListener implements SensorEventListener { @Override public void onSensorChanged(SensorEvent event) { - if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { + if (event.sensor.getType() == sensorType) { processAcceleration(event.values); setOutput(acceleration); diff --git a/fsensor/src/main/java/com/kircherelectronics/fsensor/util/gravity/GravityUtil.java b/fsensor/src/main/java/com/kircherelectronics/fsensor/util/gravity/GravityUtil.java index 38665dc..13f1e59 100644 --- a/fsensor/src/main/java/com/kircherelectronics/fsensor/util/gravity/GravityUtil.java +++ b/fsensor/src/main/java/com/kircherelectronics/fsensor/util/gravity/GravityUtil.java @@ -40,21 +40,24 @@ public static float[] getGravityFromOrientation(float[] orientation) { float[] components = new float[3]; + float pitch = orientation[1]; + float roll = orientation[2]; + // Find the gravity component of the X-axis // = g*-cos(pitch)*sin(roll); components[0] = (float) -(gravity[0] - * -Math.cos(orientation[0]) * Math - .sin(orientation[1])); + * -Math.cos(pitch) * Math + .sin(roll)); // Find the gravity component of the Y-axis // = g*-sin(pitch); components[1] = (float) (gravity[1] * -Math - .sin(orientation[0])); + .sin(pitch)); // Find the gravity component of the Z-axis // = g*cos(pitch)*cos(roll); components[2] = (float) (gravity[2] - * Math.cos(orientation[0]) * Math.cos(orientation[1])); + * Math.cos(pitch) * Math.cos(roll)); return components; }