Files @ 40e7d08e0123
Branch filter:

Location: light9/bin/gyrocontroller

David McClosky
combine_subdict optionally lets you set the name, permanent-ness
#!/usr/bin/env python
# vi: syntax=python

from __future__ import division
import run_local
from light9.Submaster import combine_subdict
from light9 import dmxclient, showconfig

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:
    """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):
        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
            levels = combine_subdict(self.kept_levels)
        else:
            levels = {self.current_sub : self.current_level}

        return levels
    def get_dmx_list(self):
        maxes = self.get_levels_as_sub()
        return maxes.get_dmx_list()
    def send_levels(self):
        levels = self.get_dmx_list()
        dmxclient.outputlevels(levels)

if __name__ == "__main__":
    if 0:
        x = range(20)
        for z in circcycle(x):
            print z