self.machine.switch_controller

class mpf.core.switch_controller.SwitchController(machine)

Bases: mpf.core.mpf_controller.MpfController

Handles all switches in the machine.

Base class for the switch controller, which is responsible for receiving all switch activity in the machine and converting them into events.

More info: http://docs.missionpinball.org/en/latest/core/switch_controller.html

Accessing the switch_controller in code

There is only one instance of the switch_controller in MPF, and it's accessible via self.machine.switch_controller.

Methods & Attributes

The switch_controller has the following methods & attributes available. Note that methods & attributes inherited from base classes are not included here.

add_monitor(monitor)

Add a monitor callback which is called on switch changes.

add_switch_handler(switch_name, callback, state=1, ms=0, return_info=False, callback_kwargs=None) → mpf.core.switch_controller.SwitchHandler

Register a handler to take action on a switch event.

Parameters:
  • switch_name -- String name of the switch you're adding this handler for.
  • callback -- The method you want called when this switch handler fires.
  • state -- Integer of the state transition you want to callback to be triggered on. Default is 1 which means it's called when the switch goes from inactive to active, but you can also use 0 which means your callback will be called when the switch becomes inactive
  • ms -- Integer. If you specify a 'ms' parameter, the handler won't be called until the witch is in that state for that many milliseconds (rounded up to the nearst machine timer tick).
  • return_info -- If True, the switch controller will pass the parameters of the switch handler as arguments to the callback, including switch_name, state, and ms. If False (default), it just calls the callback with no parameters.
  • callback_kwargs -- Additional kwargs that will be passed with the callback.

You can mix & match entries for the same switch here.

static get_active_event_for_switch(switch_name)

Return the event name which is posted when switch_name becomes active.

get_next_timed_switch_event()

Return time of the next timed switch event.

is_active(switch_name, ms=None)

Query whether a switch is active.

Returns True if the current switch is active. If optional arg ms is passed, will only return true if switch has been active for that many ms.

Note this method does consider whether a switch is NO or NC. So an NC switch will show as active if it is open, rather than closed.

is_inactive(switch_name, ms=None)

Query whether a switch is inactive.

Returns True if the current switch is inactive. If optional arg ms is passed, will only return true if switch has been inactive for that many ms.

Note this method does consider whether a switch is NO or NC. So an NC switch will show as active if it is closed, rather than open.

is_state(switch_name, state, ms=0)

Check if switch is in state.

Query whether a switch is in a given state and (optionally) whether it has been in that state for the specified number of ms.

Returns True if the switch_name has been in the state for the given number of ms. If ms is not specified, returns True if the switch is in the state regardless of how long it's been in that state.

log_active_switches(**kwargs)

Write out entries to the log file of all switches that are currently active.

This is used to set the "initial" switch states of standalone testing tools, like our log file playback utility, but it might be useful in other scenarios when weird things are happening.

This method dumps these events with logging level "INFO."

ms_since_change(switch_name)

Return the number of ms that have elapsed since this switch last changed state.

process_switch(name, state=1, logical=False)

Process a new switch state change for a switch by name.

Parameters:
  • name -- The string name of the switch.
  • state -- Boolean or int of state of the switch you're processing, True/1 is active, False/0 is inactive.
  • logical -- Boolean which specifies whether the 'state' argument represents the "physical" or "logical" state of the switch. If True, a 1 means this switch is active and a 0 means it's inactive, regardless of the NC/NO configuration of the switch. If False, then the state paramenter passed will be inverted if the switch is configured to be an 'NC' type. Typically the hardware will send switch states in their raw (logical=False) states, but other interfaces like the keyboard and OSC will use logical=True.

This is the method that is called by the platform driver whenever a switch changes state. It's also used by the "other" modules that activate switches, including the keyboard and OSC interfaces.

State 0 means the switch changed from active to inactive, and 1 means it changed from inactive to active. (The hardware & platform code handles NC versus NO switches and translates them to 'active' versus 'inactive'.)

process_switch_by_num(num, state, platform, logical=False)

Process a switch state change by switch number.

process_switch_obj(obj: mpf.devices.switch.Switch, state, logical)

Process a new switch state change for a switch by name.

Parameters:
  • obj -- The switch object.
  • state -- Boolean or int of state of the switch you're processing, True/1 is active, False/0 is inactive.
  • logical -- Boolean which specifies whether the 'state' argument represents the "physical" or "logical" state of the switch. If True, a 1 means this switch is active and a 0 means it's inactive, regardless of the NC/NO configuration of the switch. If False, then the state paramenter passed will be inverted if the switch is configured to be an 'NC' type. Typically the hardware will send switch states in their raw (logical=False) states, but other interfaces like the keyboard and OSC will use logical=True.

This is the method that is called by the platform driver whenever a switch changes state. It's also used by the "other" modules that activate switches, including the keyboard and OSC interfaces.

State 0 means the switch changed from active to inactive, and 1 means it changed from inactive to active. (The hardware & platform code handles NC versus NO switches and translates them to 'active' versus 'inactive'.)

register_switch(name)

Populate self.registered_switches.

Parameters:name -- Name of switch
remove_monitor(monitor)

Remove a monitor callback.

remove_switch_handler(switch_name, callback, state=1, ms=0)

Remove a registered switch handler.

Currently this only works if you specify everything exactly as you set it up. (Except for return_info, which doesn't matter if true or false, it will remove either / both.

remove_switch_handler_by_key(switch_handler: mpf.core.switch_controller.SwitchHandler)

Remove switch hander by key returned from add_switch_handler.

set_state(switch_name, state=1, reset_time=False)

Set the state of a switch.

update_switches_from_hw()

Update the states of all the switches be re-reading the states from the hardware platform.

This method works silently and does not post any events if any switches changed state.

verify_switches() → bool

Verify that switches states match the hardware.

Loop through all the switches and queries their hardware states via their platform interfaces and them compares that to the state that MPF thinks the switches are in.

Throws logging warnings if anything doesn't match.

This method is notification only. It doesn't fix anything.

wait_for_any_switch(switch_names: [<class 'str'>], state: int = 1, only_on_change=True, ms=0)

Wait for the first switch in the list to change into state.

wait_for_switch(switch_name: str, state: int = 1, only_on_change=True, ms=0)

Wait for a switch to change into state.