-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
716 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+277 Bytes
appinventor/appengine/src/com/google/appinventor/images/barometer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+357 Bytes
appinventor/appengine/src/com/google/appinventor/images/hygrometer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+297 Bytes
appinventor/appengine/src/com/google/appinventor/images/lightsensor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+174 Bytes
appinventor/appengine/src/com/google/appinventor/images/thermometer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
appinventor/components/src/com/google/appinventor/components/runtime/Barometer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...r/components/src/com/google/appinventor/components/runtime/BufferedSingleValueSensor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
appinventor/components/src/com/google/appinventor/components/runtime/Hygrometer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
Oops, something went wrong.