def _test_switch_configure(self): # last switch on first board self.net_cpu.expected_commands = {"SN:1F,01,04,04": "SN:P"} self.machine.default_platform.configure_switch( '0-31', SwitchConfig(debounce='auto', invert=0), {}) self.advance_time_and_run(.1) self.assertFalse(self.net_cpu.expected_commands) # next should not work with self.assertRaises(AssertionError): self.machine.default_platform.configure_switch( '0-32', SwitchConfig(debounce='auto', invert=0), {}) self.net_cpu.expected_commands = {"SN:47,01,04,04": "SN:P"} self.machine.default_platform.configure_switch( '3-15', SwitchConfig(debounce='auto', invert=0), {}) self.advance_time_and_run(.1) self.assertFalse(self.net_cpu.expected_commands) # invalid board with self.assertRaises(AssertionError): self.machine.default_platform.configure_switch( '4-0', SwitchConfig(debounce='auto', invert=0), {}) # last switch is 0x47. 0x48 = 72 with self.assertRaises(AssertionError): self.machine.default_platform.configure_switch( '72', SwitchConfig(debounce='auto', invert=0), {})
def _test_switch_configure(self): # configuring switches in PKONE does not generate any commands between the host and controller # last switch on first board self.machine.default_platform.configure_switch( '0-35', SwitchConfig(name="", debounce='auto', invert=0), {}) self.advance_time_and_run(.1) # next should not work with self.assertRaises(AssertionError): self.machine.default_platform.configure_switch( '0-36', SwitchConfig(name="", debounce='auto', invert=0), {}) # invalid board with self.assertRaises(AssertionError): self.machine.default_platform.configure_switch( '4-1', SwitchConfig(name="", debounce='auto', invert=0), {}) # invalid switch (switch number begin at 1 and not 0) with self.assertRaises(AssertionError): self.machine.default_platform.configure_switch( '0-0', SwitchConfig(name="", debounce='auto', invert=0), {})
def _initialize(self): yield from super()._initialize() self.platform = self.machine.get_platform_sections( 'switches', self.config['platform']) if self.config['type'].upper() == 'NC': self.invert = 1 self.recycle_secs = self.config['ignore_window_ms'] / 1000.0 config = SwitchConfig(invert=self.invert, debounce=self.config['debounce']) try: self.hw_switch = self.platform.configure_switch( self.config['number'], config, self.config['platform_settings']) except AssertionError as e: raise AssertionError("Failed to configure switch {} in platform. See error above".format(self.name)) from e self.add_handler(state=1, callback=self._post_events, callback_kwargs={"state": 1}) self.add_handler(state=0, callback=self._post_events, callback_kwargs={"state": 0}) if self.machine.config['mpf']['auto_create_switch_events']: self._create_activation_event( self.machine.config['mpf']['switch_event_active'].replace( '%', self.name), 1) self._create_activation_event( self.machine.config['mpf']['switch_event_inactive'].replace( '%', self.name), 0) for tag in self.tags: self._create_activation_event( self.machine.config['mpf']['switch_tag_event'].replace( '%', tag), 1) self._create_activation_event( self.machine.config['mpf']['switch_tag_event'].replace( '%', tag) + "_active", 1) self._create_activation_event( self.machine.config['mpf']['switch_tag_event'].replace( '%', tag) + "_inactive", 0) for event in Util.string_to_lowercase_list( self.config['events_when_activated']): self._create_activation_event(event, 1) for event in Util.string_to_lowercase_list( self.config['events_when_deactivated']): self._create_activation_event(event, 0)
def _initialize(self): self.platform = self.machine.get_platform_sections( 'switches', self.config['platform']) if self.config['type'].upper() == 'NC': self.invert = 1 self.recycle_secs = self.config['ignore_window_ms'] / 1000.0 config = SwitchConfig(invert=self.invert, debounce=self.config['debounce']) self.hw_switch = self.platform.configure_switch( self.config['number'], config, self.config['platform_settings']) if self.machine.config['mpf']['auto_create_switch_events']: self._create_activation_event( self.machine.config['mpf']['switch_event_active'].replace( '%', self.name), 1) self._create_activation_event( self.machine.config['mpf']['switch_event_inactive'].replace( '%', self.name), 0) for tag in self.tags: self._create_activation_event( self.machine.config['mpf']['switch_tag_event'].replace( '%', tag), 1) self._create_activation_event( self.machine.config['mpf']['switch_tag_event'].replace( '%', tag) + "_active", 1) self._create_activation_event( self.machine.config['mpf']['switch_tag_event'].replace( '%', tag) + "_inactive", 0) for event in Util.string_to_lowercase_list( self.config['events_when_activated']): self._create_activation_event(event, 1) for event in Util.string_to_lowercase_list( self.config['events_when_deactivated']): self._create_activation_event(event, 0)
async def _initialize(self): await super()._initialize() self.platform = self.machine.get_platform_sections( 'switches', self.config['platform']) if self.config['type'].upper() == 'NC': self.invert = 1 self.recycle_secs = self.config['ignore_window_ms'] / 1000.0 config = SwitchConfig(name=self.name, invert=self.invert, debounce=self.config['debounce']) if not self.platform.features[ 'allow_empty_numbers'] and self.config['number'] is None: self.raise_config_error("Switch must have a number.", 1) try: self.hw_switch = self.platform.configure_switch( self.config['number'], config, self.config['platform_settings']) except AssertionError as e: raise ConfigFileError( "Failed to configure switch {} in platform. See error above". format(self.name), 2, self.class_label) from e if self.recycle_secs: self.add_handler(state=1, callback=self._post_events_with_recycle, callback_kwargs={"state": 1}) self.add_handler(state=0, callback=self._post_events_with_recycle, callback_kwargs={"state": 0}) else: self.add_handler(state=1, callback=self._post_events, callback_kwargs={"state": 1}) self.add_handler(state=0, callback=self._post_events, callback_kwargs={"state": 0}) if self.machine.config['mpf']['auto_create_switch_events']: self._create_activation_event( self.machine.config['mpf']['switch_event_active'].replace( '%', self.name), 1) '''event: (name)_active desc: Posted when this switch becomes active. Note that this will only be posted if there is an event handler for it or if debug is set to True on this switch for performance reasons. ''' self._create_activation_event( self.machine.config['mpf']['switch_event_inactive'].replace( '%', self.name), 0) '''event: (name)_inactive desc: Posted when this switch becomes inactive. Note that this will only be posted if there is an event handler for it or if debug is set to True on this switch for performance reasons. ''' for tag in self.tags: self._create_activation_event( self.machine.config['mpf']['switch_tag_event'].replace( '%', tag), 1) '''event: sw_(tag) desc: Posted when a switch with this tag becomes active. Note that this will only be posted if there is an event handler for it or if debug is set to True on this switch for performance reasons. ''' self._create_activation_event( self.machine.config['mpf']['switch_tag_event'].replace( '%', tag) + "_active", 1) '''event: sw_(tag)_active desc: Posted when a switch with this tag becomes active. Note that this will only be posted if there is an event handler for it or if debug is set to True on this switch for performance reasons. ''' self._create_activation_event( self.machine.config['mpf']['switch_tag_event'].replace( '%', tag) + "_inactive", 0) '''event: sw_(tag)_inactive desc: Posted when a switch with this tag becomes inactive. Note that this will only be posted if there is an event handler for it or if debug is set to True on this switch for performance reasons. ''' for event in Util.string_to_event_list( self.config['events_when_activated']): self._create_activation_event(event, 1) for event in Util.string_to_event_list( self.config['events_when_deactivated']): self._create_activation_event(event, 0)