6 package com.google.appinventor.components.runtime;
19 import android.os.Handler;
30 @DesignerComponent(version = YaVersion.EV3_GYROSENSOR_COMPONENT_VERSION,
31 description =
"A component that provides a high-level interface to a gyro sensor on a " +
32 "LEGO MINDSTORMS EV3 robot.",
33 category = ComponentCategory.LEGOMINDSTORMS,
35 iconName =
"images/legoMindstormsEv3.png")
38 private static final int DELAY_MILLISECONDS = 50;
39 private static final int SENSOR_TYPE = 32;
40 private static final int SENSOR_MODE_ANGLE = 0;
41 private static final int SENSOR_MODE_RATE = 1;
42 private static final String SENSOR_MODE_ANGLE_STRING =
"angle";
43 private static final String SENSOR_MODE_RATE_STRING =
"rate";
44 private static final String DEFAULT_SENSOR_MODE_STRING = SENSOR_MODE_ANGLE_STRING;
46 private Handler eventHandler;
47 private final Runnable sensorValueChecker;
48 private int mode = SENSOR_MODE_ANGLE;
49 private String modeString = SENSOR_MODE_ANGLE_STRING;
50 private double previousValue = -1.0;
51 private boolean sensorValueChangedEventEnabled =
false;
57 super(container,
"Ev3GyroSensor");
59 eventHandler =
new Handler();
60 sensorValueChecker =
new Runnable() {
62 String functionName =
"";
65 double currentValue = getSensorValue(functionName);
67 if (previousValue < 0.0) {
68 previousValue = currentValue;
69 eventHandler.postDelayed(
this, DELAY_MILLISECONDS);
74 if (mode == SENSOR_MODE_RATE && Math.abs(currentValue) >= 1.0)
76 else if (mode == SENSOR_MODE_ANGLE && Math.abs(currentValue - previousValue) >= 1.0)
79 previousValue = currentValue;
82 eventHandler.postDelayed(
this, DELAY_MILLISECONDS);
85 eventHandler.post(sensorValueChecker);
87 Mode(DEFAULT_SENSOR_MODE_STRING);
95 @
SimpleFunction(description =
"Returns the current angle or rotation speed based on current mode, " +
96 "or -1 if the value cannot be read from sensor.")
98 String functionName =
"";
99 return getSensorValue(functionName);
106 defaultValue = DEFAULT_SENSOR_MODE_STRING)
108 public void Mode(String modeName) {
109 String functionName =
"Mode";
112 }
catch(IllegalArgumentException e) {
120 @
SimpleProperty(description =
"The sensor mode can be a text constant of either \"rate\" or \"angle\", " +
121 "which correspond to SetAngleMode or SetRateMode respectively.",
130 @
SimpleFunction(description =
"Measures the orientation of the sensor.")
132 setMode(SENSOR_MODE_ANGLE_STRING);
138 @
SimpleFunction(description =
"Measures the angular velocity of the sensor.")
140 setMode(SENSOR_MODE_RATE_STRING);
146 @
SimpleProperty(description =
"Whether the SensorValueChanged event should fire when the sensor value changed.",
149 return sensorValueChangedEventEnabled;
156 defaultValue =
"False")
159 sensorValueChangedEventEnabled = enabled;
165 @
SimpleEvent(description =
"Called then the sensor value changed.")
170 private double getSensorValue(String functionName) {
178 private void setMode(String newModeString) {
179 if (SENSOR_MODE_ANGLE_STRING.equals(newModeString))
180 mode = SENSOR_MODE_ANGLE;
181 else if (SENSOR_MODE_RATE_STRING.equals(newModeString))
182 mode = SENSOR_MODE_RATE;
184 throw new IllegalArgumentException();
186 this.modeString = newModeString;