lejos.nxt
Class SensorPort

java.lang.Object
  extended by lejos.nxt.SensorPort
All Implemented Interfaces:
ADSensorPort, BasicSensorPort, I2CPort, LegacySensorPort, ListenerCaller, SensorConstants

public class SensorPort
extends Object
implements LegacySensorPort, I2CPort, ListenerCaller

Abstraction for a NXT input port.


Nested Class Summary
protected  class SensorPort.ColorSensorReader
          Lego Color Sensor driver.
protected  class SensorPort.SensorReader
          The SensorReader class provides a way of performing type dependent way to obtain data froma sensor.
protected  class SensorPort.StandardReader
           
 
Field Summary
static int DIGI_0_ON
           
static int DIGI_1_ON
           
static int DIGI_OFF
           
static int DIGI_UNUSED
           
static SensorPort[] PORTS
          Array containing all three ports [0..3].
static int POWER_9V
           
static int POWER_RCX9V
           
static int POWER_STD
          Power types.
static SensorPort S1
          Port labeled 1 on NXT.
static SensorPort S2
          Port labeled 2 on NXT.
static SensorPort S3
          Port labeled 3 on NXT.
static SensorPort S4
          Port labeled 4 on NXT.
static int SP_ANA
           
static int SP_DIGI0
           
static int SP_DIGI1
           
static int SP_MODE_ADC
           
static int SP_MODE_INPUT
           
static int SP_MODE_OFF
           
static int SP_MODE_OUTPUT
           
 
Fields inherited from interface lejos.nxt.SensorConstants
MODE_ANGLESTEP, MODE_BOOLEAN, MODE_CELSIUS, MODE_FARENHEIT, MODE_PCTFULLSCALE, MODE_PERIODCOUNTER, MODE_RAW, MODE_TRANSITIONCNT, TYPE_ANGLE, TYPE_COLORBLUE, TYPE_COLORFULL, TYPE_COLORGREEN, TYPE_COLORNONE, TYPE_COLORRED, TYPE_CUSTOM, TYPE_HISPEED, TYPE_LIGHT_ACTIVE, TYPE_LIGHT_INACTIVE, TYPE_LOWSPEED, TYPE_LOWSPEED_9V, TYPE_NO_SENSOR, TYPE_REFLECTION, TYPE_SOUND_DB, TYPE_SOUND_DBA, TYPE_SWITCH, TYPE_TEMPERATURE
 
Fields inherited from interface lejos.nxt.I2CPort
ALWAYS_ACTIVE, LEGO_MODE, NO_RELEASE, STANDARD_MODE
 
Constructor Summary
protected SensorPort(int aId)
           
 
Method Summary
 void activate()
          Activates an RCX sensor.
 void addSensorPortListener(SensorPortListener aListener)
          Adds a port listener.
 void callListeners()
          Call Port Listeners.
 void enableColorSensor()
          Enable the use of the Color Light Sensor on this port.
 int getId()
          Return the ID of the port.
 int getMode()
          Returns mode compatible with Lego firmware.
 int getSensorPin(int pin)
          Read the current state of a sensor port pin
 int getType()
          Returns type compatible with Lego firmware.
 int i2cBusy()
          Low-level method to test if I2C connection is busy.
static int i2cBusyById(int aPortId)
          Low-level method to test if I2C connection is busy.
 int i2cComplete(byte[] buffer, int numBytes)
          Complete an I2C operation and transfer any read bytes
static int i2cCompleteById(int aPortId, byte[] buffer, int numBytes)
          Complete and I2C operation and retrieve any data read.
 void i2cDisable()
          Low-level method to disable I2C on the port.
static void i2cDisableById(int aPortId)
          Low-level method to disable I2C on the port.
 void i2cEnable(int mode)
          Low-level method to enable I2C on the port.
static void i2cEnableById(int aPortId, int mode)
          Low-level method to enable I2C on the port.
 int i2cStart(int address, int internalAddress, int numInternalBytes, byte[] buffer, int numBytes, int transferType)
          Low-level method to start an I2C transaction.
