AI2 Component  (Version nb184)
BufferedSingleValueSensor.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2019 MIT, All rights reserved
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 
7 package com.google.appinventor.components.runtime;
8 
10 
11 import android.content.Context;
12 import android.hardware.SensorEvent;
13 import android.hardware.SensorManager;
14 
19 @SimpleObject
20 public abstract class BufferedSingleValueSensor extends SingleValueSensor {
21  private AveragingBuffer buffer;
22 
24  int sensorType, int bufferSize) {
25  super(container.$form(), sensorType);
26  buffer = new AveragingBuffer(bufferSize);
27  }
28 
29  @Override
30  public void onSensorChanged(SensorEvent sensorEvent) {
31  if (enabled && sensorEvent.sensor.getType() == sensorType) {
32  final float[] values = sensorEvent.values;
33  buffer.insert(values[0]);
34  super.onSensorChanged(sensorEvent);
35  }
36  }
37 
38  protected float getAverageValue() {
39  return buffer.getAverage();
40  }
41 
42  private class AveragingBuffer {
43  private Float[] data;
44  private int next;
45 
46  private AveragingBuffer(int size) {
47  data = new Float[size];
48  next = 0;
49  }
50 
51  private void insert(Float datum) {
52  data[next++] = datum;
53  if (next == data.length) {
54  next = 0;
55  }
56  }
57 
58  private float getAverage() {
59  double sum = 0;
60  int count = 0;
61 
62  for (int i = 0; i < data.length; i++) {
63  if (data[i] != null) {
64  sum += data[i];
65  count++;
66  }
67  }
68 
69  return (float) (count == 0 ? sum : sum / count);
70  }
71  }
72 }
com.google.appinventor.components.runtime.BufferedSingleValueSensor.BufferedSingleValueSensor
BufferedSingleValueSensor(ComponentContainer container, int sensorType, int bufferSize)
Definition: BufferedSingleValueSensor.java:23
com.google.appinventor.components.runtime.SingleValueSensor.enabled
boolean enabled
Definition: SingleValueSensor.java:35
com.google.appinventor.components.runtime.BufferedSingleValueSensor.getAverageValue
float getAverageValue()
Definition: BufferedSingleValueSensor.java:38
com.google.appinventor.components
com.google.appinventor.components.runtime.SingleValueSensor
Definition: SingleValueSensor.java:27
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime.SingleValueSensor.sensorType
int sensorType
Definition: SingleValueSensor.java:32
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google
com
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.BufferedSingleValueSensor
Definition: BufferedSingleValueSensor.java:20
com.google.appinventor.components.runtime.BufferedSingleValueSensor.onSensorChanged
void onSensorChanged(SensorEvent sensorEvent)
Definition: BufferedSingleValueSensor.java:30
com.google.appinventor.components.annotations
com.google.appinventor