forked from ffont/push2-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuttons.py
More file actions
78 lines (65 loc) · 4.66 KB
/
Copy pathbuttons.py
File metadata and controls
78 lines (65 loc) · 4.66 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import mido
from .constants import ANIMATION_DEFAULT, MIDO_CONTROLCHANGE, ACTION_BUTTON_PRESSED, ACTION_BUTTON_RELEASED
from .classes import AbstractPush2Section
def get_individual_button_action_name(action_name, button_name):
return '{0} - {1}'.format(action_name, button_name)
class Push2Buttons(AbstractPush2Section):
"""Class to interface with Ableton's Push2 buttons.
See https://github.com/Ableton/push-interface/blob/master/doc/AbletonPush2MIDIDisplayInterface.asc#Buttons
"""
button_map = None
button_names_index = None
button_names_list = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.button_map = {data['Number']: data for data in self.push.push2_map['Parts']['Buttons']}
self.button_names_index = {data['Name']: data['Number'] for data in self.push.push2_map['Parts']['Buttons']}
self.button_names_list = list(self.button_names_index.keys())
@property
def available_names(self):
return self.button_names_list
def button_name_to_button_n(self, button_name):
"""
Gets button number from given button name
See https://github.com/Ableton/push-interface/blob/master/doc/AbletonPush2MIDIDisplayInterface.asc#23-midi-mapping
"""
return self.button_names_index.get(button_name, None)
def set_button_color(self, button_name, color='white', animation=ANIMATION_DEFAULT):
"""Sets the color of the button with given name.
'color' must be a valid RGB or BW color name present in the color palette. See push2_python.constants.DEFAULT_COLOR_PALETTE for default color names.
If the button only acceps BW colors, the color name will be matched against the BW palette, otherwise it will be matched against RGB palette.
'animation' must be a valid animation name from those defined in push2_python.contants.ANIMATION_*. Note that to configure an animation, both the 'start' and 'end'
colors of the animation need to be defined. The 'start' color is defined by setting a color with 'push2_python.contants.ANIMATION_STATIC' (the default).
The second color is set setting a color with whatever ANIMATION_* type is desired.
See https://github.com/Ableton/push-interface/blob/master/doc/AbletonPush2MIDIDisplayInterface.asc#setting-led-colors
"""
button_n = self.button_name_to_button_n(button_name)
if button_n is not None:
button = self.button_map[button_n]
if button['Color']:
color_idx = self.push.get_rgb_color(color)
else:
color_idx = self.push.get_bw_color(color)
msg = mido.Message(MIDO_CONTROLCHANGE, control=button_n, value=color_idx, channel=animation)
self.push.send_midi_to_push(msg)
if self.push.simulator_controller is not None:
self.push.simulator_controller.set_element_color('cc' + str(button_n), color_idx, animation)
def set_all_buttons_color(self, color='white', animation=ANIMATION_DEFAULT):
"""Sets the color of all buttons in Push2 to the given color.
'color' must be a valid RGB or BW color name present in the color palette. See push2_python.constants.DEFAULT_COLOR_PALETTE for default color names.
If the button only acceps BW colors, the color name will be matched against the BW palette, otherwise it will be matched against RGB palette.
'animation' must be a valid animation name from those defined in push2_python.contants.ANIMATION_*. Note that to configure an animation, both the 'start' and 'end'
colors of the animation need to be defined. The 'start' color is defined by setting a color with 'push2_python.contants.ANIMATION_STATIC' (the default).
The second color is set setting a color with whatever ANIMATION_* type is desired.
See https://github.com/Ableton/push-interface/blob/master/doc/AbletonPush2MIDIDisplayInterface.asc#setting-led-colors
"""
for button_name in self.available_names:
self.set_button_color(button_name, color=color, animation=animation)
def on_midi_message(self, message):
if message.type == MIDO_CONTROLCHANGE:
if message.control in self.button_map: # CC number corresponds to one of the buttons
button = self.button_map[message.control]
action = ACTION_BUTTON_PRESSED if message.value == 127 else ACTION_BUTTON_RELEASED
self.push.trigger_action(action, button['Name']) # Trigger generic button action
self.push.trigger_action(get_individual_button_action_name(action, button['Name'])) # Trigger individual button action as well
return True