From 8e46b0a4ca39b9e6ca90b8b7d0bbe35d54c2cbdb Mon Sep 17 00:00:00 2001 From: Alexander Pacha Date: Sun, 15 Dec 2013 16:24:59 +0100 Subject: [PATCH] Improved interface-design to avoid unnecessary code-duplication when implementing new providers. --- .../OrientationProvider.java | 77 +++++++++++++++++-- .../RotationVectorProvider.java | 66 +--------------- 2 files changed, 74 insertions(+), 69 deletions(-) diff --git a/src/org/hitlabnz/sensor_fusion_demo/orientationProvider/OrientationProvider.java b/src/org/hitlabnz/sensor_fusion_demo/orientationProvider/OrientationProvider.java index cb9ccaa..88a85a6 100644 --- a/src/org/hitlabnz/sensor_fusion_demo/orientationProvider/OrientationProvider.java +++ b/src/org/hitlabnz/sensor_fusion_demo/orientationProvider/OrientationProvider.java @@ -3,9 +3,16 @@ */ package org.hitlabnz.sensor_fusion_demo.orientationProvider; +import java.util.ArrayList; +import java.util.List; + import org.hitlabnz.sensor_fusion_demo.representation.Matrixf4x4; import org.hitlabnz.sensor_fusion_demo.representation.Quaternion; +import android.hardware.Sensor; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; + /** * Classes implementing this interface provide an orientation of the device either by directly accessing hardware, using * Android sensor fusion or fusing sensors itself. @@ -15,26 +22,82 @@ import org.hitlabnz.sensor_fusion_demo.representation.Quaternion; * @author Alexander Pacha * */ -public interface OrientationProvider { +public abstract class OrientationProvider implements SensorEventListener { /** - * @return Returns the current rotation of the device in the rotation matrix format (4x4 matrix) + * The list of sensors used by this provider */ - public Matrixf4x4 getRotationMatrix(); + protected List sensorList = new ArrayList(); /** - * @return Returns the current rotation of the device in the quaternion format (vector4f) + * The matrix that holds the current rotation */ - public Quaternion getQuaternion(); + protected final Matrixf4x4 currentOrientationRotationMatrix; + + /** + * The quaternion that holds the current rotation + */ + protected final Quaternion currentOrientationQuaternion; + + /** + * The sensor manager for accessing android sensors + */ + protected SensorManager sensorManager; + + /** + * Initialises a new OrientationProvider + * + * @param sensorManager The android sensor manager + */ + public OrientationProvider(SensorManager sensorManager) { + this.sensorManager = sensorManager; + + // Initialise with identity + currentOrientationRotationMatrix = new Matrixf4x4(); + + // Initialise with identity + currentOrientationQuaternion = new Quaternion(); + } /** * Starts the sensor fusion (e.g. when resuming the activity) */ - public void start(); + public void start() { + // enable our sensor when the activity is resumed, ask for + // 10 ms updates. + for (Sensor sensor : sensorList) { + // enable our sensors when the activity is resumed, ask for + // 20 ms updates (Sensor_delay_game) + sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME); + } + } /** * Stops the sensor fusion (e.g. when pausing/suspending the activity) */ - public void stop(); + public void stop() { + // make sure to turn our sensors off when the activity is paused + for (Sensor sensor : sensorList) { + sensorManager.unregisterListener(this, sensor); + } + } + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + // Not doing anything + } + + /** + * @return Returns the current rotation of the device in the rotation matrix format (4x4 matrix) + */ + public Matrixf4x4 getRotationMatrix() { + return currentOrientationRotationMatrix; + } + + /** + * @return Returns the current rotation of the device in the quaternion format (vector4f) + */ + public Quaternion getQuaternion() { + return currentOrientationQuaternion; + } } diff --git a/src/org/hitlabnz/sensor_fusion_demo/orientationProvider/RotationVectorProvider.java b/src/org/hitlabnz/sensor_fusion_demo/orientationProvider/RotationVectorProvider.java index 9d3d4ce..506260e 100644 --- a/src/org/hitlabnz/sensor_fusion_demo/orientationProvider/RotationVectorProvider.java +++ b/src/org/hitlabnz/sensor_fusion_demo/orientationProvider/RotationVectorProvider.java @@ -1,11 +1,7 @@ package org.hitlabnz.sensor_fusion_demo.orientationProvider; -import org.hitlabnz.sensor_fusion_demo.representation.Matrixf4x4; -import org.hitlabnz.sensor_fusion_demo.representation.Quaternion; - import android.hardware.Sensor; import android.hardware.SensorEvent; -import android.hardware.SensorEventListener; import android.hardware.SensorManager; /** @@ -15,27 +11,7 @@ import android.hardware.SensorManager; * @author Alexander Pacha * */ -public class RotationVectorProvider implements SensorEventListener, OrientationProvider { - - /** - * The rotation vector sensor that is being used for this provider to get device orientation - */ - private Sensor rotationVectorSensor; - - /** - * The matrix that holds the current rotation - */ - private final Matrixf4x4 currentOrientationRotationMatrix; - - /** - * The quaternion that holds the current rotation - */ - private final Quaternion currentOrientationQuaternion; - - /** - * The sensor manager for accessing android sensors - */ - private SensorManager sensorManager; +public class RotationVectorProvider extends OrientationProvider { /** * Initialises a new RotationVectorProvider @@ -43,29 +19,10 @@ public class RotationVectorProvider implements SensorEventListener, OrientationP * @param sensorManager The android sensor manager */ public RotationVectorProvider(SensorManager sensorManager) { - this.sensorManager = sensorManager; + super(sensorManager); - // find the rotation-vector sensor - rotationVectorSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR); - - // Initialise with identity - currentOrientationRotationMatrix = new Matrixf4x4(); - - // Initialise with identity - currentOrientationQuaternion = new Quaternion(); - } - - @Override - public void start() { - // enable our sensor when the activity is resumed, ask for - // 10 ms updates. - sensorManager.registerListener(this, rotationVectorSensor, 10000); - } - - @Override - public void stop() { - // make sure to turn our sensor off when the activity is paused - sensorManager.unregisterListener(this); + //The rotation vector sensor that is being used for this provider to get device orientation + sensorList.add(sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR)); } @Override @@ -85,19 +42,4 @@ public class RotationVectorProvider implements SensorEventListener, OrientationP currentOrientationQuaternion.setXYZW(q[1], q[2], q[3], -q[0]); } } - - @Override - public void onAccuracyChanged(Sensor sensor, int accuracy) { - // Not doing anything - } - - @Override - public Matrixf4x4 getRotationMatrix() { - return currentOrientationRotationMatrix; - } - - @Override - public Quaternion getQuaternion() { - return currentOrientationQuaternion; - } }