7 package com.google.appinventor.components.runtime;
20 import android.os.Handler;
30 @DesignerComponent(version = YaVersion.NXT_TOUCHSENSOR_COMPONENT_VERSION,
31 description =
"A component that provides a high-level interface to a touch sensor on a " +
32 "LEGO MINDSTORMS NXT robot.",
33 category = ComponentCategory.LEGOMINDSTORMS,
35 iconName =
"images/legoMindstormsNxt.png")
39 private enum State { UNKNOWN, PRESSED, RELEASED }
40 private static final String DEFAULT_SENSOR_PORT =
"1";
42 private Handler handler;
43 private State previousState;
44 private final Runnable sensorReader;
45 private boolean pressedEventEnabled;
46 private boolean releasedEventEnabled;
52 super(container,
"NxtTouchSensor");
53 handler =
new Handler();
54 previousState = State.UNKNOWN;
55 sensorReader =
new Runnable() {
58 SensorValue<Boolean> sensorValue = getPressedValue(
"");
59 if (sensorValue.valid) {
60 State currentState = sensorValue.value ? State.PRESSED : State.RELEASED;
62 if (currentState != previousState) {
63 if (currentState == State.PRESSED && pressedEventEnabled) {
66 if (currentState == State.RELEASED && releasedEventEnabled) {
71 previousState = currentState;
74 if (isHandlerNeeded()) {
75 handler.post(sensorReader);
87 setInputMode(functionName,
port, SENSOR_TYPE_SWITCH, SENSOR_MODE_BOOLEANMODE);
95 defaultValue = DEFAULT_SENSOR_PORT)
101 @
SimpleFunction(description =
"Returns true if the touch sensor is pressed.")
103 String functionName =
"IsPressed";
108 SensorValue<Boolean> sensorValue = getPressedValue(functionName);
109 if (sensorValue.valid) {
110 return sensorValue.value;
117 private SensorValue<Boolean> getPressedValue(String functionName) {
119 if (returnPackage !=
null) {
123 return new SensorValue<Boolean>(
true, (scaledValue != 0));
128 return new SensorValue<Boolean>(
false,
null);
135 @SimpleProperty(description =
"Whether the Pressed event should fire when the touch sensor is" +
137 category = PropertyCategory.BEHAVIOR)
139 return pressedEventEnabled;
149 boolean handlerWasNeeded = isHandlerNeeded();
151 pressedEventEnabled = enabled;
153 boolean handlerIsNeeded = isHandlerNeeded();
154 if (handlerWasNeeded && !handlerIsNeeded) {
155 handler.removeCallbacks(sensorReader);
157 if (!handlerWasNeeded && handlerIsNeeded) {
158 previousState = State.UNKNOWN;
159 handler.post(sensorReader);
163 @
SimpleEvent(description =
"Touch sensor has been pressed.")
172 @
SimpleProperty(description =
"Whether the Released event should fire when the touch sensor is" +
176 return releasedEventEnabled;
186 boolean handlerWasNeeded = isHandlerNeeded();
188 releasedEventEnabled = enabled;
190 boolean handlerIsNeeded = isHandlerNeeded();
191 if (handlerWasNeeded && !handlerIsNeeded) {
192 handler.removeCallbacks(sensorReader);
194 if (!handlerWasNeeded && handlerIsNeeded) {
195 previousState = State.UNKNOWN;
196 handler.post(sensorReader);
200 @
SimpleEvent(description =
"Touch sensor has been released.")
205 private boolean isHandlerNeeded() {
206 return pressedEventEnabled || releasedEventEnabled;
213 handler.removeCallbacks(sensorReader);