File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -122,6 +122,27 @@ def _():
122
122
123
123
return self
124
124
125
+ def onChange (self , command : Command ) -> Self :
126
+ """
127
+ Starts the command when the condition changes.
128
+
129
+ :param command: the command t start
130
+ :returns: this trigger, so calls can be chained
131
+ """
132
+
133
+ state = SimpleNamespace (pressed_last = self ._condition ())
134
+
135
+ @self ._loop .bind
136
+ def _ ():
137
+ pressed = self ._condition ()
138
+
139
+ if state .pressed_last != pressed :
140
+ command .schedule ()
141
+
142
+ state .pressed_last = pressed
143
+
144
+ return self
145
+
125
146
def whileTrue (self , command : Command ) -> Self :
126
147
"""
127
148
Starts the given command when the condition changes to `True` and cancels it when the condition
Original file line number Diff line number Diff line change @@ -42,6 +42,23 @@ def test_onFalse(scheduler: commands2.CommandScheduler):
42
42
assert not command1 .isScheduled ()
43
43
44
44
45
+ def test_onChange (scheduler : commands2 .CommandScheduler ):
46
+ finished = OOBoolean (False )
47
+ command1 = commands2 .WaitUntilCommand (finished )
48
+
49
+ button = InternalButton ()
50
+ button .setPressed (True )
51
+ button .onChange (command1 )
52
+ scheduler .run ()
53
+ assert not command1 .isScheduled ()
54
+ button .setPressed (False )
55
+ scheduler .run ()
56
+ assert command1 .isScheduled ()
57
+ finished .set (True )
58
+ scheduler .run ()
59
+ assert not command1 .isScheduled ()
60
+
61
+
45
62
def test_whileTrueRepeatedly (scheduler : commands2 .CommandScheduler ):
46
63
inits = OOInteger (0 )
47
64
counter = OOInteger (0 )
You can’t perform that action at this time.
0 commit comments