Page 162 - Hacking Roomba
P. 162

Chapter 7 — Making RoombaView                 143




                               Listing 7-4 Continued

                                 buttonGroup.add(sliderSpeed);
                               }

                               void actionPerformed(ActionEvent e) {
                                 String cmd = e.getActionCommand();
                                 Object src = e.getSource();

                                 if( cmd.equals(“speed-update”) ) {
                                   roombacomm.setSpeed(sliderSpeed.getValue());
                                   return;
                                 }
                                 else if( src == butStop ) {
                                   roombacomm.stop();
                                 } else if( src == butForward ) {
                                   roombacomm.goForward();
                                 } else if( src == butBackward ) {
                                   roombacomm.goBackward();
                                 } else if( src == butSpinLeft ) {
                                   roombacomm.spinLeft();
                                 } else if( src == butSpinRight ) {
                                   roombacomm.spinRight();
                                 } else if( src == butTurnLeft ) {
                                   roombacomm.turnLeft();
                                 } else if( src == butTurnRight ) {
                                   roombacomm.turnRight();
                               }



                             MyGUI enables you to group buttons together in a group and style all the buttons in that
                             group identically, which is what the first two lines of makeMoveButtons() do. The movement
                             buttons are graphical icons created beforehand and put in the data directory of your sketch.
                             MyGUIButtons can load a Processing PImage image object, so makeMoveButtons() loads
                             the button images and creates buttons using them. Another GUI element MyGUI provides is a
                             value slider it calls a PinSlider. This will be the speed control, similar to what
                             RoombaCommTest has.
                             When MyGUI is used, it registers with Processing a new method called actionPerformed().
                             If you implement this method in your sketch, MyGUI will call it to tell you when GUI events
                             happen. The actionPerformed() method for the movement buttons simply checks which
                             button has been pressed and activates the appropriate Roomba action, very much like the
                             keyPressed() method did in the previous section. However, one difference is dealing with
                             the slider. If the event is from a slider move, the code updates the default speed used by the var-
                             ious RoombaComm movement commands.
                             The code for the text control buttons is not shown but is virtually identical to the movement
                             buttons. Their function and implementation should all be self-explanatory except for the SPY
   157   158   159   160   161   162   163   164   165   166   167