Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package sensor.balance;
- import core.PollingSensor;
- import core.Sensor;
- import sensor.GRTSwitch;
- import event.BalanceTunePositionEvent;
- import event.BalanceTunePositionListener;
- import event.SwitchEvent;
- import event.SwitchListener;
- import java.util.Vector;
- /**
- *
- * @author ajc
- */
- public class BalanceTunePosition extends Sensor implements SwitchListener{
- public static final double CONTACT = 1;
- public static final double NOCONTACT = 0;
- private final GRTSwitch frontLeft;
- private final GRTSwitch frontRight;
- private final GRTSwitch backLeft;
- private final GRTSwitch backRight;
- private Vector balanceTunePositionListeners;
- private boolean frontLeftPressed = false;
- private boolean frontRightPressed = false;
- private boolean rearLeftPressed = false;
- private boolean rearRightPressed = false;
- public final int KEY_BALANCE_POSITION_CHANGE = 0;
- public final int KEY_FRONT_CONTACT = 1;
- public final int KEY_REAR_CONTACT = 2;
- public final int KEY_FRONTLEFT_CONTACT = 3;
- public final int KEY_FRONTRIGHT_CONTACT = 4;
- public final int KEY_REARLEFT_CONTACT = 5;
- public final int KEY_REARRIGHT_CONTACT = 6;
- public final int KEY_NO_CONTACT = 7;
- public BalanceTunePosition(GRTSwitch frontLeft, GRTSwitch frontRight,
- GRTSwitch backLeft, GRTSwitch backRight, String name) {
- super(name);
- this.frontLeft = frontLeft;
- this.frontRight = frontRight;
- this.backLeft = backLeft;
- this.backRight = backRight;
- }
- protected void startListening() {
- frontLeft.addListener(this);
- frontRight.addListener(this);
- backLeft.addListener(this);
- backRight.addListener(this);
- }
- protected void stopListening() {
- frontLeft.removeListener(this);
- frontRight.removeListener(this);
- backLeft.removeListener(this);
- backRight.removeListener(this);
- }
- protected void notifyStateChange(int key, double data){
- super.notifyStateChange(key, data);
- boolean isPressed = (data == GRTSwitch.PRESSED) ? true : false;
- switch(key){
- case KEY_FRONTLEFT_CONTACT: frontLeftPressed = isPressed;
- break;
- case KEY_FRONTRIGHT_CONTACT: frontRightPressed = isPressed;
- break;
- case KEY_REARLEFT_CONTACT: rearLeftPressed = isPressed;
- break;
- case KEY_REARRIGHT_CONTACT: rearRightPressed = isPressed;
- break;
- }
- }
- public void switchStateChanged(SwitchEvent e) {
- if(e.getSource() == frontLeft){
- notifyStateChange(KEY_FRONTLEFT_CONTACT, e.getState() ? CONTACT : NOCONTACT);
- if(frontRightPressed == (e.getState() == SwitchEvent.PRESSED))
- notifyStateChange(KEY_FRONT_CONTACT, e.getState() ? CONTACT : NOCONTACT);
- }
- if(e.getSource() == frontRight){
- notifyStateChange(KEY_FRONTRIGHT_CONTACT, e.getState() ? CONTACT : NOCONTACT);
- if(frontLeftPressed == (e.getState() == SwitchEvent.PRESSED))
- notifyStateChange(KEY_FRONT_CONTACT, e.getState() ? CONTACT : NOCONTACT);
- }
- if(e.getSource() == backLeft){
- notifyStateChange(KEY_REARLEFT_CONTACT, e.getState() ? CONTACT : NOCONTACT);
- if(rearRightPressed == (e.getState() == SwitchEvent.PRESSED))
- notifyStateChange(KEY_REAR_CONTACT, e.getState() ? CONTACT : NOCONTACT);
- }
- if(e.getSource() == backRight){
- notifyStateChange(KEY_REARRIGHT_CONTACT, e.getState() ? CONTACT : NOCONTACT);
- if(rearLeftPressed == (e.getState() == SwitchEvent.PRESSED))
- notifyStateChange(KEY_REAR_CONTACT, e.getState() ? CONTACT : NOCONTACT);
- }
- }
- }
Add Comment
Please, Sign In to add comment