Page 124 - Hacking Roomba
P. 124

Chapter 5 — Driving Roomba              105



                             Listing 5-6 shows the DriveRealTime program. It is launched from the command line as
                             usual:
                             % ./runit.sh roombacomm.DriveRealTime /dev/cu.KeySerial1

                             Figure 5-8 shows what it looks like after a few seconds of use.























                             FIGURE 5-8: DriveRealTime program in use


                             DriveRealTime subclasses a JFrame (an application window in Java Swing).The setupWindow()
                             method holds all the things necessary to make the window work properly. Most important is
                             addKeyListener(this), which will make the keyPressed() method of DriveRealTime
                             get called whenever you press a key. The last thing setupWindow() does is show the window
                             with setVisible(true). To make the program a little more visually interesting, a JTextArea
                             is created and whenever you press a key, the text area is updated with the command you pressed
                             through updateDisplay().


                               Listing 5-6: DriveRealTime.java

                             public class DriveRealTime extends JFrame implements KeyListener {
                               RoombaCommSerial roombacomm;
                               JTextArea displayText;
                               public static void main(String[] args) {
                                 new DriveRealTime(args);
                               }
                               public DriveRealTime(String[] args) {
                                 super(“DriveRealTime”);
                                 String portname = args[0];
                                 roombacomm = new RoombaCommSerial();
                                                                                              Continued
   119   120   121   122   123   124   125   126   127   128   129