Skip to content

Commit f1f73d3

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

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+
134+
@self._loop.bind
135+
def _():
136+
pressed = self._condition()
137+
138+
if this._last_pressed != pressed:
139+
command.schedule()
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)