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

Button-Modul, Functionen und Datentypen

parent 7e4a405c
No related branches found
No related tags found
No related merge requests found
Showing
with 336 additions and 100 deletions
res/Taster_Off.png

1.97 KiB

res/Taster_On.png

1.75 KiB

......@@ -10,8 +10,10 @@ import java.util.Vector;
import tec.letsgoing.ardublock.simulator.arduino.Arduino;
import tec.letsgoing.ardublock.simulator.arduino.Pin;
import tec.letsgoing.ardublock.simulator.simcode.SimCode;
import tec.letsgoing.ardublock.simulator.simcode.SimCodeFunction;
import tec.letsgoing.ardublock.simulator.simcode.blocks.CodeDelay;
import tec.letsgoing.ardublock.simulator.simcode.blocks.CodeDigitalWrite;
import tec.letsgoing.ardublock.simulator.simcode.blocks.CodeExecuteFunction;
import tec.letsgoing.ardublock.simulator.simcode.blocks.CodeSerialPrint;
import tec.letsgoing.ardublock.simulator.view.GUI;
import tec.letsgoing.ardublock.simulator.view.modules.RGB;
......@@ -26,9 +28,7 @@ public class Simulator implements Runnable, ActionListener {
private Thread guiThread;
private Thread simuThread;
private Vector<SimCode> setupCode = new Vector<SimCode>();
private Vector<SimCode> loopCode = new Vector<SimCode>();
private Vector<SimCode> functionsCode = new Vector<SimCode>();
private Vector<SimCodeFunction> functionsCode = new Vector<SimCodeFunction>();
private volatile boolean stopFlag=false;
......@@ -56,15 +56,14 @@ public class Simulator implements Runnable, ActionListener {
}
public void startSimu() {
stopFlag=false;
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();
......@@ -74,38 +73,21 @@ public class Simulator implements Runnable, ActionListener {
@Override
public void run() {
int setupCounter = 0;
int loopCounter = 0;
int maxSetup = setupCode.size();
int maxLoop = loopCode.size();
arduino.digitalWrite(20, true); // Power ON LED
arduino.getFunction("setup").run(arduino, null);
while (!stopFlag) {
if (setupCounter < maxSetup) {
setupCode.get(setupCounter).run(arduino);
setupCounter++;
continue;
}
arduino.getFunction("loop").run(arduino, null);
if (loopCounter < maxLoop) {
loopCode.get(loopCounter).run(arduino);
if (loopCode.get(loopCounter) instanceof CodeDigitalWrite) {
// TODO Functiondetection
}
loopCounter++;
if (loopCounter == maxLoop) {
loopCounter = 0;
}
continue;
}
}
}
public void stopSimu() {
stopFlag=true;
if (simuThread instanceof Thread) {
simuThread.interrupt();
simuThread.stop(); //TODO Deprecated - Alternativ check every Execution Arduino StopFlag - Folgen: SerialLog Bugs
}
arduino.digitalWrite(20, false);
......@@ -126,7 +108,9 @@ public class Simulator implements Runnable, ActionListener {
gui.dispose();
createSubClasses();
for (SimCodeFunction function : functionsCode) {
arduino.addFunction(function);
}
return true;
}
......@@ -147,18 +131,10 @@ public class Simulator implements Runnable, ActionListener {
return true;
}
public boolean setSetupCode(Vector<SimCode> _setupCode) {
setupCode = _setupCode;
return true;
}
public boolean setLoopCode(Vector<SimCode> _loopCode) {
loopCode = _loopCode;
return true;
}
public boolean setFunctionsCode(Vector<SimCode> _functionsCode) {
functionsCode = _functionsCode;
public boolean addFunctionsCode(SimCodeFunction _functionsCode) {
functionsCode.add(_functionsCode);
arduino.addFunction(_functionsCode);
return true;
}
......@@ -193,18 +169,24 @@ public class Simulator implements Runnable, ActionListener {
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 CodeSerialPrint("Hello World",true));
testLoop.add(new CodeSerialPrint("Hello World",true));
testLoop.add(new CodeDigitalWrite(1, false));
testLoop.add(new CodeDelay(testdelay));
testLoop.add(new CodeExecuteFunction("setup"));
testLoop.add(new CodeDigitalWrite(0, false));
testLoop.add(new CodeDigitalWrite(13, false));
testLoop.add(new CodeSerialPrint("Hello World",true));
testLoop.add(new CodeDelay(testdelay));
testLoop.add(new CodeDigitalWrite(2, false));
testLoop.add(new CodeDelay(testdelay));
SimCodeFunction setupCode =new SimCodeFunction("setup",testSetup);
SimCodeFunction loopCode=new SimCodeFunction("loop",testLoop);
Simulator simu = Simulator.getInstance();
simu.setSetupCode(testSetup);
simu.setLoopCode(testLoop);
simu.addFunctionsCode(setupCode);
simu.addFunctionsCode(loopCode);
}
......
......@@ -5,6 +5,7 @@ package tec.letsgoing.ardublock.simulator.arduino;
import java.util.Vector;
import tec.letsgoing.ardublock.simulator.simcode.SimCodeFunction;
import tec.letsgoing.ardublock.simulator.view.GUI;
/**
......@@ -16,7 +17,9 @@ public class Arduino {
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<SimCodeFunction> functions = new Vector<SimCodeFunction>();
public Arduino(GUI _gui) {
gui = _gui;
......@@ -25,54 +28,31 @@ public class Arduino {
}
}
public void createVariable(String _name, int _type, Object _value, boolean local) {
if (local) {
localVars.add(new Variable(_name, _type));
localVars.lastElement().setValue(_value);
} else {
public void createVariable(String _name, int _type, Object _value) {
vars.add(new Variable(_name, _type));
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;
}
}
} else {
public boolean setVariable(String _name, Object _value) {
for (Variable var : vars) {
if (var.getName() == _name) {
var.setValue(_value);
break;
}
}
}
return true;
}
public Object readVariable(String _name, boolean local) {
if (local) {
for (Variable var : localVars) {
if (var.getName() == _name) {
return var.getValue();
}
}
public Object readVariable(String _name) {
} else {
for (Variable var : vars) {
if (var.getName() == _name) {
return var.getValue();
}
}
}
return null;
}
......@@ -120,4 +100,18 @@ public class Arduino {
gui.serialPrint(content);
}
public void addFunction(SimCodeFunction function) {
functions.add(function);
}
public SimCodeFunction getFunction(String name) {
for (SimCodeFunction function : functions) {
if(function.getName()==name) {
return function;
}
}
return null;
}
}
......@@ -12,18 +12,15 @@ import tec.letsgoing.ardublock.simulator.arduino.Arduino;
*
*/
public abstract class SimCode {
protected Object returnValue = true;
protected Vector<Object> arguments = new Vector<Object>();
// TODO temporären Funktionsstack implementieren.
public abstract void run(Arduino ArduinoClass);
public abstract SimCode run(Arduino ArduinoClass, SimCodeFunction functionHead);
public Object getReturn() {
return returnValue;
}
public void setArguments(Vector<Object> _arguments) {
arguments = _arguments;
}
public abstract String toString();
}
/**
*
*/
package tec.letsgoing.ardublock.simulator.simcode;
import java.util.Vector;
import tec.letsgoing.ardublock.simulator.arduino.Arduino;
import tec.letsgoing.ardublock.simulator.arduino.Variable;
/**
* @author Lucas
*
*/
public class SimCodeFunction {
private String name;
private Vector<SimCode> codeBlocks= new Vector<SimCode>();
private Vector<Variable> vars = new Vector<Variable>();
public SimCodeFunction(String _name, Vector<SimCode> vec) {
name=_name;
codeBlocks=vec;
}
public void run(Arduino ArduinoClass, SimCodeFunction functionHead) {
vars=new Vector<Variable>();
for (SimCode code : codeBlocks) {
code.run(ArduinoClass, this);
}
}
public String getName() {
return name;
}
public void createVariable(String _name, int _type, Object _value) {
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;
}
}
return true;
}
public Object readVariable(String _name) {
for (Variable var : vars) {
if (var.getName() == _name) {
return var.getValue();
}
}
return null;
}
}
......@@ -5,6 +5,7 @@ package tec.letsgoing.ardublock.simulator.simcode.blocks;
import tec.letsgoing.ardublock.simulator.arduino.Arduino;
import tec.letsgoing.ardublock.simulator.simcode.SimCode;
import tec.letsgoing.ardublock.simulator.simcode.SimCodeFunction;
/**
* @author Lucas
......@@ -17,7 +18,7 @@ public class CodeDelay extends SimCode {
duration = _delay;
}
public void run(Arduino ArduinoClass) {
public SimCode run(Arduino ArduinoClass, SimCodeFunction functionHead) {
ArduinoClass.serialPrint("Sleep for: " + duration);
try {
Thread.sleep(duration);
......@@ -25,7 +26,14 @@ public class CodeDelay extends SimCode {
// TODO Auto-generated catch block
// e.printStackTrace();
}
return null;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "";
}
}
......@@ -6,6 +6,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;
import tec.letsgoing.ardublock.simulator.simcode.SimCodeFunction;
/**
* @author Lucas
......@@ -20,8 +21,15 @@ public class CodeDigitalWrite extends SimCode {
value = _value;
}
public void run(Arduino ArduinoClass) {
public SimCode run(Arduino ArduinoClass, SimCodeFunction functionHead) {
ArduinoClass.digitalWrite(pin, value);
return null;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "";
}
}
/**
*
*/
package tec.letsgoing.ardublock.simulator.simcode.blocks;
import tec.letsgoing.ardublock.simulator.arduino.Arduino;
import tec.letsgoing.ardublock.simulator.simcode.SimCode;
import tec.letsgoing.ardublock.simulator.simcode.SimCodeFunction;
/**
* @author Lucas
*
*/
public class CodeExecuteFunction extends SimCode {
private String name;
public CodeExecuteFunction (String _name) {
name=_name;
}
@Override
public SimCode run(Arduino ArduinoClass, SimCodeFunction functionHead) {
ArduinoClass.getFunction(name).run(ArduinoClass, null);
return null;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "";
}
}
......@@ -5,6 +5,7 @@ package tec.letsgoing.ardublock.simulator.simcode.blocks;
import tec.letsgoing.ardublock.simulator.arduino.Arduino;
import tec.letsgoing.ardublock.simulator.simcode.SimCode;
import tec.letsgoing.ardublock.simulator.simcode.SimCodeFunction;
/**
* @author Lucas
......@@ -13,13 +14,22 @@ import tec.letsgoing.ardublock.simulator.simcode.SimCode;
public class CodeSerialPrint extends SimCode {
private String content;
public CodeSerialPrint(String _content) {
public CodeSerialPrint(String _content, boolean _lineEnding) {
content = _content;
if (_lineEnding) content+="\n";
}
public void run(Arduino ArduinoClass) {
public SimCode run(Arduino ArduinoClass, SimCodeFunction functionHead) {
ArduinoClass.serialPrint(content);
return null;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "";
}
}
/**
*
*/
package tec.letsgoing.ardublock.simulator.simcode.datatypes;
import tec.letsgoing.ardublock.simulator.arduino.Arduino;
import tec.letsgoing.ardublock.simulator.simcode.SimCode;
import tec.letsgoing.ardublock.simulator.simcode.SimCodeFunction;
/**
* @author Lucas
*
*/
public class CodeBool extends SimCode {
boolean value=false;
private SimCode followBlock;
public CodeBool (boolean _value) {
value=_value;
}
public CodeBool (SimCode block) {
followBlock=block;
}
@Override
public SimCode run(Arduino ArduinoClass, SimCodeFunction functionHead) {
if (followBlock instanceof SimCode) {
CodeBool ret=(CodeBool)followBlock.run(ArduinoClass, functionHead);
value=ret.getValue();
}
return new CodeBool(value);
}
@Override
public String toString() {
return String.valueOf(value);
}
public boolean getValue() {
return value;
}
}
/**
*
*/
package tec.letsgoing.ardublock.simulator.simcode.datatypes;
import tec.letsgoing.ardublock.simulator.arduino.Arduino;
import tec.letsgoing.ardublock.simulator.simcode.SimCode;
import tec.letsgoing.ardublock.simulator.simcode.SimCodeFunction;
/**
* @author Lucas
*
*/
public class CodeDouble extends SimCode {
double value=0;
private SimCode followBlock;
public CodeDouble (double _value) {
value=_value;
}
public CodeDouble (SimCode block) {
followBlock=block;
}
@Override
public SimCode run(Arduino ArduinoClass, SimCodeFunction functionHead) {
if (followBlock instanceof SimCode) {
CodeDouble ret=(CodeDouble)followBlock.run(ArduinoClass, functionHead);
value=ret.getValue();
}
return new CodeDouble(value);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return String.valueOf(value);
}
public double getValue() {
return value;
}
}
/**
*
*/
package tec.letsgoing.ardublock.simulator.simcode.datatypes;
import tec.letsgoing.ardublock.simulator.arduino.Arduino;
import tec.letsgoing.ardublock.simulator.simcode.SimCode;
import tec.letsgoing.ardublock.simulator.simcode.SimCodeFunction;
/**
* @author Lucas
*
*/
public class CodeString extends SimCode {
private String content;
private SimCode followBlock;
public CodeString (String _content) {
content=_content;
}
public CodeString (SimCode block) {
followBlock=block;
}
@Override
public SimCode run(Arduino ArduinoClass, SimCodeFunction functionHead) {
if (followBlock instanceof SimCode) {
SimCode ret=followBlock.run(ArduinoClass, functionHead);
content=((CodeString)ret).toString();
}
return new CodeString(content);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return content;
}
}
......@@ -148,7 +148,7 @@ public class GUI extends JFrame implements Runnable, ActionListener {
String tmp = "";
for (int i = 0; i < serialprint.size(); i++) {
tmp = tmp + serialprint.get(i) + "\n";
tmp = tmp + serialprint.get(i);
}
serialLog.setText(tmp);
......
......@@ -24,6 +24,7 @@ import tec.letsgoing.ardublock.simulator.arduino.Pin;
*
*/
public class Button extends Modul implements ActionListener {
private int[] lastState= {0,0,0};
public Button() {
layerpane.setPreferredSize(new Dimension(294, 294));
......@@ -35,9 +36,7 @@ public class Button extends Modul implements ActionListener {
JToggleButton but1 = new JToggleButton();
but1.setSize(70, 70);
but1.setOpaque(true);
but1.setContentAreaFilled(true);
but1.setBorderPainted(true);
but1.setIcon(new ImageIcon("res/Taster_Off.png"));
but1.setActionCommand("0");
but1.addActionListener(this);
JLabel label1 = new JLabel();
......@@ -47,35 +46,41 @@ public class Button extends Modul implements ActionListener {
JToggleButton but2 = new JToggleButton();
but2.setSize(70, 70);
but2.setOpaque(true);
but2.setContentAreaFilled(true);
but2.setBorderPainted(true);
but2.setIcon(new ImageIcon("res/Taster_Off.png"));
but2.setActionCommand("1");
but2.addActionListener(this);
JLabel label2 = new JLabel();
label2.add(but2);
label2.setSize(70, 70);
label2.setLocation(111, 33);
label2.setLocation(111, 35);
JToggleButton but3 = new JToggleButton();
but3.setSize(70, 70);
but3.setOpaque(true);
but3.setContentAreaFilled(true);
but3.setBorderPainted(true);
but3.setIcon(new ImageIcon("res/Taster_Off.png"));
but3.setActionCommand("2");
but3.addActionListener(this);
JLabel label3 = new JLabel();
label3.add(but3);
label3.setSize(70, 70);
label3.setLocation(195, 64);
label3.setLocation(194, 64);
layerpane.add(label1, 0);
layerpane.add(label3, 0);
layerpane.add(label2, 0);
layerpane.add(label3, 0);
}
public void updateModul(Pin pin) {
int pinNumber=0;
if (pin == pins.get(0))
pinNumber=0;
if (pin == pins.get(1))
pinNumber=1;
if (pin == pins.get(2))
pinNumber=2;
if (pins.get(pinNumber).getValue()!=lastState[pinNumber]) {
pins.get(pinNumber).setValue(lastState[pinNumber]);
}
// TODO Check of the Value Changed since last time and force update -> Save Last
// Value
}
......@@ -99,9 +104,17 @@ public class Button extends Modul implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
Integer pin = Integer.parseInt(arg0.getActionCommand());
boolean bool = ((JToggleButton) arg0.getSource()).isSelected();
String path;
if (bool) {
path="res/Taster_On.png";
} else {
path="res/Taster_Off.png";
}
((JToggleButton) arg0.getSource()).setIcon(new ImageIcon(path));
int value = 0;
if (bool)
value = 1023;
lastState[pin]=value;
pins.get(pin).setValue(value);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment