ButtonHandler.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package control.button;
  2. import java.awt.Color;
  3. import java.awt.event.KeyEvent;
  4. import java.awt.event.KeyListener;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import com.pi4j.io.gpio.GpioController;
  8. import com.pi4j.io.gpio.GpioFactory;
  9. import com.pi4j.io.gpio.GpioPinDigitalInput;
  10. import com.pi4j.io.gpio.PinState;
  11. import com.pi4j.io.gpio.RaspiPin;
  12. import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
  13. import com.pi4j.io.gpio.event.GpioPinListenerDigital;
  14. import main.Window;
  15. import control.LedHandler;
  16. public class ButtonHandler implements KeyListener{
  17. List<ButtonListener> listeners;
  18. static List<Button> buttons;
  19. LedHandler led;
  20. public ButtonHandler(LedHandler led)
  21. {
  22. this.led = led;
  23. listeners = new ArrayList<ButtonListener>();
  24. buttons = new ArrayList<Button>();
  25. buttons.add(new Button(0, -1, led));
  26. buttons.add(new Button(1, 2, led));
  27. buttons.add(new Button(2, 1, led));
  28. buttons.add(new Button(3, 0, led));
  29. buttons.add(new Button(4, 3, led));
  30. buttons.add(new Button(5, 4, led));
  31. buttons.add(new Button(6, 5, led));
  32. for(Button b : buttons)
  33. {
  34. b.setColor(new Color((int)(Math.random()*254+1),(int)(Math.random()*254+1),(int)(Math.random()*254+1)));
  35. }
  36. System.out.println(Window.ON_RASP);
  37. if (Window.ON_RASP)
  38. addGpioListeners();
  39. }
  40. private void addGpioListeners(){
  41. ArrayList<GpioPinDigitalInput> inputpins = new ArrayList<GpioPinDigitalInput>();
  42. final GpioController gpio = GpioFactory.getInstance();
  43. inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, "1")); //button 1 to 6 + start button
  44. inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_03, "2"));
  45. inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_13, "3"));
  46. inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_14, "4"));
  47. inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, "5"));
  48. inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_12, "6"));
  49. inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_06, "0"));
  50. for(GpioPinDigitalInput p:inputpins){
  51. p.addListener(new GpioPinListenerDigital() {
  52. @Override
  53. public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent e) {
  54. if(e.getState() == PinState.HIGH){
  55. buttonRelease(buttons.get(Integer.parseInt(e.getPin().getName())));
  56. System.out.println(e.getPin().getName() + " Released");
  57. }else{
  58. buttonPress(buttons.get(Integer.parseInt(e.getPin().getName())));
  59. System.out.println(e.getPin().getName() + " Pressed");
  60. }
  61. }
  62. });
  63. }
  64. }
  65. public void addButtonListener(ButtonListener toAdd) {
  66. listeners.add(toAdd);
  67. }
  68. public void buttonPress(Button b) {
  69. ButtonEvent e = new ButtonEvent(b, System.currentTimeMillis());
  70. for (ButtonListener bt : listeners)
  71. bt.buttonPressed(e);
  72. }
  73. public void buttonRelease(Button b) {
  74. ButtonEvent e = new ButtonEvent(b, System.currentTimeMillis());
  75. for (ButtonListener bt : listeners)
  76. bt.buttonReleased(e);
  77. }
  78. @Override
  79. public void keyPressed(KeyEvent e) {
  80. switch(e.getKeyCode())
  81. {
  82. case KeyEvent.VK_0:
  83. buttonPress(buttons.get(0));
  84. break;
  85. case KeyEvent.VK_1:
  86. buttonPress(buttons.get(1));
  87. break;
  88. case KeyEvent.VK_2:
  89. buttonPress(buttons.get(2));
  90. break;
  91. case KeyEvent.VK_3:
  92. buttonPress(buttons.get(3));
  93. break;
  94. case KeyEvent.VK_4:
  95. buttonPress(buttons.get(4));
  96. break;
  97. case KeyEvent.VK_5:
  98. buttonPress(buttons.get(5));
  99. break;
  100. case KeyEvent.VK_6:
  101. buttonPress(buttons.get(6));
  102. break;
  103. case KeyEvent.VK_ESCAPE:
  104. if(!Window.ON_RASP)
  105. System.exit(0);
  106. break;
  107. }
  108. }
  109. public void keyReleased(KeyEvent arg0) {}
  110. public void keyTyped(KeyEvent arg0) {}
  111. public static List<Button> getButtons()
  112. {
  113. return buttons;
  114. }
  115. }