connected serial lib for relaisboard.

Now working
This commit is contained in:
Lucas Pleß 2012-06-06 23:25:07 +02:00
parent 2fee496a54
commit 6e81052e38
7 changed files with 114 additions and 40 deletions

View File

@ -14,6 +14,8 @@
<element id="extracted-dir" path="$PROJECT_DIR$/libs/httpmime-4.1.3.jar" path-in-jar="/" /> <element id="extracted-dir" path="$PROJECT_DIR$/libs/httpmime-4.1.3.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/libs/javampd-4.0.jar" path-in-jar="/" /> <element id="extracted-dir" path="$PROJECT_DIR$/libs/javampd-4.0.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/libs/jerklib.jar" path-in-jar="/" /> <element id="extracted-dir" path="$PROJECT_DIR$/libs/jerklib.jar" path-in-jar="/" />
<element id="extracted-dir" path="$PROJECT_DIR$/libs/rxtx-2.1-7-bins-r2/RXTXcomm.jar" path-in-jar="/" />
<element id="file-copy" path="$PROJECT_DIR$/librxtxSerial.so" />
</root> </root>
</artifact> </artifact>
</component> </component>

BIN
librxtxSerial.so Normal file

Binary file not shown.

View File

@ -1,3 +1,3 @@
Manifest-Version: 1.0 Manifest-Version: 1.0
Main-Class: de.ctdo.crashtest.SteuerungFrame Main-Class: de.ctdo.crashtest.Steuerung

View File

@ -1,5 +1,7 @@
package de.ctdo.crashtest; package de.ctdo.crashtest;
import de.ctdo.crashtest.domotics.IRelaisboard;
import de.ctdo.crashtest.domotics.Relaisboard;
import de.ctdo.crashtest.mpd.IMPDController; import de.ctdo.crashtest.mpd.IMPDController;
import de.ctdo.crashtest.mpd.MPDController; import de.ctdo.crashtest.mpd.MPDController;
@ -11,8 +13,29 @@ public class TestClass {
public static void main(String args[]) throws InterruptedException { public static void main(String args[]) throws InterruptedException {
IRelaisboard board = new Relaisboard("/dev/ttyUSB0");
board.open();
/*
board.setRelais(1,false);
//board.setRelais(1, true);
board.toggleRelais(1, 500);
Thread.sleep(1000);
board.toggleRelais(1, 500);
Thread.sleep(1000);
board.toggleRelais(1, 500);
//board.setRelais(1, false);
Thread.sleep(4000); */
board.blinkRelais(2, 500, 6); // hint Button
Thread.sleep(6000);
board.close();
} }

View File

@ -4,7 +4,7 @@ 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);
boolean open(); boolean open();
void close(); void close();

View File

