Back to project page Gallery97.
The source code is released under:
GNU General Public License
If you think the Android project Gallery97 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package pl.kacprzak.klub97; /*from www. j a v a 2 s. c om*/ import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; public class InputListener implements SensorEventListener { MovementEngine mE = new MovementEngine(1.0f); double timestamp = 0, dt = 0, t = 0, temp_time = 0; boolean oneTime = true; public static float[] rotMatrix = new float[16]; public static float[] ds = {0,0}; public static float direction = 0; float[] gyro = {0,0,0}; float[] acc = {0,0,0}; float[] lacc = {0,0,0}; float[] mag = {0,0,0}; float[] orientation = {0,0,0}; @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override public void onSensorChanged(SensorEvent event) { if(timestamp == 0){ dt = 0; }else{ dt = (event.timestamp - timestamp)/1000000000; } t += dt; switch (event.sensor.getType()) { case Sensor.TYPE_ACCELEROMETER: acc[0] = lowPass(event.values[0], acc[0], 0.2f); acc[1] = lowPass(event.values[1], acc[1], 0.2f); acc[2] = lowPass(event.values[2], acc[2], 0.2f); break; case Sensor.TYPE_GYROSCOPE: gyro[0] = lowPass(event.values[0], gyro[0], 0.2f); gyro[1] = lowPass(event.values[1], gyro[1], 0.2f); gyro[2] = lowPass(event.values[2], gyro[2], 0.2f); break; case Sensor.TYPE_MAGNETIC_FIELD: mag[0] = lowPass(event.values[0], mag[0], 0.2f); mag[1] = lowPass(event.values[1], mag[1], 0.2f); mag[2] = lowPass(event.values[2], mag[2], 0.2f); break; case Sensor.TYPE_LINEAR_ACCELERATION: lacc = event.values.clone(); break; } SensorManager.getRotationMatrix(rotMatrix, null, acc, mag); SensorManager.getOrientation(rotMatrix, orientation); if(dt != 0.0) mE.inputAccReadings(lacc[0], lacc[1], lacc[2], dt); mE.inputCompass(orientation[0], orientation[1], orientation[2]); mE.histogramFill(); if(t-temp_time > 0.5){ float temp[] = mE.performCalculation(); ds[0] = temp[0]; ds[1] = temp[1]; temp_time = t; //Log.e("mylog", "mylog "+" "+s[0]+" "+s[1]); mE.histogramClear(); } timestamp = event.timestamp; } public float lowPass(float newinput, float oldinput, float ALFA){ if(oldinput == 0) return newinput; return oldinput + ALFA*(newinput-oldinput); } }