Skip to content

Commit

Permalink
Merge e6572f7 into 2a076f5
Browse files Browse the repository at this point in the history
  • Loading branch information
thijstriemstra committed Oct 30, 2020
2 parents 2a076f5 + e6572f7 commit c8ab060
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
16 changes: 8 additions & 8 deletions luma/core/sprite_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
"""
Simplified sprite animation framework.
.. note:: this module is an evolving "work-in-progress" and should be treated
.. note:: This module is an evolving "work-in-progress" and should be treated
as such until such time as this notice disappears.
"""

import time
from time import sleep, perf_counter_ns
from PIL import Image


Expand Down Expand Up @@ -183,7 +183,7 @@ def __init__(self, fps=16.67):
self.last_time = None

def __enter__(self):
self.enter_time = time.monotonic()
self.enter_time = perf_counter_ns()
if not self.start_time:
self.start_time = self.enter_time
self.last_time = self.enter_time
Expand All @@ -198,15 +198,15 @@ def __exit__(self, *args):
it simply exits without blocking.
"""
self.called += 1
self.total_transit_time += time.monotonic() - self.enter_time
self.total_transit_time += perf_counter_ns() - self.enter_time
if self.max_sleep_time >= 0:
elapsed = time.monotonic() - self.last_time
elapsed = perf_counter_ns() - self.last_time
sleep_for = self.max_sleep_time - elapsed

if sleep_for > 0:
time.sleep(sleep_for)
sleep(sleep_for)

self.last_time = time.monotonic()
self.last_time = perf_counter_ns()

def effective_FPS(self):
"""
Expand All @@ -219,7 +219,7 @@ def effective_FPS(self):
"""
if self.start_time is None:
self.start_time = 0
elapsed = time.monotonic() - self.start_time
elapsed = perf_counter_ns() - self.start_time
return self.called / elapsed

def average_transit_time(self):
Expand Down
8 changes: 4 additions & 4 deletions luma/core/virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# Copyright (c) 2017-2020 Richard Hull and contributors
# See LICENSE.rst for details.

import time
from textwrap import TextWrapper
from time import sleep, perf_counter

from PIL import Image, ImageDraw, ImageFont

Expand Down Expand Up @@ -199,11 +199,11 @@ def should_redraw(self):
"""
Only requests a redraw after ``interval`` seconds have elapsed.
"""
return time.monotonic() - self.last_updated > self.interval
return perf_counter() - self.last_updated > self.interval

def paste_into(self, image, xy):
super(snapshot, self).paste_into(image, xy)
self.last_updated = time.monotonic()
self.last_updated = perf_counter()


class terminal(object):
Expand Down Expand Up @@ -373,7 +373,7 @@ def newline(self):

self.flush()
if self.animate:
time.sleep(0.2)
sleep(0.2)

def backspace(self):
"""
Expand Down
20 changes: 10 additions & 10 deletions tests/test_framerate_regulator.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014-18 Richard Hull and contributors
# Copyright (c) 2014-2020 Richard Hull and contributors
# See LICENSE.rst for details.

"""
Tests for the :py:class:`luma.core.sprite_system.framerate_regulator` class.
"""

import time
from time import perf_counter_ns
from luma.core.sprite_system import framerate_regulator


Expand All @@ -16,10 +16,10 @@ def test_init_default():
assert regulator.start_time is None
assert regulator.last_time is None
assert regulator.called == 0
before = time.monotonic()
before = perf_counter_ns()
with regulator:
pass
after = time.monotonic()
after = perf_counter_ns()

assert regulator.max_sleep_time == 1 / 16.67
assert before <= regulator.start_time <= after
Expand All @@ -28,10 +28,10 @@ def test_init_default():

def test_init_unlimited():
regulator = framerate_regulator(fps=0)
before = time.monotonic()
before = perf_counter_ns()
with regulator:
pass
after = time.monotonic()
after = perf_counter_ns()

assert regulator.max_sleep_time == -1
assert before <= regulator.start_time <= after
Expand All @@ -40,10 +40,10 @@ def test_init_unlimited():

def test_init_30fps():
regulator = framerate_regulator(fps=30)
before = time.monotonic()
before = perf_counter_ns()
with regulator:
pass
after = time.monotonic()
after = perf_counter_ns()

assert regulator.max_sleep_time == 1 / 30.00
assert before <= regulator.start_time <= after
Expand All @@ -52,11 +52,11 @@ def test_init_30fps():

def test_sleep():
regulator = framerate_regulator(fps=100.00)
before = time.monotonic()
before = perf_counter_ns()
for _ in range(200):
with regulator:
pass
after = time.monotonic()
after = perf_counter_ns()

assert regulator.called == 200
assert after - before >= 2.0
Expand Down

0 comments on commit c8ab060

Please sign in to comment.