Skip to content

Commit

Permalink
Merge pull request #1626 from avanwinkle/timer-tick-event-on-restart
Browse files Browse the repository at this point in the history
Post timer_(name)_tick event on restart when already running
  • Loading branch information
avanwinkle authored Jan 8, 2022
2 parents 0830578 + c407666 commit 8e6b74c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 7 additions & 3 deletions mpf/devices/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,12 @@ def restart(self, **kwargs):
"""
del kwargs
self.reset()
self.start()
# If the timer is not running, start it
if not self.running:
self.start()
# If the timer is running, post an updated tick event
else:
self._post_tick_events()

def stop(self, **kwargs):
"""Stop the timer and posts the 'timer_<name>_stopped' event.
Expand Down Expand Up @@ -363,8 +368,7 @@ def timer_complete(self, **kwargs):

self.debug_log("Restart on complete: True")

self.reset()
self.start()
self.restart()

def _timer_tick(self):
# Automatically called by the core clock each tick
Expand Down
7 changes: 7 additions & 0 deletions mpf/tests/test_Timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,13 @@ def test_timer_events(self):
self.advance_time_and_run()
self.assertEqual(5, timer.ticks)

# Ensure that we get the 0 tick when it restarts
self.mock_event("timer_timer_up_tick")
self.post_event('restart_timer_up')
self.assertEqual(0, timer.ticks)
self.assertEventCalledWith("timer_timer_up_tick",
ticks=0, ticks_remaining=10)

def test_interrupt_timer_by_mode_stop_with_player(self):
self.machine.events.add_handler("timer_timer_down_tick", self._timer_tick)
self.machine.events.add_handler("timer_timer_down_started", self._timer_start)
Expand Down

0 comments on commit 8e6b74c

Please sign in to comment.