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