Cyclic with little python code

This example introduces you to the cyclic programming with RevPiModIO and the cycleloop(...)! The program also uses the handlesignalend(…) function to catch the “end of program” signal and exit the program cleanly.

# -*- coding: utf-8 -*-
import revpimodio2

def cycleprogram(cycletools):
    """This function is automatically executed every IO cycle."""

    # Write the value of the first input to the first output
    rpi.io.O_1.value = rpi.io.I_1.value

    # If second input is True, always start switch-off timer
    if rpi.io.I_2.value:
        # Set off delay for 20 cycles
        cycletools.set_tofc("t_20", 20)

    # Write the value of the switch-off delay timer in the second output
    rpi.io.O_2.value = cycletools.get_tofc("t_20")

    # A2 flash every 10th cycle
    if cycletools.flag10c:
        rpi.core.A2 = revpimodio2.GREEN
    else:
        rpi.core.A2 = revpimodio2.OFF

def programend():
    """This function does cleanup work before the end of the program."""

    # Switch off LED A1 on the Core
    rpi.core.A1 = revpimodio2.OFF

# Instancing RevPiModIO with automatic process image synchronization,
# the configuration is automatically read from piCtory.
rpi = revpimodio2.RevPiModIO(autorefresh=True)

# Manage signals for "end of the program" by RevPiModIO. Pass a cleanup
# function, which is executed at program end (Ctrl + C / SIGTERM).
rpi.handlesignalend(programend)

# Switch on green LED A1 on the Core
rpi.core.A1 = revpimodio2.GREEN

# Start cyclic processing of the "cycleprogram" function.
print("Go into cycleloop")
rpi.cycleloop(cycleprogram)

print("Left cycleloop")

The standard cycle time is 50 milliseconds. It can be changed when calling cycleloop function via rpi.cycleloop(cycleprogram, cycletime=40). If the updating of the IOs including the execution of the cycle function exceeds this time, warnings are issued.

Our piCtory configuration for this example includes a RevolutionPi Core and on the right a DIO module, which is set with Inputs: 34 / Outputs: 32. The names “I_1”, “I_2”, “O_1”, “O_2” must be specified in the “Value Editor”.