Guest User

Untitled

a guest
May 24th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package sensor.balance;
  6.  
  7. import core.PollingSensor;
  8. import core.Sensor;
  9. import sensor.GRTSwitch;
  10. import event.BalanceTunePositionEvent;
  11. import event.BalanceTunePositionListener;
  12. import event.SwitchEvent;
  13. import event.SwitchListener;
  14. import java.util.Vector;
  15.  
  16. /**
  17. *
  18. * @author ajc
  19. */
  20. public class BalanceTunePosition extends Sensor implements SwitchListener{
  21.  
  22. public static final double CONTACT = 1;
  23. public static final double NOCONTACT = 0;
  24.  
  25.  
  26. private final GRTSwitch frontLeft;
  27. private final GRTSwitch frontRight;
  28. private final GRTSwitch backLeft;
  29. private final GRTSwitch backRight;
  30. private Vector balanceTunePositionListeners;
  31.  
  32. private boolean frontLeftPressed = false;
  33. private boolean frontRightPressed = false;
  34. private boolean rearLeftPressed = false;
  35. private boolean rearRightPressed = false;
  36.  
  37. public final int KEY_BALANCE_POSITION_CHANGE = 0;
  38. public final int KEY_FRONT_CONTACT = 1;
  39. public final int KEY_REAR_CONTACT = 2;
  40. public final int KEY_FRONTLEFT_CONTACT = 3;
  41. public final int KEY_FRONTRIGHT_CONTACT = 4;
  42. public final int KEY_REARLEFT_CONTACT = 5;
  43. public final int KEY_REARRIGHT_CONTACT = 6;
  44. public final int KEY_NO_CONTACT = 7;
  45.  
  46. public BalanceTunePosition(GRTSwitch frontLeft, GRTSwitch frontRight,
  47. GRTSwitch backLeft, GRTSwitch backRight, String name) {
  48. super(name);
  49. this.frontLeft = frontLeft;
  50. this.frontRight = frontRight;
  51. this.backLeft = backLeft;
  52. this.backRight = backRight;
  53. }
  54.  
  55. protected void startListening() {
  56. frontLeft.addListener(this);
  57. frontRight.addListener(this);
  58. backLeft.addListener(this);
  59. backRight.addListener(this);
  60. }
  61.  
  62. protected void stopListening() {
  63. frontLeft.removeListener(this);
  64. frontRight.removeListener(this);
  65. backLeft.removeListener(this);
  66. backRight.removeListener(this);
  67. }
  68.  
  69. protected void notifyStateChange(int key, double data){
  70. super.notifyStateChange(key, data);
  71. boolean isPressed = (data == GRTSwitch.PRESSED) ? true : false;
  72. switch(key){
  73. case KEY_FRONTLEFT_CONTACT: frontLeftPressed = isPressed;
  74. break;
  75. case KEY_FRONTRIGHT_CONTACT: frontRightPressed = isPressed;
  76. break;
  77. case KEY_REARLEFT_CONTACT: rearLeftPressed = isPressed;
  78. break;
  79. case KEY_REARRIGHT_CONTACT: rearRightPressed = isPressed;
  80. break;
  81. }
  82. }
  83.  
  84.  
  85. public void switchStateChanged(SwitchEvent e) {
  86. if(e.getSource() == frontLeft){
  87. notifyStateChange(KEY_FRONTLEFT_CONTACT, e.getState() ? CONTACT : NOCONTACT);
  88. if(frontRightPressed == (e.getState() == SwitchEvent.PRESSED))
  89. notifyStateChange(KEY_FRONT_CONTACT, e.getState() ? CONTACT : NOCONTACT);
  90. }
  91.  
  92. if(e.getSource() == frontRight){
  93. notifyStateChange(KEY_FRONTRIGHT_CONTACT, e.getState() ? CONTACT : NOCONTACT);
  94. if(frontLeftPressed == (e.getState() == SwitchEvent.PRESSED))
  95. notifyStateChange(KEY_FRONT_CONTACT, e.getState() ? CONTACT : NOCONTACT);
  96. }
  97.  
  98. if(e.getSource() == backLeft){
  99. notifyStateChange(KEY_REARLEFT_CONTACT, e.getState() ? CONTACT : NOCONTACT);
  100. if(rearRightPressed == (e.getState() == SwitchEvent.PRESSED))
  101. notifyStateChange(KEY_REAR_CONTACT, e.getState() ? CONTACT : NOCONTACT);
  102. }
  103.  
  104. if(e.getSource() == backRight){
  105. notifyStateChange(KEY_REARRIGHT_CONTACT, e.getState() ? CONTACT : NOCONTACT);
  106. if(rearLeftPressed == (e.getState() == SwitchEvent.PRESSED))
  107. notifyStateChange(KEY_REAR_CONTACT, e.getState() ? CONTACT : NOCONTACT);
  108. }
  109. }
  110. }
Add Comment
Please, Sign In to add comment