AI2 Component  (Version nb184)
LegoMindstormsNxtSensor.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 
13 
20 @SimpleObject
21 public abstract class LegoMindstormsNxtSensor extends LegoMindstormsNxtBase {
22  static class SensorValue<T> {
23  final boolean valid;
24  final T value;
25  SensorValue(boolean valid, T value) {
26  this.valid = valid;
27  this.value = value;
28  }
29  }
30 
31  static final int SENSOR_TYPE_NO_SENSOR = 0x00;
32  static final int SENSOR_TYPE_SWITCH = 0x01;
33  static final int SENSOR_TYPE_TEMPERATURE = 0x02;
34  static final int SENSOR_TYPE_REFLECTION = 0x03;
35  static final int SENSOR_TYPE_ANGLE = 0x04;
36  static final int SENSOR_TYPE_LIGHT_ACTIVE = 0x05;
37  static final int SENSOR_TYPE_LIGHT_INACTIVE = 0x06;
38  static final int SENSOR_TYPE_SOUND_DB = 0x07;
39  static final int SENSOR_TYPE_SOUND_DBA = 0x08;
40  static final int SENSOR_TYPE_CUSTOM = 0x09;
41  static final int SENSOR_TYPE_LOWSPEED = 0x0A;
42  static final int SENSOR_TYPE_LOWSPEED_9V = 0x0B;
43 
44  static final int SENSOR_MODE_RAWMODE = 0x00;
45  static final int SENSOR_MODE_BOOLEANMODE = 0x20;
46  static final int SENSOR_MODE_TRANSITIONCNTMODE = 0x40;
47  static final int SENSOR_MODE_PERIODCOUNTERMODE = 0x60;
48  static final int SENSOR_MODE_PCTFULLSCALEMODE = 0x80;
49  static final int SENSOR_MODE_CELSIUSMODE = 0xA0;
50  static final int SENSOR_MODE_FAHRENHEITMODE = 0xC0;
51  static final int SENSOR_MODE_ANGLESTEPMODE = 0xE0;
52  static final int SENSOR_MODE_MASK_SLOPE = 0x1F;
53  static final int SENSOR_MODE_MASK_MODE = 0xE0;
54 
55  private String sensorPortLetter; // "1" - "4"
56  protected int port;
57 
61  protected LegoMindstormsNxtSensor(ComponentContainer container, String logTag) {
62  super(container, logTag);
63  }
64 
68  @SimpleProperty(description = "The sensor port that the sensor is connected to.",
69  category = PropertyCategory.BEHAVIOR, userVisible = false)
70  public String SensorPort() {
71  return sensorPortLetter;
72  }
73 
74  // Since different kinds of sensors need to have a different defaultValue for the SensorPort
75  // property, the SensorPort property setter method must be defined in each subclass.
76  public abstract void SensorPort(String sensorPortLetter);
77 
78  protected final void setSensorPort(String sensorPortLetter) {
79  String functionName = "SensorPort";
80  int port;
81  try {
82  port = convertSensorPortLetterToNumber(sensorPortLetter);
83  } catch (IllegalArgumentException e) {
84  form.dispatchErrorOccurredEvent(this, functionName,
86  return;
87  }
88 
89  this.sensorPortLetter = sensorPortLetter;
90  this.port = port;
91  if (bluetooth != null && bluetooth.IsConnected()) {
92  initializeSensor(functionName);
93  }
94  }
95 
96  @Override
97  public void afterConnect(BluetoothConnectionBase bluetoothConnection) {
98  initializeSensor("Connect");
99  }
100 
101  protected abstract void initializeSensor(String functionName);
102 }
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.runtime.LegoMindstormsNxtBase.bluetooth
BluetoothClient bluetooth
Definition: LegoMindstormsNxtBase.java:77
com.google.appinventor.components.runtime.LegoMindstormsNxtSensor.afterConnect
void afterConnect(BluetoothConnectionBase bluetoothConnection)
Definition: LegoMindstormsNxtSensor.java:97
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_NXT_INVALID_SENSOR_PORT
static final int ERROR_NXT_INVALID_SENSOR_PORT
Definition: ErrorMessages.java:57
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.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.LegoMindstormsNxtSensor.LegoMindstormsNxtSensor
LegoMindstormsNxtSensor(ComponentContainer container, String logTag)
Definition: LegoMindstormsNxtSensor.java:61
com.google.appinventor.components.runtime.LegoMindstormsNxtSensor.SensorPort
String SensorPort()
Definition: LegoMindstormsNxtSensor.java:70
com.google.appinventor.components.runtime.LegoMindstormsNxtBase.logTag
final String logTag
Definition: LegoMindstormsNxtBase.java:74
com.google.appinventor.components.runtime.LegoMindstormsNxtSensor
Definition: LegoMindstormsNxtSensor.java:21
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.runtime.BluetoothConnectionBase
Definition: BluetoothConnectionBase.java:41
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.LegoMindstormsNxtSensor.initializeSensor
abstract void initializeSensor(String functionName)
com.google.appinventor.components.runtime.LegoMindstormsNxtBase.convertSensorPortLetterToNumber
final int convertSensorPortLetterToNumber(String sensorPortLetter)
Definition: LegoMindstormsNxtBase.java:474
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.LegoMindstormsNxtBase
Definition: LegoMindstormsNxtBase.java:29
com.google.appinventor.components.runtime.Form.dispatchErrorOccurredEvent
void dispatchErrorOccurredEvent(final Component component, final String functionName, final int errorNumber, final Object... messageArgs)
Definition: Form.java:1011
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.LegoMindstormsNxtSensor.port
int port
Definition: LegoMindstormsNxtSensor.java:56
com.google
com
com.google.appinventor.components.runtime.AndroidNonvisibleComponent.form
final Form form
Definition: AndroidNonvisibleComponent.java:19
com.google.appinventor.components.annotations
com.google.appinventor