Merge branch 'master' of ralfj.de:saartuer
[saartuer.git] / actor.py
1 from libtuer import ThreadFunction, logger
2 import RPi.GPIO as GPIO
3 import time
4         
5 class Actor:
6         CMD_BUZZ = 0
7         CMD_UNLOCK = 1
8         CMD_LOCK = 2
9         
10         CMDs = {
11                 CMD_UNLOCK:  ("unlock", 12, [(None, 0.2), (True, 0.3), (False, 0.5)]),
12                 CMD_LOCK:  ("lock", 16, [(None, 0.2), (True, 0.3), (False, 0.5)]),
13                 CMD_BUZZ: ("buzz", 22, [(None, 0.2), (True, 2.0), (False, 0.5)]),
14         }
15         
16         def __init__(self):
17                 self.act = ThreadFunction(self._act, name="Actor")
18                 for (name, pin, todo) in self.CMDs.values():
19                         GPIO.setup(pin, GPIO.OUT)
20         
21         def _act(self, cmd):
22                 if cmd in self.CMDs:
23                         (name, pin, todo) = self.CMDs[cmd]
24                         logger.info("Actor: Running command %s" % name)
25                         for (value, delay) in todo:
26                                 if value is not None:
27                                         logger.debug("Setting pin %d to %d" % (pin, value))
28                                         GPIO.output(pin, value)
29                                 time.sleep(delay)
30                 else:
31                         logger.error("Actor: Gut unknown command %d" % cmd)
32         
33         def stop(self):
34                 self.act.stop()