import lejos.nxt.*;

/* note the new package involved! */

import lejos.robotics.objectdetection.*;

/**
   experiment with the Ultrasonic sensor, AND the FeatureListener
   interface and the RangeFeatureDetector class (both from leJOS 
   package lejos.robotics.objectdetection)

   @author www.lejos.org
   @author adapted by Sharon Tuttle
   @version 2015-02-17
*/

public class ObjectDetectPlay
{
    // data field

    /* 
       only try to detect features within 80 -- what? what units? cm?
       away
    */

    private static final int MAX_DETECT = 80;

    /**
       when its ultrasonic sensor detects a feature --
       play a sound, and display its distance away?

       @param args not used
    */
 
    public static void main(String[] args)
    {
        System.out.println("Sensor Play");
        
        ObjectDetectPlay objectDetectInstance = new ObjectDetectPlay();
        objectDetectInstance.setUp();

        System.out.println("click Enter to quit");
        Button.ENTER.waitForPressAndRelease();
    }
    
    /**
       make ultrasonic sensor sensitive to detected features
       within a certain range
    */

    private void setUp()
    {
        /*
           create an UltrasonicSensor object based on the
               sensor port to which the sensor is connected
        */

        UltrasonicSensor ultraSensor = new UltrasonicSensor(SensorPort.S2);

        /* 
            RangeFeatureDetector constructor expects:
            *   a range finder (here, the ultrasonic sensor object)
            *   the maximum distance to report a detection (in cm?)
            *   the delay between scanning the sensor (in millisec?)
        */

        RangeFeatureDetector featDetector = new RangeFeatureDetector(
                                                ultraSensor, MAX_DETECT, 500);

        /* 
           add a FeatureListener to specify what should happen when
           a feature is detected by the ultrasonic sensor
        */
     
        featDetector.addListener(new FeatureDetected());                
    }

    
    /**
       when a feature is detected, play a tone and display
       its distance from the ultrasonic sensor on the NXT screen
    */

    private class FeatureDetected implements FeatureListener
    {
        /**
          this will be called when a sensitive UltraSonic sensor 
          is in range of a feature, playing a tone and displaying how far
          away that feature is

          <p> this method is required for the FeatureListener interface </p>
  
          @param feature detected feature
          @param detector ultrasonic sensor
        */

        public void featureDetected(Feature feature, 
                                    FeatureDetector detector) 
        {
            // how far away is the detected feature?

            int range = (int)feature.getRangeReading().getRange();

            // play a tone and display detected feature's distance

            Sound.playTone(1200 - (range * 10), 100);
            System.out.println("Range:" + range);
        }
    }
}