From 1cbb712eeeea01e3d37de3684d45dfb51075108c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Thu, 28 May 2020 18:31:48 +0200 Subject: [PATCH] Add mechanism for synchronously pulling getter block values. --- .../parts/ProgramakerCustomBlockPart.java | 38 ++++++++++++++++++- .../minicards/parts/buttons/RoundButton.java | 5 +++ .../connectors/AnyRoundInputConnector.java | 11 ++++++ .../BooleanRoundOutputConnector.java | 9 ++++- .../connectors/RoundOutputConnector.java | 9 ++++- .../StringRoundOutputConnector.java | 9 ++++- .../minicards/parts/logic/Ticker.java | 5 +++ .../minicards/parts/logic/Toggle.java | 5 +++ .../minicards/parts/samples/ColorBox.java | 5 +++ .../minicards/parts/samples/Placeholder.java | 5 +++ .../parts/strings/ConvertToString.java | 21 ++++++---- .../minicards/types/Part.java | 2 + .../types/connectors/Wiring/BooleanWire.java | 4 +- .../types/connectors/Wiring/SignalWire.java | 4 +- .../types/connectors/Wiring/StringWire.java | 4 +- .../types/connectors/Wiring/Wire.java | 10 ++++- .../connectors/output/OutputConnector.java | 2 + 17 files changed, 126 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/ProgramakerCustomBlockPart.java b/app/src/main/java/com/codigoparallevar/minicards/parts/ProgramakerCustomBlockPart.java index d0ac007..68eadf2 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/ProgramakerCustomBlockPart.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/ProgramakerCustomBlockPart.java @@ -370,10 +370,10 @@ public class ProgramakerCustomBlockPart implements Part { } } - private void runBlockOperation() { + private ProgramakerFunctionCallResult runBlockOperation() { if (!this.active) { Log.w(LogTag, "Trying to run inactive block function=" + this._block.getFunction_name()); - return; + return null; } ProgramakerApi api = this._partGrid.getApi(); @@ -386,6 +386,11 @@ public class ProgramakerCustomBlockPart implements Part { continue; } + Object queriedValue = entry.item2.query(lastValues[index]); + if (queriedValue != null) { + lastValues[index] = queriedValue; + } + if (lastValues[index] == null) { arguments.add(null); // TODO: Get default value from block definition } @@ -398,6 +403,8 @@ public class ProgramakerCustomBlockPart implements Part { Log.i(LogTag, "Execution result="+result.getResult()); onExecutionCompleted(result); + + return result; } private void onExecutionCompleted(ProgramakerFunctionCallResult result) { @@ -621,4 +628,31 @@ public class ProgramakerCustomBlockPart implements Part { input.unlink(); } } + + @Override + public Object query(Object lastValue) { + String blockType = _block.getBlock_type(); + + if ((blockType == null) || (!blockType.equals("getter"))) { + // Only relevant for getters + return null; + } + + String token = this.prepareStart(); + + if (token != null) { + try { + _partGrid.update(); + ProgramakerFunctionCallResult result = runBlockOperation(); + if (result != null) { + return result.getResult(); + } + } + finally { + this.freeBlock(token); + } + } + + return null; + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java b/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java index 1e2eefe..6b2c732 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/buttons/RoundButton.java @@ -229,6 +229,11 @@ public class RoundButton implements Part { } + @Override + public Object query(Object lastValue) { + return null; // Only pulse output, so no relevant data + } + public RoundOutputConnector getPressedOutputConnector(){ return _pressedOutputConnector; } diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/AnyRoundInputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/AnyRoundInputConnector.java index 7fd5fa4..7c5db4b 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/AnyRoundInputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/AnyRoundInputConnector.java @@ -55,6 +55,17 @@ public class AnyRoundInputConnector extends AnyInputConnector { } } + public Object query(Object lastValue) { + for (Wire linked : _attachments) { + Object returned = linked.query(lastValue); + if (returned != null) { + return returned; + } + } + + return null; + } + @Override public int getX() { return _xposition; diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundOutputConnector.java index 79febf7..e5846ca 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundOutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/BooleanRoundOutputConnector.java @@ -83,7 +83,7 @@ public class BooleanRoundOutputConnector implements Drawable, BooleanOutputConne } _connections.add(resultPoint); - wire.attachTo(resultPoint); + wire.attachTo(resultPoint, this); _wires.add(wire); } else { @@ -141,7 +141,7 @@ public class BooleanRoundOutputConnector implements Drawable, BooleanOutputConne _connections.add(inputConnector); BooleanWire wire = new BooleanWire(this, _centerX, _centerY); - wire.attachTo(inputConnector); + wire.attachTo(inputConnector, this); _wires.add(wire); } @@ -155,4 +155,9 @@ public class BooleanRoundOutputConnector implements Drawable, BooleanOutputConne wire.send(signal); } } + + @Override + public Object query(Object lastValue) { + return _part.query(lastValue); + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundOutputConnector.java index b3adb82..e19c790 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundOutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/RoundOutputConnector.java @@ -83,7 +83,7 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector { } _connections.add(resultPoint); - wire.attachTo(resultPoint); + wire.attachTo(resultPoint, this); _wires.add(wire); } else { @@ -141,7 +141,7 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector { _connections.add(inputConnector); SignalWire wire = new SignalWire(this, _centerX, _centerY); - wire.attachTo(inputConnector); + wire.attachTo(inputConnector, this); _wires.add(wire); } @@ -156,6 +156,11 @@ public class RoundOutputConnector implements Drawable, SignalOutputConnector { } } + @Override + public Object query(Object lastValue) { + return _part.query(lastValue); + } + public float getX() { return _centerX; } diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/StringRoundOutputConnector.java b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/StringRoundOutputConnector.java index 323db01..1aa3232 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/StringRoundOutputConnector.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/connectors/StringRoundOutputConnector.java @@ -83,7 +83,7 @@ public class StringRoundOutputConnector implements Drawable, StringOutputConnect } _connections.add(resultPoint); - wire.attachTo(resultPoint); + wire.attachTo(resultPoint, this); _wires.add(wire); } else { @@ -141,7 +141,7 @@ public class StringRoundOutputConnector implements Drawable, StringOutputConnect _connections.add(inputConnector); StringWire wire = new StringWire(this, _centerX, _centerY); - wire.attachTo(inputConnector); + wire.attachTo(inputConnector, this); _wires.add(wire); } @@ -155,4 +155,9 @@ public class StringRoundOutputConnector implements Drawable, StringOutputConnect wire.send(signal); } } + + @Override + public Object query(Object lastValue) { + return _part.query(lastValue); + } } diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Ticker.java b/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Ticker.java index d78078b..d7f3759 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Ticker.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Ticker.java @@ -297,6 +297,11 @@ public class Ticker implements Part { _thread = null; } + @Override + public Object query(Object lastValue) { + return null; // No relevant value (maybe time?) + } + private int getOutputConnectorCenterX() { return (_left + _right) / 2; diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Toggle.java b/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Toggle.java index 371960e..9f7a19d 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Toggle.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/logic/Toggle.java @@ -288,6 +288,11 @@ public class Toggle implements Part { } + @Override + public Object query(Object lastValue) { + return _activated; + } + public static Tuple2> deserialize(PartGrid partGrid, JSONObject data) throws JSONException { String id = data.getString("id"); int left = data.getInt("left"); diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java b/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java index aa7c880..fa0256b 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/samples/ColorBox.java @@ -220,6 +220,11 @@ public class ColorBox implements Part { } + @Override + public Object query(Object lastValue) { + return null; + } + public static Part deserialize(PartGrid partGrid, JSONObject data) throws JSONException { String id = data.getString("id"); int left = data.getInt("left"); diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/samples/Placeholder.java b/app/src/main/java/com/codigoparallevar/minicards/parts/samples/Placeholder.java index d496e96..b471966 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/samples/Placeholder.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/samples/Placeholder.java @@ -154,6 +154,11 @@ public class Placeholder implements Part { } + @Override + public Object query(Object lastValue) { + return null; + } + public static Part deserialize(PartGrid partGrid, JSONObject data) throws JSONException { String id = data.getString("id"); int left = data.getInt("left"); diff --git a/app/src/main/java/com/codigoparallevar/minicards/parts/strings/ConvertToString.java b/app/src/main/java/com/codigoparallevar/minicards/parts/strings/ConvertToString.java index 9cf3c69..75d5a26 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/parts/strings/ConvertToString.java +++ b/app/src/main/java/com/codigoparallevar/minicards/parts/strings/ConvertToString.java @@ -5,8 +5,6 @@ import android.graphics.Paint; import android.graphics.Rect; import android.util.Log; -import androidx.annotation.NonNull; - import com.codigoparallevar.minicards.PartInstantiator; import com.codigoparallevar.minicards.ScrolledCanvas; import com.codigoparallevar.minicards.parts.connectors.AnyRoundInputConnector; @@ -46,8 +44,6 @@ public class ConvertToString implements Part { private int _bottom; private List inputConnectors; private AnyInputConnector _toggleInputConnector; - - @NonNull private String _lastValue; private ConvertToString(String id, PartGrid partGrid, int left, int top, int right, int bottom) { @@ -57,7 +53,7 @@ public class ConvertToString implements Part { _top = top; _right = right; _bottom = bottom; - _lastValue = "-"; + _lastValue = null; // Input connector _toggleInputConnector = new AnyRoundInputConnector( @@ -121,7 +117,13 @@ public class ConvertToString implements Part { Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); textPaint.setColor(Color.GREEN); textPaint.setTextSize(100); - canvas.drawText(_lastValue, + + String value = "-"; + if (_lastValue != null) { + value = _lastValue; + } + + canvas.drawText(value, _left + 10, _bottom - 10, textPaint); @@ -255,7 +257,7 @@ public class ConvertToString implements Part { encoded = value.toString(); } - if (!_lastValue.equals(encoded)) { + if ((_lastValue != null) && !_lastValue.equals(encoded)) { _lastValue = encoded; _stringOutputConnector.send(new StringSignal(encoded)); @@ -297,6 +299,11 @@ public class ConvertToString implements Part { } + @Override + public Object query(Object lastValue) { + return _lastValue; + } + public static Tuple2> deserialize(PartGrid partGrid, JSONObject data) throws JSONException { String id = data.getString("id"); int left = data.getInt("left"); diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/Part.java b/app/src/main/java/com/codigoparallevar/minicards/types/Part.java index dde9f01..c9a85ab 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/Part.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/Part.java @@ -32,4 +32,6 @@ public interface Part extends Selectable, Moveable, Drawable { void resume(); void pause(); + + Object query(Object lastValue); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/BooleanWire.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/BooleanWire.java index 9275c73..ff1b1f8 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/BooleanWire.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/BooleanWire.java @@ -1,13 +1,13 @@ package com.codigoparallevar.minicards.types.connectors.Wiring; import com.codigoparallevar.minicards.types.Drawable; -import com.codigoparallevar.minicards.types.Dropper; import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.connectors.input.BooleanInputConnector; +import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.wireData.BooleanSignal; public class BooleanWire extends Wire implements Moveable, Drawable { - public BooleanWire(Dropper dropper, int xInit, int yInit) { + public BooleanWire(OutputConnector dropper, int xInit, int yInit) { super(dropper, xInit, yInit); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/SignalWire.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/SignalWire.java index 61e767c..d256b18 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/SignalWire.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/SignalWire.java @@ -1,13 +1,13 @@ package com.codigoparallevar.minicards.types.connectors.Wiring; import com.codigoparallevar.minicards.types.Drawable; -import com.codigoparallevar.minicards.types.Dropper; import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.connectors.input.SignalInputConnector; +import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.wireData.Signal; public class SignalWire extends Wire implements Moveable, Drawable { - public SignalWire(Dropper dropper, int xInit, int yInit) { + public SignalWire(OutputConnector dropper, int xInit, int yInit) { super(dropper, xInit, yInit); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/StringWire.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/StringWire.java index 9ccd9d5..95c1434 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/StringWire.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/StringWire.java @@ -1,13 +1,13 @@ package com.codigoparallevar.minicards.types.connectors.Wiring; import com.codigoparallevar.minicards.types.Drawable; -import com.codigoparallevar.minicards.types.Dropper; import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.connectors.input.StringInputConnector; +import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.wireData.StringSignal; public class StringWire extends Wire implements Moveable, Drawable { - public StringWire(Dropper dropper, int xInit, int yInit) { + public StringWire(OutputConnector dropper, int xInit, int yInit) { super(dropper, xInit, yInit); } diff --git a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/Wire.java b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/Wire.java index d292ad0..dec86e3 100644 --- a/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/Wire.java +++ b/app/src/main/java/com/codigoparallevar/minicards/types/connectors/Wiring/Wire.java @@ -9,6 +9,7 @@ import com.codigoparallevar.minicards.types.Drawable; import com.codigoparallevar.minicards.types.Dropper; import com.codigoparallevar.minicards.types.Moveable; import com.codigoparallevar.minicards.types.connectors.input.InputConnector; +import com.codigoparallevar.minicards.types.connectors.output.OutputConnector; import com.codigoparallevar.minicards.types.wireData.WireDataType; public class Wire> @@ -21,6 +22,7 @@ public class Wire