Skip to content
Snippets Groups Projects
Commit e83e095e authored by Lucas Stratmann's avatar Lucas Stratmann
Browse files

GUI Prototyp finished, Mainframe stable

parent 94f5523e
Branches
No related tags found
No related merge requests found
Showing
with 185 additions and 80 deletions
No preview for this file type
No preview for this file type
res/Measure.png

1.58 KiB

...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
*/ */
package tec.letsgoing.ardublock.simulator; package tec.letsgoing.ardublock.simulator;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector; import java.util.Vector;
import tec.letsgoing.ardublock.simulator.arduino.Arduino; import tec.letsgoing.ardublock.simulator.arduino.Arduino;
...@@ -10,13 +12,14 @@ import tec.letsgoing.ardublock.simulator.arduino.Pin; ...@@ -10,13 +12,14 @@ import tec.letsgoing.ardublock.simulator.arduino.Pin;
import tec.letsgoing.ardublock.simulator.simcode.SimCode; import tec.letsgoing.ardublock.simulator.simcode.SimCode;
import tec.letsgoing.ardublock.simulator.simcode.blocks.CodeDelay; import tec.letsgoing.ardublock.simulator.simcode.blocks.CodeDelay;
import tec.letsgoing.ardublock.simulator.simcode.blocks.CodeDigitalWrite; import tec.letsgoing.ardublock.simulator.simcode.blocks.CodeDigitalWrite;
import tec.letsgoing.ardublock.simulator.simcode.blocks.CodeSerialPrint;
import tec.letsgoing.ardublock.simulator.view.GUI; import tec.letsgoing.ardublock.simulator.view.GUI;
import tec.letsgoing.ardublock.simulator.view.modules.RGB; import tec.letsgoing.ardublock.simulator.view.modules.RGB;
/** /**
* @author Lucas Hallo Test * @author Lucas Hallo Test
*/ */
public class Simulator implements Runnable { public class Simulator implements Runnable, ActionListener {
private static Simulator instance; private static Simulator instance;
private Arduino arduino; private Arduino arduino;
private GUI gui; private GUI gui;
...@@ -30,7 +33,6 @@ public class Simulator implements Runnable { ...@@ -30,7 +33,6 @@ public class Simulator implements Runnable {
private volatile boolean stopFlag = false; private volatile boolean stopFlag = false;
private Simulator() { private Simulator() {
System.out.println("Konstruktor");
createSubClasses(); createSubClasses();
} }
...@@ -43,21 +45,33 @@ public class Simulator implements Runnable { ...@@ -43,21 +45,33 @@ public class Simulator implements Runnable {
} }
private boolean createSubClasses() { private boolean createSubClasses() {
gui = new GUI(); gui = new GUI(this);
arduino = new Arduino(gui); arduino = new Arduino(gui);
gui.connectPins(arduino); gui.connectPins(arduino);
guiThread = new Thread(gui); guiThread = new Thread(gui);
guiThread.start(); guiThread.start();
return true; return true;
} }
public void startSimu() { public void startSimu() {
if(simuThread instanceof Thread ) {
if (!simuThread.isAlive()) {
stopFlag = false; stopFlag = false;
arduino.reset();
simuThread = new Thread(this); simuThread = new Thread(this);
simuThread.start(); simuThread.start();
} }
} else {
stopFlag = false;
arduino.reset();
simuThread = new Thread(this);
simuThread.start();
}
}
@Override @Override
public void run() { public void run() {
...@@ -67,7 +81,7 @@ public class Simulator implements Runnable { ...@@ -67,7 +81,7 @@ public class Simulator implements Runnable {
int maxLoop = loopCode.size(); int maxLoop = loopCode.size();
while (!stopFlag) { while (!stopFlag) {
System.out.println("Thread"); //TODO Künstlicher Slowdown?
if (setupCounter < maxSetup) { if (setupCounter < maxSetup) {
setupCode.get(setupCounter).run(arduino); setupCode.get(setupCounter).run(arduino);
setupCounter++; setupCounter++;
...@@ -76,6 +90,9 @@ public class Simulator implements Runnable { ...@@ -76,6 +90,9 @@ public class Simulator implements Runnable {
if (loopCounter < maxLoop) { if (loopCounter < maxLoop) {
loopCode.get(loopCounter).run(arduino); loopCode.get(loopCounter).run(arduino);
if (loopCode.get(loopCounter) instanceof CodeDigitalWrite) {
//TODO Functiondetection
}
loopCounter++; loopCounter++;
if (loopCounter == maxLoop) { if (loopCounter == maxLoop) {
loopCounter = 0; loopCounter = 0;
...@@ -88,10 +105,12 @@ public class Simulator implements Runnable { ...@@ -88,10 +105,12 @@ public class Simulator implements Runnable {
public void stopSimu() { public void stopSimu() {
stopFlag = true; stopFlag = true;
simuThread.interrupt();
} }
// reload new code from Ardublock // reload new code from Ardublock
public boolean reload() { public boolean reload() {
if (simuThread instanceof Thread) {
stopSimu(); stopSimu();
try { try {
simuThread.join(); simuThread.join();
...@@ -99,18 +118,18 @@ public class Simulator implements Runnable { ...@@ -99,18 +118,18 @@ public class Simulator implements Runnable {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
}
gui.stopThread(); gui.stopThread();
gui.dispose(); gui.dispose();
createSubClasses(); createSubClasses();
startSimu();
return true; return true;
} }
// Function to reset the Arduino in the Simulation // Function to reset the Arduino in the Simulation
public boolean reset() { public boolean reset() {
if (simuThread instanceof Thread) {
stopSimu(); stopSimu();
try { try {
simuThread.join(); simuThread.join();
...@@ -118,7 +137,8 @@ public class Simulator implements Runnable { ...@@ -118,7 +137,8 @@ public class Simulator implements Runnable {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
arduino.reset(); }
startSimu(); startSimu();
return true; return true;
...@@ -139,48 +159,50 @@ public class Simulator implements Runnable { ...@@ -139,48 +159,50 @@ public class Simulator implements Runnable {
return true; return true;
} }
public void actionPerformed(ActionEvent arg0) {
String command = arg0.getActionCommand();
if (command == "go") {
this.startSimu();
} else if (command == "stop") { //TODO Only Softstop not Hard. -> New Button?
this.stopSimu();
} else if (command == "reset") {
this.reset();
} else if (command == "reload") {
this.reload();
} else if (command == "meas") {
}
}
/** /**
* @param args * @param args
* @throws InterruptedException * @throws InterruptedException
*/ */
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
Vector<SimCode> testSetup = new Vector<SimCode>(); Vector<SimCode> testSetup = new Vector<SimCode>();
Vector<SimCode> testLoop = new Vector<SimCode>(); Vector<SimCode> testLoop = new Vector<SimCode>();
testSetup.add(new CodeDelay(2000)); int testdelay = 500;
testSetup.add(new CodeDelay(1000));
testLoop.add(new CodeDigitalWrite(0, true)); testLoop.add(new CodeDigitalWrite(0, true));
testLoop.add(new CodeDelay(500)); testLoop.add(new CodeDelay(testdelay));
testLoop.add(new CodeDigitalWrite(1, true)); testLoop.add(new CodeDigitalWrite(1, true));
testLoop.add(new CodeDelay(500)); testLoop.add(new CodeDelay(testdelay));
testLoop.add(new CodeDigitalWrite(2, true)); testLoop.add(new CodeDigitalWrite(2, true));
testLoop.add(new CodeDelay(500)); testLoop.add(new CodeDelay(testdelay));
testLoop.add(new CodeSerialPrint("Hello World"));
testLoop.add(new CodeDigitalWrite(1, false)); testLoop.add(new CodeDigitalWrite(1, false));
testLoop.add(new CodeDelay(500)); testLoop.add(new CodeDelay(testdelay));
testLoop.add(new CodeDigitalWrite(0, false)); testLoop.add(new CodeDigitalWrite(0, false));
testLoop.add(new CodeDelay(500)); testLoop.add(new CodeDelay(testdelay));
testLoop.add(new CodeDigitalWrite(2, false)); testLoop.add(new CodeDigitalWrite(2, false));
testLoop.add(new CodeDelay(500)); testLoop.add(new CodeDelay(testdelay));
System.out.println("Main Entry");
Simulator simu = Simulator.getInstance(); Simulator simu = Simulator.getInstance();
simu.setSetupCode(testSetup); simu.setSetupCode(testSetup);
simu.setLoopCode(testLoop); simu.setLoopCode(testLoop);
Thread.sleep(5000);
simu.startSimu();
System.out.println("After Start");
Thread.sleep(5000);
System.out.println("Send Stop");
simu.reset();
System.out.println("Reset Done");
Thread.sleep(5000);
// simu.stopSimu();
// System.out.println("Stopped");
Thread.sleep(2000);
// simu.startSimu();
Thread.sleep(100000);
simu.stopSimu();
} }
} }
...@@ -13,9 +13,10 @@ import tec.letsgoing.ardublock.simulator.view.GUI; ...@@ -13,9 +13,10 @@ import tec.letsgoing.ardublock.simulator.view.GUI;
*/ */
public class Arduino { public class Arduino {
private Vector<Variable> vars; private Vector<Variable> vars=new Vector<Variable>();
private Pin[] pins = new Pin[20]; private Pin[] pins = new Pin[20];
private GUI gui; private GUI gui;
private Vector<Variable> localVars=new Vector<Variable>();
public Arduino(GUI _gui) { public Arduino(GUI _gui) {
gui = _gui; gui = _gui;
...@@ -24,28 +25,57 @@ public class Arduino { ...@@ -24,28 +25,57 @@ public class Arduino {
} }
} }
public void createVariable(String _name, int _type, Object _value) { public void createVariable(String _name, int _type, Object _value, boolean local) {
if (local) {
localVars.add(new Variable(_name, _type));
localVars.lastElement().setValue(_value);
} else {
vars.add(new Variable(_name, _type)); vars.add(new Variable(_name, _type));
vars.lastElement().setValue(_value); vars.lastElement().setValue(_value);
}
}
public boolean setVariable(String _name, Object _value, boolean local) {
if (local) {
for (Variable var : localVars) {
if (var.getName() == _name) {
var.setValue(_value);
break;
}
} }
public boolean setVariable(String _name, Object _value) { } else {
for (Variable var : vars) { for (Variable var : vars) {
if (var.getName() == _name) { if (var.getName() == _name) {
var.setValue(_value); var.setValue(_value);
break; break;
} }
} }
}
return true; return true;
} }
public Object readVariable(String _name) { public Object readVariable(String _name, boolean local) {
if (local) {
for (Variable var : localVars) {
if (var.getName() == _name) {
return var.getValue();
}
}
} else {
for (Variable var : vars) { for (Variable var : vars) {
if (var.getName() == _name) { if (var.getName() == _name) {
return var.getValue(); return var.getValue();
} }
} }
}
return null; return null;
} }
... ...
......
...@@ -16,7 +16,7 @@ public abstract class SimCode { ...@@ -16,7 +16,7 @@ public abstract class SimCode {
protected Vector<Object> arguments = new Vector<Object>(); protected Vector<Object> arguments = new Vector<Object>();
// TODO temporären Funktionsstack implementieren. // TODO temporären Funktionsstack implementieren.
public abstract void run(Arduino ArduinoClass); // TODO Wie wird man an die Functions kommen? public abstract void run(Arduino ArduinoClass);
public Object getReturn() { public Object getReturn() {
return returnValue; return returnValue;
... ...
......
...@@ -19,13 +19,13 @@ public class CodeDelay extends SimCode { ...@@ -19,13 +19,13 @@ public class CodeDelay extends SimCode {
public void run(Arduino ArduinoClass) { public void run(Arduino ArduinoClass) {
ArduinoClass.serialPrint("Sleep for: " + duration); ArduinoClass.serialPrint("Sleep for: " + duration);
// TODO Auto-generated method stub
try { try {
Thread.sleep(duration); Thread.sleep(duration);
} catch (InterruptedException e) { } catch (InterruptedException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); //e.printStackTrace();
} }
} }
} }
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
package tec.letsgoing.ardublock.simulator.simcode.blocks; package tec.letsgoing.ardublock.simulator.simcode.blocks;
import tec.letsgoing.ardublock.simulator.arduino.Arduino; import tec.letsgoing.ardublock.simulator.arduino.Arduino;
import tec.letsgoing.ardublock.simulator.arduino.Variable;
import tec.letsgoing.ardublock.simulator.simcode.SimCode; import tec.letsgoing.ardublock.simulator.simcode.SimCode;
/** /**
... ...
......
/**
*
*/
package tec.letsgoing.ardublock.simulator.simcode.blocks;
import tec.letsgoing.ardublock.simulator.arduino.Arduino;
import tec.letsgoing.ardublock.simulator.simcode.SimCode;
/**
* @author Lucas
*
*/
public class CodeSerialPrint extends SimCode {
private String content;
public CodeSerialPrint(String _content) {
content=_content;
}
public void run(Arduino ArduinoClass) {
ArduinoClass.serialPrint(content);
}
}
...@@ -5,11 +5,15 @@ package tec.letsgoing.ardublock.simulator.view; ...@@ -5,11 +5,15 @@ package tec.letsgoing.ardublock.simulator.view;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.awt.Color; import java.awt.Color;
import java.awt.Component;
import java.awt.Container; import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font; import java.awt.Font;
import java.util.Vector; import java.util.Vector;
import javax.swing.Box;
import javax.swing.BoxLayout; import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
...@@ -19,6 +23,7 @@ import javax.swing.JTextArea; ...@@ -19,6 +23,7 @@ import javax.swing.JTextArea;
import javax.swing.text.BadLocationException; import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret; import javax.swing.text.DefaultCaret;
import tec.letsgoing.ardublock.simulator.Simulator;
import tec.letsgoing.ardublock.simulator.arduino.Arduino; import tec.letsgoing.ardublock.simulator.arduino.Arduino;
import tec.letsgoing.ardublock.simulator.view.modules.ArduinoUno; import tec.letsgoing.ardublock.simulator.view.modules.ArduinoUno;
import tec.letsgoing.ardublock.simulator.view.modules.Button; import tec.letsgoing.ardublock.simulator.view.modules.Button;
...@@ -30,22 +35,21 @@ import tec.letsgoing.ardublock.simulator.view.modules.RGB; ...@@ -30,22 +35,21 @@ import tec.letsgoing.ardublock.simulator.view.modules.RGB;
* @author Lucas * @author Lucas
* *
*/ */
//TODO Rotate Modules
public class GUI extends JFrame implements Runnable { public class GUI extends JFrame implements Runnable {
private Vector<String> serialprint = new Vector<String>(); private Vector<String> serialprint = new Vector<String>();
private Modul[] modules = new Modul[4]; private Modul[] modules = new Modul[4];
private volatile boolean stopFlag = false; private volatile boolean stopFlag = false;
private JTextArea serialLog = new JTextArea(); private JTextArea serialLog = new JTextArea();
public GUI() { public GUI(Simulator simu) {
super(); super("ArdubBlock Simulator");
modules[0] = new RGB(); modules[0] = new RGB();
modules[1] = new Button(); modules[1] = new Button();
modules[2] = new Poti(); modules[2] = new Poti();
modules[3] = new ArduinoUno(); modules[3] = new ArduinoUno();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
Container mainPane = this.getContentPane(); Container mainPane = this.getContentPane();
JScrollPane scrollPane = new JScrollPane(serialLog); JScrollPane scrollPane = new JScrollPane(serialLog);
...@@ -57,31 +61,54 @@ public class GUI extends JFrame implements Runnable { ...@@ -57,31 +61,54 @@ public class GUI extends JFrame implements Runnable {
serialLog.setBackground(Color.black); serialLog.setBackground(Color.black);
serialLog.setFont(new Font("Arial", Font.BOLD, 15)); serialLog.setFont(new Font("Arial", Font.BOLD, 15));
for (int i = 0; i < 105; i++) {
serialLog.append(Integer.toString(i) + "\n");
}
JPanel modulPanel = new JPanel(new BorderLayout()); JPanel modulPanel = new JPanel(new BorderLayout());
modulPanel.add(modules[0].getPane(), BorderLayout.EAST); modulPanel.add(modules[0].getPane(), BorderLayout.EAST);
modulPanel.add(modules[1].getPane(), BorderLayout.CENTER); modulPanel.add(modules[1].getPane(), BorderLayout.CENTER);
modulPanel.add(modules[2].getPane(), BorderLayout.WEST); modulPanel.add(modules[2].getPane(), BorderLayout.WEST);
modulPanel.add(modules[3].getPane(), BorderLayout.NORTH); modulPanel.add(modules[3].getPane(), BorderLayout.NORTH);
JPanel controlPanel = new JPanel(); JPanel controlPanel = new JPanel();
controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS)); controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
// TODO make ControlButtons
JButton goButton=new JButton();
JButton stopButton=new JButton();
JButton resetButton=new JButton();
JButton reloadButton=new JButton();
JButton measButton=new JButton();
goButton.setIcon(new ImageIcon("res/Play.png"));
stopButton.setIcon(new ImageIcon("res/Stop.png"));
resetButton.setIcon(new ImageIcon("res/Reset.png"));
reloadButton.setIcon(new ImageIcon("res/Reload.png"));
measButton.setIcon(new ImageIcon("res/Measure.png"));
goButton.addActionListener(simu);
stopButton.addActionListener(simu);
resetButton.addActionListener(simu);
reloadButton.addActionListener(simu);
measButton.addActionListener(simu);
goButton.setActionCommand("go");
stopButton.setActionCommand("stop");
resetButton.setActionCommand("reset");
reloadButton.setActionCommand("reload");
measButton.setActionCommand("meas");
controlPanel.add(goButton);
controlPanel.add(stopButton);
controlPanel.add(resetButton);
controlPanel.add(Box.createRigidArea(new Dimension(0, 400)));
controlPanel.add(reloadButton);
controlPanel.add(measButton);
mainPane.add(modulPanel, BorderLayout.CENTER); mainPane.add(modulPanel, BorderLayout.CENTER);
mainPane.add(new JButton("Controls"), BorderLayout.LINE_END); mainPane.add(controlPanel, BorderLayout.LINE_END);
mainPane.add(scrollPane, BorderLayout.PAGE_END); mainPane.add(scrollPane, BorderLayout.PAGE_END);
this.pack(); this.pack();
this.setVisible(true); this.setVisible(true);
JScrollBar vertical = scrollPane.getVerticalScrollBar();
vertical.setValue(vertical.getMaximum());
} }
@Override
public void run() { public void run() {
while (!stopFlag) { while (!stopFlag) {
try { try {
... ...
......
...@@ -10,6 +10,7 @@ import java.util.Observable; ...@@ -10,6 +10,7 @@ import java.util.Observable;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JToggleButton;
import tec.letsgoing.ardublock.simulator.arduino.Arduino; import tec.letsgoing.ardublock.simulator.arduino.Arduino;
import tec.letsgoing.ardublock.simulator.arduino.Pin; import tec.letsgoing.ardublock.simulator.arduino.Pin;
...@@ -27,6 +28,9 @@ public class Button extends Modul { ...@@ -27,6 +28,9 @@ public class Button extends Modul {
chiplabel.setIcon(chipIcon); chiplabel.setIcon(chipIcon);
chiplabel.setSize(294, 294); chiplabel.setSize(294, 294);
layerpane.add(chiplabel, 1); layerpane.add(chiplabel, 1);
JToggleButton but= new JToggleButton();
but.isSelected();
} }
public void updateModul(Pin pin) { public void updateModul(Pin pin) {
... ...
......
...@@ -40,9 +40,7 @@ public abstract class Modul implements Observer { ...@@ -40,9 +40,7 @@ public abstract class Modul implements Observer {
} }
public void update(Observable Observable, Object arg1) { public void update(Observable Observable, Object arg1) {
System.out.println("Update");
if (Observable instanceof Pin) { if (Observable instanceof Pin) {
System.out.println("is Pin");
for (Pin p : pins) { for (Pin p : pins) {
if (p == (Pin) Observable) if (p == (Pin) Observable)
updateModul((Pin) Observable); updateModul((Pin) Observable);
... ...
......
...@@ -40,12 +40,12 @@ public class RGB extends Modul { ...@@ -40,12 +40,12 @@ public class RGB extends Modul {
Graphics2D ga = (Graphics2D) g; Graphics2D ga = (Graphics2D) g;
int transparancy = (int) (Math.max(redValue, Math.max(greenValue, blueValue)) * 0.8); int transparancy = (int) (Math.max(redValue, Math.max(greenValue, blueValue)) * 0.8);
ga.setPaint(new Color(redValue, greenValue, blueValue, transparancy)); ga.setPaint(new Color(redValue, greenValue, blueValue, transparancy));
ga.fillOval(0, 0, 70, 70); ga.fillOval(0, 0, 73, 73);
} }
}; };
ledlabel.setLocation(115, 185); ledlabel.setLocation(113, 183);
ledlabel.setSize(200, 200); ledlabel.setSize(200, 200);
layerpane.add(ledlabel, 0); layerpane.add(ledlabel, 0);
} }
...@@ -57,12 +57,10 @@ public class RGB extends Modul { ...@@ -57,12 +57,10 @@ public class RGB extends Modul {
blueValue = pin.getValue() / 4; blueValue = pin.getValue() / 4;
if (pin == pins.get(2)) if (pin == pins.get(2))
greenValue = pin.getValue() / 4; greenValue = pin.getValue() / 4;
System.out.println("New Values: " + redValue + " " + blueValue + " " + greenValue);
} }
public boolean connect(Arduino arduino) { public boolean connect(Arduino arduino) {
// TODO Pins= R G B ? // TODO Pins= R G B ?
System.out.println("Connected RGB");
Pin tmpPin = arduino.getPin(0); Pin tmpPin = arduino.getPin(0);
this.addPin(tmpPin); this.addPin(tmpPin);
tmpPin.setObserver(this); tmpPin.setObserver(this);
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment