fork(5) download
  1. import android.content.Context;
  2. import android.hardware.Sensor;
  3. import android.hardware.SensorEvent;
  4. import android.hardware.SensorEventListener;
  5. import android.hardware.SensorManager;
  6. import android.util.Log;
  7.  
  8. public class TiltSensor implements SensorEventListener {
  9.  
  10. private boolean needsRecalc = false;
  11. private float[] tilt_data = { 0, 0, 0 }, accelerometer = { 0, 0, 0 }, magnetic_field = { 0, 0, 0 }, old_acc = { 0, 0, 0 }, dampened_acc = { 0, 0, 0 };
  12. private boolean tiltAvailble = false;
  13. private final SensorManager man;
  14. final Sensor mag_sensor;
  15. final Sensor acc_sensor;
  16. boolean has_mag;
  17. boolean has_acc;
  18.  
  19. // Change this to make the sensors respond quicker, or slower:
  20. private static final int delay = SensorManager.SENSOR_DELAY_GAME;
  21.  
  22. //Call this from your activity to hook listeners if the were removed in pause()
  23. public void resume() {
  24. has_mag = man.registerListener(this, mag_sensor, delay);
  25. has_acc = man.registerListener(this, acc_sensor, delay);
  26. if (!has_mag && !has_acc) {
  27. pause();
  28. }
  29. }
  30.  
  31. //Call this from your activity to save battery while app is not in foreground
  32. public void pause() {
  33. man.unregisterListener(this);
  34. }
  35.  
  36. // Special class used to handle sensor events:
  37. @Override
  38. public void onSensorChanged(SensorEvent e) {
  39. final float[] vals = e.values;
  40. final int type = e.sensor.getType();
  41. switch (type) {
  42. case (Sensor.TYPE_ACCELEROMETER): {
  43. needsRecalc = true;
  44. if (!has_mag) {
  45. System.arraycopy(accelerometer, 0, old_acc, 0, 3);
  46. }
  47. System.arraycopy(vals, 0, accelerometer, 0, 3);
  48. if (!has_mag) {
  49. for (int i = 0; i < 3; i++) {
  50. //Accumulate changes
  51. final float sensitivity = 0.08f;
  52. dampened_acc[i] += (accelerometer[i] - old_acc[i]) * sensitivity;
  53. //Even out drift over time
  54. dampened_acc[i] *= 0.99;
  55. }
  56. }
  57. }
  58. break;
  59. case (Sensor.TYPE_MAGNETIC_FIELD): {
  60. needsRecalc = true;
  61. System.arraycopy(vals, 0, magnetic_field, 0, 3);
  62. }
  63. break;
  64. }
  65. }
  66.  
  67. @Override
  68. public void onAccuracyChanged(Sensor event, int res) {
  69. }
  70.  
  71. // The constructor will use a context object to register itself for various inputs:
  72. public TiltSensor(Context c) {
  73. man = (SensorManager) c.getSystemService(Context.SENSOR_SERVICE);
  74. mag_sensor = man.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
  75. acc_sensor = man.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  76. //Sensor gyr_sensor = man.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
  77. resume();
  78. if (has_acc) {
  79. tiltAvailble = true;
  80. if (has_mag) {
  81. Log.d("TiltCalc", "Using accelerometer + compass.");
  82. }
  83. else {
  84. Log.d("TiltCalc", "Using only accelerometer.");
  85. }
  86. }
  87. else {
  88. tiltAvailble = false;
  89. Log.d("TiltCalc", "No acceptable hardware found, tilt not available.");
  90. //No use in having listeners registered
  91. pause();
  92. }
  93. }
  94.  
  95. @Override
  96. protected void finalize() throws Throwable {
  97. pause();
  98. super.finalize();
  99. }
  100.  
  101. public boolean isTiltAbailable() {
  102. return tiltAvailble;
  103. }
  104.  
  105. // Will return the most up-to-date tilt data in the vals[] array
  106. public void getTilt(float[] vals) {
  107. // If some of the data has been changed, then we need to recalculate some things...
  108. if (needsRecalc) {
  109. if (has_acc) {
  110. if (has_mag) {
  111. float[] R = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  112. // Calculate the rotation matrix, and use that to get the orientation:
  113. if (SensorManager.getRotationMatrix(R, null, accelerometer, magnetic_field)) {
  114. SensorManager.getOrientation(R, tilt_data);
  115. }
  116. }
  117. //Cheat by assuming user will be holding device in a certain way.
  118. else {
  119. tilt_data[1] = dampened_acc[0];
  120. }
  121. }
  122. needsRecalc = false;
  123. }
  124. System.arraycopy(tilt_data, 0, vals, 0, 3);
  125. }
  126.  
  127. }
  128.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty