1 from libtuer import ThreadFunction, logger
2 import RPi.GPIO as GPIO
15 def __init__(self, name, pin, tid, todo, verbose = True):
20 self.verbose = verbose
21 # don't do the GPIO setup here, the main init did not yet run
25 logger.info("Actor: Running command %s" % self.name)
27 logger.debug("Actor: Running command %s" % self.name)
28 for (value, delay) in self.todo:
30 logger.debug("Actor: Setting pin %d to %d" % (self.pin, value))
31 GPIO.output(self.pin, value)
36 CMD_UNLOCK: CMD("unlock", pin=12, tid=0, todo=[(True, 0.3), (False, 0.1)]),
37 CMD_LOCK: CMD("lock", pin=16, tid=0, todo=[(True, 0.3), (False, 0.1)]),
38 CMD_BUZZ: CMD("buzz", pin=22, tid=1, todo=[(True, 2.5), (False, 0.1)]),
39 CMD_GREEN_ON: CMD("green on", pin=23, tid=2, verbose=False, todo=[(True, 0)]),
40 CMD_GREEN_OFF: CMD("green off", pin=23, tid=2, verbose=False, todo=[(False, 0)]),
41 CMD_RED_ON: CMD("red on", pin=26, tid=2, verbose=False, todo=[(True, 0)]),
42 CMD_RED_OFF: CMD("red off", pin=26, tid=2, verbose=False, todo=[(False, 0)]),
46 # launch threads, all running the "_execute" method
48 for cmd in Actor.CMDs.values():
49 GPIO.setup(cmd.pin, GPIO.OUT)
50 GPIO.output(cmd.pin, False)
51 if not cmd.tid in self.threads:
52 self.threads[cmd.tid] = ThreadFunction(self._execute, "Actor TID %d" % cmd.tid)
54 def _execute(self, cmd):
55 Actor.CMDs[cmd].execute()
58 # dispatch command to correct thread
59 self.threads[Actor.CMDs[cmd].tid](cmd)
62 for thread in self.threads.values():