Skip to content

Commit

Permalink
Fix[gyro_control]: implement sensor warmup period for devices with br…
Browse files Browse the repository at this point in the history
…oken GRV
  • Loading branch information
artdeell committed Nov 22, 2024
1 parent f16c039 commit 62de3d3
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.OrientationEventListener;
import android.view.Surface;
import android.view.WindowManager;
Expand All @@ -21,14 +22,17 @@ public class GyroControl implements SensorEventListener, GrabListener {
/* How much distance has to be moved before taking into account the gyro */
private static final float SINGLE_AXIS_LOW_PASS_THRESHOLD = 1.13F;
private static final float MULTI_AXIS_LOW_PASS_THRESHOLD = 1.3F;
// Warmup period of 2 since the first read from the sensor seems to produce a bogus value,
// which creates a far too large of a difference on the Y axis once actual sensor data comes in
private static final int ROTATION_VECTOR_WARMUP_PERIOD = 2;

private final WindowManager mWindowManager;
private int mSurfaceRotation;
private final SensorManager mSensorManager;
private final Sensor mSensor;
private final OrientationCorrectionListener mCorrectionListener;
private boolean mShouldHandleEvents;
private boolean mFirstPass;
private int mWarmup;
private float xFactor; // -1 or 1 depending on device orientation
private float yFactor;
private boolean mSwapXY;
Expand Down Expand Up @@ -64,7 +68,7 @@ public GyroControl(Activity activity) {

public void enable() {
if(mSensor == null) return;
mFirstPass = true;
mWarmup = ROTATION_VECTOR_WARMUP_PERIOD;
mSensorManager.registerListener(this, mSensor, 1000 * LauncherPreferences.PREF_GYRO_SAMPLE_RATE);
mCorrectionListener.enable();
mShouldHandleEvents = CallbackBridge.isGrabbing();
Expand All @@ -75,7 +79,9 @@ public void disable() {
if(mSensor == null) return;
mSensorManager.unregisterListener(this);
mCorrectionListener.disable();
mStoredX = mStoredY = 0;
resetDamper();
Log.i("GyroControl", "STOP");
CallbackBridge.removeGrabListener(this);
}

Expand All @@ -87,8 +93,8 @@ public void onSensorChanged(SensorEvent sensorEvent) {
SensorManager.getRotationMatrixFromVector(mCurrentRotation, sensorEvent.values);


if(mFirstPass){ // Setup initial position
mFirstPass = false;
if(mWarmup > 0){ // Setup initial position
mWarmup--;
return;
}
SensorManager.getAngleChange(mAngleDifference, mCurrentRotation, mPreviousRotation);
Expand Down Expand Up @@ -161,7 +167,7 @@ public void onAccuracyChanged(Sensor sensor, int i) {}

@Override
public void onGrabState(boolean isGrabbing) {
mFirstPass = true;
mWarmup = ROTATION_VECTOR_WARMUP_PERIOD;
mShouldHandleEvents = isGrabbing;
}

Expand Down

0 comments on commit 62de3d3

Please sign in to comment.