Skip to content
Permalink
Browse files

Add environment sensors

  • Loading branch information
espertus authored and ewpatton committed Sep 6, 2019
1 parent 7452a36 commit f2554cf07d5816a1fd62d268492b316562c2c720
@@ -98,6 +98,38 @@
@Source("com/google/appinventor/images/accelerometersensor.png")
ImageResource accelerometersensor();

/**
* Designer palette item: lightsensor component
* <p>
* Source: https://feathericons.com/
*/
@Source("com/google/appinventor/images/lightsensor.png")
ImageResource lightsensor();

/**
* Designer palette item: barometer component
* <p>
* Source: Ellen Spertus, released into public domain
*/
@Source("com/google/appinventor/images/barometer.png")
ImageResource barometer();

/**
* Designer palette item: thermometer component
* <p>
* Source: Ellen Spertus, released into public domain
*/
@Source("com/google/appinventor/images/thermometer.png")
ImageResource thermometer();

/**
* Designer palette item: hygrometer component
* <p>
* Source: Ellen Spertus, released into public domain
*/
@Source("com/google/appinventor/images/hygrometer.png")
ImageResource hygrometer();

/**
* Designer palette item: barcode scanner component
*/
@@ -117,6 +117,10 @@

private static void initBundledImages() {
bundledImages.put("images/accelerometersensor.png", images.accelerometersensor());
bundledImages.put("images/lightsensor.png", images.lightsensor());
bundledImages.put("images/barometer.png", images.barometer());
bundledImages.put("images/thermometer.png", images.thermometer());
bundledImages.put("images/hygrometer.png", images.hygrometer());
bundledImages.put("images/gyroscopesensor.png", images.gyroscopesensor());
bundledImages.put("images/nearfield.png", images.nearfield());
bundledImages.put("images/activityStarter.png", images.activitystarter());
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -460,8 +460,13 @@ private YaVersion() {
// - SPEECHRECOGNIZER_COMPONENT_VERSION was incremented to 2
// For YOUNG_ANDROID_VERSION 186:
// - BLOCKS_LANGUAGE_VERSION was incremented to 27
// For YOUNG_ANDROID_VERSION 187:
// - BAROMETER_COMPONENT_VERSION was initialized to 1
// - HYGROMETER_COMPONENT_VERSION was initialized to 1
// - LIGHTSENSOR_COMPONENT_VERSION was initialized to 1
// - THERMOMETER_COMPONENT_VERSION was initialized to 1

public static final int YOUNG_ANDROID_VERSION = 186;
public static final int YOUNG_ANDROID_VERSION = 187;

// ............................... Blocks Language Version Number ...............................

@@ -1231,9 +1236,25 @@ private YaVersion() {
public static final int PROXIMITYSENSOR_COMPONENT_VERSION = 1;

// Rendezvous Server Location

public static final String RENDEZVOUS_SERVER = "rendezvous.appinventor.mit.edu";

// For BAROMETER_COMPONENT_VERSION 1:
// - Initial version

// For HYGROMETER_COMPONENT_VERSION 1:
// - Initial version

// For LIGHTSENSOR_COMPONENT_VERSION 1:
// - Initial version

// For THERMOMETER_COMPONENT_VERSION 1:
// - Initial version

public static final int BAROMETER_COMPONENT_VERSION = 1;
public static final int HYGROMETER_COMPONENT_VERSION = 1;
public static final int LIGHTSENSOR_COMPONENT_VERSION = 1;
public static final int THERMOMETER_COMPONENT_VERSION = 1;

// Companion Versions and Update Information

// The PREFERRED_COMPANION is displayed to the end-user if
@@ -78,7 +78,7 @@
iconName = "images/accelerometersensor.png")
@SimpleObject
public class AccelerometerSensor extends AndroidNonvisibleComponent
implements OnStopListener, OnResumeListener, SensorComponent, SensorEventListener, Deleteable {
implements OnPauseListener, OnResumeListener, SensorComponent, SensorEventListener, Deleteable {

// Logging and Debugging
private final static String LOG_TAG = "AccelerometerSensor";
@@ -134,7 +134,7 @@
public AccelerometerSensor(ComponentContainer container) {
super(container.$form());
form.registerForOnResume(this);
form.registerForOnStop(this);
form.registerForOnPause(this);

enabled = true;
resources = container.$context().getResources();
@@ -468,10 +468,10 @@ public void onResume() {
}
}

// OnStopListener implementation
// OnPauseListener implementation

@Override
public void onStop() {
public void onPause() {
if (enabled) {
stopListening();
}
@@ -0,0 +1,64 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2019 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0


package com.google.appinventor.components.runtime;

import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.YaVersion;

import android.hardware.Sensor;

/**
* Physical world component that can measure the ambient air pressure if
* supported by the hardware.
*/
@DesignerComponent(version = YaVersion.BAROMETER_COMPONENT_VERSION,
description = "A sensor component that can measure the ambient air pressure.",
category = ComponentCategory.SENSORS,
nonVisible = true,
iconName = "images/barometer.png")
@SimpleObject
public class Barometer extends SingleValueSensor {
/**
* Creates a new Barometer component.
*
* @param container ignored (because this is a non-visible component)
*/
public Barometer(ComponentContainer container) {
super(container.$form(), Sensor.TYPE_PRESSURE);
}

@Override
protected void onValueChanged(float value) {
AirPressureChanged(value);
}

/**
* Called when a change is detected in the air pressure (provided in hPa).
*
* @param the new air pressure in hPa (millibar)
*/
@SimpleEvent
public void AirPressureChanged(float pressure) {
EventDispatcher.dispatchEvent(this, "AirPressureChanged", pressure);
}

/**
* The atmospheric pressure in hPa (millibar), if the sensor is available
* and enabled.
*
* @return the atmospheric pressure in hPa (millibar)
*/
@SimpleProperty(description = "The air pressure in hPa (millibar), if the sensor is available " +
"and enabled.")
public float AirPressure() {
return getValue();
}
}
@@ -0,0 +1,72 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2019 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0


package com.google.appinventor.components.runtime;

import com.google.appinventor.components.annotations.SimpleObject;

import android.content.Context;
import android.hardware.SensorEvent;
import android.hardware.SensorManager;

/**
* A single-value sensor whose most recent values should be buffered
* and averaged.
*/
@SimpleObject
public abstract class BufferedSingleValueSensor extends SingleValueSensor {
private AveragingBuffer buffer;

public BufferedSingleValueSensor(ComponentContainer container,
int sensorType, int bufferSize) {
super(container.$form(), sensorType);
buffer = new AveragingBuffer(bufferSize);
}

@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if (enabled && sensorEvent.sensor.getType() == sensorType) {
final float[] values = sensorEvent.values;
buffer.insert(values[0]);
super.onSensorChanged(sensorEvent);
}
}

protected float getAverageValue() {
return buffer.getAverage();
}

private class AveragingBuffer {
private Float[] data;
private int next;

private AveragingBuffer(int size) {
data = new Float[size];
next = 0;
}

private void insert(Float datum) {
data[next++] = datum;
if (next == data.length) {
next = 0;
}
}

private float getAverage() {
double sum = 0;
int count = 0;

for (int i = 0; i < data.length; i++) {
if (data[i] != null) {
sum += data[i];
count++;
}
}

return (float) (count == 0 ? sum : sum / count);
}
}
}
@@ -0,0 +1,68 @@
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2019 MIT, All rights reserved
// Released under the Apache License, Version 2.0
// http://www.apache.org/licenses/LICENSE-2.0


package com.google.appinventor.components.runtime;

import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.DesignerProperty;
import com.google.appinventor.components.annotations.SimpleEvent;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.annotations.SimpleProperty;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.common.YaVersion;

import android.hardware.Sensor;

/**
* Physical world component that can measure the relative ambient air
* humidity if supported by the hardware.
*/
@DesignerComponent(version = YaVersion.HYGROMETER_COMPONENT_VERSION,
description = "A sensor component that can measure the relative ambient air humidity. " +
"Most Android devices do not have this sensor.",
category = ComponentCategory.SENSORS,
nonVisible = true,
iconName = "images/hygrometer.png")
@SimpleObject
public class Hygrometer extends SingleValueSensor {
/**
* Creates a new Hygrometer component.
*
* @param container ignored (because this is a non-visible component)
*/
public Hygrometer(ComponentContainer container) {
super(container.$form(), Sensor.TYPE_RELATIVE_HUMIDITY);
}

@Override
protected void onValueChanged(float value) {
HumidityChanged(value);
}

/**
* Indicates the relative humidity changed.
*
* @param the new relative humidity
*/
@SimpleEvent(
description = "Called when a change is detected in the ambient air humidity (expressed as a percentage).")
public void HumidityChanged(float humidity) {
EventDispatcher.dispatchEvent(this, "HumidityChanged", humidity);
}

/**
* Returns the relative ambient humidity as a percentage.
* The sensor must be enabled and available
* to return meaningful values.
*
* @return the relative ambient humidity as a percentage
*/
@SimpleProperty(description = "The relative ambient humidity as a percentage, if the sensor is available " +
"and enabled.")
public float Humidity() {
return getValue();
}
}

0 comments on commit f2554cf

Please sign in to comment.
You can’t perform that action at this time.