Skip to content

Commit

Permalink
[add] precise fps for screen refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsimonart committed May 13, 2018
1 parent fd1f29c commit e9a109f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 27 deletions.
60 changes: 35 additions & 25 deletions GLM/source/libs/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from .rainbow import color, msg
from .guiviewer import GuiViewer
from os import system
from time import sleep
from time import sleep, process_time
from sys import argv
from threading import Thread

Expand Down Expand Up @@ -35,15 +35,17 @@ def __init__(
matrix=True,
show=False,
guishow=False,
fps=0,
fps=30,
tty='/dev/ttyACM0'):

self._types = ["image.Image", "text.Text", "slide.Slide"]
self.set_fps(fps)
self.image = Image(width=width, height=height)
self.streamer = Stream(matrix=matrix, tty=tty)
self.show = show
self.childs = []
self._types = ["image.Image", "text.Text", "slide.Slide"]
self._image = Image(width=width, height=height)
self.__streamer = Stream(matrix=matrix, tty=tty)
self.__childs = []
self.__prev_fps = 0

if guishow:
self.show_gui()

Expand All @@ -60,9 +62,9 @@ def check_type(self, element):

def set_fps(self, fps):
if fps > 0:
self.fps = 1 / fps
self._fps = 1 / fps
else:
self.fps = 0
self._fps = 0

def add(self, element, x=0, y=0, refresh=False, mode="fill", name="Child"):
"""
Expand All @@ -77,56 +79,64 @@ def add(self, element, x=0, y=0, refresh=False, mode="fill", name="Child"):
name -- name (default "Child")
"""
if self.check_type(element):
self.childs.append(
self.__childs.append(
(element.screen_data(), x, y, refresh, mode, name)
)

def remove(self, id_):
"""Delete a child by his id"""
if id_ <= len(self.childs) - 1:
msg(self.childs.pop(id_)[5], 0, "Removed", level=2)
if id_ <= len(self.__childs) - 1:
msg(self.__childs.pop(id_)[5], 0, "Removed", level=2)
else:
msg(
"no such child",
2,
"Screen.remove()",
len(self.childs),
len(self.__childs),
id_,
level=0
)


def remove_all(self):
"""Remove all childs"""
number_of_childs = len(self.childs)
self.childs = []
number_of_childs = len(self.__childs)
self.__childs = []
msg("Removed %i childs" % number_of_childs, 1, level=2)


def sleep_fps(self):
"""Rather precise (+0.00000x) fps waiter
"""
while (self.__prev_fps + self._fps) > process_time():
pass
self.__prev_fps = process_time()

def refresh(self):
"""
Flatten all childs into one Image and send it to the streamer
and/or print it in the terminal.
"""
self.image.blank()
for child in self.childs:
self.image.paste(child[0], x=child[1], y=child[2], mode=child[4])
self.sleep_fps()

self._image.blank()
for child in self.__childs:
self._image.paste(child[0], x=child[1], y=child[2], mode=child[4])

# Refresh
if child[3]:
child[0].blank()

self.streamer.set_data(self.image)
self.streamer.send_to_serial()
self.__streamer.set_data(self._image)
self.__streamer.send_to_serial()
if self.show:
system('clear')
print(self.streamer)
sleep(self.fps)
print(self.__streamer)

def __str__(self):
count = len(self.childs) - 1
count = len(self.__childs) - 1
string = color("Screen", "green") + "\n"
for n, child in enumerate(self.childs):
for n, child in enumerate(self.__childs):
if n < count:
string += color('├─', 'blue')
else:
Expand All @@ -143,14 +153,14 @@ def __str__(self):
return string

def __getitem__(self, index):
return self.childs[index]
return self.__childs[index]

def show_gui(self):
"""
Instantiates the tkinter gui and gets it running. The gui is updated
from within itself by a function that is run at the end of each
turn of the tkinter mainloop.
"""
gui_thread = Thread(target=lambda: GuiViewer(self.image))
gui_thread = Thread(target=lambda: GuiViewer(self._image))
gui_thread.daemon = True
gui_thread.start()
2 changes: 1 addition & 1 deletion GLM/source/libs/streamtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __repr__(self):

def __bytes__(self):
byte_list = [
int(self.data[i:i+8], 2)for i in range(0, self.length-1, 8)]
int(self.data[i:i+8], 2) for i in range(0, self.length-1, 8)]
return bytes(byte_list)

def set_data(self, data):
Expand Down
2 changes: 1 addition & 1 deletion GLM/source/plugins/clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def _make_layout(self):
{{ title;label;Clock }}"""
self.system_time = datetime.datetime.now()
self.time = Text(self.get_time(), font='fontbignum')
self.screen.set_fps(6)
self.screen.set_fps(1)
self.screen.add(self.time, 2, 3, True)

def _event_loop(self, event):
Expand Down

0 comments on commit e9a109f

Please sign in to comment.