Commit eebfb798 authored by Matthias Möller's avatar Matthias Möller

GUI Funktionalitäten fertig

parent 3c6c960b
No preview for this file type
File added
File added
......@@ -7,6 +7,7 @@ import javax.swing.JButton;
import com.emotiv.Iedk.Edk;
import com.emotiv.Iedk.EmoState;
import com.sun.jna.ptr.NativeLongByReference;
import Modell.*;
import Server.WebsocketServer2;
......@@ -18,15 +19,14 @@ public class EmotivController implements ActionListener, WindowListener{
WebsocketServer2 emotivServer;
public EmotivController() throws UnknownHostException{
EmotivInit emo = new EmotivInit(2);
EmotivInit emo = new EmotivInit(1);
AbstractEmostateUpdatedComponent guiModel = new MentalCommandComponent(emo);
guiModel = new HardwareComponent(guiModel);
guiModel = new BatteryComponent(guiModel);
EmotivThread emotivThread = new EmotivThread(guiModel);
this.emotivThread = emotivThread;
this.emotivGUI = new EmotivGUI(this, guiModel);
this.emotivServer = new WebsocketServer2(1313);
WebsocketServer2.initialize(1313);
}
@Override
......@@ -58,6 +58,18 @@ public class EmotivController implements ActionListener, WindowListener{
this.emotivThread.continueWorking();
}
}
else if(buttonName == "Trainiere MC2!"){
if(this.emotivThread.getStopped()){
Edk.INSTANCE.IEE_MentalCommandSetTrainingAction(0, EmoState.IEE_MentalCommandAction_t.MC_LIFT.ToInt());
Edk.INSTANCE.IEE_MentalCommandSetTrainingControl(0, Edk.IEE_MentalCommandTrainingControl_t.MC_START.getType());
}else{
this.emotivThread.stopWorking();
Edk.INSTANCE.IEE_MentalCommandSetTrainingAction(0, EmoState.IEE_MentalCommandAction_t.MC_PULL.ToInt());
Edk.INSTANCE.IEE_MentalCommandSetTrainingControl(0, Edk.IEE_MentalCommandTrainingControl_t.MC_START.getType());
this.emotivThread.continueWorking();
}
}
else if(buttonName == "Halte an!"){
this.emotivThread.stopWorking();
((JButton)e.getSource()).setText("Mach weiter!");
......@@ -68,10 +80,17 @@ public class EmotivController implements ActionListener, WindowListener{
this.emotivThread.continueWorking();
((JButton)e.getSource()).setText("Halte an!");
}else if(buttonName == "Verbindung!"){
this.emotivServer.start();
System.out.println( "WebsocketServer2 started on port: " + this.emotivServer.getPort() );
}else if(buttonName == "Verbindung"){
this.emotivThread.stopWorking();
this.emotivThread.activateWebsocketConnection();
Edk.INSTANCE.IEE_EngineClearEventQueue(0x0040);
this.emotivThread.continueWorking();
}else if(buttonName == "Training"){
this.emotivThread.stopWorking();
this.emotivThread.deactivateWebsocketConnection();
Edk.INSTANCE.IEE_EngineClearEventQueue(0x0040);
this.emotivThread.continueWorking();
}
((JButton)e.getSource()).setEnabled(true);
}
......
......@@ -2,7 +2,7 @@ package Controller;
import java.util.concurrent.Semaphore;
import com.emotiv.Iedk.Edk;
import Modell.AbstractEmotiv;
......@@ -20,8 +20,9 @@ public class EmotivThread extends Thread {
public void run(){
this.emo.initialize();
try{
try {
this.sleep(20);
System.out.println(Edk.INSTANCE.IEE_MentalCommandSetActiveActions(0, 6));
//runs all the time
while(true){
isAllowedToWork.acquire();
......@@ -65,5 +66,13 @@ public class EmotivThread extends Thread {
public boolean getStopped(){
return this.stopped;
}
public void activateWebsocketConnection() {
this.emo.activateWebsocketConnection();
}
public void deactivateWebsocketConnection() {
this.emo.deactivateWebsocketConnection();
}
}
......@@ -79,4 +79,12 @@ public abstract class AbstractEmostateUpdatedComponent extends AbstractEmotiv {
public String getBattery(){
return this.nextEmotivComponent.getBattery();
}
public void activateWebsocketConnection() {
this.nextEmotivComponent.activateWebsocketConnection();
}
public void deactivateWebsocketConnection() {
this.nextEmotivComponent.deactivateWebsocketConnection();
}
}
......@@ -51,4 +51,8 @@ public abstract class AbstractEmotiv {
public abstract String getBattery();
public abstract void activateWebsocketConnection();
public abstract void deactivateWebsocketConnection();
}
......@@ -79,5 +79,13 @@ public abstract class AbstractWebSocketComponent extends AbstractEmotiv {
public String getBattery(){
return this.nextEmotivComponent.getBattery();
}
public void activateWebsocketConnection() {
this.nextEmotivComponent.activateWebsocketConnection();
}
public void deactivateWebsocketConnection() {
this.nextEmotivComponent.deactivateWebsocketConnection();
}
}
......@@ -30,6 +30,7 @@ public class EmotivInit extends AbstractEmotiv {
public EmotivInit(int option){
this.option= option;
this.mentalCommandEventInit = new MentalCommandEventInit(this);
this.webSocketLeader = new MentalCommandWebSocket(this);
}
......@@ -45,6 +46,8 @@ public class EmotivInit extends AbstractEmotiv {
System.out.println("Emotiv Engine start up suceeded.");
this.eEvent = Edk.INSTANCE.IEE_EmoEngineEventCreate();
this.eState = Edk.INSTANCE.IEE_EmoStateCreate();
// VERY IMPORTANT!
// Activates the two MentalCommands with
break;
}
case 2: {
......@@ -161,6 +164,14 @@ public class EmotivInit extends AbstractEmotiv {
Edk.INSTANCE.IEE_EmoStateFree(eState);
}
public void activateWebsocketConnection() {
this.isGUIActive = false;
}
public void deactivateWebsocketConnection() {
this.isGUIActive = true;
}
@Override
......
......@@ -3,9 +3,12 @@ package Modell;
import com.emotiv.Iedk.Edk;
import com.emotiv.Iedk.EmoState;
import Server.WebsocketServer2;
public class MentalCommandWebSocket extends AbstractWebSocketComponent{
public int lastMentalCommand;
public double lastMentalCommandStrength;
public int actualMentalCommand;
public int iteratedTriggeredMentalCommand = 0;
public MentalCommandWebSocket(AbstractEmotiv emo) {
super(emo);
......@@ -40,17 +43,24 @@ public class MentalCommandWebSocket extends AbstractWebSocketComponent{
@Override
public void actualizeComponents() {
int newMentalCommand = EmoState.INSTANCE.IS_MentalCommandGetCurrentAction(getEmoState());
float newMentalCommandStrength = EmoState.INSTANCE.IS_MentalCommandGetCurrentActionPower(getEmoState());
int lastMentalCommand = EmoState.INSTANCE.IS_MentalCommandGetCurrentAction(getEmoState());
float lastMentalCommandStrength = EmoState.INSTANCE.IS_MentalCommandGetCurrentActionPower(getEmoState());
if(lastMentalCommand != this.lastMentalCommand || lastMentalCommandStrength != this.lastMentalCommandStrength){
this.lastMentalCommand = lastMentalCommand;
this.lastMentalCommandStrength = lastMentalCommandStrength;
//update
if(newMentalCommand != this.actualMentalCommand){
this.actualMentalCommand = newMentalCommand;
this.iteratedTriggeredMentalCommand = 0;
}
if(newMentalCommandStrength > 0.6) {
this.iteratedTriggeredMentalCommand +=1;
}
if(this.iteratedTriggeredMentalCommand > 5){
WebsocketServer2.sendMentalCommand(this.actualMentalCommand);
}
this.nextEmotivComponent.actualizeComponents();
System.out.println(newMentalCommand);
}
......
......@@ -14,7 +14,17 @@ import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
public class WebsocketServer2 extends WebSocketServer{
public WebsocketServer2( int port ) throws UnknownHostException {
public static WebsocketServer2 Singleton = null;
public static void initialize(int addr) throws UnknownHostException {
if(Singleton == null) {
Singleton = new WebsocketServer2(addr);
Singleton.start();
}
}
private WebsocketServer2( int port ) throws UnknownHostException {
super( new InetSocketAddress( port ) );
}
......@@ -24,8 +34,6 @@ public class WebsocketServer2 extends WebSocketServer{
@Override
public void onOpen( WebSocket conn, ClientHandshake handshake ) {
this.sendMentalCommand(2, 0.3);
System.out.println( conn.getRemoteSocketAddress().getAddress().getHostAddress() + " entered the room!" );
}
@Override
......@@ -43,27 +51,6 @@ public class WebsocketServer2 extends WebSocketServer{
System.out.println( "received fragment: " + fragment );
}
/**public static void main( String[] args ) throws InterruptedException , IOException {
WebSocketImpl.DEBUG = true;
int port = 1313; // 843 flash policy port
try {
port = Integer.parseInt( args[ 0 ] );
} catch ( Exception ex ) {
}
WebsocketServer2 s = new WebsocketServer2( port );
s.start();
System.out.println( "WebsocketServer2 started on port: " + s.getPort() );
BufferedReader sysin = new BufferedReader( new InputStreamReader( System.in ) );
while ( true ) {
String in = sysin.readLine();
s.sendToAll( in );
if( in.equals( "exit" ) ) {
s.stop();
break;
}
}
}**/
@Override
public void onError( WebSocket conn, Exception ex ) {
ex.printStackTrace();
......@@ -78,12 +65,11 @@ public class WebsocketServer2 extends WebSocketServer{
}
public void sendMentalCommand(int mentalCommand, double strength) {
Collection<WebSocket> sockets = connections();
public static void sendMentalCommand(int mentalCommand) {
Collection<WebSocket> sockets = Singleton.connections();
for(WebSocket socket : sockets) {
String message = "{";
message = message+ " \"mentalCommand\" : " + Integer.toString(mentalCommand) + " , ";
message = message + "\"strength\" : " + Double.toString(strength);
message = message+ " \"mentalCommand\" : " + Integer.toString(mentalCommand);
message = message + "}";
socket.send(message);
}
......
package View;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.BorderLayout;
import javax.swing.*;
import javax.swing.event.MenuEvent;
......@@ -21,16 +25,34 @@ public class EmotivGUI extends JFrame implements ActionListener{
private String emotivEventType = "";
private String emotivMentalCommandType = "";
private JLabel batteryLabel = new JLabel("");
private JLabel timeLabel = new JLabel();
private JLabel timeLabel = new JLabel("");
private JPanel contentPanel;
public EmotivGUI(EmotivController emotivController, AbstractEmotiv modell){
this.emotivController = emotivController;
this.modell = modell;
login();
this.addWindowListener(this.emotivController);
this.setTitle("EmotivTest");
this.setSize(800, 400);
this.setTitle("ExplorViz BCI Plugin");
this.setVisible(true);
this.createMenuBar();
this.createMenuPanel();
this.setLayout(new FlowLayout());
this.contentPanel = new JPanel(new FlowLayout(0,10,10));
this.contentPanel.setPreferredSize(new Dimension(500, 110));
this.add(contentPanel);
this.createTrainingContent();
this.setSize(500, 220);
}
//Layout-Funktionen
private void createMenuBar() {
JMenuBar jMenuBar = new JMenuBar();
jMenuBar.setSize(this.getWidth(), 20);
......@@ -59,33 +81,78 @@ public class EmotivGUI extends JFrame implements ActionListener{
jMenuBar.add(jMenu2);
this.setJMenuBar(jMenuBar);
}
private void createTrainingContent() {
this.contentPanel.removeAll();
this.createTrainingControlPanel();
this.createTrainingWorkPanel();
}
private void createMenuPanel() {
JPanel jPanel = new JPanel();
JButton jButton = new JButton("Training");
jButton.addActionListener(this);
jButton.addActionListener(this.emotivController);
jPanel.add(jButton);
JButton jButton2 = new JButton("Verbindung");
jButton2.addActionListener(this.emotivController);
jButton2.addActionListener(this);
jPanel.add(jButton2);
jPanel.setPreferredSize(new Dimension(500, 35));
this.add(jPanel);
}
private void createTrainingControlPanel() {
Dimension buttonSize = new Dimension(140, 25);
JPanel jPanel2 = new JPanel();
jPanel2.setLayout(new FlowLayout(0, 0, 0));
JButton jButton = new JButton("Trainiere Normal!");
JButton jButton2 = new JButton("Halte an!");
JButton jButton3 = new JButton("Trainiere MC1!");
JButton jButton4 = new JButton("Verbindung!");
JButton jButton2 = new JButton("Trainiere MC1!");
JButton jButton3 = new JButton("Trainiere MC2!");
jButton.setMultiClickThreshhold(40);
jButton2.setMultiClickThreshhold(40);
jButton3.setMultiClickThreshhold(40);
jButton4.setMultiClickThreshhold(40);
jButton.addActionListener(this.emotivController);
jButton2.addActionListener(this.emotivController);
jButton3.addActionListener(this.emotivController);
jButton4.addActionListener(this.emotivController);
jPanel2.add(this.batteryLabel);
jPanel2.add(this.timeLabel);
jButton.setPreferredSize(buttonSize);
jButton2.setPreferredSize(buttonSize);
jButton3.setPreferredSize(buttonSize);
jPanel2.add(jButton);
jPanel2.add(jButton2);
jPanel2.add(jButton3);
jPanel2.add(jButton4);
this.add(jPanel2);
jPanel2.setPreferredSize(new Dimension(140, 110));
this.contentPanel.add(jPanel2);
}
private void createTrainingWorkPanel() {
Dimension panelSize = new Dimension(340, 20);
JPanel jPanel = new JPanel(new FlowLayout(0,0,0));
jPanel.setPreferredSize(new Dimension(340, 110));
JPanel batteriePanel = new JPanel(new FlowLayout(0,0,0));
batteriePanel.add(new JLabel("Batterie: "));
batteriePanel.add(this.batteryLabel);
batteriePanel.setPreferredSize(panelSize);
jPanel.add(batteriePanel);
JPanel timePanel = new JPanel(new FlowLayout(0,0,0));
timePanel.add(new JLabel("Zeit: "));
timePanel.add(this.timeLabel);
timePanel.setPreferredSize(panelSize);
jPanel.add(timePanel);
this.contentPanel.add(jPanel);
}
private void createWebsocketConnectionPanel() {
this.contentPanel.removeAll();
this.contentPanel.updateUI();
}
......@@ -137,7 +204,7 @@ public class EmotivGUI extends JFrame implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String buttonName = ((JMenuItem)e.getSource()).getText();
String buttonName = ((AbstractButton)e.getSource()).getText();
if(buttonName == "EdkErrorCodes"){
if(this.edkErrorGUI == null){
......@@ -169,5 +236,11 @@ public class EmotivGUI extends JFrame implements ActionListener{
else if(buttonName=="Hardwareinformationen"){
}
else if(buttonName=="Training") {
this.createTrainingContent();
}
else if(buttonName == "Verbindung") {
this.createWebsocketConnectionPanel();
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment