GPIO: Trigger, Simulate & Write
Overview
This example demonstrates how to:
- Select a GPIO device to listen for triggers or send commands to.
- React to incoming triggers by taking a specific Page on-air.
- Simulate trigger events for testing.
- Write Boolean values to GPIO outputs.
When to Use
- Linking external GPIO inputs (e.g., physical buttons, sensors) to Studio actions.
- Sending control signals to GPIO outputs (e.g., relays, tally lights).
- Quickly emulating hardware signals during development or troubleshooting.
Requirements
- At least one GPIO device is configured in Studio → Settings → GPIO.
- Know how you want to address the device:
- Use the GPIO device name, exactly as it is configured in Settings → GPIO, when you want to receive
triggerevents. Incoming trigger events are routed by name. - A numeric index (0, 1, 2, …) may be used for
simulate()andwrite(), which are resolved on the server. - Index
0, or an empty value, also receivestriggerevents, because an event whose device name is not registered falls back to the device object stored under key0.
- Use the GPIO device name, exactly as it is configured in Settings → GPIO, when you want to receive
- Understand your hardware channel numbering (which input/output corresponds to which number).
Warning
An incoming trigger event is looked up by the configured device name. When that lookup misses, the event is delivered to the device object stored under the key 0 instead. Two consequences:
- Subscribing with an index other than
0- for exampleGpio.device(1).on('trigger', …)- never fires, because the object is stored under the key1and nothing ever looks it up. - A subscription made with
Gpio.device(0)orGpio.device()receives the triggers of every GPIO device. Checke.deviceif more than one device is configured.
Subscribe with the device name to receive the triggers of one specific device.
UI Elements
Device - Input Number
The GPIO device to be listened to or written to. In the example UI this is a Number input, so it addresses the device by index; replace it with a Text input holding the configured GPIO name if you need trigger events from a device other than the first one. If left empty, the first available GPIO device is used.
On Trigger:
- Trigger - Input Number (
num_GpioInput): the GPIO input channel to monitor for triggers, and the channel the Simulate button fires. - Page - Input Number (
triggerPage): page number to take on-air when the trigger is received. - Simulate - Button: triggers the specified input channel programmatically for testing.
Write:
- Input Number (
num_GpioInput1): the GPIO output channel to write to. - Switch Control (
writeValue): sets the Boolean value (True/False). - Write - Button (
simulate1): sends the selected Boolean value to the specified output channel.
The trigger event
A trigger event carries:
1 2 3 4 5 6 | |
The subscription is per device, not per input, so the handler runs for every input on that device. Compare e.args.input with the channel you care about, as the sample below does.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
Info
Api.profile.program.takeIn(page) resolves the page number against the currently open playlist only, and the comparison is numeric, so '5' and '0005' are equivalent. If no page with that number is in the open playlist, nothing happens and no error is raised.
Other Gpio API calls
Gpio.device(name).once(event, callback)and.off(event, callback?)manage subscriptions the same way ason.Gpio.simulate(device, input)andGpio.write(device, input, value)can be called at module level, without first obtaining a device object. Both are asynchronous and throw when the device cannot be found on the server.
