source: rgbkbd/trunk/rgbkbd/modes/commandmode.py @ 71

Last change on this file since 71 was 71, checked in by retracile, 9 years ago

Initial import of source code

  • Property svn:executable set to *
File size: 7.0 KB
Line 
1#!/usr/bin/python
2import math
3
4from rgbkbd.core import StaticLighting, KeyboardMode
5from rgbkbd.color import Color, Colors
6from rgbkbd.geometry import Keys
7from rgbkbd.animation import \
8    AlternatingColorPattern, \
9    BeatColorPattern, \
10    ThrobColorPattern, \
11    PulseColorPattern, \
12    ColorAnimation
13
14# Keyboard Modes
15from rgbkbd.modes.basicmodes import StaticMode, RandomMode, ColorMode
16from rgbkbd.modes.typingmode import TypingMode
17from rgbkbd.modes.secretnotesmode import SecretNotesMode
18from rgbkbd.modes.anglemotionmode import AngleMotionMode
19
20
21class CommandMode(KeyboardMode):
22    """Keyboard mode for selecting a keyboard mode"""
23    _color_modes = [
24        ("grave", Colors.BLACK),
25        ("1", Colors.DARK_GREY),
26        ("2", Colors.GREY),
27        ("3", Colors.WHITE),
28        ("4", Colors.RED),
29        ("5", Colors.PURPLE),
30        ("6", Colors.BLUE),
31        ("7", Colors.CYAN),
32        ("8", Colors.GREEN),
33        ("9", Colors.YELLOW),
34        ("0", Colors.ORANGE),
35    ]
36    _color_mode_keys = [k for k, _c in _color_modes]
37    _color_mode_by_key = dict(_color_modes)
38
39    def __init__(self, manager, keyboard):
40        static_key_lighting = [
41            (Keys.ALL, Colors.BLACK), # black out everything not otherwise set
42            ("light,lock", Colors.WHITE), # exit command mode
43            ("scroll", Colors.GREEN), # discrete writing mode
44
45            ("prtscn", Colors.WHITE), # typing mode
46        ] + self._color_modes
47
48        static = StaticLighting(keyboard, profile=static_key_lighting)
49        animations = [
50            # Key for random pattern of lit keys
51            ColorAnimation(keys="home", keyboard=keyboard,
52                color_pattern=AlternatingColorPattern(colors=[Color.random8() for n in range(50)], period=0.8*50)),
53
54            # Animation control panel
55            ColorAnimation(keys="stop", keyboard=keyboard,
56                color_pattern=AlternatingColorPattern(colors=[Colors.WHITE, Colors.BLACK], period=3)),
57            ColorAnimation(keys="prev", keyboard=keyboard,
58                color_pattern=PulseColorPattern(colors=[Colors.WHITE, Colors.BLACK], period=3)),
59            ColorAnimation(keys="play", keyboard=keyboard,
60                color_pattern=ThrobColorPattern(colors=[Colors.WHITE, Colors.BLACK], period=3)),
61            ColorAnimation(keys="next", keyboard=keyboard,
62                color_pattern=BeatColorPattern(colors=[Colors.WHITE, Colors.BLACK], period=3)),
63
64            # Direction control
65            ColorAnimation(keys="num5", keyboard=keyboard,
66                color_pattern=ThrobColorPattern(colors=[Colors.BLACK, Colors.WHITE], period=3)),
67            ColorAnimation(keys="num1,num2,num3,num4,num6,num7,num8,num9",
68                keyboard=keyboard,
69                color_pattern=ThrobColorPattern(colors=[Colors.WHITE, Colors.BLACK], period=3)),
70        ]
71        super(CommandMode, self).__init__(manager, keyboard, static, animations)
72        self.previous_mode = None
73
74    def start(self, previous_mode=None):
75        """Initialize the keyboard for command mode"""
76        #print "Starting command mode" # DEBUG
77        super(CommandMode, self).start()
78        self.previous_mode = previous_mode
79        self.keyboard.unbind()
80
81    def chord_event(self, chord):
82        """Handle chords as commands"""
83        if chord == ["light"]:
84            # Allow turning off the lights
85            self.lights_on = not self.lights_on
86            self.keyboard.lights(self.lights_on)
87        elif sorted(chord) == ['light', 'lock']:
88            # exit command mode back to the previous mode
89            #print "Exiting command mode" # DEBUG
90            self.keyboard.rebind()
91            self.manager.mode_start(self.previous_mode)
92        # There are a few keyboard modes that don't take or need color modifiers
93        elif chord == ["scroll"]: # SecretNotes mode
94            self.manager.mode_start(SecretNotesMode(manager=self.manager, keyboard=self.keyboard))
95        elif chord == ["prtscn"]: # Typing speed mode
96            self.manager.mode_start(TypingMode(manager=self.manager, keyboard=self.keyboard))
97        elif chord == ["home"]: # Random mode with random color selection
98            self.manager.mode_start(RandomMode(manager=self.manager, keyboard=self.keyboard))
99        # Modes that support color modifiers
100        elif set(self._color_mode_keys).intersection(chord):
101            # They hit at least one of the color mode keys
102            directions = {
103                "num6": (0.0  * math.pi, 50),
104                "num9": (0.25 * math.pi, 30),
105                "num8": (0.5  * math.pi, 10),
106                "num7": (0.75 * math.pi, 30),
107                "num4": (1.0  * math.pi, 50),
108                "num1": (1.25 * math.pi, 30),
109                "num2": (1.5  * math.pi, 10),
110                "num3": (1.75 * math.pi, 30),
111                "num5": (None, None), # No motion; might want it to be a circular pattern?
112            }
113            mode_types = {
114                "home": RandomMode,
115            }
116            color_pattern_types = {
117                "stop": AlternatingColorPattern,
118                "prev": PulseColorPattern,
119                "play": ThrobColorPattern,
120                "next": BeatColorPattern,
121            }
122            direction = None
123            mode_type = StaticMode
124            colors = []
125            color_pattern = None
126            for key in chord:
127                if key in mode_types.keys():
128                    mode_type = mode_types[key]
129                elif key in self._color_mode_keys:
130                    colors.append(self._color_mode_by_key[key])
131                elif key in directions.keys():
132                    # Just take the last direction
133                    direction, size = directions[key]
134                elif key in color_pattern_types.keys():
135                    # Just take the last pattern
136                    color_pattern = color_pattern_types[key]
137                else:
138                    # ERROR: an unrecognized key was pressed
139                    return
140            foreground = colors[0]
141            if len(colors) > 1:
142                background = colors[1]
143            else:
144                colors.append(Colors.BLACK)
145                background = Colors.BLACK
146
147            self.keyboard.rebind()
148
149            if color_pattern is None:
150                #print "No color pattern"
151                mode = mode_type(manager=self.manager, keyboard=self.keyboard,
152                    foreground=foreground, background=background)
153            else:
154                if direction is None:
155                    #print "No direction" # DEBUG
156                    mode = ColorMode(manager=self.manager,
157                        keyboard=self.keyboard, colors=colors, period=15,
158                        color_pattern_cls=color_pattern)
159                else:
160                    #print "Direction is %s" % direction # DEBUG
161                    mode = AngleMotionMode(manager=self.manager,
162                        keyboard=self.keyboard, colors=colors, period=15,
163                        color_pattern_cls=color_pattern, angle=direction, size=size*len(colors)/2)
164
165            self.manager.mode_start(mode)
166        # Ignore unrecognized chord events
Note: See TracBrowser for help on using the repository browser.