Class RcxBrick

java.lang.Object
io.github.jabrena.juno.api.lego.RcxBrick

public final class RcxBrick extends Object
Direct commands to an RCX brick that answer back, over the same infrared link as RcxRemote (set it up with RcxRemote.begin(int, int) first, with both the IR LED and the IR receiver module wired). It reads the touch sensors plugged into the RCX's own inputs, so a vehicle can sense obstacles without extra wiring to the Arduino. Plain Java on Infrared.

Each call is a request and a reply, which takes 100 ms or more; they return -1 when the brick does not answer (out of range, off, or the IR LED not pointing at it). The opcode and argument encodings come from the published RCX opcode tables and have not been verified on a brick.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The RCX's three sensor inputs, numbered 1 to 3 on the brick.
    static final int
     
    static final int
     
    static final int
    The highest power level of setMotorPower(int, int).
    static final int
    The RCX's three outputs, as bits so that several can be combined, e.g.
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
     
    static final int
    The six built-in sounds for playSound(int).
    static final int
     
    static final int
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    The brick's battery voltage in millivolts (about 9000 with fresh batteries, and the brick warns below about 6000), or -1 if it did not answer.
    static void
    clearSensor(int input)
    Sets the value of the sensor on input back to zero, e.g. a rotation sensor's count.
    static void
    drive(int outputs, int speed)
    Runs the outputs at speed, -7..7: the sign is the direction, the size the power level, and 0 brakes them.
    static boolean
    isPressed(int input)
    Whether the touch sensor on input is pressed; false also when the brick did not answer.
    static void
    motorFloat(int outputs)
    Switches the outputs off with the motors free to coast.
    static void
    motorOff(int outputs)
    Switches the outputs off with the motors braking.
    static void
    motorOn(int outputs)
    Turns the outputs outputs (OUTPUT_* bits) on, at the power level and in the direction last set.
    static boolean
    playSound(int sound)
    Plays one of the six built-in sounds, SOUND_*.
    static boolean
    playTone(int frequencyHz, int durationCentiseconds)
    Plays a tone of frequencyHz (about 31 to 20000 Hz) for durationCentiseconds hundredths of a second (1..255, so up to 2.55 s), then returns without waiting for it to end.
    static int
    readSensor(int input)
    The value of the sensor on input (for a touch sensor 1 pressed, 0 released), or -1 if no reply.
    static int
    rotationDegrees(int input)
    The rotation of the sensor on input in degrees, as 22.5 degrees per step rounded down.
    static int
    rotationSteps(int input)
    The signed rotation count of the sensor on input in steps (16 per turn), or 0 if the brick did not answer.
    static boolean
    setLightSensor(int input)
    Configures input for a light sensor reading the light level as a percentage, 0..100, which readSensor(int) then returns.
    static void
    setMotorDirection(int outputs, boolean forward)
    Sets the direction of the outputs, forwards or backwards.
    static void
    setMotorPower(int outputs, int level)
    Sets the power level of the outputs, 0..MAX_POWER (clamped); it takes effect while they are on.
    static boolean
    setRotationSensor(int input)
    Configures input for a rotation sensor counting 16 steps per revolution, which rotationSteps(int) and rotationDegrees(int) read; clearSensor(int) sets it back to zero.
    static boolean
    Configures input for a temperature sensor, read in tenths of a degree Celsius by temperatureTenths(int).
    static boolean
    setTouchSensor(int input)
    Configures input for a touch sensor reading 1 when pressed and 0 otherwise.
    static int
    temperatureTenths(int input)
    The temperature on input in tenths of a degree Celsius (235 is 23.5 degrees), or 0 if the brick did not answer.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • setTouchSensor

      public static boolean setTouchSensor(int input)
      Configures input for a touch sensor reading 1 when pressed and 0 otherwise. Returns whether the brick acknowledged both commands.
    • readSensor

      public static int readSensor(int input)
      The value of the sensor on input (for a touch sensor 1 pressed, 0 released), or -1 if no reply.
    • setLightSensor

      public static boolean setLightSensor(int input)
      Configures input for a light sensor reading the light level as a percentage, 0..100, which readSensor(int) then returns. Returns whether the brick acknowledged both commands.
    • setRotationSensor

      public static boolean setRotationSensor(int input)
      Configures input for a rotation sensor counting 16 steps per revolution, which rotationSteps(int) and rotationDegrees(int) read; clearSensor(int) sets it back to zero.
    • setTemperatureSensor

      public static boolean setTemperatureSensor(int input)
      Configures input for a temperature sensor, read in tenths of a degree Celsius by temperatureTenths(int).
    • rotationSteps

      public static int rotationSteps(int input)
      The signed rotation count of the sensor on input in steps (16 per turn), or 0 if the brick did not answer.
    • rotationDegrees

      public static int rotationDegrees(int input)
      The rotation of the sensor on input in degrees, as 22.5 degrees per step rounded down.
    • temperatureTenths

      public static int temperatureTenths(int input)
      The temperature on input in tenths of a degree Celsius (235 is 23.5 degrees), or 0 if the brick did not answer.
    • clearSensor

      public static void clearSensor(int input)
      Sets the value of the sensor on input back to zero, e.g. a rotation sensor's count.
    • motorOn

      public static void motorOn(int outputs)
      Turns the outputs outputs (OUTPUT_* bits) on, at the power level and in the direction last set.
    • motorOff

      public static void motorOff(int outputs)
      Switches the outputs off with the motors braking.
    • motorFloat

      public static void motorFloat(int outputs)
      Switches the outputs off with the motors free to coast.
    • setMotorDirection

      public static void setMotorDirection(int outputs, boolean forward)
      Sets the direction of the outputs, forwards or backwards.
    • setMotorPower

      public static void setMotorPower(int outputs, int level)
      Sets the power level of the outputs, 0..MAX_POWER (clamped); it takes effect while they are on.
    • drive

      public static void drive(int outputs, int speed)
      Runs the outputs at speed, -7..7: the sign is the direction, the size the power level, and 0 brakes them. That is up to four commands, each waiting for the brick's acknowledgement when the receiver is wired, so one call takes a few hundred milliseconds; set the speed once and not in a tight loop.
    • batteryMillivolts

      public static int batteryMillivolts()
      The brick's battery voltage in millivolts (about 9000 with fresh batteries, and the brick warns below about 6000), or -1 if it did not answer. It is a request and a reply, like the sensor reads.
    • playSound

      public static boolean playSound(int sound)
      Plays one of the six built-in sounds, SOUND_*. Returns whether the brick acknowledged the command, which needs the IR receiver wired; the sound then plays on the brick's own speaker and the call returns without waiting for it to end.
    • playTone

      public static boolean playTone(int frequencyHz, int durationCentiseconds)
      Plays a tone of frequencyHz (about 31 to 20000 Hz) for durationCentiseconds hundredths of a second (1..255, so up to 2.55 s), then returns without waiting for it to end. Returns whether the brick acknowledged the command. A tone replaces any sound still playing, so wait for the duration before sending the next.
    • isPressed

      public static boolean isPressed(int input)
      Whether the touch sensor on input is pressed; false also when the brick did not answer.