6 package com.google.appinventor.components.runtime;
19 import android.os.Handler;
30 @DesignerComponent(version = YaVersion.EV3_COLORSENSOR_COMPONENT_VERSION,
31 description =
"A component that provides a high-level interface to a color sensor on a " +
32 "LEGO MINDSTORMS EV3 robot.",
33 category = ComponentCategory.LEGOMINDSTORMS,
35 iconName =
"images/legoMindstormsEv3.png")
38 private static final int SENSOR_TYPE = 29;
39 private static final int SENSOR_MODE_REFLECTED = 0;
40 private static final int SENSOR_MODE_AMBIENT = 1;
41 private static final int SENSOR_MODE_COLOR = 2;
42 private static final String SENSOR_MODE_REFLECTED_STRING =
"reflected";
43 private static final String SENSOR_MODE_AMBIENT_STRING =
"ambient";
44 private static final String SENSOR_MODE_COLOR_STRING =
"color";
45 private static final int DEFAULT_BOTTOM_OF_RANGE = 30;
46 private static final int DEFAULT_TOP_OF_RANGE = 60;
47 private static final String DEFAULT_SENSOR_MODE_STRING = SENSOR_MODE_REFLECTED_STRING;
48 private static final int DELAY_MILLISECONDS = 50;
51 private String modeString = SENSOR_MODE_REFLECTED_STRING;
52 private Handler eventHandler;
53 private final Runnable sensorValueChecker;
54 private int bottomOfRange;
55 private int topOfRange;
56 private int previousLightLevel = 0;
58 private int previousColor = -1;
59 private boolean belowRangeEventEnabled;
60 private boolean withinRangeEventEnabled;
61 private boolean aboveRangeEventEnabled;
62 private boolean colorChangedEventEnabled;
68 super(container,
"Ev3ColorSensor");
70 eventHandler =
new Handler();
71 sensorValueChecker =
new Runnable() {
73 String functionName =
"";
76 if (mode == SENSOR_MODE_COLOR) {
77 int currentColor = getSensorValue(functionName);
79 if (previousColor < 0) {
80 previousColor = currentColor;
81 eventHandler.postDelayed(
this, DELAY_MILLISECONDS);
85 if (currentColor != previousColor && colorChangedEventEnabled)
86 ColorChanged(currentColor, toColorName(functionName, currentColor));
88 previousColor = currentColor;
90 int currentLightLevel = getSensorValue(functionName);
91 if (previousLightLevel < 0) {
92 previousLightLevel = currentLightLevel;
93 eventHandler.postDelayed(
this, DELAY_MILLISECONDS);
98 if (currentLightLevel < bottomOfRange) {
99 if (belowRangeEventEnabled && previousLightLevel >= bottomOfRange)
101 }
else if (currentLightLevel > topOfRange) {
102 if (aboveRangeEventEnabled && previousLightLevel <= topOfRange)
105 if (withinRangeEventEnabled && (previousLightLevel < bottomOfRange || previousLightLevel > topOfRange))
109 previousLightLevel = currentLightLevel;
113 eventHandler.postDelayed(
this, DELAY_MILLISECONDS);
116 eventHandler.post(sensorValueChecker);
124 Mode(DEFAULT_SENSOR_MODE_STRING);
130 @
SimpleFunction(description =
"It returns the light level in percentage, or " +
131 "-1 when the light level cannot be read.")
133 if (mode == SENSOR_MODE_COLOR)
136 String functionName =
"GetLightLevel";
137 return getSensorValue(functionName);
143 @
SimpleFunction(description =
"It returns the color code from 0 to 7 corresponding to no color, black, blue, green, yellow, red, white and brown.")
145 if (mode != SENSOR_MODE_COLOR)
148 String functionName =
"GetColorCode";
149 return getSensorValue(functionName);
155 @
SimpleFunction(description =
"Return the color name in one of \"No Color\", \"Black\", \"Blue\", \"Green\", \"Yellow\", \"Red\", \"White\", \"Brown\".")
157 if (mode != SENSOR_MODE_COLOR)
160 String functionName =
"GetColorName";
161 int colorCode = getSensorValue(functionName);
162 return toColorName(functionName, colorCode);
169 @
SimpleProperty(description =
"The bottom of the range used for the BelowRange, WithinRange, " +
170 "and AboveRange events.",
173 return bottomOfRange;
181 defaultValue =
"" + DEFAULT_BOTTOM_OF_RANGE)
184 this.bottomOfRange = bottomOfRange;
191 @
SimpleProperty(description =
"The top of the range used for the BelowRange, WithinRange, and " +
192 "AboveRange events.",
203 defaultValue =
"" + DEFAULT_TOP_OF_RANGE)
206 this.topOfRange = topOfRange;
213 @
SimpleProperty(description =
"Whether the BelowRange event should fire when the light level" +
214 " goes below the BottomOfRange.",
217 return belowRangeEventEnabled;
225 defaultValue =
"False")
228 belowRangeEventEnabled = enabled;
231 @
SimpleEvent(description =
"Light level has gone below the range.")
240 @
SimpleProperty(description =
"Whether the WithinRange event should fire when the light level " +
241 "goes between the BottomOfRange and the TopOfRange.",
244 return withinRangeEventEnabled;
252 defaultValue =
"False")
255 withinRangeEventEnabled = enabled;
258 @
SimpleEvent(description =
"Light level has gone within the range.")
267 @
SimpleProperty(description =
"Whether the AboveRange event should fire when the light level " +
268 "goes above the TopOfRange.",
271 return aboveRangeEventEnabled;
279 defaultValue =
"False")
282 aboveRangeEventEnabled = enabled;
285 @
SimpleEvent(description =
"Light level has gone above the range.")
294 @
SimpleProperty(description =
"Whether the ColorChanged event should fire when the Mode" +
295 " property is set to \"color\" and the detected color changes.",
298 return colorChangedEventEnabled;
306 defaultValue =
"False")
309 colorChangedEventEnabled = enabled;
315 @
SimpleEvent(description =
"Called when the detected color has changed. The ColorChanged event will occur " +
316 "if the Mode property is set to \"color\" and the ColorChangedEventEnabled property " +
322 private int getSensorValue(String functionName) {
330 if (mode == SENSOR_MODE_COLOR) {
356 private String toColorName(String functionName,
int colorCode) {
357 if (mode != SENSOR_MODE_COLOR)
385 @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_LEGO_EV3_COLOR_SENSOR_MODE,
386 defaultValue = DEFAULT_SENSOR_MODE_STRING)
388 public void Mode(String modeName) {
389 String functionName =
"Mode";
392 }
catch(IllegalArgumentException e) {
411 String functionName =
"SetColorMode";
413 setMode(SENSOR_MODE_COLOR_STRING);
414 }
catch(IllegalArgumentException e) {
422 @
SimpleFunction(description =
"Make the sensor read the light level with reflected light.")
424 String functionName =
"SetReflectedMode";
426 setMode(SENSOR_MODE_REFLECTED_STRING);
427 }
catch(IllegalArgumentException e) {
435 @
SimpleFunction(description =
"Make the sensor read the light level without reflected light.")
437 String functionName =
"SetAmbientMode";
439 setMode(SENSOR_MODE_AMBIENT_STRING);
440 }
catch(IllegalArgumentException e) {
445 private void setMode(String newModeString) {
447 previousLightLevel = -1;
449 if (SENSOR_MODE_REFLECTED_STRING.equals(newModeString))
450 mode = SENSOR_MODE_REFLECTED;
451 else if (SENSOR_MODE_AMBIENT_STRING.equals(newModeString))
452 mode = SENSOR_MODE_AMBIENT;
453 else if (SENSOR_MODE_COLOR_STRING.equals(newModeString))
454 mode = SENSOR_MODE_COLOR;
456 throw new IllegalArgumentException();
458 this.modeString = newModeString;
464 eventHandler.removeCallbacks(sensorValueChecker);