7 package com.google.appinventor.components.runtime;
11 import android.content.Context;
12 import android.hardware.SensorEvent;
13 import android.hardware.SensorManager;
21 private AveragingBuffer buffer;
26 buffer =
new AveragingBuffer(bufferSize);
32 final float[] values = sensorEvent.values;
33 buffer.insert(values[0]);
34 super.onSensorChanged(sensorEvent);
39 return buffer.getAverage();
42 private class AveragingBuffer {
46 private AveragingBuffer(
int size) {
47 data =
new Float[size];
51 private void insert(Float datum) {
53 if (next == data.length) {
58 private float getAverage() {
62 for (
int i = 0; i < data.length; i++) {
63 if (data[i] !=
null) {
69 return (
float) (count == 0 ? sum : sum / count);