-
Notifications
You must be signed in to change notification settings - Fork 4
/
ConfigurableButtonElement.py
62 lines (48 loc) · 1.93 KB
/
ConfigurableButtonElement.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import Live
from _Framework.ButtonElement import *
class ConfigurableButtonElement(ButtonElement):
""" Special button class that can be configured with custom on- and off-values """
def __init__(self, is_momentary, msg_type, channel, identifier):
ButtonElement.__init__(self, is_momentary, msg_type, channel, identifier)
self._on_value = 127
self._off_value = 4
self._is_enabled = True
self._is_notifying = False
self._force_next_value = False
self._pending_listeners = []
def set_on_off_values(self, on_value, off_value):
assert (on_value in range(128))
assert (off_value in range(128))
self._last_sent_value = -1
self._on_value = on_value
self._off_value = off_value
def set_force_next_value(self):
self._force_next_value = True
def set_enabled(self, enabled):
self._is_enabled = enabled
def turn_on(self):
self.send_value(self._on_value)
def turn_off(self):
self.send_value(self._off_value)
def reset(self):
self.send_value(4)
def add_value_listener(self, callback, identify_sender = False):
if not self._is_notifying:
ButtonElement.add_value_listener(self, callback, identify_sender)
else:
self._pending_listeners.append((callback, identify_sender))
def receive_value(self, value):
self._is_notifying = True
ButtonElement.receive_value(self, value)
self._is_notifying = False
for listener in self._pending_listeners:
self.add_value_listener(listener[0], listener[1])
self._pending_listeners = []
def send_value(self, value, force = False):
ButtonElement.send_value(self, value, force or self._force_next_value)
self._force_next_value = False
def install_connections(self):
if self._is_enabled:
ButtonElement.install_connections(self)
elif self._msg_channel != self._original_channel or self._msg_identifier != self._original_identifier:
self._install_translation(self._msg_type, self._original_identifier, self._original_channel, self._msg_identifier, self._msg_channel)