Package com.grt192.sensor

Source Code of com.grt192.sensor.GRTLineTracker

package com.grt192.sensor;

import com.grt192.core.Sensor;
import com.grt192.event.component.LineTraceEvent;
import edu.wpi.first.wpilibj.*;
import java.util.Vector;
import com.grt192.event.component.LineTrackerListener;

/**
* 2011
* @author ajc
*/
public class GRTLineTracker extends Sensor {

    public static final double PRESENT = Sensor.TRUE;
    public static final double ABSENT = Sensor.FALSE;

    private DigitalInput input;
    private Vector lineStateListeners;

    public GRTLineTracker(int digitalChannel, int polltime, String id){
        lineStateListeners = new Vector();
        setSleepTime(polltime);
        input = new DigitalInput(digitalChannel);
        setState("Line", ABSENT);
        this.id = id;
    }

    public void poll() {
        double previous = getState("Line");
        setState("Line", input.get());
        if(previous != getState("Line")){
            notifyListeners(getState("Line") == PRESENT);
        }
    }

    private void notifyListeners(boolean present) {
        for (int i = 0; i < lineStateListeners.size(); i++) {
                ((LineTrackerListener) lineStateListeners.elementAt(i)).lineStateChange(new LineTraceEvent(this, present));
        }
    }

    public void addLineTrackerListener(LineTrackerListener listener) {
        lineStateListeners.addElement(listener);
    }

    public void removeLineTrackerListener(LineTrackerListener listener) {
        lineStateListeners.removeElement(listener);
    }


}
TOP

Related Classes of com.grt192.sensor.GRTLineTracker

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.