From e7768d8b2bc9bd1f31742ebe79069d58f005d7c7 Mon Sep 17 00:00:00 2001 From: Sven Sager Date: Thu, 22 May 2025 11:09:26 +0200 Subject: [PATCH] doc(io): Add IO example program Signed-off-by: Sven Sager --- data/examples/io_controller.py | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 data/examples/io_controller.py diff --git a/data/examples/io_controller.py b/data/examples/io_controller.py new file mode 100644 index 0000000..4dfd564 --- /dev/null +++ b/data/examples/io_controller.py @@ -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()