Skip to content
Browse files

added min time between movement detection so you get multiple movemen…

…t callbacks when the user does something like a shake
  • Loading branch information...
1 parent 9028353 commit e1fe0d2dc3fcdef40772556d0658ad2af50f68b6 @gmilette gmilette committed
View
37 library/src/root/gast/speech/movement/AccelerationEventListener.java
@@ -40,21 +40,37 @@
private boolean useHighPassFilter;
private MovementDetectionListener callback;
+
+ private long lastDetectedMovement = Long.MAX_VALUE;
+
+ private final static long DEFAULT_MIN_TIME_BETWEEN_MOVEMENT = 300;
+
+ public final static long DEFAULT_MIN_TIME_BETWEEN_MOVEMENT_DISABLED = -1;
+
+ private long minTimeBetweenMovement = DEFAULT_MIN_TIME_BETWEEN_MOVEMENT_DISABLED;
public AccelerationEventListener(boolean useHighPassFilter,
MovementDetectionListener callback)
{
- this(useHighPassFilter, callback, THRESHOLD_LOW);
+ this(useHighPassFilter, callback, THRESHOLD_LOW, DEFAULT_MIN_TIME_BETWEEN_MOVEMENT_DISABLED);
}
public AccelerationEventListener(boolean useHighPassFilter,
- MovementDetectionListener callback, int threshold)
+ MovementDetectionListener callback, int threshold, long minTimeBetweenMovement)
{
this.useHighPassFilter = useHighPassFilter;
gravity = new float[3];
highPassCount = 0;
this.callback = callback;
this.threshold = threshold;
+ this.minTimeBetweenMovement = minTimeBetweenMovement;
+
+ lastDetectedMovement = System.currentTimeMillis();
+ }
+
+ public void setMinTimeBetweenMovement(long minTimeBetweenMovement)
+ {
+ this.minTimeBetweenMovement = minTimeBetweenMovement;
}
@Override
@@ -83,7 +99,7 @@ public void onSensorChanged(SensorEvent event)
if (acceleration > threshold)
{
Log.i(TAG, "Movement detected:" + acceleration);
- callback.movementDetected(true);
+ possiblyReportMovement();
}
}
}
@@ -113,6 +129,21 @@ public void onSensorChanged(SensorEvent event)
return filteredValues;
}
+
+ private void possiblyReportMovement()
+ {
+ long currentTime = System.currentTimeMillis();
+ long timeDiff = currentTime - lastDetectedMovement;
+ if (timeDiff < minTimeBetweenMovement)
+ {
+ Log.d(TAG, "movement detected too soon " + timeDiff);
+ }
+ else
+ {
+ lastDetectedMovement = currentTime;
+ callback.movementDetected(true);
+ }
+ }
/**
* TODO
View
18 library/src/root/gast/speech/movement/MovementDetector.java
@@ -38,6 +38,7 @@
private boolean useHighPassFilter;
private boolean useAccelerometer;
private int threshold;
+ private long minTimeBetweenMovementDetections;
public MovementDetector(Context context)
{
@@ -48,6 +49,17 @@ public MovementDetector(Context context, boolean useAccelerometer)
{
this(context, false, useAccelerometer, AccelerationEventListener.THRESHOLD_LOW);
}
+
+ public void setMinTimeBetweenMovementDetections(
+ long minTimeBetweenMovementDetections)
+ {
+ this.minTimeBetweenMovementDetections = minTimeBetweenMovementDetections;
+ }
+
+ public void setMinTimeBetweenShakes(long minTimeBetweenMovementDetections)
+ {
+ this.minTimeBetweenMovementDetections = minTimeBetweenMovementDetections;
+ }
/**
* @param useAccelerometer otherwise use linear acceleration
@@ -61,6 +73,7 @@ public MovementDetector(Context context, boolean useHighPassFilter, boolean useA
this.useAccelerometer = useAccelerometer;
this.useHighPassFilter = useHighPassFilter;
this.threshold = threshold;
+ this.minTimeBetweenMovementDetections = AccelerationEventListener.DEFAULT_MIN_TIME_BETWEEN_MOVEMENT_DISABLED;
}
public void startReadingAccelerationData(MovementDetectionListener resultCallback)
@@ -75,7 +88,8 @@ public void startReadingAccelerationData(MovementDetectionListener resultCallbac
if (useAccelerometer)
{
sensorListener =
- new AccelerationEventListener(useHighPassFilter, resultCallback, threshold);
+ new AccelerationEventListener(
+ useHighPassFilter, resultCallback, threshold, minTimeBetweenMovementDetections);
sensorManager.registerListener(sensorListener,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
RATE);
@@ -83,7 +97,7 @@ public void startReadingAccelerationData(MovementDetectionListener resultCallbac
else
{
sensorListener =
- new AccelerationEventListener(useHighPassFilter, resultCallback, threshold);
+ new AccelerationEventListener(useHighPassFilter, resultCallback, threshold, minTimeBetweenMovementDetections);
sensorManager.registerListener(sensorListener,
sensorManager
.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),

0 comments on commit e1fe0d2

Please sign in to comment.
Something went wrong with that request. Please try again.