HowTo: Events und eigene zyklische Aufgaben

Moderator: RevPiModIO

Post Reply
User avatar
RevPiModIO
KUNBUS
Posts: 322
Joined: 20 Jan 2017, 08:44
Answers: 0
Contact:

HowTo: Events und eigene zyklische Aufgaben

Post by RevPiModIO »

Wir erhalten immer mal wieder die Frage, ob man das Eventsystem und zyklische Aufgaben erledigen kann. Die Antwort ist JA!

Die häufigste und einfachste Aufgabe ist das Eventsystem zu verwenden und eine Status LED am Core blinken zu lassen (Signalisiert: Programm läuft). Dies haben wir in unserem Beispiel aufgenommen (neben einer Eventbasierten FlipFlop-Schaltung). Natürlich können auf diese Art noch ganz andere Dinge im While-Loop erledigt werden.

Genaue Beschreibung inkl. piCtory hier: https://revpimodio.org/events-und-zyklisch/

Code: Select all

#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# (c) Sven Sager, License: GPLv3
#
"""Combined events with mainloop() and own cyclic functions.

Let the LED A1 blink green during program is running. Switch on output O_1
if I_1 goes to True and switch off output O_1 if I_1 goes to True again.

piCtory Setup: RevPICore | DIO

"""
import revpimodio2


class MyRevPiApp():

    """Mainapp for RevPi."""

    def __init__(self):
        """Init MyRevPiApp class."""

        # Instantiate RevPiModIO
        self.rpi = revpimodio2.RevPiModIO(autorefresh=True)

        # Handle SIGINT / SIGTERM to exit program cleanly
        self.rpi.handlesignalend(self.cleanup_revpi)

        # Register event to toggle output O_1 with input I_1
        self.rpi.io.I_1.reg_event(self.event_flipflop_o1, edge=revpimodio2.RISING)

    def cleanup_revpi(self):
        """Cleanup function to leave the RevPi in a defined state."""

        # Switch of LED and outputs before exit program
        self.rpi.core.a1green.value = False
        self.rpi.io.O_1.value = False

    def event_flipflop_o1(self, ioname, iovalue):
        """Called if I_1 goes to True."""

        # Switch on/off output O_1
        self.rpi.io.O_1.value = not self.rpi.io.O_1.value

    def start(self):
        """Start event system and own cyclic loop."""

        # Start event system without blocking here
        self.rpi.mainloop(blocking=False)

        # My own loop to do some work next to the event system. We will stay
        # here till self.rpi.exitsignal.wait returns True after SIGINT/SIGTERM
        while not self.rpi.exitsignal.wait(0.5):

            # Switch on / off green part of LED A1 | or do other things
            self.rpi.core.a1green.value = not self.rpi.core.a1green.value


if __name__ == "__main__":
    # Start RevPiApp app
    root = MyRevPiApp()
    root.start()
Gruß, Sven
python3-RevPiModIO - https://revpimodio.org/ || Der RevPi ist das Beste, was passieren konnte!
Post Reply