The leJOS Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail

Trail: Communication
Lesson: Using the LEGO® remote control

Using the LEGO® remote control

by Matthias Paul Scholz


Note that this lesson already refers to the actual re-organization of the remotecontrol package in the CVS: in future the package will be part of josx.rcxcomm whereas in the actual leJOS release 2.1.0 it's located in josx.platform.rcx yet.

Overview

The josx.rcxcomm.remotecontrol package is mainly supposed to be used in connection with the LEGO® remote control contained in the (however, as we will see, there is also a class which enables you to simulate commands sent by it).

Architecture

The package consists of some classes that are running on the RCX whose task it is
  1. to listen for messages coming in from the remote control
  2. to identify the command associated with the message
  3. to execute on the RCX the command in question depending on the code the user associated with it

Listening for incoming messages

The component responsible to detect messages set forth by the remote control is josx.rcxcomm.remotecontrol.RemoteControlSensor.
Internally it's built on top of leJOS' josx.platform.rcx.SerialListener, which constantly checks the IR sensor on front of the RCX for incoming IR signals.
If such a signal is detected, the remote control sensor decides if the message is one emitted by the LEGO® remote control; if so, all the registered listeners (see below) are notified.

Listeners

As often with Java and leJOS, a listener serves as the bridge between the sensor and the code to be executed on events: it's specified by the josx.rcxcomm.remotecontrol.RemoteControlListener interface which declares the following methods that correspond to the buttons of the remote control: A class implemeting this interface is added by the sensor's

        public void addRemoteControlListener(RemoteControlListener aListener)
        
method.
Any time an according message is emitted by the remote control, the sensor notifies all added listeners calling the corresponding listener's method.

One of the drawbacks of the usage of infrared signals by LEGO MINDSTORMS™ instead of, say, radio waves, is that the IR sensor of the RCX has to point straight to the remote control when pressing a button - else the signal will never arrive at the sensor and will simply get lost. Sometimes, though, it might help to point the remote control towards the ceiling of the room.

Using the LEGO® remote control with leJOS

It ought to be clear now what you have to do to use your LEGO® remote control with leJOS:
  1. Instantiate a josx.rcxcomm.remotecontrol.RemoteControlSensor somewhere in the code running on your RCX
  2. Write a class implementing the methods of the josx.rcxcomm.remotecontrol.RemoteControlListener interface: add to the body of each of its methods the code that may be executed when the associated button on the remote control is pressed. A natural implementation for the soundPressed() method, for example, might be to play a sound by calling Sound.beep(); after all, you are completely free in the action your program takes as a reaction to any remote control command!

If your application is interested only in a part of the messages of the remote control, you can just extend the josx.rcxcomm.remotecontrol.RemoteControlAdapter (that implements the listener interface with empty method bodies) and override only the methods you are interested in.

Simulating the LEGO® remote control

Those who want to use the PC to send LEGO® remote control commands may do this by the class josx.rcxcomm.remotecontrol.RemoteControlMessenger that is - as all the communication classes supposed to run rather on the PC than on the RCX - contained in the pcrcxcomm.jar.
It offers the method

            public void send(byte[] aMessageCode) 
        
method, where aMessageCode has to be one of the public RemoteControlMessenger message opcode constants (see there).

Note there is a ready-made tool available which simulates the LEGO® remote control on the PC either as a Java™ application or as a JSP web page, named Maroc.

Of course you are free to use the rcxcomm classes to send messages to the remote control sensor by yourself. The RemoteControlSensor accepts not only native message bytes sent by the LEGO® remote control but also the F7 LEGO firmware protocol, which means that you can use josx.rcxcomm.RCXF7Port as well.
Keep in mind, though, that the remote control sensor class will respond to opcodes belonging to LEGO® remote control commands only.

A complete example which uses the remotecontrol package

is RemoteControlTest.java which may be found in the examples/comms/remotectl section of the leJOS tree.

The remotecontrol API

may be found here.
The leJOS Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail