Skip to content

Commit 8afcbcf

Browse files
committed
switch from freeze/resume to paused property
1 parent ea69347 commit 8afcbcf

File tree

1 file changed

+40
-41
lines changed

1 file changed

+40
-41
lines changed

adafruit_led_animation/animation.py

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# The MIT License (MIT)
22
#
33
# Copyright (c) 2019 Kattni Rembor for Adafruit Industries
4+
# Copyright (c) 2019 Roy Hooper
45
#
56
# Permission is hereby granted, free of charge, to any person obtaining a copy
67
# of this software and associated documentation files (the "Software"), to deal
@@ -116,20 +117,24 @@ def show(self):
116117
"""
117118
self.pixel_object.show()
118119

119-
def freeze(self):
120+
@property
121+
def paused(self):
120122
"""
121-
Stops the animation until resumed.
123+
Whether the animation is paused.
122124
"""
123-
self._paused = True
124-
self._time_left_at_pause = max(0, monotonic_ns() - self._next_update)
125+
return self._paused
125126

126-
def resume(self):
127-
"""
128-
Resumes the animation.
129-
"""
130-
self._next_update = monotonic_ns() + self._time_left_at_pause
131-
self._time_left_at_pause = 0
132-
self._paused = False
127+
@paused.setter
128+
def paused(self, value):
129+
if self._paused == value:
130+
return
131+
132+
self._paused = value
133+
if value:
134+
self._time_left_at_pause = max(0, monotonic_ns() - self._next_update)
135+
else:
136+
self._next_update = monotonic_ns() + self._time_left_at_pause
137+
self._time_left_at_pause = 0
133138

134139
def fill(self, color):
135140
"""
@@ -498,28 +503,25 @@ def fill(self, color):
498503
"""
499504
self.current_animation.fill(color)
500505

501-
def freeze(self):
506+
@property
507+
def paused(self):
502508
"""
503-
Freeze the current animation in the sequence.
504-
Also stops auto_advance.
509+
Whether the sequence is paused.
505510
"""
506-
if self._paused:
507-
return
508-
self._paused = True
509-
self._paused_at = monotonic_ns()
510-
self.current_animation.freeze()
511+
return self._paused
511512

512-
def resume(self):
513-
"""
514-
Resume the current animation in the sequence, and resumes auto advance if enabled.
515-
"""
516-
if not self._paused:
513+
@paused.setter
514+
def paused(self, value):
515+
if self._paused == value:
517516
return
518-
self._paused = False
517+
self._paused = value
519518
now = monotonic_ns()
520-
self._last_advance += now - self._paused_at
521-
self._paused_at = 0
522-
self.current_animation.resume()
519+
if value:
520+
self._paused_at = now
521+
else:
522+
self._last_advance += now - self._paused_at
523+
self._paused_at = 0
524+
self.current_animation.paused = value
523525

524526

525527
class AnimationGroup:
@@ -551,10 +553,6 @@ def animate(self):
551553

552554
return any([item.animate() for item in self._members])
553555

554-
def _for_all(self, method, *args, **kwargs):
555-
for item in self._members:
556-
getattr(item, method)(*args, **kwargs)
557-
558556
@property
559557
def color(self):
560558
"""
@@ -571,16 +569,17 @@ def fill(self, color):
571569
"""
572570
Fills all pixel objects in the group with a color.
573571
"""
574-
self._for_all('fill', color)
572+
for member in self._members:
573+
member.fill(color)
575574

576-
def freeze(self):
575+
@property
576+
def paused(self):
577577
"""
578-
Freeze all animations in the group.
578+
Whether the group is paused.
579579
"""
580-
self._for_all('freeze')
580+
return all([member.paused for member in self._members])
581581

582-
def resume(self):
583-
"""
584-
Resume all animations in the group.
585-
"""
586-
self._for_all('resume')
582+
@paused.setter
583+
def paused(self, value):
584+
for member in self._members:
585+
member.paused = value

0 commit comments

Comments
 (0)