Events with the mainloop()

We’re testing the mainloop() here.

We register an event on the “Input”. With the piCtory configuration below, ALL inputs are summarized as bytes(). If one of the inputs changes, the function “event function” is called. In this function, all inputs are mirrored to the outputs and the time is output in the console.

Source code for RevPiModIO Version 2

#!/usr/bin/python3
import revpimodio2
import time
class RevPiModIOTest():
"""Small test class for the mainloop().
When the instantiation of the class calls the start() function
, we go into the mainloop(). The program then waits for events.
Ctrl + C will leave the mainloop() cleanly.
"""
def __init__(self):
"""Invoked during instantiation."""
# RevPiModIO instantiating and set the Module in the autorefresh, so
# the process image is automatically synchronized.
self.revpi = revpimodio2.RevPiModIO(autorefresh=True)
# Handle exit signal (Ctrl+C / SIGTERM) and exit the Loop.
# You can pass a function to cleanup your RevPi (switch LEDs
# off - it is the last read / write on process image before exit.
self.revpi.handlesignalend(self.exitfunktion)
# Register a function in event handling for Input, which is executed,
# when the Input value changes.
self.revpi.io.Input.reg_event(self.eventfunktion)
def eventfunktion(self, ioname, iovalue):
"""Only executed if an input pin changes it's value.
@param ioname: Is passed automatically and contains the IO name
@param iovlaue: Value the IO has at the time of triggering"""
# Inputs are mirrored to outputs and a screen is Output.
# Only if the event is triggered!
self.revpi.io.Output.value = iovalue
print(time.time(), ioname, iovalue)
def exitfunktion(self):
"""Diese Funktion erledigt Aufraumarbeiten vor Programmende."""
# Turn off LED A1 on the Core
self.revpi.core.A1 = revpimodio2.OFF
def start(self):
"""Here the actual program runs in the endless loop."""
# Set LED A1 at Core to green
self.revpi.core.A1 = 1
# Go into the mainloop and wait for events
print("Go in to the mainloop()")
self.revpi.mainloop()
if __name__ == "__main__":
root = RevPiModIOTest()
root.start()

Source code for RevPiModIO Version 1

#!/usr/bin/python3
import revpimodio
import signal
import time
class RevPiModIOTest():
"""Small test class for the mainloop().
When the instantiation of the class calls the start() function
, we go into the mainloop(). The program then waits for events.
Ctrl + C will leave the mainloop() cleanly.
"""
def __init__(self):
"""Invoked during instantiation."""
# RevPiModIO instantiating and set the Module in the auto_refresh, so
# the process image is automatically synchronized.
self.revpi = revpimodio.RevPiModIO(auto_refresh=True)
# Register a function in event handling for Input, which is executed,
# when the Input value changes.
self.revpi.devices[32].reg_event("Input", self.eventfunktion)
# Signal events
signal.signal(signal.SIGINT, self._sigexit)
signal.signal(signal.SIGTERM, self._sigexit)
def _sigexit(self, signum, frame):
"""Signal handler to exit."""
# Quit the mainloop and give back the control
self.revpi.devices.exit()
def eventfunktion(self, ioname, iovalue):
"""Only executed if an input pin changes it's value.
@param ioname: Is passed automatically and contains the IO name
@param iovlaue: Value the IO has at the time of triggering"""
# Inputs are mirrored to outputs and a screen is Output.
# Only if the event is triggered!
self.revpi.devices[32]["Output"].value = iovalue
print(time.time(), ioname, iovalue)
def start(self):
"""Here the actual program runs in the endless loop."""
# Set LED A1 at Core to green
self.revpi.devices.core.A1 = 1
# Go into the mainloop and wait for events
print("Go in to the mainloop()")
self.revpi.devices.mainloop()
# Turn off LED A1 if the the mainloop() is quitted
self.revpi.devices.core.A1 = 0
self.revpi.devices.writeprocimg()
if __name__ == "__main__":
root = RevPiModIOTest()
root.start()

The matching piCtory configuration looks like this. Make sure that the DIO inputs are set to 19 and the outputs are set to 17! We need this so that we can mirror ALL inputs to the outputs with a line of code.