Added support for GPIO joystick
Users can now simply test on their own pc without argument added to the launch file
This commit is contained in:
@@ -17,7 +17,7 @@ import com.pi4j.io.gpio.event.GpioPinListenerDigital;
|
|||||||
import main.Window;
|
import main.Window;
|
||||||
import control.LedHandler;
|
import control.LedHandler;
|
||||||
|
|
||||||
public class ButtonHandler{
|
public class ButtonHandler implements KeyListener{
|
||||||
|
|
||||||
List<ButtonListener> listeners;
|
List<ButtonListener> listeners;
|
||||||
static List<Button> buttons;
|
static List<Button> buttons;
|
||||||
@@ -42,8 +42,13 @@ public class ButtonHandler{
|
|||||||
{
|
{
|
||||||
b.setColor(new Color((int)(Math.random()*254+1),(int)(Math.random()*254+1),(int)(Math.random()*254+1)));
|
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();
|
||||||
|
|
||||||
// create gpio controller
|
}
|
||||||
|
|
||||||
|
private void addGpioListeners(){
|
||||||
ArrayList<GpioPinDigitalInput> inputpins = new ArrayList<GpioPinDigitalInput>();
|
ArrayList<GpioPinDigitalInput> inputpins = new ArrayList<GpioPinDigitalInput>();
|
||||||
final GpioController gpio = GpioFactory.getInstance();
|
final GpioController gpio = GpioFactory.getInstance();
|
||||||
|
|
||||||
@@ -61,10 +66,10 @@ public class ButtonHandler{
|
|||||||
@Override
|
@Override
|
||||||
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent e) {
|
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent e) {
|
||||||
if(e.getState() == PinState.HIGH){
|
if(e.getState() == PinState.HIGH){
|
||||||
buttonPress(buttons.get(Integer.parseInt(e.getPin().getName())));
|
buttonRelease(buttons.get(Integer.parseInt(e.getPin().getName())));
|
||||||
System.out.println(e.getPin().getName() + " Released");
|
System.out.println(e.getPin().getName() + " Released");
|
||||||
}else{
|
}else{
|
||||||
buttonRelease(buttons.get(Integer.parseInt(e.getPin().getName())));
|
buttonPress(buttons.get(Integer.parseInt(e.getPin().getName())));
|
||||||
System.out.println(e.getPin().getName() + " Pressed");
|
System.out.println(e.getPin().getName() + " Pressed");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,41 +94,41 @@ public class ButtonHandler{
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// @Override
|
@Override
|
||||||
// public void keyPressed(KeyEvent e) {
|
public void keyPressed(KeyEvent e) {
|
||||||
// switch(e.getKeyCode())
|
switch(e.getKeyCode())
|
||||||
// {
|
{
|
||||||
// case KeyEvent.VK_0:
|
case KeyEvent.VK_0:
|
||||||
// buttonPress(buttons.get(0));
|
buttonPress(buttons.get(0));
|
||||||
// break;
|
break;
|
||||||
// case KeyEvent.VK_1:
|
case KeyEvent.VK_1:
|
||||||
// buttonPress(buttons.get(1));
|
buttonPress(buttons.get(1));
|
||||||
// break;
|
break;
|
||||||
// case KeyEvent.VK_2:
|
case KeyEvent.VK_2:
|
||||||
// buttonPress(buttons.get(2));
|
buttonPress(buttons.get(2));
|
||||||
// break;
|
break;
|
||||||
// case KeyEvent.VK_3:
|
case KeyEvent.VK_3:
|
||||||
// buttonPress(buttons.get(3));
|
buttonPress(buttons.get(3));
|
||||||
// break;
|
break;
|
||||||
// case KeyEvent.VK_4:
|
case KeyEvent.VK_4:
|
||||||
// buttonPress(buttons.get(4));
|
buttonPress(buttons.get(4));
|
||||||
// break;
|
break;
|
||||||
// case KeyEvent.VK_5:
|
case KeyEvent.VK_5:
|
||||||
// buttonPress(buttons.get(5));
|
buttonPress(buttons.get(5));
|
||||||
// break;
|
break;
|
||||||
// case KeyEvent.VK_6:
|
case KeyEvent.VK_6:
|
||||||
// buttonPress(buttons.get(6));
|
buttonPress(buttons.get(6));
|
||||||
// break;
|
break;
|
||||||
// case KeyEvent.VK_ESCAPE:
|
case KeyEvent.VK_ESCAPE:
|
||||||
// if(!Window.ON_RASP)
|
if(!Window.ON_RASP)
|
||||||
// System.exit(0);
|
System.exit(0);
|
||||||
// break;
|
break;
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// public void keyReleased(KeyEvent arg0) {}
|
public void keyReleased(KeyEvent arg0) {}
|
||||||
// public void keyTyped(KeyEvent arg0) {}
|
public void keyTyped(KeyEvent arg0) {}
|
||||||
//
|
|
||||||
public static List<Button> getButtons()
|
public static List<Button> getButtons()
|
||||||
{
|
{
|
||||||
return buttons;
|
return buttons;
|
||||||
|
|||||||
@@ -7,6 +7,17 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
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;
|
import control.joystick.Joystick.Position;
|
||||||
|
|
||||||
public class JoystickHandler implements KeyListener{
|
public class JoystickHandler implements KeyListener{
|
||||||
@@ -20,6 +31,48 @@ public class JoystickHandler implements KeyListener{
|
|||||||
listeners = new ArrayList<JoystickListener>();
|
listeners = new ArrayList<JoystickListener>();
|
||||||
keys = new HashSet<Integer>();
|
keys = new HashSet<Integer>();
|
||||||
j = new Joystick();
|
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) {
|
public void addJoystickListener(JoystickListener toAdd) {
|
||||||
@@ -73,14 +126,26 @@ public class JoystickHandler implements KeyListener{
|
|||||||
j.setPosition(Position.CENTER);
|
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
|
@Override
|
||||||
public void keyPressed(KeyEvent e) {
|
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)
|
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);
|
Set<Integer> keysCopy = new HashSet<Integer>(keys);
|
||||||
System.out.println("YES!");
|
|
||||||
keys.add(e.getKeyCode());
|
keys.add(e.getKeyCode());
|
||||||
updateJoystickPosition();
|
updateJoystickPosition();
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ public class Window extends JFrame {
|
|||||||
super("Arcade");
|
super("Arcade");
|
||||||
setSize(1280, 1024);
|
setSize(1280, 1024);
|
||||||
|
|
||||||
this.ON_RASP = ON_RASP;
|
Window.ON_RASP = ON_RASP;
|
||||||
System.out.println(ON_RASP);
|
System.out.println(ON_RASP);
|
||||||
|
|
||||||
//Set window close listener
|
//Set window close listener
|
||||||
@@ -56,8 +56,10 @@ public class Window extends JFrame {
|
|||||||
setContentPane(view);
|
setContentPane(view);
|
||||||
|
|
||||||
//Create EventListeners
|
//Create EventListeners
|
||||||
new GPIOListener();
|
if(!Window.ON_RASP){
|
||||||
addKeyListener(jsh);
|
addKeyListener(bth);
|
||||||
|
addKeyListener(jsh);
|
||||||
|
}
|
||||||
bth.addButtonListener(control);
|
bth.addButtonListener(control);
|
||||||
jsh.addJoystickListener(control);
|
jsh.addJoystickListener(control);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user