static int i2cStartById(int aPortId, int address, int internalAddress, int numInternalBytes, byte[] buffer, int numBytes, int transferType)
          Low-level method to start an I2C transaction.
 void passivate()
          Passivates an RCX sensor.
 boolean readBooleanValue()
          Reads the boolean value of the sensor.
 int readRawValue()
          Reads the raw value of the sensor.
 int readRawValues(int[] values)
          Return a variable number of raw sensor values
 int readSensorPin(int pin)
          Read the current ADC value from a sensor port pin
 int readValue()
          Returns value compatible with Lego firmware.
 int readValues(int[] values)
          Return a variable number of sensor values
 void reset()
          Reset this port and attempt to reset any attached device.
 void setMode(int mode)
          Sets mode compatible with Lego firmware.
 void setPowerType(int type)
          Low-level method to set the input power setting for a sensor.
 void setSensorPin(int pin, int val)
          Set the output state of a sensor pin
 void setSensorPinMode(int pin, int mode)
          Low level method to set the operating mode for a sensor pin.
 void setType(int newType)
          Sets type compatible with Lego firmware.
 void setTypeAndMode(int type, int mode)
          Sets type and mode compatible with Lego firmware.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

POWER_STD

public static final int POWER_STD
Power types.

See Also:
Constant Field Values

POWER_RCX9V

public static final int POWER_RCX9V
See Also:
Constant Field Values

POWER_9V

public static final int POWER_9V
See Also:
Constant Field Values

SP_DIGI0

public static final int SP_DIGI0
See Also:
Constant Field Values

SP_DIGI1

public static final int SP_DIGI1
See Also:
Constant Field Values

SP_ANA

public static final int SP_ANA
See Also:
Constant Field Values

SP_MODE_OFF

public static final int SP_MODE_OFF
See Also:
Constant Field Values

SP_MODE_INPUT

public static final int SP_MODE_INPUT
See Also:
Constant Field Values

SP_MODE_OUTPUT

public static final int SP_MODE_OUTPUT
See Also:
Constant Field Values

SP_MODE_ADC

public static final int SP_MODE_ADC
See Also:
Constant Field Values

DIGI_UNUSED

public static final int DIGI_UNUSED
See Also:
Constant Field Values

DIGI_OFF

public static final int DIGI_OFF
See Also:
Constant Field Values

DIGI_0_ON

public static final int DIGI_0_ON
See Also:
Constant Field Values

DIGI_1_ON

public static final int DIGI_1_ON
See Also:
Constant Field Values

S1

public static final SensorPort S1
Port labeled 1 on NXT.


S2

public static final SensorPort S2
Port labeled 2 on NXT.


S3

public static final SensorPort S3
Port labeled 3 on NXT.


S4

public static final SensorPort S4
Port labeled 4 on NXT.


PORTS

public static final SensorPort[] PORTS
Array containing all three ports [0..3].

Constructor Detail

SensorPort

protected SensorPort(int aId)
Method Detail

enableColorSensor

public void enableColorSensor()
Enable the use of the Color Light Sensor on this port. The code for this sensor is relatively large, so it is not presnt by default. Calling this function will enable this code. NOTE: Calling this function will reset the port. If you are using higher level inetrfaces (like the ColorLightSensor class, then this call will be made automatically.).


reset

public void reset()
Reset this port and attempt to reset any attached device.


getId

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

Returns:
The Id of this sensor

addSensorPortListener

public void addSensorPortListener(SensorPortListener aListener)
Adds a port listener.

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

Parameters:
aListener - Listener for call backs
See Also:
SensorPortListener

activate

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

Specified by:
activate in interface LegacySensorPort

passivate

public final void passivate()
Passivates an RCX sensor.

Specified by:
passivate in interface LegacySensorPort

getMode

public int getMode()
Returns mode compatible with Lego firmware.

Specified by:
getMode in interface BasicSensorPort
Returns:
the current mode

getType

public int getType()
Returns type compatible with Lego firmware.

Specified by:
getType in interface BasicSensorPort
Returns:
The type of the sensor

setTypeAndMode

public void setTypeAndMode(int type,
                           int mode)
Sets type and mode compatible with Lego firmware.

Specified by:
setTypeAndMode in interface BasicSensorPort
Parameters:
type - the sensor type
mode - the sensor mode

setType

public void setType(int newType)
Sets type compatible with Lego firmware.

Specified by:
setType in interface BasicSensorPort
Parameters:
newType - the sensor type

setMode

public void setMode(int mode)
Sets mode compatible with Lego firmware.

Specified by:
setMode in interface BasicSensorPort
Parameters:
mode - the mode to set.

readRawValue

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

Specified by:
readRawValue in interface ADSensorPort
Returns:
the raw sensor value

readValue

public int readValue()
Returns value compatible with Lego firmware.

