|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjosx.platform.rcx.Sensor
public class Sensor
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
}
}
});
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
public static final Sensor S2
public static final Sensor S3
public static final Sensor[] SENSORS
| Method Detail |
|---|
public final int readValue()
public final int readRawValue()
public final boolean readBooleanValue()
public final int getId()
public void addSensorListener(SensorListener aListener)
NOTE 1: You can add at most 8 listeners.
NOTE 2: Synchronizing inside listener methods could result
in a deadlock.
SensorListenerpublic final void activate()
public final void passivate()
public final void setTypeAndMode(int aType,
int aMode)
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).SensorConstantspublic final void setPreviousValue(int aValue)
public static int readSensorValue(int aSensorId,
int aRequestType)
aSensorId - Sensor ID (0..2).aRequestType - 0 = raw value, 1 = canonical value, 2 = boolean value.public void callListeners()
callListeners in interface ListenerCaller
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||