made relaisboard lamp blinking loopable

This commit is contained in:
Lucas Pleß 2012-06-13 23:00:51 +02:00
parent 6d43e934a9
commit 51f32a5e7a
3 changed files with 89 additions and 18 deletions

View File

@ -5,6 +5,8 @@ public interface IRelaisboard {
void setRelais(final int relais, final boolean state); void setRelais(final int relais, final boolean state);
void toggleRelais(final int relais, final int milliseconds); void toggleRelais(final int relais, final int milliseconds);
void blinkRelais(final int relais, final int pause, final int count); void blinkRelais(final int relais, final int pause, final int count);
void blinkRelais(final int relais, final int pause);
void blinkRelaisStop(final int relais);
boolean open(); boolean open();
void close(); void close();

View File

@ -4,7 +4,8 @@ import de.ctdo.crashtest.log.Logger;
import gnu.io.*; import gnu.io.*;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Enumeration; import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class Relaisboard implements IRelaisboard { public class Relaisboard implements IRelaisboard {
private SerialPort serialPort; private SerialPort serialPort;
@ -12,6 +13,8 @@ public class Relaisboard implements IRelaisboard {
private Boolean serialPortGeoeffnet = false; private Boolean serialPortGeoeffnet = false;
private String portName = "/dev/ttyUSB0"; private String portName = "/dev/ttyUSB0";
private Object mLock = new Object(); private Object mLock = new Object();
private final Map<Integer,Thread> threadMap = new ConcurrentHashMap<Integer, Thread>();
private final Map<Integer,Boolean> stopMap = new ConcurrentHashMap<Integer, Boolean>();
public Relaisboard(final String port) { public Relaisboard(final String port) {
this.portName = port; this.portName = port;
@ -30,6 +33,8 @@ public class Relaisboard implements IRelaisboard {
outputStream.write(charsOff[relais]); outputStream.write(charsOff[relais]);
} }
System.out.println("Relaisboard: sendData " + relais + " = " + state);
outputStream.flush(); outputStream.flush();
} catch (IOException e) { } catch (IOException e) {
Logger.sLog("Relaisboard error: Fehler beim Senden"); Logger.sLog("Relaisboard error: Fehler beim Senden");
@ -89,15 +94,17 @@ public class Relaisboard implements IRelaisboard {
public void close() { public void close() {
if ( serialPortGeoeffnet ) { if ( serialPortGeoeffnet ) {
serialPort.close(); serialPort.close();
}
serialPortGeoeffnet = false; serialPortGeoeffnet = false;
outputStream = null; outputStream = null;
} }
}
@Override @Override
public void setRelais(final int relais, final boolean state) { public void setRelais(final int relais, final boolean state) {
if(!serialPortGeoeffnet) return; if(!serialPortGeoeffnet) return;
stopRelaisThread(relais);
Runnable r = new Runnable() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
@ -112,6 +119,8 @@ public class Relaisboard implements IRelaisboard {
public void toggleRelais(final int relais, final int milliseconds) { public void toggleRelais(final int relais, final int milliseconds) {
if(!serialPortGeoeffnet) return; if(!serialPortGeoeffnet) return;
stopRelaisThread(relais);
Runnable r = new Runnable() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
@ -119,9 +128,7 @@ public class Relaisboard implements IRelaisboard {
try { try {
Thread.sleep(milliseconds); Thread.sleep(milliseconds);
} catch (InterruptedException e) { } catch (InterruptedException e) { }
e.printStackTrace();
}
sendData(relais, false); sendData(relais, false);
} }
@ -134,6 +141,8 @@ public class Relaisboard implements IRelaisboard {
public void blinkRelais(final int relais, final int pause, final int count) { public void blinkRelais(final int relais, final int pause, final int count) {
if(!serialPortGeoeffnet) return; if(!serialPortGeoeffnet) return;
stopRelaisThread(relais);
Runnable r = new Runnable() { Runnable r = new Runnable() {
@Override @Override
public void run() { public void run() {
@ -144,21 +153,76 @@ public class Relaisboard implements IRelaisboard {
try { try {
Thread.sleep(pause); Thread.sleep(pause);
} catch (InterruptedException e) { } catch (InterruptedException e) { }
e.printStackTrace();
} if(stopMap.get(relais)) return;
sendData(relais, false); sendData(relais, false);
try { try {
Thread.sleep(pause); Thread.sleep(pause);
} catch (InterruptedException e) { } catch (InterruptedException e) { }
e.printStackTrace();
if(stopMap.get(relais)) return;
} }
if(stopMap.get(relais)) sendData(relais,false); // switch off afterwards
}
};
stopMap.put(relais, false);
Thread thread = new Thread(r);
threadMap.put(relais,thread);
thread.start();
}
@Override
public void blinkRelais(final int relais, final int pause) {
if(!serialPortGeoeffnet) return;
stopRelaisThread(relais);
Runnable r = new Runnable() {
@Override
public void run() {
while(true) {
sendData(relais, true);
try {
Thread.sleep(pause);
} catch (InterruptedException e) { }
if(stopMap.get(relais)) return;
sendData(relais, false);
try {
Thread.sleep(pause);
} catch (InterruptedException e) { }
if(stopMap.get(relais)) return;
} }
} }
}; };
new Thread(r).start(); stopMap.put(relais, false);
Thread thread = new Thread(r);
threadMap.put(relais,thread);
thread.start();
}
@Override
public void blinkRelaisStop(final int relais) {
stopRelaisThread(relais);
}
private void stopRelaisThread(final int relais) {
Thread thread = (Thread)threadMap.get(relais);
if(thread != null) {
stopMap.put(relais, true);
thread.interrupt();
threadMap.remove(relais);
}
} }
} }

View File

@ -152,7 +152,7 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
sayScore(); sayScore();
relaisboard.setRelais(6, false); // disable third green circle relaisboard.setRelais(6, false); // disable third green circle
relaisboard.blinkRelais(2, 700, 6); // hint Button relaisboard.blinkRelais(2, 700); // hint Button
break; break;
case ROKET_STARTED: case ROKET_STARTED:
@ -163,8 +163,9 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
bunti.setLampel(false,true,false); bunti.setLampel(false,true,false);
bunti.setPar56(0, 255, 0); bunti.setPar56(0, 255, 0);
relaisboard.toggleRelais(0, 300); relaisboard.toggleRelais(0, 300); // r0kets toogle
relaisboard.toggleRelais(3, 10000); relaisboard.toggleRelais(3, 10000); // oven
relaisboard.blinkRelaisStop(2); // stop hint button lamp
guiControl.setWall("Pizza ist fertig!"); guiControl.setWall("Pizza ist fertig!");
guiControl.showCountDown(true); guiControl.showCountDown(true);
@ -330,6 +331,10 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
relaisboard.setRelais(3, false); relaisboard.setRelais(3, false);
} else if(params.startsWith("rokets")) { } else if(params.startsWith("rokets")) {
relaisboard.toggleRelais(0, 300); relaisboard.toggleRelais(0, 300);
} else if(params.startsWith("lamp blink")) {
relaisboard.blinkRelais(2, 700);
} else if(params.startsWith("lamp stop")) {
relaisboard.blinkRelaisStop(2);
} }
@ -372,7 +377,7 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
ircClient.say("i will tell you the current game score"); ircClient.say("i will tell you the current game score");
} else if(message.contains("relais")) { } else if(message.contains("relais")) {
ircClient.say("control the relais board"); ircClient.say("control the relais board");
ircClient.say("valid commands: lamp on, lamp off, oven on, oven off, rokets"); ircClient.say("valid commands: lamp on, lamp off, lamp blink, lamp stop, oven on, oven off, rokets");
} else { } else {
ircClient.say("dafuq?"); ircClient.say("dafuq?");
} }