<commit_before>"""Contains the MatrixLight class."""

from operator import itemgetter

from mpf.core.machine import MachineController
from mpf.core.mode import Mode
from mpf.core.system_wide_device import SystemWideDevice


class MatrixLight(SystemWideDevice):

    """Represents a light connected to a traditional lamp matrix in a pinball machine.

    This light could be an incandescent lamp or a replacement single-color
    LED. The key is that they're connected up to a lamp matrix.
    """

    config_section = 'matrix_lights'
    collection = 'lights'
    class_label = 'light'
    machine = None

    lights_to_update = set()
    lights_to_fade = set()

    @classmethod
    def device_class_init(cls, machine: MachineController):
        """Initialise lights.

        Args:
            machine: MachineController object
        """
        cls.machine = machine
        cls.lights_to_fade = set()
        cls.lights_to_update = set()

        machine.validate_machine_config_section('matrix_light_settings')

        machine.clock.schedule_interval(cls.update_matrix_lights,
                                        1 / machine.config['mpf']['default_matrix_light_hw_update_hz'])

        machine.mode_controller.register_stop_method(cls.mode_stop)

    @classmethod
    def update_matrix_lights(cls, dt):
        """Write changed lights to hardware.

        Args:
            dt: time since last call
        """
        for light in list(MatrixLight.lights_to_fade):
            if light.fade_in_progress:
                light.fade_task(dt)

        # called periodically (default at the end of every frame) to actually
        # write the new light states to the hardware
        if MatrixLight.lights_to_update:
            for light in MatrixLight.lights_to_update:
                light.update_hw_light()

            MatrixLight.lights_to_update = set()<commit_msg>keep reference to update task<commit_after>"""Contains the MatrixLight class."""

from operator import itemgetter

from mpf.core.machine import MachineController
from mpf.core.mode import Mode
from mpf.core.system_wide_device import SystemWideDevice


class MatrixLight(SystemWideDevice):

    """Represents a light connected to a traditional lamp matrix in a pinball machine.

    This light could be an incandescent lamp or a replacement single-color
    LED. The key is that they're connected up to a lamp matrix.
    """

    config_section = 'matrix_lights'
    collection = 'lights'
    class_label = 'light'
    machine = None

    lights_to_update = set()
    lights_to_fade = set()
    _updater_task = None

    @classmethod
    def device_class_init(cls, machine: MachineController):
        """Initialise lights.

        Args:
            machine: MachineController object
        """
        cls.machine = machine
        cls.lights_to_fade = set()
        cls.lights_to_update = set()

        machine.validate_machine_config_section('matrix_light_settings')

        cls._updater_task = machine.clock.schedule_interval(
            cls.update_matrix_lights, 1 / machine.config['mpf']['default_matrix_light_hw_update_hz'])

        machine.mode_controller.register_stop_method(cls.mode_stop)

    @classmethod
    def update_matrix_lights(cls, dt):
        """Write changed lights to hardware.

        Args:
            dt: time since last call
        """
        for light in list(MatrixLight.lights_to_fade):
            if light.fade_in_progress:
                light.fade_task(dt)

        # called periodically (default at the end of every frame) to actually
        # write the new light states to the hardware
        if MatrixLight.lights_to_update:
            for light in MatrixLight.lights_to_update:
                light.update_hw_light()

            MatrixLight.lights_to_update = set()
{'fraction_non_alphanumeric': 0.07176656151419558, 'fraction_numerical': 0.0005257623554153522, 'mean_word_length': 2.687015503875969, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4524564', 'n_tokens_mistral': 1048, 'n_tokens_neox': 999, 'n_words': 291}