comparison bin/gyrocontroller @ 246:89cd37c4314b

gyrocontroller and Submaster.combine_subdict gyrocontroller is not tested yet (nor does it have enough interface code for that to be possible)
author David McClosky <dmcc@bigasterisk.com>
date Wed, 15 Jun 2005 01:57:06 +0000
parents
children a92b6d1ac072
comparison
equal deleted inserted replaced
245:feb7910eb6d4 246:89cd37c4314b
1 #!/usr/bin/env python
2 # vi: syntax=python
3
4 from __future__ import division
5 import run_local
6 from light9.Submaster import combine_subdict
7 from light9 import dmxclient, showconfig
8
9 class circcycle:
10 """Like itertools.cycle, but with a prev() method too. You lose
11 all iterator benefits by using this, since it will store the whole
12 sequence/iterator in memory. Also, do not run this on an infinite
13 iterator, as it tuple'ifies the input."""
14 def __init__(self, sequence):
15 self.sequence = tuple(sequence)
16 self.index = 0
17 def __iter__(self):
18 return self
19 def change_index(self, delta):
20 self.index = (self.index + delta) % len(self.sequence)
21 def next(self):
22 ret = self.sequence[self.index]
23 self._change_index(1)
24 return ret
25 def prev(self):
26 ret = self.sequence[self.index]
27 self._change_index(-1)
28 return ret
29
30 class AbstractSimpleController:
31 """Simple controller with minimal input and output:
32
33 Input is 4 directions and 3 buttons.
34 Output is an integer and a color and maybe more.
35
36 Left + B1/right + B1: prev/next sub
37 Y-axis + B2: set current level
38 B3: toggle keep/solo mode
39 Double-B3: clear kept levels"""
40 def __init__(self, subnames):
41 self.subnames = subnames
42 self.refresh()
43 def refresh(self):
44 # reload subs from disk
45 self.submasters = Submasters()
46 self.all_subs = circcycle(self.subnames)
47 self.current_sub = self.all_subs.next()
48 self.current_level = 1.0
49 self.kept_levels = {} # subname : level [0..1]
50 self.keep_solo_mode = 'keep' # either 'keep' or 'solo'
51 def clear_kept_levels(self):
52 self.kept_levels.clear()
53 def next(self):
54 if self.keep_solo_mode == 'keep':
55 self.kept_levels[self.current_sub] = self.current_level
56
57 self.current_sub = self.submasters.get_sub_by_name(self.all_subs.next())
58 def prev(self):
59 if self.keep_solo_mode == 'keep':
60 self.kept_levels[self.current_sub] = self.current_level
61
62 self.current_sub = self.submasters.get_sub_by_name(self.all_subs.prev())
63 def get_levels_as_sub(self):
64 if self.keep_solo_mode == 'keep':
65 # send all levels in self.kept_levels
66 levels = combine_subdict(self.kept_levels)
67 else:
68 levels = {self.current_sub : self.current_level}
69
70 return levels
71 def get_dmx_list(self):
72 maxes = self.get_levels_as_sub()
73 return maxes.get_dmx_list()
74 def send_levels(self):
75 levels = self.get_dmx_list()
76 dmxclient.outputlevels(levels)
77
78 if __name__ == "__main__":
79 if 0:
80 x = range(20)
81 for z in circcycle(x):
82 print z