From a338e40f16f8e84acfe752a76b820f63fa20a5ce Mon Sep 17 00:00:00 2001 From: rowlap Date: Thu, 31 Oct 2019 00:35:43 +0000 Subject: [PATCH 1/2] Fix tween() default start Take a copy instead of a reference to _values, so that start isn't changing through the loop. --- library/piglow.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/piglow.py b/library/piglow.py index 5795aa4..74062db 100644 --- a/library/piglow.py +++ b/library/piglow.py @@ -248,7 +248,7 @@ def tween(duration, end, start=None): steps = int(duration / fps) if start is None: - start = _values + start = _values.copy() for x in range(steps): new = [] From b8c9956e346d100365916aec0f9d6f1bca34a1c7 Mon Sep 17 00:00:00 2001 From: rowlap Date: Sun, 3 Nov 2019 22:51:26 +0000 Subject: [PATCH 2/2] Optimize tween * Move the floating point division out of the inner loop * Use map() / list comprehensions where possible, in preference to nested loop * Don't call show() explicitly if _set() already did it * Regression test (not committed) shows identical _values being written at each step --- library/piglow.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/library/piglow.py b/library/piglow.py index 74062db..3972e76 100644 --- a/library/piglow.py +++ b/library/piglow.py @@ -1,4 +1,7 @@ +from __future__ import division + import atexit +import operator import time from sys import exit @@ -250,16 +253,17 @@ def tween(duration, end, start=None): if start is None: start = _values.copy() + deltas = map(operator.sub, end, start) + deltas_per_frame = [d / steps for d in deltas] + + new = start for x in range(steps): - new = [] - for y in range(18): - s = start[y] - e = end[y] - c = float(e - s) - b = s + ((c / float(steps)) * (x + 1)) - new.append(int(b)) - _set(0, new) - show() + new = list(map(operator.add, new, deltas_per_frame)) + new_ints = [round(n) for n in new] + _set(0, new_ints) + # avoid double write + if not auto_update: + show() time.sleep(fps)