Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2338366080 | ||
|
|
1225a82a07 | ||
|
|
9be062fcd9 | ||
|
|
3bf8280f79 | ||
|
|
35884a2726 | ||
|
|
c5c9b58b16 | ||
|
|
678201d118 | ||
|
|
36cbc2b012 | ||
|
|
f7175aa70a | ||
|
|
27c04e2b2a | ||
|
|
5683b38210 | ||
|
|
28c8d35670 | ||
|
|
564f10af62 | ||
|
|
68d6c61ca5 | ||
|
|
a2c4f6f9eb | ||
|
|
d8dbcc0719 | ||
|
|
2c7c917d02 | ||
|
|
435296b9be | ||
|
|
39484a720d | ||
|
|
c256cdfe13 |
+26
@@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="lib" path="/home/janco/Documenten/github/Arcade-controls/pi4j-service.jar">
|
||||
<attributes>
|
||||
<attribute name="javadoc_location" value="http://pi4j.com/apidocs/"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="lib" path="/home/janco/Documenten/github/Arcade-controls/pi4j-gpio-extension.jar">
|
||||
<attributes>
|
||||
<attribute name="javadoc_location" value="http://pi4j.com/apidocs/"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="lib" path="/home/janco/Documenten/github/Arcade-controls/pi4j-device.jar">
|
||||
<attributes>
|
||||
<attribute name="javadoc_location" value="http://pi4j.com/apidocs/"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="lib" path="/home/janco/Documenten/github/Arcade-controls/pi4j-core.jar">
|
||||
<attributes>
|
||||
<attribute name="javadoc_location" value="http://pi4j.com/apidocs/"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Arcade-controls</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,36 @@
|
||||
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) {
|
||||
if(e.getButton().getButtonID()>0)
|
||||
{
|
||||
view.setColor(e.getButton().getColor());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void buttonReleased(ButtonEvent e) {}
|
||||
|
||||
@Override
|
||||
public void onJoystickMoved(JoystickEvent e) {
|
||||
view.setString(e.getJoystick().getPos().name());
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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,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<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, 2, led));
|
||||
buttons.add(new Button(2, 1, led));
|
||||
buttons.add(new Button(3, 0, led));
|
||||
buttons.add(new Button(4, 3, led));
|
||||
buttons.add(new Button(5, 4, led));
|
||||
buttons.add(new Button(6, 5, led));
|
||||
|
||||
for(Button b : buttons)
|
||||
{
|
||||
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) {
|
||||
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;
|
||||
case KeyEvent.VK_ESCAPE:
|
||||
if(!Window.ON_RASP)
|
||||
System.exit(0);
|
||||
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,32 @@
|
||||
package control.joystick;
|
||||
|
||||
public class Joystick {
|
||||
|
||||
public enum Position {
|
||||
UP,
|
||||
LEFT,
|
||||
DOWN,
|
||||
RIGHT,
|
||||
UP_RIGHT,
|
||||
DOWN_RIGHT,
|
||||
UP_LEFT,
|
||||
DOWN_LEFT,
|
||||
CENTER
|
||||
}
|
||||
|
||||
private Position pos;
|
||||
|
||||
public Joystick()
|
||||
{
|
||||
pos = Position.CENTER;
|
||||
}
|
||||
|
||||
public void setPosition(Position pos)
|
||||
{
|
||||
this.pos = pos;
|
||||
}
|
||||
public Position getPos() {
|
||||
return pos;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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,176 @@
|
||||
package control.joystick;
|
||||
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.util.ArrayList;
|
||||
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{
|
||||
|
||||
List<JoystickListener> listeners;
|
||||
Set<Integer> keys;
|
||||
Joystick j;
|
||||
|
||||
public JoystickHandler()
|
||||
{
|
||||
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) {
|
||||
listeners.add(toAdd);
|
||||
}
|
||||
|
||||
public void onJoystickMoved(Joystick j) {
|
||||
JoystickEvent e = new JoystickEvent(j, System.currentTimeMillis());
|
||||
for (JoystickListener yst : listeners)
|
||||
yst.onJoystickMoved(e);
|
||||
}
|
||||
|
||||
private void updateJoystickPosition()
|
||||
{
|
||||
if(keys.contains(KeyEvent.VK_UP) && keys.contains(KeyEvent.VK_RIGHT))
|
||||
{
|
||||
j.setPosition(Position.UP_RIGHT);
|
||||
}
|
||||
else if(keys.contains(KeyEvent.VK_UP) && keys.contains(KeyEvent.VK_LEFT))
|
||||
{
|
||||
j.setPosition(Position.UP_LEFT);
|
||||
}
|
||||
else if(keys.contains(KeyEvent.VK_DOWN) && keys.contains(KeyEvent.VK_RIGHT))
|
||||
{
|
||||
j.setPosition(Position.DOWN_RIGHT);
|
||||
}
|
||||
else if(keys.contains(KeyEvent.VK_DOWN) && keys.contains(KeyEvent.VK_LEFT))
|
||||
{
|
||||
j.setPosition(Position.DOWN_LEFT);
|
||||
}
|
||||
|
||||
else if(keys.contains(KeyEvent.VK_UP))
|
||||
{
|
||||
j.setPosition(Position.UP);
|
||||
}
|
||||
else if(keys.contains(KeyEvent.VK_LEFT))
|
||||
{
|
||||
j.setPosition(Position.LEFT);
|
||||
}
|
||||
else if(keys.contains(KeyEvent.VK_RIGHT))
|
||||
{
|
||||
j.setPosition(Position.RIGHT);
|
||||
}
|
||||
else if(keys.contains(KeyEvent.VK_DOWN))
|
||||
{
|
||||
j.setPosition(Position.DOWN);
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
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) {
|
||||
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);
|
||||
keys.add(e.getKeyCode());
|
||||
updateJoystickPosition();
|
||||
|
||||
if(!keys.equals(keysCopy))
|
||||
{
|
||||
onJoystickMoved(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyReleased(KeyEvent e) {
|
||||
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);
|
||||
|
||||
keys.remove(e.getKeyCode());
|
||||
updateJoystickPosition();
|
||||
|
||||
if(!keys.equals(keysCopy))
|
||||
{
|
||||
onJoystickMoved(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void keyTyped(KeyEvent e) {}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package control.joystick;
|
||||
|
||||
public interface JoystickListener {
|
||||
public void onJoystickMoved(JoystickEvent e);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import sys
|
||||
import time
|
||||
from neopixel import *
|
||||
|
||||
# LED strip configuration:
|
||||
LED_COUNT = 66 # Number of LED pixels.
|
||||
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).
|
||||
LED_FREQ_HZ = 700000 # LED signal frequency in hertz (usually 800khz)
|
||||
LED_DMA = 5 # DMA channel to use for generating signal (try 5)
|
||||
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
|
||||
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
|
||||
|
||||
def setLed(strip, l,r,g,b):
|
||||
strip.setPixelColor(l,Color(r,g,b))
|
||||
|
||||
def strobetest(strip):
|
||||
on = True
|
||||
while True:
|
||||
if on:
|
||||
on = False
|
||||
for i in range(strip.numPixels()):
|
||||
strip.setPixelColor(i, Color(255,255,255))
|
||||
else:
|
||||
on = True
|
||||
for i in range(strip.numPixels()):
|
||||
strip.setPixelColor(i, Color(0,0,0))
|
||||
strip.show()
|
||||
time.sleep(100.0/1000)
|
||||
|
||||
if __name__ == '__main__':
|
||||
ledstrip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
|
||||
ledstrip.begin()
|
||||
s = sys.stdin.readline().strip()
|
||||
while s not in ['break', 'quit']:
|
||||
message = s.split("|")
|
||||
returnms = 'e'
|
||||
if(message[0] == '1'):
|
||||
if (len(message) == 5):
|
||||
try:
|
||||
led = int(message[1])
|
||||
r = int(message[2])
|
||||
g = int(message[3])
|
||||
b = int(message[4])
|
||||
setLed(ledstrip, led,r,g,b)
|
||||
returnms = 'a'
|
||||
except ValueError:
|
||||
returnms = 'e'
|
||||
elif (message[0] == '0'):
|
||||
ledstrip.show()
|
||||
returnms = 'a'
|
||||
s = sys.stdin.readline().strip()
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package main;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
if(args.length != 1)
|
||||
{
|
||||
new Window(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
new Window(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package main;
|
||||
|
||||
import java.awt.event.WindowAdapter;
|
||||
import java.awt.event.WindowEvent;
|
||||
|
||||
import javax.swing.JFrame;
|
||||
|
||||
import model.GameModel;
|
||||
import view.GameView;
|
||||
import control.GPIOListener;
|
||||
import control.GameControl;
|
||||
import control.LedHandler;
|
||||
import control.button.ButtonHandler;
|
||||
import control.joystick.JoystickHandler;
|
||||
|
||||
public class Window extends JFrame {
|
||||
|
||||
public static boolean ON_RASP;
|
||||
|
||||
public Window(boolean ON_RASP)
|
||||
{
|
||||
//Create window
|
||||
super("Arcade");
|
||||
setSize(1280, 1024);
|
||||
|
||||
Window.ON_RASP = ON_RASP;
|
||||
System.out.println(ON_RASP);
|
||||
|
||||
//Set window close listener
|
||||
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
||||
addWindowListener(new WindowAdapter(){
|
||||
public void windowClosing(WindowEvent e) {
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
|
||||
//Set window to fullscreen
|
||||
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
|
||||
setUndecorated(true);
|
||||
|
||||
//Create Events
|
||||
LedHandler led = null;
|
||||
|
||||
if(ON_RASP) //TODO REMOVE
|
||||
{
|
||||
led = new LedHandler();
|
||||
}
|
||||
|
||||
ButtonHandler bth = new ButtonHandler(led);
|
||||
JoystickHandler jsh = new JoystickHandler();
|
||||
|
||||
//Create Instances
|
||||
GameView view = new GameView(led);
|
||||
GameModel model = new GameModel(view);
|
||||
GameControl control = new GameControl(model, view);
|
||||
setContentPane(view);
|
||||
|
||||
//Create EventListeners
|
||||
if(!Window.ON_RASP){
|
||||
addKeyListener(bth);
|
||||
addKeyListener(jsh);
|
||||
}
|
||||
bth.addButtonListener(control);
|
||||
jsh.addJoystickListener(control);
|
||||
|
||||
//Display
|
||||
pack();
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package model;
|
||||
|
||||
import view.GameView;
|
||||
|
||||
public class GameModel{
|
||||
|
||||
GameView view;
|
||||
|
||||
public GameModel(GameView view)
|
||||
{
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package view;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.Timer;
|
||||
|
||||
import main.Window;
|
||||
import control.LedHandler;
|
||||
|
||||
public class GameView extends JPanel implements ActionListener{
|
||||
|
||||
Timer t;
|
||||
Color c;
|
||||
String str;
|
||||
LedHandler led;
|
||||
|
||||
public GameView(LedHandler led)
|
||||
{
|
||||
this.led=led;
|
||||
t = new Timer(1000/30, this);
|
||||
t.start();
|
||||
c = new Color(100,100,100);
|
||||
str = "CENTER";
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
repaint();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g)
|
||||
{
|
||||
super.paintComponent(g);
|
||||
Graphics2D g2d = (Graphics2D) g;
|
||||
|
||||
g2d.setPaint(c);
|
||||
g2d.fillRect(0, 0, getWidth(), getHeight());
|
||||
|
||||
g2d.setPaint(Color.WHITE);
|
||||
g2d.drawString(str, 100, 100);
|
||||
}
|
||||
|
||||
public void setColor(Color c)
|
||||
{
|
||||
this.c = c;
|
||||
if(Window.ON_RASP)
|
||||
{
|
||||
for(int i =6; i < 55; i++){
|
||||
led.setLed(i, c.getRed(), c.getGreen(), c.getBlue());
|
||||
}
|
||||
led.show();
|
||||
}
|
||||
}
|
||||
|
||||
public void setString(String str) {
|
||||
this.str = str;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user