Context-detection API for Android developed as a university project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Sampler.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package uk.co.md87.android.common.accel;
  23. import android.os.Handler;
  24. /**
  25. * A utility class which handles sampling accelerometer data from an
  26. * {@link AccelReader}. The Sampler takes 128 samples with a 50ms delay between
  27. * each sample. When the Sampler has finished, it executes a runnable so that
  28. * the data may be retrieved and analysed.
  29. *
  30. * @author chris
  31. */
  32. public class Sampler implements Runnable {
  33. private final Handler handler;
  34. private final AccelReader reader;
  35. private final Runnable finishedRunnable;
  36. private final float[] data = new float[6];
  37. private int nextSample;
  38. public Sampler(final Handler handler, final AccelReader reader,
  39. final Runnable finishedRunnable) {
  40. this.handler = handler;
  41. this.reader = reader;
  42. this.finishedRunnable = finishedRunnable;
  43. }
  44. public void start() {
  45. data[0] = Float.MAX_VALUE;
  46. data[1] = Float.MIN_VALUE;
  47. data[2] = 0;
  48. data[3] = Float.MAX_VALUE;
  49. data[4] = Float.MIN_VALUE;
  50. data[5] = 0;
  51. nextSample = 0;
  52. reader.startSampling();
  53. handler.postDelayed(this, 50);
  54. }
  55. public float[] getData() {
  56. return data;
  57. }
  58. /** {@inheritDoc} */
  59. @Override
  60. public void run() {
  61. final float[] values = reader.getSample();
  62. data[0] = Math.min(data[0], values[0]);
  63. data[1] = Math.max(data[1], values[0]);
  64. data[2] += values[0];
  65. data[3] = Math.min(data[3], values[1]);
  66. data[4] = Math.max(data[4], values[1]);
  67. data[5] += values[1];
  68. if (++nextSample == 128) {
  69. reader.stopSampling();
  70. finishedRunnable.run();
  71. return;
  72. }
  73. handler.postDelayed(this, 50);
  74. }
  75. public void stop() {
  76. handler.removeCallbacks(this);
  77. }
  78. }