The code below utilizes a touch panel array to quickly create many button watch events. The watch events can be processed by the same callback function by processing the event to determine where the button event came from and what panel was used. That information is passed to another function to update all buttons in the array when an action occurs on another panel.
from mojo import context
context.log.info('Sample Python program')
#Gather the context for your devices
dvVaria = context.devices.get("AMX-11241")
dvVaria2 = context.devices.get("AMX-11242")
#Put the objects in an array
dvTPs = [dvVaria,dvVaria2]
#Group ports and buttons numbers that you would create watches for
ports = [1,2]
transportButtons = [1,3,4]
menuButtons = [5,18,22]
#This dictionary creates a reverse lookup for when a button event returns the name of the device and not the object reference
device_lookup = {
"AMX-11241": dvVaria,
"AMX-11242": dvVaria2
}
#This function updates all other panels with the same state as the panel that originated the press
def setOtherPanels(originatingDevice, port_number, id, value):
for panel in dvTPs:
if panel == originatingDevice:
continue
panel.port[port_number].channel[id].value = value
#This function parses/gathers information from the event including port numbers, originating device object, and id (button/channel number) pressed
#This function could be re-used for all button events
def processButtonEvent(event):
path_parts = event.path.split('/')
port_index = path_parts.index("port") + 1
port_number = int(path_parts[port_index])
originatingDevice = device_lookup.get(event.device,None)
id = int(event.id)
return{
"port_number": port_number,
"id": id,
"originatingDevice": originatingDevice
}
#Function takes in button presses from the transportButtons array of button numbers
def processTransportButtons(event):
#The button is sent to the processButtonEvent function to gather its information and save it to event_info
event_info = processButtonEvent(event)
#Notice all of the values are referenced by the return dictionary reference i.e. event_info[" *dictionary entry* "]
if(event.value):
event_info["originatingDevice"].port[event_info["port_number"]].channel[event_info["id"]].value = True
setOtherPanels(event_info["originatingDevice"],event_info["port_number"],event_info["id"],True)
else:
event_info["originatingDevice"].port[event_info["port_number"]].channel[event_info["id"]].value = False
setOtherPanels(event_info["originatingDevice"],event_info["port_number"],event_info["id"],False)
#Function processes button events from the menuButtons array
#In this exaple the buttons do the same thing as in the processTransportButtons callback
def processMenuButtons(event):
event_info = processButtonEvent(event)
if(event.value):
event_info["originatingDevice"].port[event_info["port_number"]].channel[event_info["id"]].value = True
setOtherPanels(event_info["originatingDevice"],event_info["port_number"],event_info["id"],True)
else:
event_info["originatingDevice"].port[event_info["port_number"]].channel[event_info["id"]].value = False
setOtherPanels(event_info["originatingDevice"],event_info["port_number"],event_info["id"],False)
#The next two for loops create a watch for each button in the transportButtons and menuButtons arrays
#The watches are created for each panel and each port, in total, each 4 line loop creates 12 watches
for panel in dvTPs:
for portnumber in ports:
for channel in transportButtons:
panel.port[portnumber].button[channel].watch(processTransportButtons)
for panel in dvTPs:
for portnumber in ports:
for channel in menuButtons:
panel.port[portnumber].button[channel].watch(processMenuButtons)
# leave this as the last line in the Python script
context.run(globals())
Note: Because this process can create so many buttons quickly and easily, it is important to note that watches cannot be created for a button that does not exist. This will cause an exception in the code that will stop the code running on the controller.