aboutsummaryrefslogtreecommitdiff
path: root/host/matelight/queuemgr.py
blob: df753097e3dcbe689656f4cebc689b9b64c78b3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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)))