erste rudimentaer funktionierende version mit tisch und button und lichtschranke

This commit is contained in:
Lucas Pleß 2012-05-17 01:13:06 +02:00
parent 7f2b98a554
commit 418bdf2801
3 changed files with 50 additions and 105 deletions

View File

@ -4,11 +4,11 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class Statemachine { public class Statemachine {
private final char BLUE_BUTTON = 'd'; private final char BLUE_BUTTON = 'E';
private final char LIGHT_BARRIER = 'k'; private final char LIGHT_BARRIER = 'F';
private final char TABLE_ONE = 'c'; private final char TABLE_ONE = 'G';
private final char TABLE_TWO = 'v'; private final char TABLE_TWO = 'H';
private final char TABLE_THREE = 'b'; private final char TABLE_THREE = 'I';
private long lastHandleInput = 0; private long lastHandleInput = 0;
private final List<StateChangeListener> stateChangeListenerList = new ArrayList<StateChangeListener>(); private final List<StateChangeListener> stateChangeListenerList = new ArrayList<StateChangeListener>();
private int stateChangeCounter = 0; private int stateChangeCounter = 0;
@ -122,13 +122,17 @@ public class Statemachine {
case TABLE_GAME_TWO: case TABLE_GAME_TWO:
if(input == TABLE_THREE) { if(input == TABLE_THREE) {
retVal = state.TABLE_GAME_THREE; retVal = state.TABLE_GAME_THREE;
} else { } else if (input == TABLE_TWO) {
} else {
retVal = state.TABLE_GAME_ONE; retVal = state.TABLE_GAME_ONE;
} }
break; break;
case TABLE_GAME_THREE: case TABLE_GAME_THREE:
if(input == TABLE_TWO) { if(input == TABLE_TWO) {
retVal = state.TABLE_GAME_FOUR; retVal = state.TABLE_GAME_FOUR;
} else if (input == TABLE_THREE) {
} else { } else {
retVal = state.TABLE_GAME_ONE; retVal = state.TABLE_GAME_ONE;
} }
@ -136,6 +140,8 @@ public class Statemachine {
case TABLE_GAME_FOUR: case TABLE_GAME_FOUR:
if(input == TABLE_THREE) { if(input == TABLE_THREE) {
retVal = state.TABLE_GAME_FIVE; retVal = state.TABLE_GAME_FIVE;
} else if (input == TABLE_TWO) {
} else { } else {
retVal = state.TABLE_GAME_ONE; retVal = state.TABLE_GAME_ONE;
} }
@ -143,6 +149,8 @@ public class Statemachine {
case TABLE_GAME_FIVE: case TABLE_GAME_FIVE:
if(input == TABLE_ONE) { if(input == TABLE_ONE) {
retVal = state.TABLE_GAME_SIX; retVal = state.TABLE_GAME_SIX;
} else if (input == TABLE_THREE) {
} else { } else {
retVal = state.TABLE_GAME_ONE; retVal = state.TABLE_GAME_ONE;
} }
@ -150,6 +158,8 @@ public class Statemachine {
case TABLE_GAME_SIX: case TABLE_GAME_SIX:
if(input == TABLE_THREE) { if(input == TABLE_THREE) {
retVal = state.TABLE_GAME_SEVEN; retVal = state.TABLE_GAME_SEVEN;
} else if (input == TABLE_ONE) {
} else { } else {
retVal = state.TABLE_GAME_ONE; retVal = state.TABLE_GAME_ONE;
} }
@ -157,6 +167,8 @@ public class Statemachine {
case TABLE_GAME_SEVEN: case TABLE_GAME_SEVEN:
if(input == BLUE_BUTTON) { if(input == BLUE_BUTTON) {
retVal = state.FINISH; retVal = state.FINISH;
} else if (input == TABLE_THREE) {
} else { } else {
retVal = state.TABLE_GAME_ONE; retVal = state.TABLE_GAME_ONE;
} }

View File

@ -7,104 +7,13 @@ import java.nio.CharBuffer;
public class Steuerung { public class Steuerung {
private static enum STATE {
IDLE,
ROOM_ENTERED,
TABLE_GAME,
}
private STATE current_state = STATE.IDLE;
public static void main(String args[]) { public static void main(String args[]) {
//new Steuerung().run();
new SteuerungFrame(); new SteuerungFrame();
} }
public void FlushInputBuffer() {
int TempValue = 0;
do {
try {
TempValue = System.in.read();
}
catch (java.io.IOException e) {
}
} while (TempValue != 10);
}
public char GetChar() {
int ReadValue = 0;
try {
ReadValue = System.in.read();
}
catch (java.io.IOException e) {
}
this.FlushInputBuffer();
return (char) ReadValue;
}
public void run() {
boolean doRun = true;
//BufferedReader in = new BufferedReader(new InputStreamReader( System.in ) );
InputStreamReader in = new InputStreamReader(System.in);
while(doRun) {
char[] buffer = new char[100];
try {
char c = GetChar();
System.out.println("char = " + c);
//if(command.equals("exit") || command.equals("quit")) doRun = false;
//handleCommand(command);
Thread.sleep(200);
// } catch (IOException e) {
// e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Current state after: " + current_state);
}
}
private void handleCommand(String command) {
System.out.println("Current state before: " + current_state);
switch (current_state) {
case IDLE:
if(command.equals("c")) {
current_state = STATE.ROOM_ENTERED;
}
break;
case ROOM_ENTERED:
break;
case TABLE_GAME:
break;
}
}
} }

View File

@ -9,6 +9,7 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Random;
public class SteuerungFrame extends JFrame implements StateChangeListener { public class SteuerungFrame extends JFrame implements StateChangeListener {
private JPanel pnlRoot; private JPanel pnlRoot;
@ -50,9 +51,17 @@ public class SteuerungFrame extends JFrame implements StateChangeListener {
@Override @Override
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
lastKey = e.getKeyChar(); lastKey = e.getKeyChar();
//System.out.println(e.getKeyChar());
machine.handleInput(e.getKeyChar()); if (lastKey == KeyEvent.VK_F) {
//bunti.setPar56(1, 0xff, 0xff, 0xff); Random r = new Random();
setLEDs(r.nextInt(255), r.nextInt(255),r.nextInt(255));
}
if (lastKey == KeyEvent.VK_1) {
machine.reset();
} else {
machine.handleInput(e.getKeyChar());
}
updateGui(); updateGui();
} }
@ -78,40 +87,55 @@ public class SteuerungFrame extends JFrame implements StateChangeListener {
switch (newState) { switch (newState) {
case IDLE: case IDLE:
bunti.setPar56(0,0,0,0); setLEDs(0,0,0);
bunti.setPar56(1,0,0,0);
bunti.setPar56(2,0,0,0);
bunti.setPar56(3,0,0,0);
bunti.setLampel(false,false,false); bunti.setLampel(false,false,false);
break; break;
case ENTERED_ROOM: case ENTERED_ROOM:
bunti.setLampel(true,false,false); bunti.setLampel(true,false,false);
setLEDs(255,0,0);
break; break;
case TABLE_GAME_ONE: case TABLE_GAME_ONE:
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
setLEDs(255,255,0);
break; break;
case TABLE_GAME_TWO: case TABLE_GAME_TWO:
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
setLEDs(0,255,0);
break; break;
case TABLE_GAME_THREE: case TABLE_GAME_THREE:
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
setLEDs(0,255,255);
break; break;
case TABLE_GAME_FOUR: case TABLE_GAME_FOUR:
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
setLEDs(0,0,255);
break; break;
case TABLE_GAME_FIVE: case TABLE_GAME_FIVE:
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
setLEDs(255,255,255);
break; break;
case TABLE_GAME_SIX: case TABLE_GAME_SIX:
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
setLEDs(30,30,30);
break; break;
case TABLE_GAME_SEVEN: case TABLE_GAME_SEVEN:
bunti.setLampel(false,false,true); bunti.setLampel(false,false,true);
setLEDs(30,0,255);
break;
case FINISH:
setLEDs(0, 255, 0);
break; break;
} }
} }
private void setLEDs(int red, int green, int blue) {
bunti.setPar56(0,red,green,blue);
bunti.setPar56(1,red,green,blue);
bunti.setPar56(2,red,green,blue);
bunti.setPar56(3,red,green,blue);
}
private void initGui() { private void initGui() {
Container pane = getContentPane(); Container pane = getContentPane();