fb72df73b268a843843e56c2ab9a8ee5ed652e84
[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         CMD_GREEN_ON  = 3
10         CMD_GREEN_OFF = 4
11         CMD_RED_ON    = 5
12         CMD_RED_OFF   = 6
13         
14         class CMD():
15                 def __init__(self, name, pin, tid, todo, verbose = True):
16                         self.name = name
17                         self.pin = pin
18                         self.tid = tid
19                         self.todo = todo
20                         self.verbose = verbose
21                         # don't do the GPIO setup here, the main init did not yet run
22                 
23                 def execute(self):
24                         if self.verbose:
25                                 logger.info("Actor: Running command %s" % self.name)
26                         else:
27                                 logger.debug("Actor: Running command %s" % self.name)
28                         for (value, delay) in self.todo:
29                                 if value is not None:
30                                         logger.debug("Actor: Setting pin %d to %d" % (self.pin, value))
31                                         GPIO.output(self.pin, value)
32                                 if delay > 0:
33                                         time.sleep(delay)
34         
35         CMDs = {
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)]),
43         }
44         
45         def __init__(self):
46                 # launch threads, all running the "_execute" method
47                 self.threads = {}
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)
53         
54         def _execute(self, cmd):
55                 Actor.CMDs[cmd].execute()
56         
57         def act(self, cmd):
58                 # dispatch command to correct thread
59                 self.threads[Actor.CMDs[cmd].tid](cmd)
60         
61         def stop(self):
62                 for thread in self.threads.values():
63                         thread.stop()