lejos.nxt
Class NXTEvent

java.lang.Object
  extended by lejos.nxt.NXTEvent

public class NXTEvent
extends Object

This class allows communication of event data between the leJOS firmware and the leJOS low level classes. It can be used to detect I/O completion, Port values changing, button presses etc. To use create a class having the required device type and filter (this may identify a particular port, or I/O operation). Then call the waitEvent function to wait for events from the firmware. This call will block until either the firmware signals an event or the timeout occurs. Upon completion the eventData field will contain information about the event(s) that have been reported. Events themselves are normally reset by calling the associated function to read/write the associated device. If an event is not cleared, it will be reported again in subsequent calls to eventWait.

NOTE: This is a low level system interface and should probably not be used directly by user code.

Author:
andy

Field Summary
static int ANALOG_PORTS
          Event type for the Analogue ports
static int BLUETOOTH
          Event type for the Bluetooth device
static int BUTTONS
          Event type for the NXT Buttons
static int I2C_PORTS
          Event type for the i2c ports
static int NONE
          Event type for no hardware events
static int RS485
          Event type for the RS485 device
static int SYSTEM
          Event type for system events
static int TIMEOUT
           
static int USB
          Event type for the USB device
static int USER1
          These bits are reserved in the eventData field to indicate that a user event has occurred.
static int USER2
           
static int USER3
           
static int USER4
           
static long WAIT_FOREVER
          Value used to make a timeout be forever.
 
Constructor Summary
NXTEvent()
           
 
Method Summary
static NXTEvent allocate(int type, int filter, int update)
          Create a new event ready for use.
 void clearEvent(int event)
          Clear an event.
 void free()
          Release an event.
 int getEventData()
           
 int getFilter()
          Return the current filter settings.
 void notifyEvent(int event)
          This call can be used to raise a user event.
 int registerEvent()
          Register this event with the system.
 void setFilter(int filter)
          Set the filter to be applied to this event.
 int unregisterEvent()
          Unregister this event.
 int waitEvent(int newFilter, long timeout)
          Wait for an event to occur using the specified filter or for the specified timeout.
 int waitEvent(long timeout)
          Wait for an event to occur or for the specified timeout.
static boolean waitEvent(NXTEvent[] events, long timeout)
          Wait for multiple events.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NONE

public static final int NONE
Event type for no hardware events

See Also:
Constant Field Values

BLUETOOTH

public static final int BLUETOOTH
Event type for the Bluetooth device

See Also:
Constant Field Values

USB

public static final int USB
Event type for the USB device

See Also:
Constant Field Values

RS485

public static final int RS485
Event type for the RS485 device

See Also:
Constant Field Values

ANALOG_PORTS

public static final int ANALOG_PORTS
Event type for the Analogue ports

See Also:
Constant Field Values

I2C_PORTS

public static final int I2C_PORTS
Event type for the i2c ports

See Also:
Constant Field Values

BUTTONS

public static final int BUTTONS
Event type for the NXT Buttons

See Also:
Constant Field Values

SYSTEM

public static final int SYSTEM
Event type for system events

See Also:
Constant Field Values

TIMEOUT

public static final int TIMEOUT
See Also:
Constant Field Values

USER1

public static final int USER1
These bits are reserved in the eventData field to indicate that a user event has occurred. User events are created by the notifyEvent method.

See Also:
Constant Field Values

USER2

public static final int USER2
See Also:
Constant Field Values

USER3

public static final int USER3
See Also:
Constant Field Values

USER4

public static final int USER4
See Also:
Constant Field Values

WAIT_FOREVER

public static final long WAIT_FOREVER
Value used to make a timeout be forever.

See Also:
Constant Field Values
Constructor Detail

NXTEvent

public NXTEvent()
Method Detail

registerEvent

public int registerEvent()
Register this event with the system. Events must be registered before they are waited on.

Returns:
>= 0 if the event has been registered < 0 if not.

unregisterEvent

public int unregisterEvent()
Unregister this event. After calling this function the event should not be waited on.

Returns:
>= 0 if the event was unregistered < 0 if not

waitEvent

public int waitEvent(long timeout)
              throws InterruptedException
Wait for an event to occur or for the specified timeout. If a timeout occurs then the TIMEOUT event bit will be set in the result. This bit is the sign bit so timeouts can be detected by testing for a -ve result.

Parameters:
timeout - the timeout in ms. Note a value of <= 0 will return immeadiately.
Returns:
the event flags
Throws:
InterruptedException

waitEvent

public int waitEvent(int newFilter,
                     long timeout)
              throws InterruptedException
Wait for an event to occur using the specified filter or for the specified timeout.

Parameters:
newFilter - The type specific filter for this wait.
timeout - the timeout in ms. Note a value of <= 0 will return immediately.
Returns:
the event flags or 0 if the event timed out
Throws:
InterruptedException

waitEvent

public static boolean waitEvent(NXTEvent[] events,
                                long timeout)
                         throws InterruptedException
Wait for multiple events.

Parameters:
events - an array of events to wait on.
timeout - the wait timeout. Note a value of <= 0 will return immediately.
Returns:
true if an event occurred, false otherwise.
Throws:
InterruptedException

notifyEvent

public void notifyEvent(int event)
This call can be used to raise a user event. User events will wake a thread from an eventWait call.

Parameters:
event -

clearEvent

public void clearEvent(int event)
Clear an event.

Parameters:
event - The events to be cleared.

getEventData

public int getEventData()

setFilter

public void setFilter(int filter)
Set the filter to be applied to this event.

Parameters:
filter - The new filter value.

getFilter

public int getFilter()
Return the current filter settings.

Returns:
the filter

allocate

public static NXTEvent allocate(int type,
                                int filter,
                                int update)
Create a new event ready for use.

Parameters:
type - The event type.
filter - The event specific filter.
update - The update period used when checking the event.
Returns:
The new event object.

free

public void free()
Release an event.