From 7ca9c5ad24da8374b034955dd6afb9e28480dc4f Mon Sep 17 00:00:00 2001 From: Anthony van Winkle Date: Sat, 16 Mar 2024 13:40:07 -0500 Subject: [PATCH] Ensure LED fade brightness is never negative --- mpf/platforms/fast/fast_led.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/mpf/platforms/fast/fast_led.py b/mpf/platforms/fast/fast_led.py index 9692982ef..b14503721 100644 --- a/mpf/platforms/fast/fast_led.py +++ b/mpf/platforms/fast/fast_led.py @@ -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: @@ -49,6 +48,7 @@ def current_color(self): return result + class FASTExpLED(FASTRGBLED): """FAST RGB LED on an expansion board.""" @@ -60,12 +60,13 @@ def __repr__(self): """Return representation of this LED.""" return f'' + # 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] @@ -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)