Skip to content

Commit e52fd5a

Browse files
committed
add gif pause filler
1 parent fc4531b commit e52fd5a

1 file changed

Lines changed: 46 additions & 8 deletions

File tree

display_server.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import time
22
import math
3+
import copy
34

45
from collections import namedtuple
56

@@ -113,22 +114,54 @@ def run(self):
113114
self.display_text(text_image, slide_image=True)
114115

115116

117+
class GifPauseFiller(threading.Thread):
118+
119+
def __init__(self, image, controller):
120+
super().__init__()
121+
self.controller = controller
122+
self.image = Image.open(image)
123+
self.stop = False
124+
125+
self.frames = []
126+
self.convert_gif()
127+
128+
def convert_gif(self):
129+
self.image.seek(1)
130+
try:
131+
while True:
132+
data = copy.copy(self.image.resize((self.controller.image_width, self.controller.image_height), Image.ANTIALIAS))
133+
data = data.convert('RGB')
134+
data = np.array(data, dtype=np.uint8)
135+
self.frames.append(data)
136+
self.image.seek(self.image.tell() + 1)
137+
except EOFError:
138+
pass
139+
140+
def run(self):
141+
while True:
142+
for frame in self.frames:
143+
if self.stop:
144+
return
145+
self.controller.display(frame)
146+
147+
116148
class IdleWatcher(threading.Thread):
117149

118-
def __init__(self, idle_time):
150+
def __init__(self, idle_time, pause_filler_class, pause_filler_args):
119151
super().__init__()
120152
self.idle_time = idle_time
153+
self.pause_filler = pause_filler
154+
self.pause_filler_args = pause_filler_args
121155

122156
def run(self):
123-
global pause_filler
124157
while True:
125158
time.sleep(timedelta(seconds=self.idle_time).total_seconds())
126159
time_now = datetime.utcnow()
127160
time_delta = (time_now - last_frame).total_seconds()
128161

129-
if time_delta > self.idle_time and not pause_filler.is_alive():
130-
pause_filler = PauseFiller(args.motd, controller)
131-
pause_filler.start()
162+
if time_delta > self.idle_time and not self.pause_filler.is_alive():
163+
self.pause_filler = self.pause_filler.__class__(*self.pause_filler_args)
164+
self.pause_filler.start()
132165

133166

134167
if __name__ == "__main__":
@@ -141,6 +174,7 @@ def run(self):
141174
parser.add_argument('-p', '--port', type=int, default=1337, help='port to listen on')
142175
parser.add_argument('--idle-time', type=int, default=20, help='max. number of seconds matelight shall idle')
143176
parser.add_argument('--motd', default='Contribute on code.ilexlux.xyz!')
177+
parser.add_argument('--gif', help='Path to gif that shall be used as screensaver')
144178
parser.add_argument('--allowed-host', default='conrol.ilexlux.xyz', help='name of host to accept data from')
145179

146180
args = parser.parse_args()
@@ -151,11 +185,15 @@ def run(self):
151185
server = DataListener(controller, port=args.port, ip=ip_of_allowed_host)
152186
last_frame = datetime.utcnow()
153187

154-
pause_filler = PauseFiller(args.motd, controller)
155-
pause_filler.start()
188+
if args.gif is not None:
189+
pause_filler = GifPauseFiller(args.gif, controller)
190+
idle_watcher = IdleWatcher(args.idle_time, pause_filler, (args.gif, controller))
191+
else:
192+
pause_filler = PauseFiller(args.motd, controller)
193+
idle_watcher = IdleWatcher(args.idle_time, pause_filler, (args.motd, controller))
156194

157-
idle_watcher = IdleWatcher(args.idle_time)
158195
idle_watcher.start()
196+
pause_filler.start()
159197

160198
try:
161199
for _, frame in server:

0 commit comments

Comments
 (0)