Permalink
Browse files

Added movement detection threshold as a parameter and some presets so…

… you can detect when the user really means to shake
  • Loading branch information...
1 parent 1f7e965 commit 2bb07446312d24dc9567527d1c3f6b60a0920d1c @gmilette gmilette committed Mar 21, 2013
@@ -28,7 +28,10 @@
public class AccelerationEventListener implements SensorEventListener
{
private static final String TAG = "AccelerationEventListener";
- private static final int THRESHHOLD = 2;
+
+ public static final int THRESHOLD_HIGH = 10;
+ public static final int THRESHOLD_LOW = 2;
+ private int threshold;
private static final float ALPHA = 0.8f;
private static final int HIGH_PASS_MINIMUM = 10;
@@ -37,14 +40,21 @@
private boolean useHighPassFilter;
private MovementDetectionListener callback;
-
+
+ public AccelerationEventListener(boolean useHighPassFilter,
+ MovementDetectionListener callback)
+ {
+ this(useHighPassFilter, callback, THRESHOLD_LOW);
+ }
+
public AccelerationEventListener(boolean useHighPassFilter,
- MovementDetectionListener callback)
+ MovementDetectionListener callback, int threshold)
{
this.useHighPassFilter = useHighPassFilter;
gravity = new float[3];
highPassCount = 0;
this.callback = callback;
+ this.threshold = threshold;
}
@Override
@@ -70,9 +80,9 @@ public void onSensorChanged(SensorEvent event)
// A "movement" is only triggered of the total acceleration is
// above a threshold
- if (acceleration > THRESHHOLD)
+ if (acceleration > threshold)
{
- Log.i(TAG, "Movement detected");
+ Log.i(TAG, "Movement detected:" + acceleration);
callback.movementDetected(true);
}
}
@@ -109,6 +119,7 @@ public void onSensorChanged(SensorEvent event)
*/
public void stop()
{
+
}
/**
@@ -31,28 +31,36 @@
private static final String TAG = "MovementDetector";
private static final int RATE = SensorManager.SENSOR_DELAY_NORMAL;
- private static final int TTS_STREAM = AudioManager.STREAM_NOTIFICATION;
private SensorManager sensorManager;
private boolean readingAccelerationData;
private AccelerationEventListener sensorListener;
private boolean useHighPassFilter;
private boolean useAccelerometer;
+ private int threshold;
public MovementDetector(Context context)
{
this(context, false);
}
-
+
+ public MovementDetector(Context context, boolean useAccelerometer)
+ {
+ this(context, false, useAccelerometer, AccelerationEventListener.THRESHOLD_LOW);
+ }
+
/**
* @param useAccelerometer otherwise use linear acceleration
+ * @param threshold from {@link AccelerationEventListener#THRESHOLD_HIGH}
*/
- public MovementDetector(Context context, boolean useAccelerometer)
+ public MovementDetector(Context context, boolean useHighPassFilter, boolean useAccelerometer, int threshold)
{
sensorManager =
(SensorManager) context
.getSystemService(Context.SENSOR_SERVICE);
this.useAccelerometer = useAccelerometer;
+ this.useHighPassFilter = useHighPassFilter;
+ this.threshold = threshold;
}
public void startReadingAccelerationData(MovementDetectionListener resultCallback)
@@ -67,15 +75,15 @@ public void startReadingAccelerationData(MovementDetectionListener resultCallbac
if (useAccelerometer)
{
sensorListener =
- new AccelerationEventListener(useHighPassFilter, resultCallback);
+ new AccelerationEventListener(useHighPassFilter, resultCallback, threshold);
sensorManager.registerListener(sensorListener,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
RATE);
}
else
{
sensorListener =
- new AccelerationEventListener(useHighPassFilter, resultCallback);
+ new AccelerationEventListener(useHighPassFilter, resultCallback, threshold);
sensorManager.registerListener(sensorListener,
sensorManager
.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),

0 comments on commit 2bb0744

Please sign in to comment.