Added working buttons stuff
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package control;
|
||||
|
||||
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 JoystickListener, ButtonListener{
|
||||
|
||||
GameModel model;
|
||||
GameView view;
|
||||
|
||||
public GameControl(GameModel model, GameView view)
|
||||
{
|
||||
this.model = model;
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buttonPressed(ButtonEvent e) {
|
||||
view.setColor(e.getButton().getColor());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buttonReleased(ButtonEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onJoyStickMoved(JoystickEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
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==-1)
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package control.button;
|
||||
|
||||
import java.awt.Color;
|
||||
|
||||
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()
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package control.button;
|
||||
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import control.LedHandler;
|
||||
|
||||
public class ButtonHandler implements KeyListener{
|
||||
|
||||
List<ButtonListener> listeners;
|
||||
static List<Button> buttons;
|
||||
LedHandler led;
|
||||
|
||||
public ButtonHandler(LedHandler led)
|
||||
{
|
||||
this.led = led;
|
||||
|
||||
listeners = new ArrayList<ButtonListener>();
|
||||
buttons = new ArrayList<Button>();
|
||||
|
||||
buttons.add(new Button(0, -1, led));
|
||||
buttons.add(new Button(1, 5, led)); //TODO change to real ID's;
|
||||
buttons.add(new Button(2, 4, led));
|
||||
buttons.add(new Button(3, 3, led));
|
||||
buttons.add(new Button(4, 2, led));
|
||||
buttons.add(new Button(5, 1, led));
|
||||
buttons.add(new Button(6, 0, led));
|
||||
}
|
||||
|
||||
public void addButtonListener(ButtonListener toAdd) {
|
||||
listeners.add(toAdd);
|
||||
}
|
||||
|
||||
public void buttonPress(Button b) {
|
||||
ButtonEvent e = new ButtonEvent(b, System.currentTimeMillis());
|
||||
for (ButtonListener bt : listeners)
|
||||
bt.buttonPressed(e);
|
||||
}
|
||||
|
||||
public void buttonRelease(Button b) {
|
||||
ButtonEvent e = new ButtonEvent(b, System.currentTimeMillis());
|
||||
for (ButtonListener bt : listeners)
|
||||
bt.buttonReleased(e);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
switch(e.getKeyCode())
|
||||
{
|
||||
case KeyEvent.VK_0:
|
||||
buttonPress(buttons.get(0));
|
||||
break;
|
||||
case KeyEvent.VK_1:
|
||||
buttonPress(buttons.get(1));
|
||||
break;
|
||||
case KeyEvent.VK_2:
|
||||
buttonPress(buttons.get(2));
|
||||
break;
|
||||
case KeyEvent.VK_3:
|
||||
buttonPress(buttons.get(3));
|
||||
break;
|
||||
case KeyEvent.VK_4:
|
||||
buttonPress(buttons.get(4));
|
||||
break;
|
||||
case KeyEvent.VK_5:
|
||||
buttonPress(buttons.get(5));
|
||||
break;
|
||||
case KeyEvent.VK_6:
|
||||
buttonPress(buttons.get(6));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void keyReleased(KeyEvent arg0) {}
|
||||
public void keyTyped(KeyEvent arg0) {}
|
||||
|
||||
public static List<Button> getButtons()
|
||||
{
|
||||
return buttons;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package control.button;
|
||||
|
||||
public interface ButtonListener {
|
||||
public void buttonPressed(ButtonEvent e);
|
||||
public void buttonReleased(ButtonEvent e);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package control.joystick;
|
||||
|
||||
public class Joystick {
|
||||
|
||||
public Joystick()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package control.joystick;
|
||||
|
||||
public class JoystickEvent {
|
||||
private Joystick joystick;
|
||||
private Long now;
|
||||
|
||||
public JoystickEvent(Joystick joystick, Long now){
|
||||
this.joystick = joystick;
|
||||
this.now = now;
|
||||
}
|
||||
|
||||
public Joystick getJoystick() {
|
||||
return joystick;
|
||||
}
|
||||
|
||||
public Long getNow() {
|
||||
return now;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package control.joystick;
|
||||
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import control.LedHandler;
|
||||
|
||||
public class JoystickHandler implements KeyListener{
|
||||
|
||||
List<JoystickListener> listeners;
|
||||
LedHandler led;
|
||||
|
||||
public JoystickHandler(LedHandler led)
|
||||
{
|
||||
this.led = led;
|
||||
listeners = new ArrayList<JoystickListener>();
|
||||
}
|
||||
|
||||
public void addJoyStickListener(JoystickListener toAdd) {
|
||||
listeners.add(toAdd);
|
||||
}
|
||||
|
||||
public void onJoyStickMoved() {
|
||||
JoystickEvent e = new JoystickEvent(new Joystick(), 1000L); //TODO edit
|
||||
for (JoystickListener yst : listeners)
|
||||
yst.onJoyStickMoved(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyPressed(KeyEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent e) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package control.joystick;
|
||||
|
||||
public interface JoystickListener {
|
||||
public void onJoyStickMoved(JoystickEvent e);
|
||||
}
|
||||
Reference in New Issue
Block a user