Skip to content

Instantly share code, notes, and snippets.

@adammhaile
Last active April 24, 2016 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adammhaile/d54f19fff1b82e8543da9208e455369d to your computer and use it in GitHub Desktop.
Save adammhaile/d54f19fff1b82e8543da9208e455369d to your computer and use it in GitHub Desktop.
from bibliopixel.animation import BaseAnimation
from threading import Lock
import time
class AnimationChooser(BaseAnimation):
def __init__(self, led):
super(AnimationChooser, self).__init__(led)
self.anims = {}
self.curAnim = None
self._internalDelay = 0 # never wait
self.fps = None
self.untilComplete = False
self.anim_choice = None
self.choose_lock = Lock()
# overriding to handle all the animations
def stopThread(self, wait=False):
for a, r in self.anims:
# a bit of a hack. they aren't threaded, but stops them anyway
a._stopEvent.set()
super(AnimationChooser, self).stopThread(wait)
def addAnim(self, name, anim, amt=1, fps=None, max_steps=0, untilComplete=False, max_cycles=0, seconds=None):
a = (
anim,
{
"amt": amt,
"fps": fps,
"max_steps": max_steps,
"untilComplete": untilComplete,
"max_cycles": max_cycles,
"seconds": seconds
}
)
self.anims[name] = a
def preRun(self, amt=1):
if len(self.anims) == 0:
raise Exception("Must provide at least one animation.")
self.animIndex = -1
def run(self, amt=1, fps=None, sleep=None, max_steps=0, untilComplete=False, max_cycles=0, threaded=False, joinThread=False, callback=None, seconds=None):
self.fps = fps
self.untilComplete = untilComplete
super(AnimationChooser, self).run(amt=1, fps=None, sleep=None, max_steps=0, untilComplete=untilComplete,
max_cycles=0, threaded=threaded, joinThread=joinThread, callback=callback, seconds=seconds)
def choose_anim(self, choice):
if choice not in self.anims:
raise ValueError('{} is not a valid animation'.format(choice))
self.choose_lock.acquire()
self.anim_choice = choice
self.curAnim._stopEvent.set()
self.choose_lock.release()
def step(self, amt=1):
if self.anim_choice:
self.curAnim = self.anims[self.anim_choice]
anim, run = self.curAnim
anim._stopEvent.clear()
run.update(threaded=False, joinThread=False, callback=None)
run['fps'] = run.get('fps') or self.fps
anim.run(**(run))
else:
time.sleep(0.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment