Page 183 - Hacking Roomba
P. 183

164       Part II — Fun Things to Do




                             The playMidiNote() method uses no Java MIDI classes, and it can be called like any other
                             method. This is the manner in which a port to a non–OS X platform would occur.


                               Listing 8-5: playMidiNote() in RoombaCommPanel

                               public void playMidiNote(int notenum, int velocity) {
                                 updateDisplay(“play note: “+notenum+”,”+velocity+”\n”);
                                 if( !roombacomm.connected() ) return;

                                 if( notenum >= 31 ) {                // G and above
                                   if( velocity == 0 ) return;
                                   if( velocity < 4 ) velocity = 4;   // has problems at lower
                               durations
                                   else
                                     velocity = velocity/2;
                                   roombacomm.playNote(notenum, velocity);
                                 }
                                 else if( notenum == 24 ) {           // C
                                   roombacomm.vacuum( !(velocity==0) );
                                 }
                                 else if( notenum == 25 ) {           // C#
                                   boolean lon = (velocity!=0);
                                   int inten = (lon) ? 255:128; // either full bright or half
                               bright
                                   roombacomm.setLEDs(lon,lon,lon,lon,lon,lon, velocity*2,
                               inten);
                                 }
                                 else if( notenum == 28 ) {           // E
                                   if( velocity!=0 ) roombacomm.spinLeftAt(velocity*2);
                                   else roombacomm.stop();
                                 }
                                 else if( notenum == 29 ) {           // F
                                   if( velocity!=0 ) roombacomm.spinRightAt(velocity*2);
                                   else roombacomm.stop();
                                 }
                               }




                             Working with Core MIDI

                             In the Mac OS X Core MIDI API, to participate in MIDI communications, you first create a
                             MIDIClient object by calling new MIDIClient(), passing in the name you want your client
                             to be called. Once you have a MIDIClient, you can create a virtual MIDI interface by calling
                             destinationCreate() on it. This is shown in the setupMidi() method in Listing 8-6.
                             Listing 8-6 shows the MIDI handling methods of RoombaMidi.java.
   178   179   180   181   182   183   184   185   186   187   188