def __init__(self, machine, name, config, collection=None): self.log = logging.getLogger("Switch." + name) super(Switch, self).__init__(machine, name, config, collection, platform_section="switches") self.machine = machine self.name = name self.config = config self.deactivation_events = list() self.activation_events = list() self.state = 0 """ The logical state of a switch. 1 = active, 0 = inactive. This takes into consideration the NC or NO settings for the switch.""" self.hw_state = 0 """ The physical hardware state of the switch. 1 = active, 0 = inactive. This is what the actual hardware is reporting and does not consider whether a switch is NC or NO.""" # todo read these in and/or change to dict self.type = "NO" """ Specifies whether the switch is normally open ('NO', default) or normally closed ('NC').""" if "type" in config and config["type"].upper() == "NC": self.type = "NC" if "debounce" not in config: config["debounce"] = True if "activation_events" in config: self.activation_events = Config.string_to_lowercase_list(config["activation_events"]) if "deactivation_events" in config: self.deactivation_events = Config.string_to_lowercase_list(config["deactivation_events"]) # We save out number_str since the platform driver will convert the # number into a hardware number, but we need the original number for # some things later. self.config["number_str"] = str(config["number"]).upper() self.last_changed = None self.hw_timestamp = None self.log.debug("Creating '%s' with config: %s", name, config) self.hw_switch, self.number, self.hw_state = self.platform.configure_switch(config) self.log.debug("Current hardware state of switch '%s': %s", self.name, self.hw_state) # If we're using physical hardware, set the initial logical switch # state based on the hw_state if self.machine.physical_hw: if self.type == "NC": self.state = self.hw_state ^ 1 else: self.state = self.hw_state
def _initialize_switches(self): self.update_switches_from_hw() for switch in self.machine.switches: # Populate self.switches self.set_state(switch.name, switch.state, reset_time=True) # Populate self.registered_switches self.registered_switches[switch.name + '-0'] = list() self.registered_switches[switch.name + '-1'] = list() if self.machine.config['mpf']['auto_create_switch_events']: switch.activation_events.add( self.machine.config['mpf']['switch_event_active'].replace( '%', switch.name)) switch.deactivation_events.add( self.machine.config['mpf'][ 'switch_event_inactive'].replace( '%', switch.name)) if 'activation_events' in switch.config: for event in Config.string_to_lowercase_list( switch.config['activation_events']): if "|" in event: ev_name, ev_time = event.split("|") self.add_switch_handler( switch_name=switch.name, callback=self.machine.events.post, state=1, ms=Timing.string_to_ms(ev_time), callback_kwargs={'event': ev_name} ) else: switch.activation_events.add(event) if 'deactivation_events' in switch.config: for event in Config.string_to_lowercase_list( switch.config['deactivation_events']): if "|" in event: ev_name, ev_time = event.split("|") self.add_switch_handler( switch_name=switch.name, callback=self.machine.events.post, state=0, ms=Timing.string_to_ms(ev_time), callback_kwargs={'event': ev_name} ) else: switch.deactivation_events.add(event)
def _initialize_switches(self): # Set "start active" switches start_active = list() if not self.machine.physical_hw: try: start_active = Config.string_to_lowercase_list( self.machine. config['virtual platform start active switches']) except KeyError: pass for switch in self.machine.switches: # Populate self.switches if switch.name in start_active: switch.state = 1 # set state based on physical state self.set_state(switch.name, switch.state, reset_time=True) # Populate self.registered_switches self.registered_switches[switch.name + '-0'] = list() self.registered_switches[switch.name + '-1'] = list()
def initialize_hw_states(self): """Reads and processes the hardware states of the physical switches. We can't do this in __init__() because we need the switch controller to be setup first before we set up the hw switches. This method is called via an event handler which listens for `init_phase_2`. """ start_active = list() if not self.machine.physical_hw: try: start_active = Config.string_to_lowercase_list( self.machine.config['virtual platform start active switches']) except KeyError: pass self.log.debug("Syncing the logical and physical switch states.") for switch in self.machine.switches: if switch.name in start_active: switch.state = 1 self.set_state(switch.name, switch.state, reset_time=True)
def _configure(self): self.config = self.machine.config["languages"] self.machine.language = self self.languages = Config.string_to_lowercase_list(self.machine.config["languages"]) # Set the default language to the first entry in the list self.set_language(self.languages[0]) self.default_language = self.languages[0] self.find_text = re.compile("(\(.*?\))")
def _initialize_switches(self): # Set "start active" switches start_active = list() if not self.machine.physical_hw: try: start_active = Config.string_to_lowercase_list( self.machine.config['virtual platform start active switches']) except KeyError: pass for switch in self.machine.switches: # Populate self.switches if switch.name in start_active: switch.state = 1 # set state based on physical state self.set_state(switch.name, switch.state, reset_time=True) # Populate self.registered_switches self.registered_switches[switch.name + '-0'] = list() self.registered_switches[switch.name + '-1'] = list()
def do_load(self, callback, show_actions=None): self.show_actions = list() self.asset_manager.log.debug("Loading Show %s", self.file_name) if not show_actions: show_actions = self.load_show_from_disk() for step_num in range(len(show_actions)): step_actions = dict() step_actions['tocks'] = show_actions[step_num]['tocks'] # look for empty steps. If we find them we'll just add their tock # time to the previous step. if len(show_actions[step_num]) == 1: # 1 because it still has tocks show_actions[-1]['tocks'] += step_actions['tocks'] continue # Events # make sure events is a list of strings if ('events' in show_actions[step_num] and show_actions[step_num]['events']): event_list = (Config.string_to_lowercase_list( show_actions[step_num]['events'])) step_actions['events'] = event_list # slide_player if ('display' in show_actions[step_num] and show_actions[step_num]['display']): step_actions['display'] = ( self.machine.display.slidebuilder.preprocess_settings( show_actions[step_num]['display'])) # Sounds if ('sounds' in show_actions[step_num] and show_actions[step_num]['sounds']): # make sure we have a list of dicts if type(show_actions[step_num]['sounds']) is dict: show_actions[step_num]['sounds'] = ( [show_actions[step_num]['sounds']]) for entry in show_actions[step_num]['sounds']: try: entry['sound'] = self.machine.sounds[entry['sound']] except KeyError: self.asset_manager.log.critical("Invalid sound '%s' " "found in show. ", entry['sound']) raise step_actions['sounds'] = show_actions[step_num]['sounds'] self.show_actions.append(step_actions) # count how many total locations are in the show. We need this later # so we can know when we're at the end of a show self.total_locations = len(self.show_actions) self.loaded = True if callback: callback() self._asset_loaded()
def rotate(self, direction=None, steps=1, states=None, exclude_states=None, mode=None, **kwargs): """Rotates (or "shifts") the state of all the shots in this group. This is used for things like lane change, where hitting the flipper button shifts all the states of the shots in the group to the left or right. This method actually transfers the current state of each shot profile to the left or the right, and the shot on the end rolls over to the taret on the other end. Args: direction: String that specifies whether the rotation direction is to the left or right. Values are 'right' or 'left'. Default of None will cause the shot group to rotate in the direction as specified by the rotation_pattern. steps: Integer of how many steps you want to rotate. Default is 1. states: A string of a state or a list of strings that represent the targets that will be selected to rotate. If None (default), then all targets will be included. exclude_states: A string of a state or a list of strings that controls whether any targets will *not* be rotated. (Any targets with an active profile in one of these states will not be included in the rotation. Default is None which means all targets will be rotated) Note that this shot group must, and rotation_events for this shot group, must both be enabled for the rotation events to work. """ if not self.rotation_enabled: if self.debug: self.log.debug("Received rotation request. " "Rotation Enabled: %s. Will NOT rotate", self.rotation_enabled) return # if we don't have states or exclude_states, we'll see if the first shot # in the group has them and use those. Since all the shots should have # the same profile applied, it's ok to just pick from the first one. if states: states = Config.string_to_lowercase_list(states) else: states = self.shots[0].active_settings['settings']['state_names_to_rotate'] if exclude_states: exclude_states = Config.string_to_lowercase_list(exclude_states) else: exclude_states = ( self.shots[0].active_settings['settings']['state_names_to_not_rotate']) shot_list = list() # build of a list of shots we're actually going to rotate for shot in self.shots: if ((not states or shot.enable_table[mode]['current_state_name'] in states) and shot.enable_table[mode]['current_state_name'] not in exclude_states): shot_list.append(shot) shot_state_list = deque() for shot in shot_list: try: current_state = shot.running_light_show.current_location except AttributeError: current_state = -1 shot_state_list.append( (shot.player[shot.active_settings['settings']['player_variable']], current_state)) if self.debug: self.log.debug('Rotating. Mode: %s, Direction: %s, Include states: ' '%s, Exclude states: %s, Shots to be rotated: %s', mode, direction, states, exclude_states, [x.name for x in shot_list]) for shot in shot_list: shot.log.debug("This shot is part of a rotation event. Current" " state: %s", shot.enable_table[mode]['current_state_name']) # figure out which direction we're going to rotate if not direction: direction = shot_list[0].enable_table[mode]['settings']['rotation_pattern'][0] shot_list[0].enable_table[mode]['settings']['rotation_pattern'].rotate(-1) if self.debug: self.log.debug("Since no direction was specified, pulling from" " rotation pattern: '%s'", direction) # rotate that list if direction == 'right': shot_state_list.rotate(steps) else: shot_state_list.rotate(steps * -1) # step through all our shots and update their states for i in range(len(shot_list)): shot_list[i].jump(mode=mode, state=shot_state_list[i][0], lightshow_step=shot_state_list[i][1])
def __init__(self, machine, name, config, collection=None): self.log = logging.getLogger('Switch.' + name) super(Switch, self).__init__(machine, name, config, collection, platform_section='switches') self.machine = machine self.name = name self.config = config self.deactivation_events = list() self.activation_events = list() self.state = 0 """ The logical state of a switch. 1 = active, 0 = inactive. This takes into consideration the NC or NO settings for the switch.""" self.hw_state = 0 """ The physical hardware state of the switch. 1 = active, 0 = inactive. This is what the actual hardware is reporting and does not consider whether a switch is NC or NO.""" # todo read these in and/or change to dict self.type = 'NO' """ Specifies whether the switch is normally open ('NO', default) or normally closed ('NC').""" if 'type' in config and config['type'].upper() == 'NC': self.type = 'NC' if 'debounce' not in config: config['debounce'] = True if 'activation_events' in config: self.activation_events = Config.string_to_lowercase_list( config['activation_events']) if 'deactivation_events' in config: self.deactivation_events = Config.string_to_lowercase_list( config['deactivation_events']) # We save out number_str since the platform driver will convert the # number into a hardware number, but we need the original number for # some things later. self.config['number_str'] = str(config['number']).upper() self.last_changed = None self.hw_timestamp = None self.log.debug("Creating '%s' with config: %s", name, config) self.hw_switch, self.number, self.hw_state = \ self.platform.configure_switch(config) self.log.debug("Current hardware state of switch '%s': %s", self.name, self.hw_state) # If we're using physical hardware, set the initial logical switch # state based on the hw_state if self.machine.physical_hw: if self.type == 'NC': self.state = self.hw_state ^ 1 else: self.state = self.hw_state