Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timer kwargs evaluation #1761

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 35 additions & 48 deletions mpf/devices/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,12 @@ def reset(self, **kwargs):

Args:
----
**kwargs: Not used in this method. Only exists since this method is
often registered as an event handler which may contain
additional keyword arguments.
**kwargs: Optional kwargs that may affect the evaluation of the
timer value placeholder template.
"""
del kwargs

self.debug_log("Resetting timer. New value: %s", self.start_value)

self.jump(self.start_value)
self.jump(self.start_value, **kwargs)

def start(self, **kwargs):
"""Start this timer based on the starting value that's already been configured.
Expand Down Expand Up @@ -246,12 +243,10 @@ def restart(self, **kwargs):

Args:
----
**kwargs: Not used in this method. Only exists since this method is
often registered as an event handler which may contain
additional keyword arguments.
**kwargs: Optional kwargs that may affect the evaluation of the
timer value placeholder template.
"""
del kwargs
self.reset()
self.reset(**kwargs)
# If the timer is not running, start it
if not self.running:
self.start()
Expand Down Expand Up @@ -300,16 +295,13 @@ def pause(self, timer_value=0, **kwargs):
timer_value: How many seconds you want to pause the timer for. Note
that this pause time is real-world seconds and does not take
into consideration this timer's tick interval.
**kwargs: Not used in this method. Only exists since this method is
often registered as an event handler which may contain
additional keyword arguments.
**kwargs: Optional kwargs that may affect the evaluation of the
timer value placeholder template.
"""
del kwargs

if not timer_value:
pause_ms = 0 # make sure it's not None, etc.
else:
pause_ms = self._get_timer_value(timer_value) * 1000 # delays happen in ms
pause_ms = self._get_timer_value(timer_value, in_ms=True, **kwargs) # delays happen in ms

self.info_log("Pausing Timer for %s ms", pause_ms)

Expand Down Expand Up @@ -417,13 +409,10 @@ def add(self, timer_value, **kwargs):
----
timer_value: The number of ticks you want to add to this timer's
current value.
kwargs: Not used in this method. Only exists since this method is
often registered as an event handler which may contain
additional keyword arguments.
**kwargs: Optional kwargs that may affect the evaluation of the
timer value placeholder template.
"""
del kwargs

timer_value = self._get_timer_value(timer_value)
timer_value = self._get_timer_value(timer_value, **kwargs)
ticks_added = timer_value

new_value = self.ticks + ticks_added
Expand Down Expand Up @@ -457,13 +446,10 @@ def subtract(self, timer_value, **kwargs):
----
timer_value: The number of ticks you want to subtract from this
timer's current value.
**kwargs: Not used in this method. Only exists since this method is
often registered as an event handler which may contain
additional keyword arguments.
**kwargs: Optional kwargs that may affect the evaluation of the
timer value placeholder template.
"""
del kwargs

ticks_subtracted = self._get_timer_value(timer_value)
ticks_subtracted = self._get_timer_value(timer_value, **kwargs)

self.ticks -= ticks_subtracted

Expand Down Expand Up @@ -526,10 +512,19 @@ def _remove_system_timer(self):
self.timer = None

@staticmethod
def _get_timer_value(timer_value):
def _get_timer_value(timer_value, in_ms=False, **kwargs):
"""Return an int value for the number of ticks on the timer."""
if hasattr(timer_value, "evaluate"):
# Convert to int for ticks; config_spec must be float for change_tick_interval
return int(timer_value.evaluate(kwargs) * (1000 if in_ms else 1))
return timer_value

@staticmethod
def _get_timer_tick_secs(timer_value, **kwargs):
"""Return a float value for the number of seconds between each tick."""
if hasattr(timer_value, "evaluate"):
# Convert to int for ticks; config_spec must be float for change_tick_interval
return int(timer_value.evaluate([]))
return timer_value.evaluate(kwargs)
return timer_value

def change_tick_interval(self, change=0.0, **kwargs):
Expand All @@ -541,14 +536,10 @@ def change_tick_interval(self, change=0.0, **kwargs):
tick rate. Note this value is multiplied by the current tick
interval: >1 will increase the tick interval (slow the timer) and
<1 will decrease the tick interval (accelerate the timer).
To set an absolute value, use the set_tick_interval() method.
**kwargs: Not used in this method. Only exists since this method is
often registered as an event handler which may contain
additional keyword arguments.
**kwargs: Optional kwargs that may affect the evaluation of the
timer value placeholder template.
"""
del kwargs

self.tick_secs *= change.evaluate([])
self.tick_secs *= change.evaluate(kwargs)
self._create_system_timer()

def set_tick_interval(self, timer_value, **kwargs):
Expand All @@ -561,11 +552,10 @@ def set_tick_interval(self, timer_value, **kwargs):
----
timer_value: The new number of seconds between each tick of this
timer. This value should always be positive.
**kwargs: Not used in this method. Only exists since this method is
often registered as an event handler which may contain
additional keyword arguments.
**kwargs: Optional kwargs that may affect the evaluation of the
timer value placeholder template.
"""
self.tick_secs = abs(self._get_timer_value(timer_value.evaluate(kwargs)))
self.tick_secs = abs(self._get_timer_tick_secs(timer_value, **kwargs))
self._create_system_timer()

def jump(self, timer_value, **kwargs):
Expand All @@ -577,13 +567,10 @@ def jump(self, timer_value, **kwargs):
Args:
----
timer_value: Integer of the current value you want this timer to be.
**kwargs: Not used in this method. Only exists since this method is
often registered as an event handler which may contain
additional keyword arguments.
**kwargs: Optional kwargs that may affect the evaluation of the
timer value placeholder template.
"""
del kwargs

self.ticks = self._get_timer_value(timer_value)
self.ticks = self._get_timer_value(timer_value, **kwargs)

if self.max_value and self.ticks > self.max_value:
self.ticks = self.max_value
Expand Down
Loading