6 package com.google.appinventor.components.runtime;
18 import android.os.Handler;
29 @DesignerComponent(version = YaVersion.EV3_TOUCHSENSOR_COMPONENT_VERSION,
30 description =
"A component that provides a high-level interface to a touch sensor on a " +
31 "LEGO MINDSTORMS EV3 robot.",
32 category = ComponentCategory.LEGOMINDSTORMS,
34 iconName =
"images/legoMindstormsEv3.png")
37 private static final int SENSOR_VALUE_THRESHOLD = 50;
38 private static final int SENSOR_TYPE = 16;
39 private static final int SENSOR_MODE_TOUCH = 0;
40 private static final String SENSOR_MODE_TOUCH_STRING =
"touch";
41 private static final int DELAY_MILLISECONDS = 50;
43 private String modeString = SENSOR_MODE_TOUCH_STRING;
44 private int mode = SENSOR_MODE_TOUCH;
45 private Handler eventHandler;
46 private final Runnable sensorValueChecker;
47 private int savedPressedValue = -1;
48 private boolean pressedEventEnabled;
49 private boolean releasedEventEnabled;
55 super(container,
"Ev3TouchSensor");
57 eventHandler =
new Handler();
58 sensorValueChecker =
new Runnable() {
60 String functionName =
"";
63 int currentPressedValue = getPressedValue(functionName);
65 if (savedPressedValue < 0) {
66 savedPressedValue = currentPressedValue;
67 eventHandler.postDelayed(
this, DELAY_MILLISECONDS);
71 if (savedPressedValue < SENSOR_VALUE_THRESHOLD) {
72 if (releasedEventEnabled && currentPressedValue >= SENSOR_VALUE_THRESHOLD)
75 if (pressedEventEnabled && currentPressedValue < SENSOR_VALUE_THRESHOLD)
79 savedPressedValue = currentPressedValue;
82 eventHandler.postDelayed(
this, DELAY_MILLISECONDS);
85 eventHandler.post(sensorValueChecker);
94 @
SimpleFunction(description =
"Returns true if the touch sensor is pressed.")
96 String functionName =
"IsPressed";
97 return getPressedValue(functionName) >= SENSOR_VALUE_THRESHOLD;
105 defaultValue =
"False")
108 pressedEventEnabled = enabled;
115 @
SimpleProperty(description =
"Whether the Released event should fire when the touch sensor is " +
119 return pressedEventEnabled;
125 @
SimpleEvent(description =
"Called when the touch sensor is pressed.")
134 @
SimpleProperty(description =
"Whether the Released event should fire when the touch sensor is " +
138 return releasedEventEnabled;
146 defaultValue =
"False")
149 releasedEventEnabled = enabled;
155 @
SimpleEvent(description =
"Called when the touch sensor is pressed.")
160 private int getPressedValue(String functionName) {
172 eventHandler.removeCallbacks(sensorValueChecker);