140 likes | 260 Views
Controlling the Hardware (LCD, Buttons and Sound). Fall, 2008. Presentaion Schedule. LCD. LCD (Liquid Crystal Display) text mode and graphics mode. Text LCD methods NXT LCD screen is 16 characters wide and eight characters deep. x ranges from 0 to 15, and y from 0 to 7.
E N D
LCD • LCD (Liquid Crystal Display) • text mode and graphics mode. • Text LCD methods • NXT LCD screen is 16 characters wide and eight characters deep. • x ranges from 0 to 15, and y from 0 to 7. • public static drawString(String str, int x, int y); • a string of text to the LCD screen starting at text co-ordinate • (1 character == 8 pixels) • drawString(String str, int x, int y, boolean invert); • inverting the text drawing white characters on black background. • public static drawInt(int i, int x, int y); • an integer starting at co-ordinate (x,y). left aligned • public static drawInt(int i, int places, int x, int y); • right-aligns the integer • the number of characters indicated by places
LCD • drawChar(char c, int x, int y, boolean invert); • a character at text co-ordinate (x, y) with optional inversion of the character. • public static clear(); • Clears the display import lejos.nxt.*; import java.io.*; public class LCDTest { public static void main (String[] args) throws Exception { LCD.drawString("F_memory:", 0, 0); LCD.drawString("SCREEN_WIDTH:", 0, 8, true); LCD.drawString("SCREEN_HEIGHT:", 0, 15, true); LCD.drawInt((int) Runtime.getRuntime().freeMemory(), 4, 11, 0); LCD.drawInt(LCD.SCREEN_WIDTH, 3, 12, 1); LCD.drawInt(LCD.SCREEN_HEIGHT, 3, 12, 2); Thread.sleep(10000); } }
Buttons • Buttons • The NXT buttons are accessed by static fields: • Button.ENTER • Button.ESCAPE • Button.LEFT • Button.RIGHT • public final boolean isPressed(); • Check if the button is pressed.
Buttons : Example 1 import lejos.nxt.*; public class ButtonPresses { public static void main (String[] args) throws Exception { while (true) { LCD.clear(); if (Button.ENTER.isPressed()) { LCD.drawString("ENTER",0,0); Thread.sleep(1000); } if (Button.ESCAPE.isPressed()) { LCD.drawString("ESCAPE",0,0); Thread.sleep(1000); break; } if (Button.LEFT.isPressed()) { LCD.drawString("LEFT",0,0); Thread.sleep(1000); } if (Button.RIGHT.isPressed()) { LCD.drawString("RIGHT",0,0); Thread.sleep(1000); } } } }
Buttons • void waitForPressAndRelease() • Wait until the button is released • static int waitForPress() • wait for some button to be pressed • returns the id of the button that is pressed. • public static int readButtons() • 0x01 (ENTER button pressed), • 0x02 (LEFT button pressed), • 0x04 (RIGHT button pressed), • 0x08 (ESCAPE button pressed). • If all buttons are released, this method returns 0. • public void addButtonListener(ButtonListener aListener) • Adds a listener of button events. • Each button can serve at most 4 listeners.
Buttons : Example 2 import lejos.nxt.*; public class ButtonTest { public static void main (String[] args)throws Exception { int bid; Button.ENTER.waitForPressAndRelease(); LCD.drawString("Finished", 3, 4); Thread.sleep(2000); bid=Button.ESCAPE.waitForPress(); LCD.drawString("ESCAPE...", 3, 5); LCD.drawInt(bid, 11, 5); Thread.sleep(2000); bid=LCD.readButtons(); LCD.drawInt(bid, 11, 7); Thread.sleep(2000); } }
Sound • Sound • public static playTone(int aFrequency, int aDuration, int aVolume); • aFrequency - The frequency of the tone in Hertz (Hz). • aDuration - The duration of the tone, in milliseconds. • (aVolume) : The volume of the playback 100 corresponds to 100%
Sound: Example1 import lejos.nxt.*; public class Tune { // NOTE: This tune was generated from a midi using Guy // Truffelli's Brick Music Studio www.aga.it/~guy/lego private static final short [] note = { 2349,115, 0,5, 1760,165, 0,35, 1760,28, 0,13, 1976,23, 0,18, 1760,18, 0,23, 1568,15, 0,25, 1480,103, 0,18, 1175,180, 0,20, 1760,18, 0,23, 1976,20, 0,20, 1760,15, 0,25, 1568,15, 0,25, 2217,98, 0,23, 1760,88, 0,33, 1760,75, 0,5, 1760,20, 0,20, 1760,20, 0,20, 1976,18, 0,23, 1760,18, 0,23, 2217,225, 0,15, 2217,218}; public static void main(String [] args) { for(int i=0;i<note.length; i+=2) { final short w = note[i+1]; final int n = note[i]; if (n != 0) Sound.playTone(n, w*10); try { Thread.sleep(w*10); } catch (InterruptedException e) {} } } }
Sound • public static void systemSound (boolean aQueued, int aCode); • aQueued parameter is ignored on the NXT. • code = 0 : Short beep public static void beep(); • code = 1 : Double beep public static void twoBeeps(); • code = 2 : Descending arpeggio public static void beepSequence(); • code = 3 : Ascending arpeggio public static void beepSequenceUp(); • code = 4 : Long, low buzz public static void buzz(); • public static void pause(int t); • produce a rest when playing a tune:
Sound • public static int playSample(File file, intvol) • play 8-bit WAV files. • vol: volume control (0 ~ 100) • Upload a WAV File to NXT • Command prompt: nxtbrowse • Connect Upload file • Select a WAV File • ringin.wav, ringout.wav
Sound: Example2 import lejos.nxt.*; import java.io.*; public class SoundSample { static File f1,f2; public static void main(String [] options) throws Exception { f1 = new File("ringin.wav"); f2 = new File("ringout.wav"); LCD.drawString("Play a WAV file",0,0); while(true) { Sound.playSample(f1,100); Thread.sleep(1500); if (Button.readButtons()>0) break; } Sound.playSample(f2,100); } }
Reference • Reference • http://www.bartneck.de/2008/03/04/java-lego-nxt-eclipse-tutorial/ • http://www.seas.upenn.edu/~pfpcse/NXT/nxtjavatutorial.html • http://lejos-osek.sourceforge.net/nxt2nxt.htm • http://lejos.sourceforge.net/ • http://www.juanantonio.info/jab_cms.php?id=206 • http://lejos.sourceforge.net/nxt/nxj/api/index.html