Improved interface-design to avoid unnecessary code-duplication when implementing new providers.

This commit is contained in:
Alexander Pacha
2013-12-15 16:24:59 +01:00
parent 6324c1ac07
commit 8e46b0a4ca
2 changed files with 74 additions and 69 deletions

View File

@@ -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<Sensor> sensorList = new ArrayList<Sensor>();
/**
* @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;
}
}

View File

@@ -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;
}
}