Specified by:
readValue in interface ADSensorPort
Returns:
the computed value

readValues

public int readValues(int[] values)
Return a variable number of sensor values

Parameters:
values - An array in which to return the sensor values.
Returns:
The number of values returned.

readRawValues

public int readRawValues(int[] values)
Return a variable number of raw sensor values

Parameters:
values - An array in which to return the sensor values.
Returns:
The number of values returned.

readBooleanValue

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

Specified by:
readBooleanValue in interface ADSensorPort
Returns:
the boolean state of the sensor

setPowerType

public void setPowerType(int type)
Low-level method to set the input power setting for a sensor. Values are: 0 - no power, 1 RCX active power, 2 power always on.

Parameters:
type - Power type to use

callListeners

public void callListeners()
Call Port Listeners. Used by ListenerThread.

Specified by:
callListeners in interface ListenerCaller

i2cEnableById

public static void i2cEnableById(int aPortId,
                                 int mode)
Low-level method to enable I2C on the port.

Parameters:
aPortId - The port number for this device
mode - I/O mode to use

i2cDisableById

public static void i2cDisableById(int aPortId)
Low-level method to disable I2C on the port.

Parameters:
aPortId - The port number for this device

i2cBusyById

public static int i2cBusyById(int aPortId)
Low-level method to test if I2C connection is busy.

Parameters:
aPortId - The port number for this device
Returns:
> 0 if busy 0 if not

i2cStartById

public static int i2cStartById(int aPortId,
                               int address,
                               int internalAddress,
                               int numInternalBytes,
                               byte[] buffer,
                               int numBytes,
                               int transferType)
Low-level method to start an I2C transaction.

Parameters:
aPortId - The port number for this device
address - The I2C address of the device
internalAddress - The internal address to use for this operation
numInternalBytes - The number of bytes in the internal address
buffer - The buffer for write operations
numBytes - Number of bytes to write or read
transferType - 1==write 0==read
Returns:
< 0 if there is an error

i2cCompleteById

public static int i2cCompleteById(int aPortId,
                                  byte[] buffer,
                                  int numBytes)
Complete and I2C operation and retrieve any data read.

Parameters:
aPortId - The Port number for the device
buffer - The buffer to be used for read operations
numBytes - Number of bytes to read
Returns:
< 0 if the is an error, or number of bytes transferred

i2cEnable

public void i2cEnable(int mode)
Low-level method to enable I2C on the port.

Specified by:
i2cEnable in interface I2CPort
Parameters:
mode - The operating mode for the device

i2cDisable

public void i2cDisable()
Low-level method to disable I2C on the port.

Specified by:
i2cDisable in interface I2CPort

i2cBusy

public int i2cBusy()
Low-level method to test if I2C connection is busy.

Specified by:
i2cBusy in interface I2CPort
Returns:
> 0 if the device is busy 0 if it is not

i2cStart

public int i2cStart(int address,
                    int internalAddress,
                    int numInternalBytes,
                    byte[] buffer,
                    int numBytes,
                    int transferType)
Low-level method to start an I2C transaction.

Specified by:
i2cStart in interface I2CPort
Parameters:
address - Address of the device
internalAddress - Internal register address for this operation
numInternalBytes - Size of the internal address
buffer - Buffer for write operations
numBytes - Number of bytes to read/write
transferType - 1==write 0 ==read
Returns:
< 0 error

i2cComplete

public int i2cComplete(byte[] buffer,
                       int numBytes)
Complete an I2C operation and transfer any read bytes

Specified by:
i2cComplete in interface I2CPort
Parameters:
buffer - Buffer for read data
numBytes - Number of bytes to read
Returns:
< 0 error otherwise number of bytes read.

setSensorPinMode

public void setSensorPinMode(int pin,
                             int mode)
Low level method to set the operating mode for a sensor pin.

Parameters:
pin - The pin id
mode - The new mode

setSensorPin

public void setSensorPin(int pin,
                         int val)
Set the output state of a sensor pin

Parameters:
pin - The pin id
val - The new output value (0/1)

getSensorPin

public int getSensorPin(int pin)
Read the current state of a sensor port pin

Parameters:
pin - The pin id.
Returns:
The current pin state (0/1)

readSensorPin

public int readSensorPin(int pin)
Read the current ADC value from a sensor port pin

Parameters:
pin - The id of the pin to read (SP_DIGI1/SP_ANA)
Returns:
The return from the ADC