Skip to content

Commit

Permalink
Added variable and pin level change from the debug GUI.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylussaud authored and mbats committed Jun 12, 2015
1 parent 31b7fdc commit 6a5c619
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import fr.obeo.dsl.arduino.Pin;
import fr.obeo.dsl.arduino.Project;
import fr.obeo.dsl.arduino.Variable;
import fr.obeo.dsl.arduino.simulator.Simulator;

/**
Expand All @@ -29,6 +30,18 @@ public SiriusSimulator(Project project, Session session) {
this.session = session;
}

@Override
public void init() {
super.init();
refresh();
}

@Override
public void loop() {
super.loop();
refresh();
}

@Override
public void setPinLevel(Pin pin, Integer level) {
boolean shouldRefresh = !level.equals(getPinLevel(pin));
Expand All @@ -39,6 +52,16 @@ public void setPinLevel(Pin pin, Integer level) {
}
}

@Override
public void setVariableValue(Variable variable, Object value) {
boolean shouldRefresh = !value.equals(getVariableValue(variable));
super.setVariableValue(variable, value);

if (shouldRefresh) {
refresh();
}
}

/**
* Refreshes Sirius.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package fr.obeo.dsl.arduino.simulator.debug;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.emf.ecore.EObject;

import fr.obeo.dsl.arduino.Connector;
import fr.obeo.dsl.arduino.Pin;
import fr.obeo.dsl.arduino.Value;
import fr.obeo.dsl.arduino.Variable;
Expand Down Expand Up @@ -86,16 +88,19 @@ public void updateData(String threadName, EObject instruction) {
setCurrentInstruction(Thread.currentThread().getName(), instruction);
}
for (Entry<Variable, Object> entry : nextSuspendVariables.entrySet()) {
variable(threadName, "variable", entry.getKey().getName(),
entry.getValue());
variable(threadName,
simulator.getProject().getHardware().getName(), "variable",
entry.getKey().getName(), entry.getValue(), true);
}
if (!nextSuspendVariables.isEmpty()) {
lastSuspendVariables = nextSuspendVariables;
nextSuspendVariables = new HashMap<Variable, Object>();
}
for (Entry<Pin, Integer> entry : nextSuspendPins.entrySet()) {
variable(threadName, "pin", String.valueOf(entry.getKey().getId()),
entry.getValue());
variable(threadName,
simulator.getProject().getHardware().getName(), "pin",
String.valueOf(entry.getKey().getId()), entry.getValue(),
true);
}
if (!nextSuspendPins.isEmpty()) {
lastSuspendPins = nextSuspendPins;
Expand All @@ -105,7 +110,8 @@ public void updateData(String threadName, EObject instruction) {

public void variableChanged(Variable variable, Object value) {
final Object lastValue = lastSuspendVariables.get(variable);
if ((lastValue == null && value != null) || (lastValue != null && !lastValue.equals(value))) {
if ((lastValue == null && value != null)
|| (lastValue != null && !lastValue.equals(value))) {
nextSuspendVariables.put(variable, value);
} else {
nextSuspendVariables.remove(variable);
Expand All @@ -121,4 +127,116 @@ public void pinChanged(Pin pin, Integer level) {
}
}

public boolean validateVariableValue(String threadName,
String variableName, String value) {
final boolean res;

final Pin pin = lookForPin(variableName);
if (pin != null) {
Integer level = null;
try {
level = Integer.valueOf(value);
} catch (Exception e) {
// nothing to do here.
}
res = level != null;
} else {
final Variable variable = lookForVariable(variableName);
res = getValue(variable, value) != null;
}

return res;
}

public Object getVariableValue(String threadName, String stackName,
String variableName, String value) {
final Object res;

final Pin pin = lookForPin(variableName);
if (pin != null) {
Integer level = Integer.valueOf(value);
res = level;
} else {
final Variable variable = lookForVariable(variableName);
final Object valueObject = getValue(variable, value);
res = valueObject;
}

return res;
}

public void setVariableValue(String threadName, String stackName,
String variableName, Object value) {
final Pin pin = lookForPin(variableName);
if (pin != null) {
simulator.setPinLevel(pin, (Integer) value);
} else {
final Variable variable = lookForVariable(variableName);
simulator.setVariableValue(variable, value);
}

}

private Pin lookForPin(String variableName) {
Pin pin = null;

for (Connector connector : simulator.getProject().getSketch()
.getHardware().getConnectors()) {
if (String.valueOf(connector.getPin().getId()).equals(variableName)) {
pin = connector.getPin();
break;
}
}

return pin;
}

private Variable lookForVariable(String variableName) {
Variable variable = null;

final Iterator<EObject> it = simulator.getProject().getSketch()
.eAllContents();
while (it.hasNext()) {
final EObject eObj = it.next();
if (eObj instanceof Variable
&& ((Variable) eObj).getName().equals(variableName)) {
variable = (Variable) eObj;
break;
}
}

return variable;
}

private Object getValue(Variable variable, String value) {
final Object res;

final Object currentValue = simulator.getVariableValue(variable);
if (currentValue instanceof String) {
res = value;
} else if (currentValue instanceof Integer) {
Integer integerValue = null;
try {
integerValue = Integer.decode(value);
} catch (Exception e) {
// nothing to do here
}
res = integerValue;
} else if (currentValue instanceof Double) {
Double doubleValue = null;
try {
doubleValue = Double.parseDouble(value);
} catch (Exception e) {
// nothing to do here
}
res = doubleValue;
} else if (currentValue instanceof Boolean) {
res = Boolean.valueOf(value);
} else {
res = null;
}

return res;
}

}

0 comments on commit 6a5c619

Please sign in to comment.