AI2 Component  (Version nb184)
Ev3TouchSensor.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2016 MIT, All rights reserved
3 // Released under the Apache License, Version 2.0
4 // http://www.apache.org/licenses/LICENSE-2.0
5 
6 package com.google.appinventor.components.runtime;
7 
18 import android.os.Handler;
19 
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,
33  nonVisible = true,
34  iconName = "images/legoMindstormsEv3.png")
35 @SimpleObject
36 public class Ev3TouchSensor extends LegoMindstormsEv3Sensor implements Deleteable {
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;
42 
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;
50 
54  public Ev3TouchSensor(ComponentContainer container) {
55  super(container, "Ev3TouchSensor");
56 
57  eventHandler = new Handler();
58  sensorValueChecker = new Runnable() {
59  public void run() {
60  String functionName = "";
61 
62  if (bluetooth != null && bluetooth.IsConnected()) {
63  int currentPressedValue = getPressedValue(functionName);
64 
65  if (savedPressedValue < 0) {
66  savedPressedValue = currentPressedValue;
67  eventHandler.postDelayed(this, DELAY_MILLISECONDS);
68  return;
69  }
70 
71  if (savedPressedValue < SENSOR_VALUE_THRESHOLD) {
72  if (releasedEventEnabled && currentPressedValue >= SENSOR_VALUE_THRESHOLD)
73  Pressed();
74  } else {
75  if (pressedEventEnabled && currentPressedValue < SENSOR_VALUE_THRESHOLD)
76  Released();
77  }
78 
79  savedPressedValue = currentPressedValue;
80  }
81 
82  eventHandler.postDelayed(this, DELAY_MILLISECONDS);
83  }
84  };
85  eventHandler.post(sensorValueChecker);
86 
87  PressedEventEnabled(false);
88  ReleasedEventEnabled(false);
89  }
90 
94  @SimpleFunction(description = "Returns true if the touch sensor is pressed.")
95  public boolean IsPressed() {
96  String functionName = "IsPressed";
97  return getPressedValue(functionName) >= SENSOR_VALUE_THRESHOLD;
98  }
99 
105  defaultValue = "False")
107  public void PressedEventEnabled(boolean enabled) {
108  pressedEventEnabled = enabled;
109  }
110 
115  @SimpleProperty(description = "Whether the Released event should fire when the touch sensor is " +
116  "pressed.",
117  category = PropertyCategory.BEHAVIOR)
118  public boolean PressedEventEnabled() {
119  return pressedEventEnabled;
120  }
121 
125  @SimpleEvent(description = "Called when the touch sensor is pressed.")
126  public void Pressed() {
127  EventDispatcher.dispatchEvent(this, "Pressed");
128  }
129 
134  @SimpleProperty(description = "Whether the Released event should fire when the touch sensor is " +
135  "released.",
136  category = PropertyCategory.BEHAVIOR)
137  public boolean ReleasedEventEnabled() {
138  return releasedEventEnabled;
139  }
140 
146  defaultValue = "False")
148  public void ReleasedEventEnabled(boolean enabled) {
149  releasedEventEnabled = enabled;
150  }
151 
155  @SimpleEvent(description = "Called when the touch sensor is pressed.")
156  public void Released() {
157  EventDispatcher.dispatchEvent(this, "Released");
158  }
159 
160  private int getPressedValue(String functionName) {
161  int value = readInputPercentage(functionName,
162  0, // assume layer = 0
164  SENSOR_TYPE,
165  mode);
166  return value;
167  }
168 
169  // Deleteable implementation
170  @Override
171  public void onDelete() {
172  eventHandler.removeCallbacks(sensorValueChecker);
173  super.onDelete();
174  }
175 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.runtime.LegoMindstormsEv3Sensor.sensorPortNumber
int sensorPortNumber
Definition: LegoMindstormsEv3Sensor.java:26
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.LegoMindstormsEv3Base.bluetooth
BluetoothClient bluetooth
Definition: LegoMindstormsEv3Base.java:30
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components
com.google.appinventor.components.runtime.LegoMindstormsEv3Sensor.readInputPercentage
final int readInputPercentage(String functionName, int layer, int no, int type, int mode)
Definition: LegoMindstormsEv3Sensor.java:61
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.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.Ev3TouchSensor.Ev3TouchSensor
Ev3TouchSensor(ComponentContainer container)
Definition: Ev3TouchSensor.java:54
com.google.appinventor.components.runtime.Ev3TouchSensor.ReleasedEventEnabled
boolean ReleasedEventEnabled()
Definition: Ev3TouchSensor.java:137
com.google.appinventor.components.runtime.Ev3TouchSensor
Definition: Ev3TouchSensor.java:36
com.google.appinventor.components.runtime.Ev3TouchSensor.PressedEventEnabled
void PressedEventEnabled(boolean enabled)
Definition: Ev3TouchSensor.java:107
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.Ev3TouchSensor.Pressed
void Pressed()
Definition: Ev3TouchSensor.java:126
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.Ev3TouchSensor.ReleasedEventEnabled
void ReleasedEventEnabled(boolean enabled)
Definition: Ev3TouchSensor.java:148
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime.LegoMindstormsEv3Sensor
Definition: LegoMindstormsEv3Sensor.java:24
com.google.appinventor.components.runtime.Ev3TouchSensor.IsPressed
boolean IsPressed()
Definition: Ev3TouchSensor.java:95
com.google.appinventor.components.runtime.Deleteable
Definition: Deleteable.java:15
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.runtime.Ev3TouchSensor.Released
void Released()
Definition: Ev3TouchSensor.java:156
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google
com
com.google.appinventor.components.runtime.Ev3TouchSensor.PressedEventEnabled
boolean PressedEventEnabled()
Definition: Ev3TouchSensor.java:118
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor.components.runtime.Ev3TouchSensor.onDelete
void onDelete()
Definition: Ev3TouchSensor.java:171
com.google.appinventor