diff --git a/UML.dia b/UML.dia index 959f68d670150cf6fb994762b216cd9ed1139a3d..ce92ab334b33e615a1532707c84125b71d872af4 100644 Binary files a/UML.dia and b/UML.dia differ diff --git a/UML.dia~ b/UML.dia~ index 7a7dafea856b70c28090bba445fc49e1cbfc01b9..959f68d670150cf6fb994762b216cd9ed1139a3d 100644 Binary files a/UML.dia~ and b/UML.dia~ differ diff --git a/res/Measure.png b/res/Measure.png new file mode 100644 index 0000000000000000000000000000000000000000..84554c2d55b678e9a21029c6691e6202b0275c35 Binary files /dev/null and b/res/Measure.png differ diff --git a/src/tec/letsgoing/ardublock/simulator/Simulator.java b/src/tec/letsgoing/ardublock/simulator/Simulator.java index f70b425242c02cb96c379f4efb7d33e3b357f380..1590f29136860862ba219a2e254cd85ab6c1793f 100644 --- a/src/tec/letsgoing/ardublock/simulator/Simulator.java +++ b/src/tec/letsgoing/ardublock/simulator/Simulator.java @@ -3,6 +3,8 @@ */ package tec.letsgoing.ardublock.simulator; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.util.Vector; import tec.letsgoing.ardublock.simulator.arduino.Arduino; @@ -10,13 +12,14 @@ import tec.letsgoing.ardublock.simulator.arduino.Pin; import tec.letsgoing.ardublock.simulator.simcode.SimCode; import tec.letsgoing.ardublock.simulator.simcode.blocks.CodeDelay; 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.modules.RGB; /** * @author Lucas Hallo Test */ -public class Simulator implements Runnable { +public class Simulator implements Runnable, ActionListener { private static Simulator instance; private Arduino arduino; private GUI gui; @@ -30,7 +33,6 @@ public class Simulator implements Runnable { private volatile boolean stopFlag = false; private Simulator() { - System.out.println("Konstruktor"); createSubClasses(); } @@ -43,20 +45,32 @@ public class Simulator implements Runnable { } private boolean createSubClasses() { - gui = new GUI(); + gui = new GUI(this); arduino = new Arduino(gui); gui.connectPins(arduino); guiThread = new Thread(gui); guiThread.start(); + return true; } public void startSimu() { - stopFlag = false; - simuThread = new Thread(this); - simuThread.start(); + if(simuThread instanceof Thread ) { + if (!simuThread.isAlive()) { + stopFlag = false; + arduino.reset(); + simuThread = new Thread(this); + simuThread.start(); + } + } else { + stopFlag = false; + arduino.reset(); + simuThread = new Thread(this); + simuThread.start(); + } + } @Override @@ -67,7 +81,7 @@ public class Simulator implements Runnable { int maxLoop = loopCode.size(); while (!stopFlag) { - System.out.println("Thread"); + //TODO Künstlicher Slowdown? if (setupCounter < maxSetup) { setupCode.get(setupCounter).run(arduino); setupCounter++; @@ -76,6 +90,9 @@ public class Simulator implements Runnable { if (loopCounter < maxLoop) { loopCode.get(loopCounter).run(arduino); + if (loopCode.get(loopCounter) instanceof CodeDigitalWrite) { + //TODO Functiondetection + } loopCounter++; if (loopCounter == maxLoop) { loopCounter = 0; @@ -88,37 +105,40 @@ public class Simulator implements Runnable { public void stopSimu() { stopFlag = true; + simuThread.interrupt(); } // reload new code from Ardublock public boolean reload() { - stopSimu(); - try { - simuThread.join(); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + if (simuThread instanceof Thread) { + stopSimu(); + try { + simuThread.join(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } gui.stopThread(); gui.dispose(); createSubClasses(); - startSimu(); - return true; } // Function to reset the Arduino in the Simulation public boolean reset() { - stopSimu(); - try { - simuThread.join(); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + if (simuThread instanceof Thread) { + stopSimu(); + try { + simuThread.join(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } - arduino.reset(); + startSimu(); return true; @@ -138,49 +158,51 @@ public class Simulator implements Runnable { functionsCode = _functionsCode; 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 * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { - // TODO Auto-generated method stub Vector<SimCode> testSetup = 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 CodeDelay(500)); + testLoop.add(new CodeDelay(testdelay)); 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 CodeDelay(500)); + testLoop.add(new CodeDelay(testdelay)); + testLoop.add(new CodeSerialPrint("Hello World")); 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 CodeDelay(500)); + testLoop.add(new CodeDelay(testdelay)); 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(); simu.setSetupCode(testSetup); 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(); } + + } diff --git a/src/tec/letsgoing/ardublock/simulator/arduino/Arduino.java b/src/tec/letsgoing/ardublock/simulator/arduino/Arduino.java index 11200072691d41104bca069b94f763d06fa24cd2..4265bf7a2400af96abfa3c6355ac0b2ad3ac23ed 100644 --- a/src/tec/letsgoing/ardublock/simulator/arduino/Arduino.java +++ b/src/tec/letsgoing/ardublock/simulator/arduino/Arduino.java @@ -13,9 +13,10 @@ import tec.letsgoing.ardublock.simulator.view.GUI; */ public class Arduino { - private Vector<Variable> vars; + private Vector<Variable> vars=new Vector<Variable>(); private Pin[] pins = new Pin[20]; private GUI gui; + private Vector<Variable> localVars=new Vector<Variable>(); public Arduino(GUI _gui) { gui = _gui; @@ -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.lastElement().setValue(_value); + } } - public boolean setVariable(String _name, Object _value) { - for (Variable var : vars) { - if (var.getName() == _name) { - var.setValue(_value); - break; + public boolean setVariable(String _name, Object _value, boolean local) { + if (local) { + for (Variable var : localVars) { + if (var.getName() == _name) { + var.setValue(_value); + break; + } + } + + } else { + for (Variable var : vars) { + if (var.getName() == _name) { + var.setValue(_value); + break; + } } } + + return true; } - public Object readVariable(String _name) { - for (Variable var : vars) { - if (var.getName() == _name) { - return var.getValue(); + 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) { + if (var.getName() == _name) { + return var.getValue(); + } } + } + + return null; } diff --git a/src/tec/letsgoing/ardublock/simulator/simcode/SimCode.java b/src/tec/letsgoing/ardublock/simulator/simcode/SimCode.java index e60efaa0f375440dc98129af2ba6a14d00badbdb..11a4b9a16feebc7dc63007a09def21d4fa0ba3e5 100644 --- a/src/tec/letsgoing/ardublock/simulator/simcode/SimCode.java +++ b/src/tec/letsgoing/ardublock/simulator/simcode/SimCode.java @@ -16,7 +16,7 @@ public abstract class SimCode { protected Vector<Object> arguments = new Vector<Object>(); // 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() { return returnValue; diff --git a/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeDelay.java b/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeDelay.java index 72518246bb4bf1fa761a8b5dd85b4a2ecfe7a2a6..327c6df8fd935c57f5bb51101e63f2433c965ad1 100644 --- a/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeDelay.java +++ b/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeDelay.java @@ -19,13 +19,13 @@ public class CodeDelay extends SimCode { public void run(Arduino ArduinoClass) { ArduinoClass.serialPrint("Sleep for: " + duration); - // TODO Auto-generated method stub try { Thread.sleep(duration); } catch (InterruptedException e) { // TODO Auto-generated catch block - e.printStackTrace(); + //e.printStackTrace(); } + } } diff --git a/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeDigitalWrite.java b/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeDigitalWrite.java index 533906c42a47d6f2ce5e1b754d964e74b9d598a0..ca5c17629c193c25c6da75acd3e10cab2cb0e181 100644 --- a/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeDigitalWrite.java +++ b/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeDigitalWrite.java @@ -4,6 +4,7 @@ package tec.letsgoing.ardublock.simulator.simcode.blocks; import tec.letsgoing.ardublock.simulator.arduino.Arduino; +import tec.letsgoing.ardublock.simulator.arduino.Variable; import tec.letsgoing.ardublock.simulator.simcode.SimCode; /** diff --git a/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeSerialPrint.java b/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeSerialPrint.java new file mode 100644 index 0000000000000000000000000000000000000000..6384aad0c3856346762f77be540724eccdc12461 --- /dev/null +++ b/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeSerialPrint.java @@ -0,0 +1,25 @@ +/** + * + */ +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); + + } + +} diff --git a/src/tec/letsgoing/ardublock/simulator/view/GUI.java b/src/tec/letsgoing/ardublock/simulator/view/GUI.java index c54b095363bb30540293e95c18d883933f8db074..d67e7e58975c5a073717eaaa4b2eed2e8059c450 100644 --- a/src/tec/letsgoing/ardublock/simulator/view/GUI.java +++ b/src/tec/letsgoing/ardublock/simulator/view/GUI.java @@ -5,11 +5,15 @@ package tec.letsgoing.ardublock.simulator.view; import java.awt.BorderLayout; import java.awt.Color; +import java.awt.Component; import java.awt.Container; +import java.awt.Dimension; import java.awt.Font; import java.util.Vector; +import javax.swing.Box; import javax.swing.BoxLayout; +import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; @@ -19,6 +23,7 @@ import javax.swing.JTextArea; import javax.swing.text.BadLocationException; import javax.swing.text.DefaultCaret; +import tec.letsgoing.ardublock.simulator.Simulator; import tec.letsgoing.ardublock.simulator.arduino.Arduino; import tec.letsgoing.ardublock.simulator.view.modules.ArduinoUno; import tec.letsgoing.ardublock.simulator.view.modules.Button; @@ -30,22 +35,21 @@ import tec.letsgoing.ardublock.simulator.view.modules.RGB; * @author Lucas * */ -//TODO Rotate Modules public class GUI extends JFrame implements Runnable { private Vector<String> serialprint = new Vector<String>(); private Modul[] modules = new Modul[4]; private volatile boolean stopFlag = false; private JTextArea serialLog = new JTextArea(); - public GUI() { - super(); + public GUI(Simulator simu) { + super("ArdubBlock Simulator"); modules[0] = new RGB(); modules[1] = new Button(); modules[2] = new Poti(); modules[3] = new ArduinoUno(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - + this.setResizable(false); Container mainPane = this.getContentPane(); JScrollPane scrollPane = new JScrollPane(serialLog); @@ -57,31 +61,54 @@ public class GUI extends JFrame implements Runnable { serialLog.setBackground(Color.black); 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()); modulPanel.add(modules[0].getPane(), BorderLayout.EAST); modulPanel.add(modules[1].getPane(), BorderLayout.CENTER); modulPanel.add(modules[2].getPane(), BorderLayout.WEST); modulPanel.add(modules[3].getPane(), BorderLayout.NORTH); - JPanel controlPanel = new JPanel(); 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(new JButton("Controls"), BorderLayout.LINE_END); + mainPane.add(controlPanel, BorderLayout.LINE_END); mainPane.add(scrollPane, BorderLayout.PAGE_END); this.pack(); this.setVisible(true); - JScrollBar vertical = scrollPane.getVerticalScrollBar(); - vertical.setValue(vertical.getMaximum()); } - @Override public void run() { while (!stopFlag) { try { diff --git a/src/tec/letsgoing/ardublock/simulator/view/modules/Button.java b/src/tec/letsgoing/ardublock/simulator/view/modules/Button.java index a20fed48b75cc5a8c2248b4f83a3272fce206062..d624fcbfda72a1c6c53a222fe108cb0819a18130 100644 --- a/src/tec/letsgoing/ardublock/simulator/view/modules/Button.java +++ b/src/tec/letsgoing/ardublock/simulator/view/modules/Button.java @@ -10,6 +10,7 @@ import java.util.Observable; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JLabel; +import javax.swing.JToggleButton; import tec.letsgoing.ardublock.simulator.arduino.Arduino; import tec.letsgoing.ardublock.simulator.arduino.Pin; @@ -27,10 +28,13 @@ public class Button extends Modul { chiplabel.setIcon(chipIcon); chiplabel.setSize(294, 294); layerpane.add(chiplabel, 1); + + JToggleButton but= new JToggleButton(); + but.isSelected(); } public void updateModul(Pin pin) { - + } public boolean connect(Arduino arduino) { diff --git a/src/tec/letsgoing/ardublock/simulator/view/modules/Modul.java b/src/tec/letsgoing/ardublock/simulator/view/modules/Modul.java index 7e7b957afb9f61c8386b6585daf4aed85cc2e258..2b46547d24d705e3528e9e7b965bc567a1a1a012 100644 --- a/src/tec/letsgoing/ardublock/simulator/view/modules/Modul.java +++ b/src/tec/letsgoing/ardublock/simulator/view/modules/Modul.java @@ -40,9 +40,7 @@ public abstract class Modul implements Observer { } public void update(Observable Observable, Object arg1) { - System.out.println("Update"); if (Observable instanceof Pin) { - System.out.println("is Pin"); for (Pin p : pins) { if (p == (Pin) Observable) updateModul((Pin) Observable); diff --git a/src/tec/letsgoing/ardublock/simulator/view/modules/RGB.java b/src/tec/letsgoing/ardublock/simulator/view/modules/RGB.java index 72a8c8e05c78cb3685eb4b6dbc65d8c9405c79e2..55fc88442838f2715e1a29dbdbab610913c2c450 100644 --- a/src/tec/letsgoing/ardublock/simulator/view/modules/RGB.java +++ b/src/tec/letsgoing/ardublock/simulator/view/modules/RGB.java @@ -40,12 +40,12 @@ public class RGB extends Modul { Graphics2D ga = (Graphics2D) g; int transparancy = (int) (Math.max(redValue, Math.max(greenValue, blueValue)) * 0.8); 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); layerpane.add(ledlabel, 0); } @@ -57,12 +57,10 @@ public class RGB extends Modul { blueValue = pin.getValue() / 4; if (pin == pins.get(2)) greenValue = pin.getValue() / 4; - System.out.println("New Values: " + redValue + " " + blueValue + " " + greenValue); } public boolean connect(Arduino arduino) { // TODO Pins= R G B ? - System.out.println("Connected RGB"); Pin tmpPin = arduino.getPin(0); this.addPin(tmpPin); tmpPin.setObserver(this);