From 71e38c8f2acd0af0b5d07da51440ac91c8b3befb Mon Sep 17 00:00:00 2001
From: Lucas Stratmann <lucas.stratmann@student.reutlingen-university.de>
Date: Tue, 1 Sep 2020 17:20:49 +0200
Subject: [PATCH] Poti-module working

---
 .../ardublock/simulator/Simulator.java        | 32 +++++++++-----
 .../ardublock/simulator/view/GUI.java         |  1 +
 .../simulator/view/modules/Button.java        |  6 +--
 .../simulator/view/modules/Poti.java          | 42 ++++++++++++++++---
 .../ardublock/simulator/view/modules/RGB.java |  6 +--
 5 files changed, 65 insertions(+), 22 deletions(-)

diff --git a/src/tec/letsgoing/ardublock/simulator/Simulator.java b/src/tec/letsgoing/ardublock/simulator/Simulator.java
index 01a0623..ca867aa 100644
--- a/src/tec/letsgoing/ardublock/simulator/Simulator.java
+++ b/src/tec/letsgoing/ardublock/simulator/Simulator.java
@@ -19,6 +19,8 @@ import tec.letsgoing.ardublock.simulator.simcode.datatypes.SimTypeInt;
 import tec.letsgoing.ardublock.simulator.simcode.datatypes.SimTypeString;
 import tec.letsgoing.ardublock.simulator.simcode.functions.CodeExecuteFunction;
 import tec.letsgoing.ardublock.simulator.simcode.functions.SimCodeFunction;
+import tec.letsgoing.ardublock.simulator.simcode.io.CodeAnalogRead;
+import tec.letsgoing.ardublock.simulator.simcode.io.CodeAnalogWrite;
 import tec.letsgoing.ardublock.simulator.simcode.io.CodeDigitalWrite;
 import tec.letsgoing.ardublock.simulator.simcode.math.CodeAdd;
 import tec.letsgoing.ardublock.simulator.simcode.vars.CodeMillis;
