From c05e0ff4e92744b1bbb2c6dfecbb2c2b60d06e18 Mon Sep 17 00:00:00 2001 From: Kuluum Date: Wed, 18 Oct 2017 22:43:16 +0300 Subject: [PATCH] Fix orientation delay --- .../com/sensormanager/OrientationRecord.java | 49 ++++++++++--------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/android/src/main/java/com/sensormanager/OrientationRecord.java b/android/src/main/java/com/sensormanager/OrientationRecord.java index 3811f85..c3dd816 100644 --- a/android/src/main/java/com/sensormanager/OrientationRecord.java +++ b/android/src/main/java/com/sensormanager/OrientationRecord.java @@ -86,30 +86,35 @@ public void onSensorChanged(SensorEvent sensorEvent) { boolean success = mSensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic); if (success) { long curTime = System.currentTimeMillis(); - float orientation[] = new float[3]; - mSensorManager.getOrientation(R, orientation); - float heading = (float)((Math.toDegrees(orientation[0])) % 360.0f); - float pitch = (float)((Math.toDegrees(orientation[1])) % 360.0f); - float roll = (float)((Math.toDegrees(orientation[2])) % 360.0f); - - if (heading < 0) { - heading = 360 - (0 - heading); - } - - if (pitch < 0) { - pitch = 360 - (0 - pitch); + i++; + if ((curTime - lastUpdate) > delay) { + i = 0; + float orientation[] = new float[3]; + mSensorManager.getOrientation(R, orientation); + + float heading = (float) ((Math.toDegrees(orientation[0])) % 360.0f); + float pitch = (float) ((Math.toDegrees(orientation[1])) % 360.0f); + float roll = (float) ((Math.toDegrees(orientation[2])) % 360.0f); + + if (heading < 0) { + heading = 360 - (0 - heading); + } + + if (pitch < 0) { + pitch = 360 - (0 - pitch); + } + + if (roll < 0) { + roll = 360 - (0 - roll); + } + + map.putDouble("azimuth", heading); + map.putDouble("pitch", pitch); + map.putDouble("roll", roll); + sendEvent("Orientation", map); + lastUpdate = curTime; } - - if (roll < 0) { - roll = 360 - (0 - roll); - } - - map.putDouble("azimuth", heading); - map.putDouble("pitch", pitch); - map.putDouble("roll", roll); - sendEvent("Orientation", map); - lastUpdate = curTime; } } }