NXT LCD and Buttons


On the front of the NXT controller there are four buttons and a small LCD. These input/output devices can be accessed in leJOS NXJ through the classes Button and LCD. Go to leJOS API for more details on the possibilities in these two classe. This note only describe simple use of methods in these classes to access the NXT devices.

Figure 1 NXT LCD and Buttons.

Buttons

There are four buttons: an orange button in the middle called ENTER, two light grey triangular buttons next to the ENTER button. These are called LEFT and RIGHT. The fourth button is darker grey and below ENTER. This is called ESCAPE.

The input from each button or pushbutton can be modelled as a discrete (digital) signal yielding simply two values either a 1 or a 0 ( also called On or Off, true or false, pressed or released, respectively). This value is the state of a button. The events associated with a button is the possible state changes of a single button: From 0 to 1 or from 1 to 0. These two events can be described as a button being pressed or button being released.

In the Button class the four buttons are represented by four constant static objects: ENTER, LEFT, RIGHT and ESCAPE of type Button. The method isPressed() called on one of the four objects returns a boolean that is true if that particular button is pressed and false if released. This can be used to access the state of any particular button. E.g. to wait until ENTER is in state pressed simply write:

while ( ! Button.ENTER.isPressed() ) ;
To wait for the event "ENTER being pressed" there are two possible starting situations corresponding to the two possible states, pressed or released. Since we want to wait until the state changes from released to pressed we can do it as follows:
while ( Button.ENTER.isPressed() ) ; // ENTER released while ( ! Button.ENTER.isPressed() ) ; // ENTER pressed
The usual Java event model is also implemented in leJOS NXJ in the ButtonListener interface and the method addButtonListener. The events modelled in the ButtonListener interface are button being pressed and button being released as illustrated in:
import lejos.nxt.*; public class ListenForENTER { public static void main(String [] args) { LCD.drawString("ListenForENTER", 0, 0); LCD.refresh(); Button.ENTER.addButtonListener(new ButtonListener() { public void buttonPressed(Button b){ LCD.clear(); LCD.drawString("ENTER pressed", 0, 0); LCD.refresh(); } public void buttonReleased(Button b){ LCD.clear(); LCD.drawString("ENTER released", 0, 0); LCD.refresh(); } }); while (! Button.ESCAPE.isPressed()); LCD.clear(); LCD.drawString("Program stopped", 0, 0); LCD.refresh(); } }

LCD

The LCD is a 100 x 64 pixel black & white graphical display, Hardware Developer Kit, page 11.

The LCD class provide access to the LCD device. It has methods to display text and graphics. When the LCD class is used to output text each character or digit has a width of 6 pixels and a height of 8 pixels. This means that there is room for 8 lines of 16 characters on the LCD. The positions of the characters are fixed and given as a pair of numbers (x, y) where x is the position within a line from 0 to 15 and y is the line number from 0 to 7. The position (0,0) is the upper left corner and the lower right corner is (15,7). The static methods drawString(string, x, y) and drawInt(i, x, y) can be used to place strings and numbers in a display buffer and LCD.refresh() updates the LCD with the display buffer content. LCD.clear() erase the display buffer.


Last update: 30-08-08