@@ -26,6 +28,11 @@ import tec.letsgoing.ardublock.simulator.view.GUI;
 
 /**
  * @author Lucas
+ * 
+ * Pinmapping: 
+ * RGB: 9,10,11
+ * Poti: A5
+ * Button: 3,4,5
  */
 
 public class Simulator implements Runnable, ActionListener {
@@ -179,32 +186,35 @@ public class Simulator implements Runnable, ActionListener {
 	public static void main(String[] args) throws InterruptedException {
 		Vector<SimCode> testSetup = new Vector<SimCode>();
 		Vector<SimCode> testLoop = new Vector<SimCode>();
-		int testdelay = 500;
+		int testdelay = 200;
 
 		SimTypeBool b1 = new SimTypeBool(true);
 		SimTypeBool b0 = new SimTypeBool(false);
 
 		SimTypeString s1 = new SimTypeString("CodeString Test");
-		SimTypeInt d1 = new SimTypeInt(5);
-		SimTypeInt d5 = new SimTypeInt(23);
-		SimTypeInt d2 = new SimTypeInt(0);
-		SimTypeInt d3 = new SimTypeInt(1);
+		SimTypeInt d1 = new SimTypeInt(9);
+		SimTypeInt d5 = new SimTypeInt(5);
+		SimTypeInt d3 = new SimTypeInt(0);
+		SimTypeInt d9 = new SimTypeInt(9);
 		SimTypeInt delay = new SimTypeInt(testdelay);
 		SimTypeString s2 = new SimTypeString(d1);
 		CodeAdd s5 = new CodeAdd(d1, d5);
 
-		testLoop.add(new CodeDigitalWrite(d2, b0));
-		testLoop.add(new CodeDelay(delay));
-		testLoop.add(new CodeDigitalWrite(d2, b1));
-		testLoop.add(new CodeDelay(delay));
+		//testLoop.add(new CodeDigitalWrite(d2, b0));
+		//testLoop.add(new CodeDelay(delay));
+		//testLoop.add(new CodeDigitalWrite(d2, b1));
+		//testLoop.add(new CodeDelay(delay));
 		Vector<SimCode> forVec = new Vector<SimCode>();
 		forVec.add(new CodeDigitalWrite(d3, b1));
 		forVec.add(new CodeDelay(delay));
 		forVec.add(new CodeDigitalWrite(d3, b0));
 		forVec.add(new CodeDelay(delay));
 
-		testLoop.add(new CodeFor(d1, forVec));
-		testLoop.add(new CodeSerialPrint(new SimTypeString(new CodeMillis()), b1));
+		//testLoop.add(new CodeFor(d1, forVec));
+		//testLoop.add(new CodeSerialPrint(new SimTypeString(new CodeMillis()), b1));
+		testLoop.add(new CodeSerialPrint(new SimTypeString(new CodeAnalogRead(d5)),b1));
+		testLoop.add(new CodeAnalogWrite(d9,new SimTypeInt(new CodeAnalogRead(d5))));
+		testLoop.add(new CodeDelay(delay));
 
 		SimCodeFunction setupCode = new SimCodeFunction("setup", testSetup);
 		SimCodeFunction loopCode = new SimCodeFunction("loop", testLoop);
diff --git a/src/tec/letsgoing/ardublock/simulator/view/GUI.java b/src/tec/letsgoing/ardublock/simulator/view/GUI.java
index 025ca54..4b560d3 100644
--- a/src/tec/letsgoing/ardublock/simulator/view/GUI.java
+++ b/src/tec/letsgoing/ardublock/simulator/view/GUI.java
@@ -65,6 +65,7 @@ public class GUI extends JFrame implements Runnable, ActionListener {
 		mainPane.add(createControlPanel(simu), BorderLayout.LINE_END);
 		mainPane.add(createSerialLog(), BorderLayout.PAGE_END);
 		this.pack();
+		//this.setLocation(-1000, 0); Code to Run the Window on seconds screen
 		this.setVisible(true);
 
 	}
diff --git a/src/tec/letsgoing/ardublock/simulator/view/modules/Button.java b/src/tec/letsgoing/ardublock/simulator/view/modules/Button.java
index 092a23e..cd63318 100644
--- a/src/tec/letsgoing/ardublock/simulator/view/modules/Button.java
+++ b/src/tec/letsgoing/ardublock/simulator/view/modules/Button.java
@@ -79,15 +79,15 @@ public class Button extends Modul implements ActionListener {
 	}
 
 	public boolean connect(Arduino arduino) {
-		Pin tmpPin = arduino.getPin(11);
+		Pin tmpPin = arduino.getPin(3);
 		this.addPin(tmpPin);
 		tmpPin.setObserver(this);
 
-		tmpPin = arduino.getPin(12);
+		tmpPin = arduino.getPin(4);
 		this.addPin(tmpPin);
 		tmpPin.setObserver(this);
 
-		tmpPin = arduino.getPin(13);
+		tmpPin = arduino.getPin(5);
 		this.addPin(tmpPin);
 		tmpPin.setObserver(this);
 
diff --git a/src/tec/letsgoing/ardublock/simulator/view/modules/Poti.java b/src/tec/letsgoing/ardublock/simulator/view/modules/Poti.java
index 2ab4e96..be3ad1e 100644
--- a/src/tec/letsgoing/ardublock/simulator/view/modules/Poti.java
+++ b/src/tec/letsgoing/ardublock/simulator/view/modules/Poti.java
@@ -6,6 +6,10 @@ package tec.letsgoing.ardublock.simulator.view.modules;
 import java.awt.Dimension;
 import javax.swing.ImageIcon;
 import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JSlider;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
 
 import tec.letsgoing.ardublock.simulator.arduino.Arduino;
 import tec.letsgoing.ardublock.simulator.arduino.Pin;
@@ -14,8 +18,9 @@ import tec.letsgoing.ardublock.simulator.arduino.Pin;
  * @author Lucas
  *
  */
-public class Poti extends Modul {
+public class Poti extends Modul implements  ChangeListener {
 	int value = 0;
+	JSlider slider;
 
 	public Poti(ImageIcon _icon) {
 		layerpane.setPreferredSize(new Dimension(294, 294));
@@ -23,16 +28,43 @@ public class Poti extends Modul {
 		ImageIcon chipIcon = _icon;
 		chiplabel.setIcon(chipIcon);
 		chiplabel.setSize(294, 294);
-		layerpane.add(chiplabel, 1);
-
+		slider=new JSlider(JSlider.HORIZONTAL,0,1024,512);
+		
+		slider.setMajorTickSpacing(256);
+		slider.setMinorTickSpacing(64);
+		slider.setPaintTicks(true);
+		slider.setPaintLabels(true);
+		slider.setPreferredSize(new Dimension(180,50));
+		slider.setOpaque(false);
+		slider.addChangeListener(this);
+		
+		JPanel sliderPanel=new JPanel();
+		sliderPanel.add(slider);
+		sliderPanel.setSize(200, 50);
+		sliderPanel.setLocation(47,170);
+		sliderPanel.setOpaque(false);
+		layerpane.add(chiplabel, 0);
+		layerpane.add(sliderPanel,0);
+		
+		
 	}
 
 	public void updateModul(Pin arg0) {
-
+		if (pins.get(0).getValue()!=slider.getValue()) {
+			pins.get(0).setValue(slider.getValue());
+		}
 	}
 
 	public boolean connect(Arduino arduino) {
-
+		this.addPin(arduino.getPin(5+13));
+		arduino.getPin(5+13).setObserver(this);
 		return true;
 	}
+
+	@Override
+	public void stateChanged(ChangeEvent arg0) {
+		if (slider.getValue()==1024) slider.setValue(1023); //Kleiner Trick damit die 1024 angezeigt wird aber keinen Fehler verursacht
+		pins.get(0).setValue(slider.getValue());
+		
+	}
 }
diff --git a/src/tec/letsgoing/ardublock/simulator/view/modules/RGB.java b/src/tec/letsgoing/ardublock/simulator/view/modules/RGB.java
index 8ee84cd..104e87e 100644
--- a/src/tec/letsgoing/ardublock/simulator/view/modules/RGB.java
+++ b/src/tec/letsgoing/ardublock/simulator/view/modules/RGB.java
@@ -57,15 +57,15 @@ public class RGB extends Modul {
 
 	public boolean connect(Arduino arduino) {
 		// TODO Pins= R G B ? Aktuell RBG
-		Pin tmpPin = arduino.getPin(0);
+		Pin tmpPin = arduino.getPin(9);
 		this.addPin(tmpPin);
 		tmpPin.setObserver(this);
 
-		tmpPin = arduino.getPin(1);
+		tmpPin = arduino.getPin(10);
 		this.addPin(tmpPin);
 		tmpPin.setObserver(this);
 
-		tmpPin = arduino.getPin(2);
+		tmpPin = arduino.getPin(11);
 		this.addPin(tmpPin);
 		tmpPin.setObserver(this);
 
-- 
GitLab