- add simple lcd support
[enigma2.git] / timer.py
1 import bisect
2 from time import *
3 from enigma import *
4
5 class TimerEntry:
6         EventPrepare = 0
7         EventStart   = 1
8         EventEnd     = 2
9         EventAbort   = 3
10         
11         StateWait    = 0
12         StatePrepare = 1
13         StateRunning = 2
14         StateEnded   = 3
15         
16         def __init__(self, begin, end):
17                 self.begin = begin
18                 self.prepare_time = 10
19                 self.end = end
20                 self.state = 0
21         
22         def getTime(self):
23                 if self.state == 0:
24                         return self.begin - self.prepare_time
25                 elif self.state == 1:
26                         return self.begin
27                 else:
28                         return self.end 
29         
30         def __lt__(self, o):
31                 return self.getTime() < o.getTime()
32         
33         def activate(self, event):
34                 print "timer %s got activated (%d)!" % (self.description, event)
35
36 class Timer:
37
38         MaxWaitTime = 100
39
40         def __init__(self):
41                 self.timer_list = [ ]
42                 self.processed_timers = [ ]
43                 
44                 self.timer = eTimer()
45                 self.timer.timeout.get().append(self.calcNextActivation)
46                 
47                 self.calcNextActivation()
48         
49         def addTimerEntry(self, entry):
50                 bisect.insort(self.timer_list, entry)
51                 self.calcNextActivation()
52         
53         def setNextActivation(self, when):
54                 delay = int((when - time()) * 1000)
55                 print "next activation: %d (in %d seconds)" % (when, delay)
56                 
57                 self.timer.start(delay, 1)
58                 self.next = when
59
60         def calcNextActivation(self):
61                 self.processActivation()
62         
63                 min = int(time()) + self.MaxWaitTime
64                 
65                 # calculate next activation point
66                 if len(self.timer_list):
67                         w = self.timer_list[0].getTime()
68                         if w < min:
69                                 min = w
70                 
71                 self.setNextActivation(min)
72         
73         def doActivate(self, w):
74                 w.activate(w.state)
75                 self.timer_list.remove(w)
76                 w.state += 1
77                 if w.state < TimerEntry.StateEnded:
78                         bisect.insort(self.timer_list, w)
79                 else:
80                         bisect.insort(self.processed_timers, w)
81         
82         def processActivation(self):
83                 t = int(time()) + 1
84                 
85                 # we keep on processing the first entry until it goes into the future.
86                 while len(self.timer_list) and self.timer_list[0].getTime() < t:
87                         self.doActivate(self.timer_list[0])
88
89 #t = Timer()
90 #base = time() + 5
91 #t.addTimerEntry(TimerEntry(base+10, base+20, None, None, "test #1: 10 - 20"))
92 #t.addTimerEntry(TimerEntry(base+10, base+30, None, None, "test #2: 10 - 30"))
93 #t.addTimerEntry(TimerEntry(base+15, base+20, None, None, "test #3: 15 - 20"))
94 #t.addTimerEntry(TimerEntry(base+20, base+35, None, None, "test #4: 20 - 35"))