doc(io): Add IO example program
Signed-off-by: Sven Sager <s.sager@kunbus.com>
This commit is contained in:
46
data/examples/io_controller.py
Normal file
46
data/examples/io_controller.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from time import time
|
||||
|
||||
from gi.repository import GLib
|
||||
from pydbus import SystemBus
|
||||
|
||||
detected_signal = False
|
||||
timestamp = time()
|
||||
|
||||
bus = SystemBus()
|
||||
bus_revpi = bus.get("com.revolutionpi.ios1", "/com/revolutionpi/ios1")
|
||||
interface = bus_revpi["com.revolutionpi.ios1.IoManager"]
|
||||
|
||||
io_o_1_path = interface.Get("O_1")
|
||||
io_revpiled_path = interface.Get("RevPiLED")
|
||||
|
||||
io_RevPiLED = bus.get("com.revolutionpi.ios1", io_revpiled_path)
|
||||
io_O_1 = bus.get("com.revolutionpi.ios1", io_o_1_path)
|
||||
|
||||
|
||||
def signal_handler(io_name, io_value):
|
||||
global timestamp
|
||||
|
||||
print(f"Signal received: {io_name} = {io_value}")
|
||||
if io_name == "O_1":
|
||||
timestamp = time()
|
||||
io_RevPiLED.Set("com.revolutionpi.ios1.OutInt", "value", GLib.Variant("i", int(io_value)))
|
||||
|
||||
elif io_name == "I_1":
|
||||
io_RevPiLED.Set("com.revolutionpi.ios1.OutInt", "value", GLib.Variant("i", int(io_value)))
|
||||
io_O_1.Set("com.revolutionpi.ios1.OutBool", "value", GLib.Variant("b", not io_value))
|
||||
print(time() - timestamp)
|
||||
|
||||
|
||||
# Start event detection
|
||||
interface.ActivateIoEvents()
|
||||
|
||||
interface.onIoChanged = signal_handler
|
||||
|
||||
try:
|
||||
loop = GLib.MainLoop()
|
||||
loop.run()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
# Stop event detection
|
||||
interface.DeactivateIoEvents()
|
||||
Reference in New Issue
Block a user