josx.platform.rcx
Class ReducedSensor

java.lang.Object
  |
  +--josx.platform.rcx.ReducedSensor

public class ReducedSensor
extends Object

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.

Example:

   Sensor.S1.setTypeAndMode (3, 0x80);
   Sensor.S1.activate();
   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 ReducedSensor S1
          Sensor labeled 1 on RCX.
static ReducedSensor S2
          Sensor labeled 2 on RCX.
 
Method Summary
 void activate()
          Activates the sensor.
 void passivate()
          Passivates 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

S1

public static final ReducedSensor S1
Sensor labeled 1 on RCX.


S2

public static final ReducedSensor S2
Sensor labeled 2 on RCX.

Method Detail

readValue

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


readRawValue

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


activate

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.


passivate

public final void passivate()
Passivates the sensor.


setTypeAndMode

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).

Parameters:
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:
SensorConstants

setPreviousValue

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


readSensorValue

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

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