#!/usr/bin/env python # vi: syntax=python import run_local from light9.Submaster import Submasters, combine_subdict from light9.subclient import SubClient class circcycle: """Like itertools.cycle, but with a prev() method too. You lose all iterator benefits by using this, since it will store the whole sequence/iterator in memory. Also, do not run this on an infinite iterator, as it tuple'ifies the input.""" def __init__(self, sequence): self.sequence = tuple(sequence) self.index = 0 def __iter__(self): return self def change_index(self, delta): self.index = (self.index + delta) % len(self.sequence) def next(self): ret = self.sequence[self.index] self.change_index(1) return ret def prev(self): ret = self.sequence[self.index] self.change_index(-1) return ret class AbstractSimpleController(SubClient): """Simple controller with minimal input and output: Input is 4 directions and 3 buttons. Output is an integer and a color and maybe more. Left + B1/right + B1: prev/next sub Y-axis + B2: set current level B3: toggle keep/solo mode Double-B3: clear kept levels""" def __init__(self, subnames): SubClient.__init__(self) self.subnames = subnames self.refresh() def refresh(self): # reload subs from disk self.submasters = Submasters() self.all_subs = circcycle(self.subnames) self.current_sub = self.all_subs.next() self.current_level = 1.0 self.kept_levels = {} # subname : level [0..1] self.keep_solo_mode = 'keep' # either 'keep' or 'solo' def clear_kept_levels(self): self.kept_levels.clear() def next(self): if self.keep_solo_mode == 'keep': self.kept_levels[self.current_sub] = self.current_level self.current_sub = self.submasters.get_sub_by_name(self.all_subs.next()) def prev(self): if self.keep_solo_mode == 'keep': self.kept_levels[self.current_sub] = self.current_level self.current_sub = self.submasters.get_sub_by_name(self.all_subs.prev()) def get_levels_as_sub(self): if self.keep_solo_mode == 'keep': # send all levels in self.kept_levels levelsub = combine_subdict(self.kept_levels) else: levelsub = self.current_sub * self.current_level return levelsub if __name__ == "__main__": if 0: x = range(20) for z in circcycle(x): print z