import ukcrobots.core.LightSensor;
import ukcrobots.core.SensorListener;
import josx.platform.rcx.LCD;

/**
 * A main method for the LightReader class.
 */
public class LightReaderMain
{
    public static void main(String[] args)
    {
        LightReader reader = new LightReader();
        reader.run();
    }
}

/**
 * An illustration of how to use a light sensor with
 * an attached listener.
 * Light sensor readings are displayed as the values change.
 *
 * @author David J. Barnes (d.j.barnes@ukc.ac.uk)
 * @version 2003.03.03
 */
class LightReader
{
    private LightSensor light;

    public LightReader()
    {
        light = new LightSensor(3);

        // Add a listener for light readings.
        light.addSensorListener(new SensorListener()
        {
            /**
             * The sensor's reading has changed.
             * Display the new value.
             * @param oldValue The previous light level.
             * @param currentValue The current light level.
             */
            public void stateChanged(int oldValue, int currentValue)
            {
                LCD.showNumber(currentValue);
            }
        });
    }

    /**
     * Simply activate the light sensor and it will
     * read and display changes continuously.
     */
    public void run()
    {
        // Activate the light sensor so that its listener receives
        // notifications.
        light.activate();
        
        // Now the listener takes over everything else ...
    }
}

