@@ -6,6 +6,14 @@ import java.awt.event.KeyListener;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.pi4j.io.gpio.GpioController;
|
||||
import com.pi4j.io.gpio.GpioFactory;
|
||||
import com.pi4j.io.gpio.GpioPinDigitalInput;
|
||||
import com.pi4j.io.gpio.PinState;
|
||||
import com.pi4j.io.gpio.RaspiPin;
|
||||
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
|
||||
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
|
||||
|
||||
import main.Window;
|
||||
import control.LedHandler;
|
||||
|
||||
@@ -34,6 +42,39 @@ public class ButtonHandler implements KeyListener{
|
||||
{
|
||||
b.setColor(new Color((int)(Math.random()*254+1),(int)(Math.random()*254+1),(int)(Math.random()*254+1)));
|
||||
}
|
||||
System.out.println(Window.ON_RASP);
|
||||
if (Window.ON_RASP)
|
||||
addGpioListeners();
|
||||
|
||||
}
|
||||
|
||||
private void addGpioListeners(){
|
||||
ArrayList<GpioPinDigitalInput> inputpins = new ArrayList<GpioPinDigitalInput>();
|
||||
final GpioController gpio = GpioFactory.getInstance();
|
||||
|
||||
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, "1")); //button 1 to 6 + start button
|
||||
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_03, "2"));
|
||||
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_13, "3"));
|
||||
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_14, "4"));
|
||||
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, "5"));
|
||||
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_12, "6"));
|
||||
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_06, "0"));
|
||||
|
||||
|
||||
for(GpioPinDigitalInput p:inputpins){
|
||||
p.addListener(new GpioPinListenerDigital() {
|
||||
@Override
|
||||
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent e) {
|
||||
if(e.getState() == PinState.HIGH){
|
||||
buttonRelease(buttons.get(Integer.parseInt(e.getPin().getName())));
|
||||
System.out.println(e.getPin().getName() + " Released");
|
||||
}else{
|
||||
buttonPress(buttons.get(Integer.parseInt(e.getPin().getName())));
|
||||
System.out.println(e.getPin().getName() + " Pressed");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void addButtonListener(ButtonListener toAdd) {
|
||||
|
||||
@@ -7,6 +7,17 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import main.Window;
|
||||
|
||||
import com.pi4j.component.Component;
|
||||
import com.pi4j.io.gpio.GpioController;
|
||||
import com.pi4j.io.gpio.GpioFactory;
|
||||
import com.pi4j.io.gpio.GpioPinDigitalInput;
|
||||
import com.pi4j.io.gpio.PinState;
|
||||
import com.pi4j.io.gpio.RaspiPin;
|
||||
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
|
||||
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
|
||||
|
||||
import control.joystick.Joystick.Position;
|
||||
|
||||
public class JoystickHandler implements KeyListener{
|
||||
@@ -20,6 +31,48 @@ public class JoystickHandler implements KeyListener{
|
||||
listeners = new ArrayList<JoystickListener>();
|
||||
keys = new HashSet<Integer>();
|
||||
j = new Joystick();
|
||||
if(Window.ON_RASP)
|
||||
addGpioListeners();
|
||||
}
|
||||
|
||||
private void addGpioListeners(){
|
||||
ArrayList<GpioPinDigitalInput> inputpins = new ArrayList<GpioPinDigitalInput>();
|
||||
final GpioController gpio = GpioFactory.getInstance();
|
||||
|
||||
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_04, "UP")); //button 1 to 6 + start button
|
||||
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_05, "LEFT"));
|
||||
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_11, "RIGHT"));
|
||||
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_10, "DOWN"));
|
||||
|
||||
|
||||
for(GpioPinDigitalInput p:inputpins){
|
||||
p.addListener(new GpioPinListenerDigital() {
|
||||
@Override
|
||||
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent e) {
|
||||
if(e.getState() == PinState.HIGH){
|
||||
keyReleased(new KeyEvent(
|
||||
new java.awt.Component(){},
|
||||
KeyEvent.KEY_RELEASED,
|
||||
System.nanoTime(),
|
||||
0,
|
||||
stringToKeyevent(e.getPin().getName()),
|
||||
KeyEvent.CHAR_UNDEFINED)
|
||||
);
|
||||
System.out.println(e.getPin().getName() + " Released");
|
||||
}else{
|
||||
keyPressed(new KeyEvent(
|
||||
new java.awt.Component(){},
|
||||
KeyEvent.KEY_PRESSED,
|
||||
System.nanoTime(),
|
||||
0,
|
||||
stringToKeyevent(e.getPin().getName()),
|
||||
KeyEvent.CHAR_UNDEFINED)
|
||||
);
|
||||
System.out.println(e.getPin().getName() + " Pressed");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public void addJoystickListener(JoystickListener toAdd) {
|
||||
@@ -73,14 +126,26 @@ public class JoystickHandler implements KeyListener{
|
||||
j.setPosition(Position.CENTER);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private int stringToKeyevent(String s){
|
||||
switch(s){
|
||||
case "UP":
|
||||
return KeyEvent.VK_UP;
|
||||
case "DOWN":
|
||||
return KeyEvent.VK_DOWN;
|
||||
case "LEFT":
|
||||
return KeyEvent.VK_LEFT;
|
||||
case "RIGHT":
|
||||
return KeyEvent.VK_RIGHT;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
System.out.println("Test " + e.getKeyCode());
|
||||
if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT)
|
||||
{
|
||||
Set<Integer> keysCopy = new HashSet<Integer>(keys);
|
||||
System.out.println("YES!");
|
||||
keys.add(e.getKeyCode());
|
||||
updateJoystickPosition();
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import javax.swing.JFrame;
|
||||
|
||||
import model.GameModel;
|
||||
import view.GameView;
|
||||
import control.GPIOListener;
|
||||
import control.GameControl;
|
||||
import control.LedHandler;
|
||||
import control.button.ButtonHandler;
|
||||
@@ -22,7 +23,7 @@ public class Window extends JFrame {
|
||||
super("Arcade");
|
||||
setSize(1280, 1024);
|
||||
|
||||
this.ON_RASP = ON_RASP;
|
||||
Window.ON_RASP = ON_RASP;
|
||||
System.out.println(ON_RASP);
|
||||
|
||||
//Set window close listener
|
||||
@@ -55,8 +56,10 @@ public class Window extends JFrame {
|
||||
setContentPane(view);
|
||||
|
||||
//Create EventListeners
|
||||
addKeyListener(bth);
|
||||
addKeyListener(jsh);
|
||||
if(!Window.ON_RASP){
|
||||
addKeyListener(bth);
|
||||
addKeyListener(jsh);
|
||||
}
|
||||
bth.addButtonListener(control);
|
||||
jsh.addJoystickListener(control);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user