diff --git a/res/PM24_Potentiometer.png b/res/PM24_Potentiometer.png index d09f69371f543e7228ff65f5ebe5e4d6fb37418e..fe747c12d4b5e5242468d83fb01adc6fcd3912b7 100644 Binary files a/res/PM24_Potentiometer.png and b/res/PM24_Potentiometer.png differ diff --git a/res/PM26_Taster.png b/res/PM26_Taster.png index 36962e109a506ba2e2b70d41f78f91867a596da7..0a7f44353609feae5a502f76979dcab412a85b44 100644 Binary files a/res/PM26_Taster.png and b/res/PM26_Taster.png differ diff --git a/res/PM31_RGB_LED.png b/res/PM31_RGB_LED.png index 0aa86674a3d1afcd883831c143bb7606216afbf0..0409072cf000a451ba0c6f041e709dba326dab78 100644 Binary files a/res/PM31_RGB_LED.png and b/res/PM31_RGB_LED.png differ diff --git a/res/Stop.png b/res/Stop.png index dd598a8425071661b69bd856756d086665efb0be..5761184e9372b22cc71545120d4533f17b0b1f49 100644 Binary files a/res/Stop.png and b/res/Stop.png differ diff --git a/src/tec/letsgoing/ardublock/simulator/Simulator.java b/src/tec/letsgoing/ardublock/simulator/Simulator.java index df47c80088bc1eb660e31f672f8d83d79fc0aac9..885ec4914dda459b2f89e54a4f4aa94c05748670 100644 --- a/src/tec/letsgoing/ardublock/simulator/Simulator.java +++ b/src/tec/letsgoing/ardublock/simulator/Simulator.java @@ -51,13 +51,12 @@ public class Simulator implements Runnable, ActionListener { guiThread = new Thread(gui); guiThread.start(); - return true; } public void startSimu() { - if(simuThread instanceof Thread ) { + if (simuThread instanceof Thread) { if (!simuThread.isAlive()) { stopFlag = false; arduino.reset(); @@ -70,7 +69,7 @@ public class Simulator implements Runnable, ActionListener { simuThread = new Thread(this); simuThread.start(); } - + } @Override @@ -79,6 +78,7 @@ public class Simulator implements Runnable, ActionListener { int loopCounter = 0; int maxSetup = setupCode.size(); int maxLoop = loopCode.size(); + arduino.digitalWrite(20, true); // Power ON LED while (!stopFlag) { if (setupCounter < maxSetup) { @@ -90,7 +90,7 @@ public class Simulator implements Runnable, ActionListener { if (loopCounter < maxLoop) { loopCode.get(loopCounter).run(arduino); if (loopCode.get(loopCounter) instanceof CodeDigitalWrite) { - //TODO Functiondetection + // TODO Functiondetection } loopCounter++; if (loopCounter == maxLoop) { @@ -104,7 +104,11 @@ public class Simulator implements Runnable, ActionListener { public void stopSimu() { stopFlag = true; - simuThread.interrupt(); + if (simuThread instanceof Thread) { + simuThread.interrupt(); + } + arduino.digitalWrite(20, false); + } // reload new code from Ardublock @@ -137,7 +141,7 @@ public class Simulator implements Runnable, ActionListener { e.printStackTrace(); } } - + startSimu(); return true; @@ -157,7 +161,7 @@ public class Simulator implements Runnable, ActionListener { functionsCode = _functionsCode; return true; } - + public void actionPerformed(ActionEvent arg0) { String command = arg0.getActionCommand(); if (command == "go") { @@ -186,12 +190,14 @@ public class Simulator implements Runnable, ActionListener { testLoop.add(new CodeDelay(testdelay)); testLoop.add(new CodeDigitalWrite(1, true)); testLoop.add(new CodeDelay(testdelay)); + testLoop.add(new CodeDigitalWrite(13, true)); testLoop.add(new CodeDigitalWrite(2, true)); testLoop.add(new CodeDelay(testdelay)); testLoop.add(new CodeSerialPrint("Hello World")); testLoop.add(new CodeDigitalWrite(1, false)); testLoop.add(new CodeDelay(testdelay)); testLoop.add(new CodeDigitalWrite(0, false)); + testLoop.add(new CodeDigitalWrite(13, false)); testLoop.add(new CodeDelay(testdelay)); testLoop.add(new CodeDigitalWrite(2, false)); testLoop.add(new CodeDelay(testdelay)); @@ -202,6 +208,4 @@ public class Simulator implements Runnable, ActionListener { } - - } diff --git a/src/tec/letsgoing/ardublock/simulator/arduino/Arduino.java b/src/tec/letsgoing/ardublock/simulator/arduino/Arduino.java index 910fabc54e5519935b41208542864a5fd9641706..0328bedfafa278a39c789af5c9fbfdc2286a885f 100644 --- a/src/tec/letsgoing/ardublock/simulator/arduino/Arduino.java +++ b/src/tec/letsgoing/ardublock/simulator/arduino/Arduino.java @@ -13,14 +13,14 @@ import tec.letsgoing.ardublock.simulator.view.GUI; */ public class Arduino { - private Vector<Variable> vars=new Vector<Variable>(); - private Pin[] pins = new Pin[20]; + private Vector<Variable> vars = new Vector<Variable>(); + private Pin[] pins = new Pin[21]; private GUI gui; - private Vector<Variable> localVars=new Vector<Variable>(); + private Vector<Variable> localVars = new Vector<Variable>(); public Arduino(GUI _gui) { gui = _gui; - for (int i = 0; i < 20; i++) { + for (int i = 0; i < 21; i++) { pins[i] = new Pin(); } } @@ -30,8 +30,8 @@ public class Arduino { localVars.add(new Variable(_name, _type)); localVars.lastElement().setValue(_value); } else { - vars.add(new Variable(_name, _type)); - vars.lastElement().setValue(_value); + vars.add(new Variable(_name, _type)); + vars.lastElement().setValue(_value); } } @@ -44,7 +44,7 @@ public class Arduino { break; } } - + } else { for (Variable var : vars) { if (var.getName() == _name) { @@ -53,8 +53,7 @@ public class Arduino { } } } - - + return true; } @@ -65,17 +64,16 @@ public class Arduino { return var.getValue(); } } - + } else { for (Variable var : vars) { if (var.getName() == _name) { return var.getValue(); } } - + } - - + return null; } @@ -95,7 +93,7 @@ public class Arduino { } // TODO Check Wie der Analogpin übergeben wird - + public boolean analogWrite(int _pin, int _value) { pins[_pin].setValue(_value); return true; @@ -112,7 +110,7 @@ public class Arduino { public boolean reset() { vars = new Vector<Variable>(); - for (int i = 0; i < 20; i++) { + for (int i = 0; i < 21; i++) { pins[i].setValue(0); } return true; diff --git a/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeDelay.java b/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeDelay.java index 327c6df8fd935c57f5bb51101e63f2433c965ad1..713f1107be508ed41854aa350d2b9af4ae36807e 100644 --- a/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeDelay.java +++ b/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeDelay.java @@ -23,7 +23,7 @@ public class CodeDelay extends SimCode { 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/CodeSerialPrint.java b/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeSerialPrint.java index 6384aad0c3856346762f77be540724eccdc12461..1e63874bdec20d984d4857922f5ae9167be729da 100644 --- a/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeSerialPrint.java +++ b/src/tec/letsgoing/ardublock/simulator/simcode/blocks/CodeSerialPrint.java @@ -12,11 +12,11 @@ import tec.letsgoing.ardublock.simulator.simcode.SimCode; */ public class CodeSerialPrint extends SimCode { private String content; - + public CodeSerialPrint(String _content) { - content=_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 9f5d36bd007897acb6c727f3a230541be9d358af..b941db4660bcb22d5e5b7f0f634ab3fc167ca264 100644 --- a/src/tec/letsgoing/ardublock/simulator/view/GUI.java +++ b/src/tec/letsgoing/ardublock/simulator/view/GUI.java @@ -9,12 +9,15 @@ import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.util.Vector; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.ImageIcon; import javax.swing.JButton; +import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollBar; @@ -35,7 +38,7 @@ import tec.letsgoing.ardublock.simulator.view.modules.RGB; * @author Lucas * */ -public class GUI extends JFrame implements Runnable { +public class GUI extends JFrame implements Runnable, ActionListener { private Vector<String> serialprint = new Vector<String>(); private Modul[] modules = new Modul[4]; private volatile boolean stopFlag = false; @@ -52,78 +55,92 @@ public class GUI extends JFrame implements Runnable { this.setResizable(false); Container mainPane = this.getContentPane(); - JScrollPane scrollPane = new JScrollPane(serialLog); - DefaultCaret caret = (DefaultCaret) serialLog.getCaret(); - caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); - serialLog.setRows(8); - serialLog.setAutoscrolls(true); - serialLog.setForeground(Color.white); - serialLog.setBackground(Color.black); - serialLog.setFont(new Font("Arial", Font.BOLD, 15)); - - JPanel modulPanel = new JPanel(new BorderLayout()); - modulPanel.add(modules[0].getPane(), BorderLayout.EAST); + modulPanel.add(modules[0].getPane(), BorderLayout.WEST); modulPanel.add(modules[1].getPane(), BorderLayout.CENTER); - modulPanel.add(modules[2].getPane(), BorderLayout.WEST); - modulPanel.add(modules[3].getPane(), BorderLayout.NORTH); + modulPanel.add(modules[2].getPane(), BorderLayout.EAST); + modulPanel.add(modules[3].getPane(), BorderLayout.PAGE_END); + + mainPane.add(modulPanel, BorderLayout.CENTER); + mainPane.add(createControlPanel(simu), BorderLayout.LINE_END); + mainPane.add(createSerialLog(), BorderLayout.PAGE_END); + this.pack(); + this.setVisible(true); + + } + + public void run() { + while (!stopFlag) { + try { + Thread.sleep(7); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + super.repaint(); + } + } + + private JPanel createControlPanel(Simulator simu) { JPanel controlPanel = new JPanel(); - //TODO Rotate Modules over Arduino - //TODO RGB und Poti Tausch controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS)); - - JButton goButton=new JButton(); - JButton stopButton=new JButton(); - JButton resetButton=new JButton(); - JButton reloadButton=new JButton(); - JButton measButton=new JButton(); - + + JButton goButton = new JButton(); + JButton stopButton = 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(Box.createRigidArea(new Dimension(0, 450))); controlPanel.add(reloadButton); controlPanel.add(measButton); + return controlPanel; + } - mainPane.add(modulPanel, BorderLayout.CENTER); - mainPane.add(controlPanel, BorderLayout.LINE_END); - mainPane.add(scrollPane, BorderLayout.PAGE_END); - this.pack(); - this.setVisible(true); + private JPanel createSerialLog() { + JPanel panel = new JPanel(new BorderLayout()); + JScrollPane scrollPane = new JScrollPane(serialLog); + serialLog.setRows(8); + serialLog.setAutoscrolls(true); + serialLog.setForeground(Color.black); + serialLog.setBackground(Color.white); + serialLog.setFont(new Font("Arial", Font.BOLD, 15)); + DefaultCaret caret = (DefaultCaret) serialLog.getCaret(); + caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); - } + JButton clearButton = new JButton("Clear Serial Output"); + clearButton.setActionCommand("clearSerial"); + clearButton.addActionListener(this); + panel.add(clearButton, BorderLayout.CENTER); - public void run() { - while (!stopFlag) { - try { - Thread.sleep(7); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - super.repaint(); - } + JCheckBox checkBox = new JCheckBox("Autoscroll"); + checkBox.setActionCommand("autoscroll"); + checkBox.addActionListener(this); + checkBox.setSelected(true); + panel.add(checkBox, BorderLayout.EAST); + + panel.add(scrollPane, BorderLayout.PAGE_END); + return panel; } - public boolean serialPrint(String content) { + public boolean serialPrint(String content) { // TODO Wenn Bool die New Line übergeben wird, kann ich sagen 200 + // Prints und egal ob Lines oder nicht serialprint.add(content); if (serialprint.size() > 100) { serialprint.remove(0); @@ -149,4 +166,20 @@ public class GUI extends JFrame implements Runnable { stopFlag = true; } + public void actionPerformed(ActionEvent arg0) { + if (arg0.getActionCommand() == "clearSerial") { + serialprint.clear(); + serialLog.setText(""); + } else if (arg0.getActionCommand() == "autoscroll") { + if (((JCheckBox) arg0.getSource()).isSelected()) { + DefaultCaret caret = (DefaultCaret) serialLog.getCaret(); + caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); + } else { + DefaultCaret caret = (DefaultCaret) serialLog.getCaret(); + caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE); + } + } + + } + } diff --git a/src/tec/letsgoing/ardublock/simulator/view/modules/ArduinoUno.java b/src/tec/letsgoing/ardublock/simulator/view/modules/ArduinoUno.java index 2e3a8726c8a269194b06a0e4362d4a8eb315e692..960430d74bdc638c79ca25f011d98677b60e21e9 100644 --- a/src/tec/letsgoing/ardublock/simulator/view/modules/ArduinoUno.java +++ b/src/tec/letsgoing/ardublock/simulator/view/modules/ArduinoUno.java @@ -23,6 +23,8 @@ import tec.letsgoing.ardublock.simulator.arduino.Pin; */ public class ArduinoUno extends Modul { Arduino arduino; + private boolean ledOn; + private int led13 = 0; public ArduinoUno() { layerpane.setPreferredSize(new Dimension(587, 418)); @@ -31,15 +33,58 @@ public class ArduinoUno extends Modul { chipIcon = new ImageIcon(chipIcon.getImage().getScaledInstance(587, 418, Image.SCALE_SMOOTH)); chiplabel.setIcon(chipIcon); chiplabel.setSize(587, 418); - layerpane.add(chiplabel, 1); + layerpane.add(chiplabel, 2); + + JLabel labelPower = new JLabel() { + @Override + public void paintComponent(Graphics g) { + Graphics2D ga = (Graphics2D) g; + int transparancy = 0; + if (ledOn) + transparancy = 220; + ga.setPaint(new Color(255, 255, 0, transparancy)); + ga.fillRect(0, 0, 20, 10); + } + + }; + + labelPower.setLocation(504, 126); + labelPower.setSize(20, 20); + layerpane.add(labelPower, 0); + + JLabel label13 = new JLabel() { + @Override + public void paintComponent(Graphics g) { + Graphics2D ga = (Graphics2D) g; + ga.setPaint(new Color(255, 255, 0, led13 / 4)); + ga.fillRect(0, 0, 20, 10); + } + + }; + + label13.setLocation(257, 89); + label13.setSize(20, 20); + layerpane.add(label13, 1); // TODO Strange different Order doesnt work + } - public void updateModul(Pin arg0) { + public void updateModul(Pin pin) { + if (pin == pins.get(0)) + ledOn = (pin.getValue() == 1023); + if (pin == pins.get(1)) { + led13 = pin.getValue(); + } } public boolean connect(Arduino _arduino) { + Pin tmpPin = _arduino.getPin(20); + this.addPin(tmpPin); + tmpPin.setObserver(this); + tmpPin = _arduino.getPin(13); + this.addPin(tmpPin); + tmpPin.setObserver(this); return true; } diff --git a/src/tec/letsgoing/ardublock/simulator/view/modules/Button.java b/src/tec/letsgoing/ardublock/simulator/view/modules/Button.java index d624fcbfda72a1c6c53a222fe108cb0819a18130..9b1316437a7e065337865221579cc066425b5a89 100644 --- a/src/tec/letsgoing/ardublock/simulator/view/modules/Button.java +++ b/src/tec/letsgoing/ardublock/simulator/view/modules/Button.java @@ -5,11 +5,15 @@ package tec.letsgoing.ardublock.simulator.view.modules; import java.awt.Dimension; import java.awt.Image; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.util.Observable; import javax.swing.ImageIcon; import javax.swing.JButton; +import javax.swing.JCheckBox; import javax.swing.JLabel; +import javax.swing.JPanel; import javax.swing.JToggleButton; import tec.letsgoing.ardublock.simulator.arduino.Arduino; @@ -19,7 +23,7 @@ import tec.letsgoing.ardublock.simulator.arduino.Pin; * @author Lucas * */ -public class Button extends Modul { +public class Button extends Modul implements ActionListener { public Button() { layerpane.setPreferredSize(new Dimension(294, 294)); @@ -27,19 +31,78 @@ public class Button extends Modul { ImageIcon chipIcon = new ImageIcon("res/PM26_Taster.png"); chiplabel.setIcon(chipIcon); chiplabel.setSize(294, 294); - layerpane.add(chiplabel, 1); - - JToggleButton but= new JToggleButton(); - but.isSelected(); + layerpane.add(chiplabel, 0); + + JToggleButton but1 = new JToggleButton(); + but1.setSize(70, 70); + but1.setOpaque(true); + but1.setContentAreaFilled(true); + but1.setBorderPainted(true); + but1.setActionCommand("0"); + but1.addActionListener(this); + JLabel label1 = new JLabel(); + label1.add(but1); + label1.setSize(70, 70); + label1.setLocation(29, 64); + + JToggleButton but2 = new JToggleButton(); + but2.setSize(70, 70); + but2.setOpaque(true); + but2.setContentAreaFilled(true); + but2.setBorderPainted(true); + but2.setActionCommand("1"); + but2.addActionListener(this); + JLabel label2 = new JLabel(); + label2.add(but2); + label2.setSize(70, 70); + label2.setLocation(111, 33); + + JToggleButton but3 = new JToggleButton(); + but3.setSize(70, 70); + but3.setOpaque(true); + but3.setContentAreaFilled(true); + but3.setBorderPainted(true); + but3.setActionCommand("2"); + but3.addActionListener(this); + JLabel label3 = new JLabel(); + label3.add(but3); + label3.setSize(70, 70); + label3.setLocation(195, 64); + + layerpane.add(label1, 0); + layerpane.add(label3, 0); + layerpane.add(label2, 0); + } public void updateModul(Pin pin) { - + // TODO Check of the Value Changed since last time and force update -> Save Last + // Value } public boolean connect(Arduino arduino) { + Pin tmpPin = arduino.getPin(11); + this.addPin(tmpPin); + tmpPin.setObserver(this); + + tmpPin = arduino.getPin(12); + this.addPin(tmpPin); + tmpPin.setObserver(this); + + tmpPin = arduino.getPin(13); + this.addPin(tmpPin); + tmpPin.setObserver(this); return true; } + public void actionPerformed(ActionEvent arg0) { + Integer pin = Integer.parseInt(arg0.getActionCommand()); + boolean bool = ((JToggleButton) arg0.getSource()).isSelected(); + int value = 0; + if (bool) + value = 1023; + pins.get(pin).setValue(value); + } + } diff --git a/src/tec/letsgoing/ardublock/simulator/view/modules/RGB.java b/src/tec/letsgoing/ardublock/simulator/view/modules/RGB.java index b69b55c3db52d7d9d7da8b0a3524a430af20c5b5..9574a229f0f998f91dee623383e4e229a8d83372 100644 --- a/src/tec/letsgoing/ardublock/simulator/view/modules/RGB.java +++ b/src/tec/letsgoing/ardublock/simulator/view/modules/RGB.java @@ -45,7 +45,7 @@ public class RGB extends Modul { }; - ledlabel.setLocation(113, 183); + ledlabel.setLocation(107, 37); ledlabel.setSize(200, 200); layerpane.add(ledlabel, 0); }