aboutsummaryrefslogtreecommitdiff
path: root/host/matelight/queuemgr.py
diff options
context:
space:
mode:
authorjaseg <jaseg@jaseg.net>2013-12-18 18:22:00 +0100
committerjaseg <jaseg@jaseg.net>2013-12-18 18:22:00 +0100
commit93592ee43978005a86e0d83486c7b866e2c1b61f (patch)
tree90d0e090eb62418635081370eb3b83c132ad56ba /host/matelight/queuemgr.py
parent037b3fc1d453e60a3cca0cf0775f6397bd7337cb (diff)
downloadmatelight-93592ee43978005a86e0d83486c7b866e2c1b61f.tar.gz
matelight-93592ee43978005a86e0d83486c7b866e2c1b61f.tar.bz2
matelight-93592ee43978005a86e0d83486c7b866e2c1b61f.zip
Moving on with the host program
Diffstat (limited to 'host/matelight/queuemgr.py')
-rw-r--r--host/matelight/queuemgr.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/host/matelight/queuemgr.py b/host/matelight/queuemgr.py
new file mode 100644
index 0000000..df75309
--- /dev/null
+++ b/host/matelight/queuemgr.py
@@ -0,0 +1,52 @@
+#!/usr/bin/env python3
+
+from renderers import TextRenderer, ImageRenderer
+import host, config
+import time, math
+
+score = lambda now, last, lifetime, priority, item: priority*math.log(now-last)/(10.0+item.duration)
+
+class FuzzyQueue:
+ def __init__(self, default):
+ self._default = default
+ self.put(default, 0.0, 0)
+ self._l = []
+
+ def put(self, item, priority=1.0, lifetime=0):
+ lifetime += time.time()
+ self._l.append((0, lifetime, priority, item))
+
+ def pop(self):
+ """ Get an item from the queue
+
+ NOTE: This is *not* a regular pop, as it does not necessarily remove the item from the queue.
+ """
+ now = time.time()
+ # Choose item based on last used and priority
+ _, index, (_, lifetime, priority, item) = max(sorted([(score(now, *v), i, v) for i, v in self._l]))
+ # If item's lifetime is exceeded, remove
+ if lifetime < now and item is not self._default:
+ del self._l[index]
+ # Otherwise, set item's last played time
+ self._l[index] = (now, lifetime, prioity, item)
+ # Finally, return
+ return item
+
+q = FuzzyQueue()
+
+def insert_text(text, priority=1.0, lifetime=0, escapes=True):
+ q.put(TextRenderer(text, escapes), priority, lifetime)
+
+def insert_image(image, priority=1.0, lifetime=0):
+ q.put(ImageRenderer(image), priority, lifetime)
+
+def render_thread():
+ while True:
+ start = time.time()
+ for frame, delay in q.pop().frames(start):
+ then = time.time()
+ if then-start+delay > RENDERER_TIMEOUT:
+ break
+ sendframe(frame)
+ now = time.time()
+ time.sleep(min(RENDERER_TIMEOUT, delay - (now-then)))