AI2 Component  (Version nb184)
NxtTouchSensor.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2012 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
19 
20 import android.os.Handler;
21 
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,
34  nonVisible = true,
35  iconName = "images/legoMindstormsNxt.png")
36 @SimpleObject
37 public class NxtTouchSensor extends LegoMindstormsNxtSensor implements Deleteable {
38 
39  private enum State { UNKNOWN, PRESSED, RELEASED }
40  private static final String DEFAULT_SENSOR_PORT = "1";
41 
42  private Handler handler;
43  private State previousState;
44  private final Runnable sensorReader;
45  private boolean pressedEventEnabled;
46  private boolean releasedEventEnabled;
47 
51  public NxtTouchSensor(ComponentContainer container) {
52  super(container, "NxtTouchSensor");
53  handler = new Handler();
54  previousState = State.UNKNOWN;
55  sensorReader = new Runnable() {
56  public void run() {
57  if (bluetooth != null && bluetooth.IsConnected()) {
58  SensorValue<Boolean> sensorValue = getPressedValue("");
59  if (sensorValue.valid) {
60  State currentState = sensorValue.value ? State.PRESSED : State.RELEASED;
61 
62  if (currentState != previousState) {
63  if (currentState == State.PRESSED && pressedEventEnabled) {
64  Pressed();
65  }
66  if (currentState == State.RELEASED && releasedEventEnabled) {
67  Released();
68  }
69  }
70 
71  previousState = currentState;
72  }
73  }
74  if (isHandlerNeeded()) {
75  handler.post(sensorReader);
76  }
77  }
78  };
79 
80  SensorPort(DEFAULT_SENSOR_PORT);
81  PressedEventEnabled(false);
82  ReleasedEventEnabled(false);
83  }
84 
85  @Override
86  protected void initializeSensor(String functionName) {
87  setInputMode(functionName, port, SENSOR_TYPE_SWITCH, SENSOR_MODE_BOOLEANMODE);
88  }
89 
95  defaultValue = DEFAULT_SENSOR_PORT)
96  @SimpleProperty(userVisible = false)
97  public void SensorPort(String sensorPortLetter) {
98  setSensorPort(sensorPortLetter);
99  }
100 
101  @SimpleFunction(description = "Returns true if the touch sensor is pressed.")
102  public boolean IsPressed() {
103  String functionName = "IsPressed";
104  if (!checkBluetooth(functionName)) {
105  return false;
106  }
107 
108  SensorValue<Boolean> sensorValue = getPressedValue(functionName);
109  if (sensorValue.valid) {
110  return sensorValue.value;
111  }
112 
113  // invalid response
114  return false;
115  }
116 
117  private SensorValue<Boolean> getPressedValue(String functionName) {
118  byte[] returnPackage = getInputValues(functionName, port);
119  if (returnPackage != null) {
120  boolean valid = getBooleanValueFromBytes(returnPackage, 4);
121  if (valid) {
122  int scaledValue = getSWORDValueFromBytes(returnPackage, 12);
123  return new SensorValue<Boolean>(true, (scaledValue != 0));
124  }
125  }
126 
127  // invalid response
128  return new SensorValue<Boolean>(false, null);
129  }
130 
135  @SimpleProperty(description = "Whether the Pressed event should fire when the touch sensor is" +
136  " pressed.",
137  category = PropertyCategory.BEHAVIOR)
138  public boolean PressedEventEnabled() {
139  return pressedEventEnabled;
140  }
141 
146  @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN, defaultValue = "False")
148  public void PressedEventEnabled(boolean enabled) {
149  boolean handlerWasNeeded = isHandlerNeeded();
150 
151  pressedEventEnabled = enabled;
152 
153  boolean handlerIsNeeded = isHandlerNeeded();
154  if (handlerWasNeeded && !handlerIsNeeded) {
155  handler.removeCallbacks(sensorReader);
156  }
157  if (!handlerWasNeeded && handlerIsNeeded) {
158  previousState = State.UNKNOWN;
159  handler.post(sensorReader);
160  }
161  }
162 
163  @SimpleEvent(description = "Touch sensor has been pressed.")
164  public void Pressed() {
165  EventDispatcher.dispatchEvent(this, "Pressed");
166  }
167 
172  @SimpleProperty(description = "Whether the Released event should fire when the touch sensor is" +
173  " released.",
174  category = PropertyCategory.BEHAVIOR)
175  public boolean ReleasedEventEnabled() {
176  return releasedEventEnabled;
177  }
178 
183  @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN, defaultValue = "False")
185  public void ReleasedEventEnabled(boolean enabled) {
186  boolean handlerWasNeeded = isHandlerNeeded();
187 
188  releasedEventEnabled = enabled;
189 
190  boolean handlerIsNeeded = isHandlerNeeded();
191  if (handlerWasNeeded && !handlerIsNeeded) {
192  handler.removeCallbacks(sensorReader);
193  }
194  if (!handlerWasNeeded && handlerIsNeeded) {
195  previousState = State.UNKNOWN;
196  handler.post(sensorReader);
197  }
198  }
199 
200  @SimpleEvent(description = "Touch sensor has been released.")
201  public void Released() {
202  EventDispatcher.dispatchEvent(this, "Released");
203  }
204 
205  private boolean isHandlerNeeded() {
206  return pressedEventEnabled || releasedEventEnabled;
207  }
208 
209  // Deleteable implementation
210 
211  @Override
212  public void onDelete() {
213  handler.removeCallbacks(sensorReader);
214  super.onDelete();
215  }
216 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.runtime.LegoMindstormsNxtBase.getSWORDValueFromBytes
final int getSWORDValueFromBytes(byte[] bytes, int offset)
Definition: LegoMindstormsNxtBase.java:411
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components.runtime.LegoMindstormsNxtBase.bluetooth
BluetoothClient bluetooth
Definition: LegoMindstormsNxtBase.java:77
com.google.appinventor.components.runtime.NxtTouchSensor.NxtTouchSensor
NxtTouchSensor(ComponentContainer container)
Definition: NxtTouchSensor.java:51
com.google.appinventor.components.runtime.NxtTouchSensor.PressedEventEnabled
boolean PressedEventEnabled()
Definition: NxtTouchSensor.java:138
com.google.appinventor.components
com.google.appinventor.components.runtime.LegoMindstormsNxtSensor.setSensorPort
final void setSensorPort(String sensorPortLetter)
Definition: LegoMindstormsNxtSensor.java:78
com.google.appinventor.components.runtime.BluetoothConnectionBase.IsConnected
final boolean IsConnected()
Definition: BluetoothConnectionBase.java:204
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.runtime.NxtTouchSensor.IsPressed
boolean IsPressed()
Definition: NxtTouchSensor.java:102
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.annotations.SimpleEvent
Definition: SimpleEvent.java:20
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.NxtTouchSensor.ReleasedEventEnabled
void ReleasedEventEnabled(boolean enabled)
Definition: NxtTouchSensor.java:185
com.google.appinventor.components.runtime.LegoMindstormsNxtSensor.SensorPort
String SensorPort()
Definition: LegoMindstormsNxtSensor.java:70
com.google.appinventor.components.runtime.LegoMindstormsNxtBase.getInputValues
final byte[] getInputValues(String functionName, int port)
Definition: LegoMindstormsNxtBase.java:168
com.google.appinventor.components.runtime.NxtTouchSensor.Released
void Released()
Definition: NxtTouchSensor.java:201
com.google.appinventor.components.runtime.NxtTouchSensor.initializeSensor
void initializeSensor(String functionName)
Definition: NxtTouchSensor.java:86
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.runtime.LegoMindstormsNxtSensor
Definition: LegoMindstormsNxtSensor.java:21
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime.Deleteable
Definition: Deleteable.java:15
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.LegoMindstormsNxtBase.checkBluetooth
final boolean checkBluetooth(String functionName)
Definition: LegoMindstormsNxtBase.java:250
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.NxtTouchSensor.PressedEventEnabled
void PressedEventEnabled(boolean enabled)
Definition: NxtTouchSensor.java:148
com.google.appinventor.components.runtime.LegoMindstormsNxtSensor.port
int port
Definition: LegoMindstormsNxtSensor.java:56
com.google.appinventor.components.runtime.LegoMindstormsNxtBase.getBooleanValueFromBytes
final boolean getBooleanValueFromBytes(byte[] bytes, int offset)
Definition: LegoMindstormsNxtBase.java:399
com.google
com
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_LEGO_NXT_SENSOR_PORT
static final String PROPERTY_TYPE_LEGO_NXT_SENSOR_PORT
Definition: PropertyTypeConstants.java:101
com.google.appinventor.components.runtime.NxtTouchSensor.Pressed
void Pressed()
Definition: NxtTouchSensor.java:164
com.google.appinventor.components.runtime.NxtTouchSensor
Definition: NxtTouchSensor.java:37
com.google.appinventor.components.runtime.LegoMindstormsNxtBase.setInputMode
final void setInputMode(String functionName, int port, int sensorType, int sensorMode)
Definition: LegoMindstormsNxtBase.java:158
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor.components.runtime.NxtTouchSensor.ReleasedEventEnabled
boolean ReleasedEventEnabled()
Definition: NxtTouchSensor.java:175
com.google.appinventor
com.google.appinventor.components.runtime.NxtTouchSensor.onDelete
void onDelete()
Definition: NxtTouchSensor.java:212