How to use mpf - 10 common examples

To help you get started, we’ve selected a few mpf examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github missionpinball / mpf-mc / tests / MpfMcTestCase.py View on Github external
print("Running", self._test_name)
        # This setup is done in run() because we need to give control to the
        # kivy event loop which we can only do by returning from the run()
        # that's called. So we override run() and setup mpf-mc and then call
        # our own run_test() on a callback. Then we can wait until the
        # environment is setup (which can take a few frames), then we call
        # super().run() to get the actual TestCase.run() method to run and
        # we return the results.

        # We have to do this in run() and not setUp() because run actually
        # calls setUp(), so since we were overriding it ours doesn't call it
        # so we just do our setup here since if we manually called setUp() then
        # it would be called again when we call super().run().


        mpf_config = ConfigProcessor.load_config_file(self.get_options()[
                                                      'mcconfigfile'])

        mpf_config = load_machine_config(
                Util.string_to_list(self.get_config_file()),
                self.get_machine_path(),
                mpf_config['mpf-mc']['paths']['config'], mpf_config)
        self.preprocess_config(mpf_config)

        self.mc = TestMpfMc(options=self.get_options(),
                            config=mpf_config,
                            machine_path=self.get_machine_path())

        self.patch_bcp()

        from kivy.core.window import Window
        Window.create_window()
github missionpinball / mpf / tests / test_DeviceLED.py View on Github external
def testInterruptFadeOut(self):
        """Interrupt (kill) a one second fade from white to off"""

        # LED should start out on (white)
        self.machine.leds.led_01.color(RGBColor('White'))
        self.assertEqual(RGBColor('White'), self.machine.leds.led_01.state['color'])

        self.machine.leds.led_01.color(RGBColor('Off'), fade_ms=1000)
        fade_start_time = self.machine.clock.get_time()
        self.assertEqual(RGBColor('White'), self.machine.leds.led_01.state['color'])
        self.assertEqual(RGBColor('White'), self.machine.leds.led_01.state['start_color'])
        self.assertEqual(RGBColor('Off'), self.machine.leds.led_01.state['destination_color'])
        self.machine_run()

        # Advance time 50% of the fade and check colors
        self.advance_time_and_run(0.5)
        self.assertEqual(RGBColor((128, 128, 128)), self.machine.leds.led_01.state['color'])
        self.assertEqual(RGBColor('White'), self.machine.leds.led_01.state['start_color'])
        self.assertEqual(RGBColor('Off'), self.machine.leds.led_01.state['destination_color'])
        self.assertEqual(RGBColor((128, 128, 128)), self.machine.leds.led_01.hw_driver.current_color)

        # Check cache (should not have changed)
        self.assertEqual(RGBColor('Off'), self.machine.leds.led_01.cache['color'])
        self.assertEqual(RGBColor('Off'), self.machine.leds.led_01.cache['destination_color'])
        self.assertEqual(RGBColor('White'), self.machine.leds.led_01.cache['start_color'])
        self.assertEqual(fade_start_time, self.machine.leds.led_01.cache['start_time'])
        self.assertEqual(fade_start_time + 1, self.machine.leds.led_01.cache['destination_time'])
github missionpinball / mpf / tests / test_RGBColor.py View on Github external
def test_off_color(self):
        # Tests the 'Off' color (nicely readable in LED show files)
        color = RGBColor()
        color.name = 'Off'
        self.assertEqual((0, 0, 0), color.rgb)
        self.assertIn(color.name, ['black', 'off'])
github missionpinball / mpf / tests / test_EventManager.py View on Github external
def test_delay_remove_race(self):
        self.called = False
        self.delay = DelayManager(self.machine.delayRegistry)

        self.delay.add(ms=6000, name="first", callback=self.delay_first)
        self.delay.add(ms=6000, name="second", callback=self.delay_second)
        self.advance_time_and_run(10)
github missionpinball / mpf / tests / test_Utility_Functions.py View on Github external
def test_int_to_hex_string(self):
        result = Util.int_to_hex_string(255)
        self.assertEqual(result, 'FF')

        self.assertRaises(ValueError, Util.int_to_hex_string, 256)
github missionpinball / mpf / tests / test_OPP.py View on Github external
def _crc_message(self, msg, term=True):
        crc_msg = msg + opp.OppRs232Intf.calc_crc8_part_msg(msg, 0, len(msg))
        if term:
            crc_msg += '\xff'
        return crc_msg
github missionpinball / mpf / mpf / core / config_processor.py View on Github external
config = FileManager.load(filename, expected_version_str, True)
        subfiles = []

        if not config:
            return dict(), []

        self.log.info('Loading config: %s', filename)

        if config_type in ("machine", "mode"):
            self._check_sections(config, config_type, filename, ignore_unknown_sections)

        try:
            if 'config' in config:
                path = os.path.split(filename)[0]

                for file in Util.string_to_list(config['config']):
                    full_file = os.path.join(path, file)
                    subfiles.append(full_file)
                    subconfig, subsubfiles = self._load_config_file_and_return_loaded_files(full_file, config_type)
                    subfiles.extend(subsubfiles)
                    config = Util.dict_merge(config, subconfig)
            return config, subfiles
        except TypeError:
            return dict(), []
github missionpinball / mpf / mpf / config_players / event_player.py View on Github external
def play(self, settings, context, calling_context, priority=0, **kwargs):
        """Post (delayed) events."""
        del kwargs
        for event, s in settings.items():
            s = deepcopy(s)
            if '|' in event:
                event, delay = event.split("|")
                delay = Util.string_to_ms(delay)
                self.delay.add(callback=self._post_event, ms=delay,
                               event=event, s=s)
            elif ':' in event:
                event, delay = event.split(":")
                delay = Util.string_to_ms(delay)
                self.delay.add(callback=self._post_event, ms=delay,
                               event=event, s=s)
            else:
                self._post_event(event, s)
github missionpinball / mpf-mc / mpfmc / commands / mc.py View on Github external
const='virtual', help=argparse.SUPPRESS)

        parser.add_argument("--vpx",
                            action="store_const", dest="force_platform",
                            const='virtual_pinball', help=argparse.SUPPRESS)

        parser.add_argument("-X",
                            action="store_const", dest="force_platform",
                            const='smart_virtual', help=argparse.SUPPRESS)

        parser.add_argument("--no-sound",
                            action="store_true", dest="no_sound", default=False)

        args = parser.parse_args(args)

        args.configfile = Util.string_to_event_list(args.configfile)

        # Configure logging. Creates a logfile and logs to the console.
        # Formatting options are documented here:
        # https://docs.python.org/2.7/library/logging.html#logrecord-attributes

        try:
            os.makedirs(os.path.join(machine_path, 'logs'))
        except OSError as exception:
            if exception.errno != errno.EEXIST:
                raise

        if args.mc_logfile:
            args.logfile = args.mc_logfile

        full_logfile_path = os.path.join(machine_path, args.logfile)
github missionpinball / mpf / mpf / devices / shot.py View on Github external
    @event_handler(5)
    def event_hit(self, **kwargs):
        """Handle hit control event."""
        success = self.hit()
        if not success:
            return None

        if self.profile.config['block']:
            min_priority = kwargs.get("_min_priority", {"all": 0})
            min_shots = min_priority.get("shot", 0)
            min_priority["shot"] = self.mode.priority if self.mode.priority > min_shots else min_shots
            return {"_min_priority": min_priority}

        return None