diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..35a95a8 --- /dev/null +++ b/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..2ae30a8 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Arcade-controls + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/README.md b/README.md index a608315..7e33520 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,2 @@ -# Arcade -Arcade game for the fourth term at Avans Hogeschool Breda. +branch for the buttons and joystick for gpio raspberry + diff --git a/control/GameControl.java b/control/GameControl.java index f84ab47..cfbc3f5 100644 --- a/control/GameControl.java +++ b/control/GameControl.java @@ -1,52 +1,33 @@ package control; -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; -import java.awt.event.WindowEvent; -import java.awt.event.WindowFocusListener; - import model.GameModel; import view.GameView; +import control.button.ButtonEvent; +import control.button.ButtonListener; +import control.joystick.JoystickEvent; +import control.joystick.JoystickListener; -public class GameControl implements MouseListener, KeyListener, WindowFocusListener{ +public class GameControl implements JoystickListener, ButtonListener{ GameModel model; GameView view; - public GameControl(GameModel model, GameView view) { this.model = model; this.view = view; - } - public void keyPressed(KeyEvent e) {} + @Override + public void buttonPressed(ButtonEvent e) { - public void keyReleased(KeyEvent e) { - if(e.getKeyCode() == KeyEvent.VK_ESCAPE) - { - System.exit(0); - } } - public void keyTyped(KeyEvent e) {} + @Override + public void buttonReleased(ButtonEvent e) {} - public void mouseClicked(MouseEvent e) {} + @Override + public void onJoystickMoved(JoystickEvent e) { - public void mouseEntered(MouseEvent e) {} - - public void mouseExited(MouseEvent e) {} - - public void mousePressed(MouseEvent e) {} - - public void mouseReleased(MouseEvent e) {} - - public void windowGainedFocus(WindowEvent e) {} - - public void windowLostFocus(WindowEvent e) {} - - + } } diff --git a/control/LedHandler.java b/control/LedHandler.java new file mode 100644 index 0000000..eb16234 --- /dev/null +++ b/control/LedHandler.java @@ -0,0 +1,98 @@ +package control; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; + +public class LedHandler { + private BufferedReader inp; + private BufferedWriter out; + private Process p; + + public LedHandler(){ + try { + p = Runtime.getRuntime().exec("sudo python led.py"); + inp = new BufferedReader( new InputStreamReader(p.getInputStream()) ); + out = new BufferedWriter( new OutputStreamWriter(p.getOutputStream()) ); + + //setLed(15, 100, 100, 100); + //strobo(); + } + catch (Exception err) { + err.printStackTrace(); + } + } + + public void close(){ + try { + inp.close(); + } catch (IOException e) { + e.printStackTrace(); + } + try { + out.close(); + } catch (IOException e) { + e.printStackTrace(); + } + p.destroy(); + } + + + /** + * Set the color of a specific led + * @param Pixel number of the led + * @param Red value + * @param Green value + * @param Blue value + * Remember: Button leds are GRB, normal ledstrip is RGB + */ + public void setLed(int led, int r, int g, int b) { + if(led<0) + return; + try { + System.out.println("Set the led with " + r); + out.write( "1|" + led + "|" + r + "|" + g + "|" + b + "\n" ); + out.flush(); + } + catch (Exception err) { + err.printStackTrace(); + } + } + + /** + * Shows the (new) colors + */ + public void show(){ + try { + out.write( "0\n" ); + out.flush(); + } + catch (Exception err) { + err.printStackTrace(); + } + } + + public void strobo(){ + boolean on = true; + while(true){ + if(on){ + for(int i = 1; i < 66; i++){ + setLed(i, 0, 0, 0); + } + }else{ + for(int i = 1; i < 66; i++){ + setLed(i, 255, 255, 255); + } + } + on = !on; + show(); + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } +} diff --git a/control/button/Button.java b/control/button/Button.java new file mode 100644 index 0000000..765dd44 --- /dev/null +++ b/control/button/Button.java @@ -0,0 +1,46 @@ +package control.button; + +import java.awt.Color; + +import main.Window; +import control.LedHandler; + +public class Button { + + Color color; + int ledID; + int buttonID; + LedHandler led; + + public Button(int buttonID, int ledID, LedHandler led) + { + color = new Color(255,255,255); + this.ledID = ledID; + this.buttonID = buttonID; + this.led = led; + setLed(); + } + + private void setLed() + { + if(Window.ON_RASP) + { + led.setLed(ledID, color.getGreen(), color.getRed(), color.getBlue()); + led.show(); + } + } + + public void setColor(Color newColor) + { + color = newColor; + setLed(); + } + + public Color getColor(){ + return color; + } + public int getButtonID() + { + return buttonID; + } +} diff --git a/control/button/ButtonEvent.java b/control/button/ButtonEvent.java new file mode 100644 index 0000000..1477528 --- /dev/null +++ b/control/button/ButtonEvent.java @@ -0,0 +1,19 @@ +package control.button; + +public class ButtonEvent { + private Button button; + private Long now; + + public ButtonEvent(Button button, Long now){ + this.button = button; + this.now = now; + } + + public Button getButton() { + return button; + } + + public Long getNow() { + return now; + } +} diff --git a/control/button/ButtonHandler.java b/control/button/ButtonHandler.java new file mode 100644 index 0000000..e75415b --- /dev/null +++ b/control/button/ButtonHandler.java @@ -0,0 +1,136 @@ +package control.button; + +import java.awt.Color; +import java.awt.event.KeyEvent; +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; + +public class ButtonHandler implements KeyListener{ + + List listeners; + static List