Skip to content

Commit 59d9d10

Browse files
committed
em: add block, positions, and global score motor running
1 parent 944eb6e commit 59d9d10

File tree

2 files changed

+72
-32
lines changed

2 files changed

+72
-32
lines changed

Editor/Descriptors/EMAddPointsUnitDescriptor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ protected override void DefinedPort(IUnitPort port, UnitPortDescription desc)
4444
desc.summary = "The total amount of points to add.";
4545
break;
4646

47+
case nameof(EMAddPointsUnit.blockPoints):
48+
desc.summary = "Block single pulse points when score motor running.";
49+
break;
50+
51+
case nameof(EMAddPointsUnit.positions):
52+
desc.summary = "Score motor positions.";
53+
break;
54+
4755
case nameof(EMAddPointsUnit.duration):
4856
desc.summary = "The amount of time (in ms) the score motor runs.";
4957
break;

Runtime/Nodes/EM/EMAddPointsUnit.cs

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,23 @@ public class EMAddPointsUnit : GleUnit
3030
[PortLabelHidden]
3131
public ControlInput InputTrigger;
3232

33+
[DoNotSerialize]
34+
public ValueInput blockPoints { get; private set; }
35+
36+
[DoNotSerialize]
37+
public ValueInput positions { get; private set; }
38+
3339
[DoNotSerialize]
3440
public ValueInput duration { get; private set; }
3541

3642
[DoNotSerialize]
3743
[PortLabel("Unscaled")]
3844
public ValueInput unscaledTime { get; private set; }
3945

46+
[DoNotSerialize]
47+
[PortLabelHidden]
48+
public ControlOutput OutputTrigger;
49+
4050
[DoNotSerialize]
4151
public ControlOutput started;
4252

@@ -53,17 +63,21 @@ public class EMAddPointsUnit : GleUnit
5363
[PortLabel("Point Value")]
5464
public ValueOutput OutputPointValue { get; private set; }
5565

56-
private Bool running = false;
66+
private static string VARIABLE_EM_SCORE_MOTOR = "EM_SCORE_MOTOR";
5767

5868
protected override void Definition()
5969
{
6070
InputTrigger = ControlInputCoroutine(nameof(InputTrigger), Process);
6171

6272
pointValue = ValueInput(nameof(pointValue), 0);
6373

64-
duration = ValueInput(nameof(duration), .750f);
74+
blockPoints = ValueInput(nameof(blockPoints), true);
75+
positions = ValueInput(nameof(positions), 6);
76+
duration = ValueInput(nameof(duration), 750);
6577
unscaledTime = ValueInput(nameof(unscaledTime), false);
6678

79+
OutputTrigger = ControlOutput(nameof(OutputTrigger));
80+
6781
started = ControlOutput(nameof(started));
6882
stopped = ControlOutput(nameof(stopped));
6983

@@ -74,22 +88,17 @@ protected override void Definition()
7488

7589
private IEnumerator Process(Flow flow)
7690
{
77-
if (running) {
78-
var points = flow.GetValue<int>(pointValue);
91+
var running = false;
7992

80-
Debug.Log($"Score motor is already running. Ignoring {points} point(s).");
81-
82-
yield return null;
93+
if (Variables.Application.IsDefined(VARIABLE_EM_SCORE_MOTOR)) {
94+
running = Variables.Application.Get<bool>(VARIABLE_EM_SCORE_MOTOR);
8395
}
84-
else {
85-
Debug.Log("Starting score motor");
86-
87-
yield return started;
8896

89-
running = true;
97+
yield return OutputTrigger;
9098

91-
var points = flow.GetValue<int>(pointValue);
99+
var points = flow.GetValue<int>(pointValue);
92100

101+
if (points > 0) {
93102
var pulses =
94103
(points % 100000 == 0) ? points / 100000 :
95104
(points % 10000 == 0) ? points / 10000 :
@@ -98,34 +107,57 @@ private IEnumerator Process(Flow flow)
98107
(points % 10 == 0) ? points / 10 :
99108
points;
100109

101-
var pointsPerPulse = points / pulses;
110+
if (pulses == 1) {
111+
if (!running || (running && !flow.GetValue<bool>(blockPoints))) {
112+
Debug.Log($"Single pulse triggering with {points} points");
102113

103-
var seconds = flow.GetValue<float>(duration) / 6;
104-
var realtime = flow.GetValue<bool>(unscaledTime);
114+
flow.SetValue(OutputPointValue, points);
105115

106-
for (int loop = 0; loop < 6; loop++) {
107-
var outputPoints = loop < pulses ? pointsPerPulse : 0;
116+
yield return pulse;
117+
}
118+
}
119+
else if (running)
120+
{
121+
Debug.Log($"Score motor is already running.");
122+
}
123+
else {
124+
Debug.Log("Starting score motor");
108125

109-
Debug.Log($"Pulse {loop + 1} of 6 - waiting {seconds} and triggering with {outputPoints} points");
126+
Variables.Application.Set(VARIABLE_EM_SCORE_MOTOR, true);
127+
128+
yield return started;
110129

111-
if (realtime) {
112-
yield return new WaitForSecondsRealtime(seconds);
113-
}
114-
else {
115-
yield return new WaitForSeconds(seconds);
116-
}
130+
var motorPositions = flow.GetValue<int>(positions);
131+
132+
var delay = (flow.GetValue<float>(duration) / 1000f) / motorPositions;
133+
var realtime = flow.GetValue<bool>(unscaledTime);
134+
135+
var pointsPerPulse = points / pulses;
117136

118-
flow.SetValue(OutputPointValue, outputPoints);
137+
for (int loop = 0; loop < motorPositions; loop++) {
138+
var outputPoints = loop < pulses ? pointsPerPulse : 0;
119139

120-
yield return pulse;
121-
}
140+
Debug.Log($"Pulse {loop + 1} of {motorPositions} - waiting {delay}ms and triggering with {outputPoints} points");
141+
142+
if (realtime) {
143+
yield return new WaitForSecondsRealtime(delay);
144+
}
145+
else {
146+
yield return new WaitForSeconds(delay);
147+
}
148+
149+
flow.SetValue(OutputPointValue, outputPoints);
150+
151+
yield return pulse;
152+
}
122153

123-
Debug.Log("Stopping score motor");
154+
Debug.Log("Stopping score motor");
124155

125-
running = false;
156+
Variables.Application.Set(VARIABLE_EM_SCORE_MOTOR, false);
126157

127-
yield return stopped;
128-
}
158+
yield return stopped;
159+
}
160+
}
129161
}
130162
}
131163
}

0 commit comments

Comments
 (0)