@ -11,38 +11,34 @@ public class Relaisboard implements IRelaisboard {
private OutputStream outputStream; private OutputStream outputStream;
private Boolean serialPortGeoeffnet = false; private Boolean serialPortGeoeffnet = false;
private String portName = "/dev/ttyUSB0"; private String portName = "/dev/ttyUSB0";
private Object mLock = new Object();
public Relaisboard(final String port) { public Relaisboard(final String port) {
this.portName = port; this.portName = port;
} }
private synchronized void send(final char ch) {
if (!serialPortGeoeffnet) return;
try {
outputStream.write(ch);
} catch (IOException e) {
Logger.sLog("Fehler beim Senden");
}
}
private void sendData(final int relais, final boolean state) { private void sendData(final int relais, final boolean state) {
if(relais >= 0 && relais < 8) { if(relais >= 0 && relais < 8 && outputStream != null) {
char charsOff[] = { 'a','b','c','d','e','f','g','h' }; char charsOff[] = { 'a','b','c','d','e','f','g','h' };
char charsOn[] = { 'A','B','C','D','E','F','G','H' }; char charsOn[] = { 'A','B','C','D','E','F','G','H' };
if(state) { try {
send(charsOn[relais]); if(state) {
} else { outputStream.write(charsOn[relais]);
send(charsOff[relais]); } else {
outputStream.write(charsOff[relais]);
}
outputStream.flush();
} catch (IOException e) {
Logger.sLog("Fehler beim Senden");
} }
} }
} }
@Override @Override
public boolean open() public boolean open() {
{
serialPortGeoeffnet = false; serialPortGeoeffnet = false;
Boolean foundPort = false; Boolean foundPort = false;
@ -100,33 +96,73 @@ public class Relaisboard implements IRelaisboard {
public void setRelais(final int relais, final boolean state) { public void setRelais(final int relais, final boolean state) {
if(!serialPortGeoeffnet) return; if(!serialPortGeoeffnet) return;
Runnable r = new Runnable() { synchronized (mLock) {
@Override Runnable r = new Runnable() {
public void run() { @Override
sendData(relais, state); public void run() {
} sendData(relais, state);
}; }
};
new Thread(r).start(); new Thread(r).start();
}
} }
@Override @Override
public void toggleRelais(final int relais, final int milliseconds) { public void toggleRelais(final int relais, final int milliseconds) {
if(!serialPortGeoeffnet) return; if(!serialPortGeoeffnet) return;
Runnable r = new Runnable() { synchronized (mLock) {
@Override Runnable r = new Runnable() {
public void run() { @Override
sendData(relais, true); public void run() {
sendData(relais, true);
try { try {
Thread.sleep(milliseconds); Thread.sleep(milliseconds);
} catch (InterruptedException e) { } } catch (InterruptedException e) {
e.printStackTrace();
}
sendData(relais, false); sendData(relais, false);
} }
}; };
new Thread(r).start(); new Thread(r).start();
}
}
@Override
public void blinkRelais(final int relais, final int pause, final int count) {
if(!serialPortGeoeffnet) return;
synchronized (mLock) {
Runnable r = new Runnable() {
@Override
public void run() {
int i;
for(i = 0; i< count; i++) {
sendData(relais, true);
try {
Thread.sleep(pause);
} catch (InterruptedException e) {
e.printStackTrace();
}
sendData(relais, false);
try {
Thread.sleep(pause);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
new Thread(r).start();
}
} }
} }

View File

@ -12,6 +12,7 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
private IStatemachine machine; private IStatemachine machine;
private IBuntiClient bunti; private IBuntiClient bunti;
private IMPDController mpdController; private IMPDController mpdController;
private IRelaisboard relaisboard;
public TheGame(IGuiControl guiControl) { public TheGame(IGuiControl guiControl) {
this.guiControl = guiControl; this.guiControl = guiControl;
@ -19,7 +20,8 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
this.ircClient = new IrcClient("crashtest", "#crashtest","irc.hackint.eu"); this.ircClient = new IrcClient("crashtest", "#crashtest","irc.hackint.eu");
this.bunti = new BuntiClient("bunti.ctdo.de", 8080); this.bunti = new BuntiClient("bunti.ctdo.de", 8080);
this.mpdController = new MPDController("dampfradio.raum.ctdo.de"); this.mpdController = new MPDController("dampfradio.raum.ctdo.de");
this.machine = new Statemachine(); this.relaisboard = new Relaisboard("/dev/ttyUSB0");
this.machine = new Statemachine();
initGame(); initGame();
} }
@ -30,6 +32,8 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
machine.addListener(this); machine.addListener(this);
machine.reset(); machine.reset();
relaisboard.open();
relaisboard.toggleRelais(2, 2000);
} }
@ -45,11 +49,12 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
switch (newState) { switch (newState) {
case IDLE: case IDLE:
machine.stopTimer(); machine.stopTimer();
guiControl.setExtra("");
guiControl.setWall("");
mpdController.setVolume(45); mpdController.setVolume(45);
mpdController.playSong("start", "mix"); mpdController.playSong("start", "mix");
bunti.setPar56(0,0,0); bunti.setPar56(0,0,0);
bunti.setLampel(false,false,false); bunti.setLampel(false,false,false);
@ -68,6 +73,8 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
machine.startTimer(60*8); machine.startTimer(60*8);
break; break;
case TABLE_GAME_ONE: case TABLE_GAME_ONE:
guiControl.setWall("64K RAM SYSTEM 38911 BASIC BYTES FREE. **** COMMODORE 64 BASIC V2 ****");
mpdController.setVolume(70); mpdController.setVolume(70);
mpdController.playSong("K2", "Der Berg Ruft"); mpdController.playSong("K2", "Der Berg Ruft");
@ -120,6 +127,9 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
guiControl.showCountDown(true); guiControl.showCountDown(true);
machine.pauseTimer(true); machine.pauseTimer(true);
sayScore(); sayScore();
relaisboard.blinkRelais(2, 500, 6); // hint Button
break; break;
case ROKET_STARTED: case ROKET_STARTED:
mpdController.setVolume(50); mpdController.setVolume(50);
@ -131,6 +141,8 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
ircClient.say("table game complete, r0kets now"); ircClient.say("table game complete, r0kets now");
relaisboard.toggleRelais(0, 300);
guiControl.showCountDown(true); guiControl.showCountDown(true);
machine.startTimer(7*60); machine.startTimer(7*60);
break; break;
@ -163,7 +175,7 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
@Override @Override
public void keyPress(char key) { public void keyPress(char key) {
machine.handleInput(key); machine.handleInput(key);
guiControl.setExtra("btn: " + key); //guiControl.setExtra("btn: " + key);
} }
@ -175,6 +187,7 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
machine.reset(); machine.reset();
bunti.setPar56(0xff,0xff,0xff); bunti.setPar56(0xff,0xff,0xff);
ircClient.say("bye"); ircClient.say("bye");
relaisboard.close();
} }
/** /**