Emulating Events in MUSE Code
Frequently Asked Questions
Brand:
- AMX
Models:
- MUSE
- Programming
- Python
Question:
Can you emulate an event in MUSE programming?
Answer:
Yes, you can use helper classes to create event objects in code, and then pass those objects to callback functions as if they were watches or listeners being triggered by the MUSE controller. Below is an example that creates a fake push event from a button. You can use this same idea and pass any information you need to callback functions as test cases or as a way to utilize already completed code.
from mojo import context
context.log.info('Sample Python program')
#This class creates an object that emulates a button object, the only requirement is the value for PUSH/RELEASE, everything else can be used or ignored since they are given initial values
class doPush:
def __init__(self,value,source=None,id=None,path=None,device=None,newValue=None,oldValue=None,normalized=None):
self.value = value
self.source = source
self.id = id
self.path = path
self.device = device
self.newValue = newValue
self.oldValue = oldValue
self.normalized = normalized
#Creates the objects for the controller (idevice), touch panel (dvTP), and CE-REL8 (dvREL8)
dvTP = context.devices.get("AMX-12345")
idevice = context.devices.get("idevice")
#Creates simple variables that access the first relay port on both the idevice and CE-REL8
dvRelay1 = idevice.relay[0]
#This function processes button events for Port 1 Button 1 on the touch panel
#The value of the relay is tied to the state of the button, while the button is pressed the relay will engage, when the button is released it will disengage
def Relay1(event):
context.log.info(f"Dictionary = {event.__dict__}")
dvRelay1.state.value = event.value
#Creates a watch for Port 1, Button 1
dvTP.port[1].button[1].watch(Relay1)
#Callback function for doPush trigger
def DO_PUSH(event):
if(event.value):
#This line calls the callback function used for Relay1, you pass it the doPush object, passing doPush True emulates someone pushing button 1 on the touch panel
Relay1(doPush(True))
else:
#If you need other attributes added besides value, this line shows how you can pass those values into your object
Relay1(doPush(False,source="Code"))
#Creates a watch for a button, just needed some kind of trigger to call the doPush
dvTP.port[1].button[2].watch(DO_PUSH)
# leave this as the last line in the Python script
context.run(globals())