Skip to content

Commit c44554b

Browse files
committed
Add Trigger.onChange
Resolves #65
1 parent 4ca8a52 commit c44554b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

commands2/button/trigger.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,26 @@ def _():
122122

123123
return self
124124

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+
self._last_pressed = self._condition()
133+
@self._loop.bind
134+
def _():
135+
pressed = self._condition()
136+
137+
if (this._last_pressed != pressed) {
138+
command.schedule();
139+
}
140+
141+
this._last_pressed = pressed
142+
143+
return self
144+
125145
def whileTrue(self, command: Command) -> Self:
126146
"""
127147
Starts the given command when the condition changes to `True` and cancels it when the condition

0 commit comments

Comments
 (0)