leJOS Utilities
leJOS Utilities

ButtonCounter

This class allows simple data entry by pressing the NXT buttons. It counts the number of presses of the Left and Right buttons, Pressing Left (or Right) simultaneously with Enter decrements the count.

To start counting, call one of these methods:

  • void count(String message)

  • void count(String message, int left, int right)

These methods exit when the user presses ESC. Then the values can be retrieved by

  • int getLeftCount()

  • int getRightCount()

The complete API for Button Counter is here

DataLogger

This class stores float values in an array and transmits them to the DataViewer application running on a PC. It will use BlueTooth or USB as selected by the user at run time.

Methods:

  • void writeLog(float value)

    Writes a value to the array
  • void writeLog(float v0, float v1)

    Writes two values to the array
  • void transmit()

    Transmits the data to the PC. When this method is called, a message is displayed on the NXT screen allowing the user to select USB or BlueTooth as the transmission channel

The complete API of Datalogger is here.

NXTDataLogger

This class provides logging methods for all primitive datatypes and transmits them to the NXT Charting Logger application running on a PC. It supports real time and deferred (cached) data logging using either BlueTooth or USB as implemented by the user.

Methods:

  • startRealtimeLog(NXTConnection connection)
    Start a realtime logging session using the passed NXTConnection. The setColumns() method must be called after this method is called and before the first writeLog() method is called.

  • startCachingLog()
    Sets caching (deferred) logging mode and intializes the data cache. This is the default mode at instantiation. The setColumns() method must be called after this method is called and before the first writeLog() method is called.

  • void setColumns(LogColumn[] columnDefs)
    Sets up the column definitions for your logging session.

  • void writeLog(<primitive datatype>)
    Writes a value to the cache (set using the startCachingLog() method) or logger live data stream (set using the startRealtimeLog() method), depending on the logging mode. 

  • writeComment(String comment)
    Writes a comment to the NXT Charting Logger (realtime mode only).

  • void finishLine()
    Completes one row of logged data and timestamps it.

  • sendCache(NXTConnection connection)
    Sends the log cache using passed NXTConnection. The connection must already be established. Valid only for caching (deferred) logging using startCachingLog().

  • stopLogging()
    Stops the current logging session and closes down the data streams and connection.

To support the structured nature of the API and NXT Charting Logger data requirements, there is a specific order in which the methods must be called to set up a logging session.

An example that shows how to set up and use real-time logging:


NXTDataLogger dlog = new NXTDataLogger();
NXTConnection conn = Bluetooth.waitForConnection(5000, NXTConnection.PACKET);
try {
dlog.startRealtimeLog(conn);
} catch (IOException e) {
// Do nothing. This is hideously bad.
}

dlog.setColumns(new LogColumn[] {
new LogColumn("g X", LogColumn.DT_FLOAT),
new LogColumn("g Y", LogColumn.DT_FLOAT),
new LogColumn("pitch", LogColumn.DT_FLOAT,2),// use different range axis (2)
new LogColumn("angular velocity", LogColumn.DT_FLOAT,3),// use different range axis (3)
new LogColumn("heading", LogColumn.DT_FLOAT,4)// use different range axis (4)
});

// code example below assumes gyro object has the illustrated methods
gyro.setAccScale2G();
float gx, gy, pitch;

while(Button.ESCAPE.isUp()) {
gx=gyro.getLastAccel(0);
gy=gyro.getLastAccel(1);
pitch=gyro.getLastPitch();
dlog.writeLog(gx);
dlog.writeLog(gy);
dlog.writeLog(pitch);
dlog.writeLog(gyro.getAngularVelocity());
dlog.writeLog(gyro.getHeading());
dlog.finishLine();
Delay.msDelay(50);
gyro.readAllData();
}
dlog.stopLogging();

The details and complete API of NXTDataLogger are here.

Stopwatch

This class represents an elapsed time stopwatch. You can construct as many instances as you need.

There are only two methods

  • void reset()

    resets the watch to zero

  • int elapsed()

    returns the elapsed time (since the last reset() in milliseconds.

The API of Stopwatch is here.

Timer

This class is used with a Timer Listener, to which it sends timedOut() message periodically. It uses a private thread.

Methods:

  • void start()

    call this first.

  • void setDelay(int delay)

    set the interval between calls to the listener.

  • void stop()

The Timer API is here.

Timer Listener

To use the Timer, you need a class that implements this interface. This interface has only one method.

  • void timedOut()

    which is called every time the Timer fires.

Back to top

Java Utilities

leJOS NXJ supports several of the Java data structures. They are:

It also supports other Java utilities:

Back to top