X-Git-Url: https://git.ralfj.de/saartuer.git/blobdiff_plain/8b136f42bf0c83f8aee52c315f7dbebc371cce1b..82c945d5140bce7c1884c8bc734f3403626bb5f1:/actor.py?ds=inline diff --git a/actor.py b/actor.py index 3abd232..fb72df7 100644 --- a/actor.py +++ b/actor.py @@ -1,32 +1,63 @@ from libtuer import ThreadFunction, logger import RPi.GPIO as GPIO +import time class Actor: - CMD_BUZZ = 0 - CMD_OPEN = 1 - CMD_CLOSE = 2 + CMD_BUZZ = 0 + CMD_UNLOCK = 1 + CMD_LOCK = 2 + CMD_GREEN_ON = 3 + CMD_GREEN_OFF = 4 + CMD_RED_ON = 5 + CMD_RED_OFF = 6 + + class CMD(): + def __init__(self, name, pin, tid, todo, verbose = True): + self.name = name + self.pin = pin + self.tid = tid + self.todo = todo + self.verbose = verbose + # don't do the GPIO setup here, the main init did not yet run + + def execute(self): + if self.verbose: + logger.info("Actor: Running command %s" % self.name) + else: + logger.debug("Actor: Running command %s" % self.name) + for (value, delay) in self.todo: + if value is not None: + logger.debug("Actor: Setting pin %d to %d" % (self.pin, value)) + GPIO.output(self.pin, value) + if delay > 0: + time.sleep(delay) CMDs = { - CMD_BUZZ: ("buzz", 12, [(True, 0.3), (False, 2.0)]), - CMD_OPEN: ("open", 16, [(None, 0.2), (True, 0.3), (False, 1.0)]), - CMD_CLOSE: ("close", 22, [(None, 0.2), (True, 0.3), (False, 1.0)]), + CMD_UNLOCK: CMD("unlock", pin=12, tid=0, todo=[(True, 0.3), (False, 0.1)]), + CMD_LOCK: CMD("lock", pin=16, tid=0, todo=[(True, 0.3), (False, 0.1)]), + CMD_BUZZ: CMD("buzz", pin=22, tid=1, todo=[(True, 2.5), (False, 0.1)]), + CMD_GREEN_ON: CMD("green on", pin=23, tid=2, verbose=False, todo=[(True, 0)]), + CMD_GREEN_OFF: CMD("green off", pin=23, tid=2, verbose=False, todo=[(False, 0)]), + CMD_RED_ON: CMD("red on", pin=26, tid=2, verbose=False, todo=[(True, 0)]), + CMD_RED_OFF: CMD("red off", pin=26, tid=2, verbose=False, todo=[(False, 0)]), } def __init__(self): - self.act = ThreadFunction(self._act) - for (name, pin, todo) in self.CMDs.values(): - GPIO.setup(pin, GPIO.OUT) + # launch threads, all running the "_execute" method + self.threads = {} + for cmd in Actor.CMDs.values(): + GPIO.setup(cmd.pin, GPIO.OUT) + GPIO.output(cmd.pin, False) + if not cmd.tid in self.threads: + self.threads[cmd.tid] = ThreadFunction(self._execute, "Actor TID %d" % cmd.tid) - def _act(self, cmd): - if cmd in self.CMDs: - (name, pin, todo) = self.CMDs[cmd] - logger.info("Actor: Running command %s" % name) - for (value, delay) in todo: - if value is not None: - GPIO.output(pin, value) - time.sleep(delay) - else: - logger.error("Actor: Gut unknown command %d" % cmd) + def _execute(self, cmd): + Actor.CMDs[cmd].execute() + + def act(self, cmd): + # dispatch command to correct thread + self.threads[Actor.CMDs[cmd].tid](cmd) def stop(self): - pass + for thread in self.threads.values(): + thread.stop()