Class Sensor

  extended byjosx.platform.rcx.Sensor
All Implemented Interfaces:

public class Sensor
extends Object
implements ListenerCaller

Abstraction for a sensor (considerably changed since alpha5). There are three Sensor instances available: Sensor.S1, Sensor.S2 and Sensor.S3. They correspond to sensor inputs labeled 1, 2 and 3 in the RCX, respectively. Before using a sensor, you should set its mode and type with setTypeAndMode using constants defined in SensorConstants. You should also activate the sensor.

You can poll for sensor values in a loop using the readValue method or one of the other read methods. There is also a low level method which can be used when maximum performance is required. Another way to monitor sensor values is to add a SensorListener. All sensor events are dispatched to listeners by a single thread created by this class. The thread is a daemon thread and so will not prevent termination of an application if all other threads have exited.


   Sensor.S1.setTypeAndMode (3, 0x80);
   Sensor.S1.addSensorListener (new SensorListener() {
     public void stateChanged (Sensor src, int oldValue, int newValue) {
       // Will be called whenever sensor value changes
       LCD.showNumber (newValue);
       try {
         Thread.sleep (100);
       } catch (InterruptedException e) {
         // ignore

See Also:
SensorConstants, SensorListener

Field Summary
static Sensor S1
          Sensor labeled 1 on RCX.
static Sensor S2
          Sensor labeled 2 on RCX.
static Sensor S3
          Sensor labeled 3 on RCX.
static Sensor[] SENSORS
          Array containing all three sensors [0..2].
Method Summary
 void activate()
          Activates the sensor.
 void addSensorListener(SensorListener aListener)
          Adds a sensor listener.
 void callListeners()
 int getId()
          Return the ID of the sensor.
 void passivate()
          Passivates the sensor.
 boolean readBooleanValue()
          Reads the boolean value of the sensor.
 int readRawValue()
          Reads the raw value of the sensor.
static int readSensorValue(int aSensorId, int aRequestType)
          Low-level API for reading sensor values.
 int readValue()
          Reads the canonical value of the sensor.
 void setPreviousValue(int aValue)
          Resets the canonical sensor value.
 void setTypeAndMode(int aType, int aMode)
          Sets the sensor's mode and type.
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait

Field Detail


public static final Sensor S1
Sensor labeled 1 on RCX.


public static final Sensor S2
Sensor labeled 2 on RCX.


public static final Sensor S3
Sensor labeled 3 on RCX.


public static final Sensor[] SENSORS
Array containing all three sensors [0..2].

Method Detail


public final int readValue()
Reads the canonical value of the sensor.


public final int readRawValue()
Reads the raw value of the sensor.


public final boolean readBooleanValue()
Reads the boolean value of the sensor.


public final int getId()
Return the ID of the sensor. One of 0, 1 or 2.


public void addSensorListener(SensorListener aListener)
Adds a sensor listener.

NOTE 1: You can add at most 8 listeners.
NOTE 2: Synchronizing inside listener methods could result in a deadlock.

See Also:


public final void activate()
Activates the sensor. This method should be called if you want to get accurate values from the sensor. In the case of light sensors, you should see the led go on when you call this method.


public final void passivate()
Passivates the sensor.


public final void setTypeAndMode(int aType,
                                 int aMode)
Sets the sensor's mode and type. If this method isn't called, the default type is 3 (LIGHT) and the default mode is 0x80 (PERCENT).

aType - 0 = RAW, 1 = TOUCH, 2 = TEMP, 3 = LIGHT, 4 = ROT.
aMode - 0x00 = RAW, 0x20 = BOOL, 0x40 = EDGE, 0x60 = PULSE, 0x80 = PERCENT, 0xA0 = DEGC, 0xC0 = DEGF, 0xE0 = ANGLE. Also, mode can be OR'd with slope (0..31).
See Also:


public final void setPreviousValue(int aValue)
Resets the canonical sensor value. This may be useful for rotation sensors.


public static int readSensorValue(int aSensorId,
                                  int aRequestType)
Low-level API for reading sensor values.

aSensorId - Sensor ID (0..2).
aRequestType - 0 = raw value, 1 = canonical value, 2 = boolean value.


public void callListeners()
Specified by:
callListeners in interface ListenerCaller