Skip to content

Commit

Permalink
Merge pull request #1780 from avanwinkle/fast-led-negative-brightness
Browse files Browse the repository at this point in the history
Ensure LED fade brightness is never negative
  • Loading branch information
avanwinkle committed Mar 16, 2024
2 parents 6fb8341 + 7ca9c5a commit ff98d73
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions mpf/platforms/fast/fast_led.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from typing import Optional
from typing import List

from mpf.core.utility_functions import Util
from mpf.platforms.interfaces.light_platform_interface import LightPlatformInterface
from mpf.platforms.fast.fast_defines import EXPANSION_BOARD_FEATURES


class FASTRGBLED:

Expand Down Expand Up @@ -49,6 +48,7 @@ def current_color(self):

return result


class FASTExpLED(FASTRGBLED):

"""FAST RGB LED on an expansion board."""
Expand All @@ -60,12 +60,13 @@ def __repr__(self):
"""Return representation of this LED."""
return f'<FASTExpLED: {self.number}>'

# pylint: disable-msg=super-init-not-called
def __init__(self, number: str, hardware_fade_ms: int, platform) -> None:
"""Initialize FAST LED."""
self.number = number # 5 char hex string, board address, breakout
self.number_int = int(number, 16)
self.platform = platform
self.address = f'{number[0:3]}' # '880'
self.address = f'{number[0:3]}' # '880'
self.exp_board = platform.exp_boards_by_address[self.address[0:2]]
self.breakout_board = platform.exp_breakout_boards[self.address]

Expand Down Expand Up @@ -124,8 +125,12 @@ def get_fade_and_brightness(self, current_time):
self._last_brightness = brightness
done = True

# There is a bug that can sometimes cause the start_time to be ahead of the current_time,
# resulting in a negative brightness value. This may be a floating-point rounding error,
# or maybe something else. I can't figure it out, so just floor the value to be non-negative.
if brightness < 0:
self.led.log.warning("Calculated a negative brightness (%s) for led %s channel %s. current_time: %s"+\
brightness = 0
self.led.log.warning("Calculated a negative brightness (%s) for led %s channel %s. current_time: %s "
"start_brightness: %s, start_time: %s, target_brightness: %s, target_time: %s",
brightness, self.led, self.channel, current_time, start_brightness, start_time,
target_brightness, target_time)
Expand Down

0 comments on commit ff98d73

Please sign in to comment.