From 51f32a5e7ab7b32c720016b20edfbb032b8fcbc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Wed, 13 Jun 2012 23:00:51 +0200 Subject: [PATCH] made relaisboard lamp blinking loopable --- .../ctdo/crashtest/domotics/IRelaisboard.java | 2 + .../ctdo/crashtest/domotics/Relaisboard.java | 90 ++++++++++++++++--- src/de/ctdo/crashtest/game/TheGame.java | 15 ++-- 3 files changed, 89 insertions(+), 18 deletions(-) diff --git a/src/de/ctdo/crashtest/domotics/IRelaisboard.java b/src/de/ctdo/crashtest/domotics/IRelaisboard.java index 2bcbfaa..9be3e66 100644 --- a/src/de/ctdo/crashtest/domotics/IRelaisboard.java +++ b/src/de/ctdo/crashtest/domotics/IRelaisboard.java @@ -5,6 +5,8 @@ public interface IRelaisboard { void setRelais(final int relais, final boolean state); 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); + void blinkRelaisStop(final int relais); boolean open(); void close(); diff --git a/src/de/ctdo/crashtest/domotics/Relaisboard.java b/src/de/ctdo/crashtest/domotics/Relaisboard.java index b178e34..563fb89 100644 --- a/src/de/ctdo/crashtest/domotics/Relaisboard.java +++ b/src/de/ctdo/crashtest/domotics/Relaisboard.java @@ -4,7 +4,8 @@ import de.ctdo.crashtest.log.Logger; import gnu.io.*; import java.io.IOException; import java.io.OutputStream; -import java.util.Enumeration; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; public class Relaisboard implements IRelaisboard { private SerialPort serialPort; @@ -12,6 +13,8 @@ public class Relaisboard implements IRelaisboard { private Boolean serialPortGeoeffnet = false; private String portName = "/dev/ttyUSB0"; private Object mLock = new Object(); + private final Map threadMap = new ConcurrentHashMap(); + private final Map stopMap = new ConcurrentHashMap(); public Relaisboard(final String port) { this.portName = port; @@ -30,6 +33,8 @@ public class Relaisboard implements IRelaisboard { outputStream.write(charsOff[relais]); } + System.out.println("Relaisboard: sendData " + relais + " = " + state); + outputStream.flush(); } catch (IOException e) { Logger.sLog("Relaisboard error: Fehler beim Senden"); @@ -89,15 +94,17 @@ public class Relaisboard implements IRelaisboard { public void close() { if ( serialPortGeoeffnet ) { serialPort.close(); - serialPortGeoeffnet = false; - outputStream = null; } + serialPortGeoeffnet = false; + outputStream = null; } @Override public void setRelais(final int relais, final boolean state) { if(!serialPortGeoeffnet) return; + stopRelaisThread(relais); + Runnable r = new Runnable() { @Override public void run() { @@ -112,6 +119,8 @@ public class Relaisboard implements IRelaisboard { public void toggleRelais(final int relais, final int milliseconds) { if(!serialPortGeoeffnet) return; + stopRelaisThread(relais); + Runnable r = new Runnable() { @Override public void run() { @@ -119,9 +128,7 @@ public class Relaisboard implements IRelaisboard { try { Thread.sleep(milliseconds); - } catch (InterruptedException e) { - e.printStackTrace(); - } + } catch (InterruptedException e) { } sendData(relais, false); } @@ -134,6 +141,8 @@ public class Relaisboard implements IRelaisboard { public void blinkRelais(final int relais, final int pause, final int count) { if(!serialPortGeoeffnet) return; + stopRelaisThread(relais); + Runnable r = new Runnable() { @Override public void run() { @@ -144,21 +153,76 @@ public class Relaisboard implements IRelaisboard { try { Thread.sleep(pause); - } catch (InterruptedException e) { - e.printStackTrace(); - } + } catch (InterruptedException e) { } + + if(stopMap.get(relais)) return; sendData(relais, false); try { Thread.sleep(pause); - } catch (InterruptedException e) { - e.printStackTrace(); - } + } catch (InterruptedException e) { } + + 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); + } } } diff --git a/src/de/ctdo/crashtest/game/TheGame.java b/src/de/ctdo/crashtest/game/TheGame.java index cbfae9b..cb761ca 100644 --- a/src/de/ctdo/crashtest/game/TheGame.java +++ b/src/de/ctdo/crashtest/game/TheGame.java @@ -151,8 +151,8 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent sayScore(); - relaisboard.setRelais(6, false); // disable third green circle - relaisboard.blinkRelais(2, 700, 6); // hint Button + relaisboard.setRelais(6, false); // disable third green circle + relaisboard.blinkRelais(2, 700); // hint Button break; case ROKET_STARTED: @@ -163,8 +163,9 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent bunti.setLampel(false,true,false); bunti.setPar56(0, 255, 0); - relaisboard.toggleRelais(0, 300); - relaisboard.toggleRelais(3, 10000); + relaisboard.toggleRelais(0, 300); // r0kets toogle + relaisboard.toggleRelais(3, 10000); // oven + relaisboard.blinkRelaisStop(2); // stop hint button lamp guiControl.setWall("Pizza ist fertig!"); guiControl.showCountDown(true); @@ -330,6 +331,10 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent relaisboard.setRelais(3, false); } else if(params.startsWith("rokets")) { 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"); } else if(message.contains("relais")) { 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 { ircClient.say("dafuq?"); }