Skip to content

Commit b79095e

Browse files
authored
Merge pull request #1613 from jedgarpark/ableton-macropad
refactored for Macropad library
2 parents 433d85a + 66546e1 commit b79095e

File tree

1 file changed

+32
-46
lines changed

1 file changed

+32
-46
lines changed

Macropad_Ableton/code.py

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,34 @@
44
# In Ableton, choose "Launchpad Mini Mk3" as controller with MacroPad 2040 as in and out
55
# Use empty fifth scene to allow "unlaunching" of tracks with encoder modifier
66
import board
7-
from digitalio import DigitalInOut, Pull
8-
import keypad
7+
from adafruit_macropad import MacroPad
98
import displayio
109
import terminalio
11-
import neopixel
12-
import rotaryio
1310
from adafruit_simplemath import constrain
1411
from adafruit_display_text import label
15-
from adafruit_debouncer import Debouncer
1612
import usb_midi
1713
import adafruit_midi
1814
from adafruit_midi.control_change import ControlChange
1915
from adafruit_midi.note_off import NoteOff
2016
from adafruit_midi.note_on import NoteOn
2117
from adafruit_midi.midi_message import MIDIUnknownEvent
2218

19+
macropad = MacroPad()
20+
2321
TITLE_TEXT = "Live Launcher 2040"
2422
print(TITLE_TEXT)
2523
TRACK_NAMES = ["DRUM", "BASS", "SYNTH"] # Customize these
2624
LIVE_CC_NUMBER = 74 # CC number to send w encoder
2725
FADER_TEXT = "cutoff" # change for intended CC name
2826

29-
27+
# --- MIDI recieve is complex, so not using macropad.midi
3028
midi = adafruit_midi.MIDI(
3129
midi_in=usb_midi.ports[0],
3230
in_channel=(0, 1, 2),
3331
midi_out=usb_midi.ports[1],
3432
out_channel=0
3533
)
3634

37-
# ---Specify key pins---
38-
key_pins = (
39-
board.KEY1, board.KEY2, board.KEY3,
40-
board.KEY4, board.KEY5, board.KEY6,
41-
board.KEY7, board.KEY8, board.KEY9,
42-
board.KEY10, board.KEY11, board.KEY12
43-
)
44-
# ---Create keypad object---
45-
keys = keypad.Keys(key_pins, value_when_pressed=False, pull=True)
4635

4736
# ---Official Launchpad colors---
4837
LP_COLORS = (
@@ -76,17 +65,12 @@
7665
modifier = False # use to add encoder switch modifier to keys for clip mute
7766
MODIFIER_NOTES = [41, 42, 43, 41, 42, 43, 41, 42, 43, 41, 42, 43] # blank row in Live
7867

79-
# ---Encoder setup---
80-
encoder = rotaryio.IncrementalEncoder(board.ENCODER_B, board.ENCODER_A)
81-
sw = DigitalInOut(board.ENCODER_SWITCH)
82-
sw.switch_to_input(pull=Pull.UP)
83-
switch = Debouncer(sw)
8468
last_position = 0 # encoder position state
8569

8670
# ---NeoPixel setup---
8771
BRIGHT = 0.125
8872
DIM = 0.0625
89-
pixels = neopixel.NeoPixel(board.NEOPIXEL, 12, brightness=BRIGHT, auto_write=False)
73+
macropad.pixels.brightness = BRIGHT
9074

9175
# ---Display setup---
9276
display = board.DISPLAY
@@ -97,7 +81,7 @@
9781
FONT = terminalio.FONT
9882
# Draw a title label
9983
title = TITLE_TEXT
100-
title_area = label.Label(FONT, text=title, color=0xFFFFFF,x=6, y=3)
84+
title_area = label.Label(FONT, text=title, color=0xFFFFFF, x=6, y=3)
10185
screen.append(title_area)
10286

10387
# --- create display strings and positions
@@ -139,7 +123,7 @@
139123
group = displayio.Group(max_size=4, x=x, y=y)
140124
group.append(label_area)
141125
screen.append(group)
142-
labels.append(label_area) # these can be individually addressed later
126+
labels.append(label_area) # these are individually addressed later
143127

144128
num = 1
145129

@@ -158,18 +142,22 @@
158142
)
159143
# send neopixel lightup code to key, text to display
160144
if msg_in.note in LP_PADS:
161-
pixels[LP_PADS[msg_in.note]] = LP_COLORS[msg_in.velocity]
162-
pixels.show()
145+
macropad.pixels[LP_PADS[msg_in.note]] = LP_COLORS[msg_in.velocity]
146+
macropad.pixels.show()
163147
if msg_in.velocity == 21: # active pad is indicated by Live as vel 21
164148
labels[LP_PADS[msg_in.note]+3].text = "o"
165149
else:
166150
labels[LP_PADS[msg_in.note]+3].text = "."
167151

168-
elif (
169-
isinstance(msg_in, NoteOff)
170-
or isinstance(msg_in, NoteOn)
171-
and msg_in.velocity == 0
172-
):
152+
elif isinstance(msg_in, NoteOff):
153+
print(
154+
"received NoteOff",
155+
"from channel",
156+
msg_in.channel + 1,
157+
"\n"
158+
)
159+
160+
elif isinstance(msg_in, NoteOn) and msg_in.velocity == 0:
173161
print(
174162
"received NoteOff",
175163
"from channel",
@@ -200,11 +188,11 @@
200188
elif msg_in is not None:
201189
midi.send(msg_in)
202190

203-
event = keys.events.get() # check for keypad events
191+
key_event = macropad.keys.events.get() # check for keypad events
204192

205-
if not event: # Event is None; no keypad event happened, do other stuff
193+
if not key_event: # Event is None; no keypad event happened, do other stuff
206194

207-
position = encoder.position # store encoder position state
195+
position = macropad.encoder # store encoder position state
208196
cc_position = constrain((position + CC_OFFSET), 0, 127) # lock to cc range
209197
if last_position is None or position != last_position:
210198

@@ -219,35 +207,33 @@
219207
cc_val_text_area.text = str(cc_position)
220208
last_position = position
221209

222-
switch.update() # check the encoder switch w debouncer
223-
if switch.fell:
210+
macropad.encoder_switch_debounced.update() # check the encoder switch w debouncer
211+
if macropad.encoder_switch_debounced.pressed:
224212
print("Mod")
225213
push_text_area.text = "[.]"
226214
modifier = True
227-
pixels.brightness = DIM
228-
pixels.show()
215+
macropad.pixels.brightness = DIM
229216

230-
if switch.rose:
217+
if macropad.encoder_switch_debounced.released:
231218
modifier = False
232219
push_text_area.text = "[o]"
233-
pixels.brightness = BRIGHT
234-
pixels.show()
220+
macropad.pixels.brightness = BRIGHT
235221

236222
continue
237223

238-
num = event.key_number
224+
num = key_event.key_number
239225

240-
if event.pressed and not modifier:
226+
if key_event.pressed and not modifier:
241227
midi.send(NoteOn(LIVE_NOTES[num], 127))
242228
print("\nsent note", LIVE_NOTES[num], "\n")
243229

244-
if event.pressed and modifier:
230+
if key_event.pressed and modifier:
245231
midi.send(NoteOn(MODIFIER_NOTES[num], 127))
246232

247-
if event.released and not modifier:
233+
if key_event.released and not modifier:
248234
midi.send(NoteOff(LIVE_NOTES[num], 0))
249235

250-
if event.released and modifier:
236+
if key_event.released and modifier:
251237
midi.send(NoteOff(MODIFIER_NOTES[num], 0))
252238

253-
pixels.show()
239+
macropad.pixels.show()

0 commit comments

Comments
 (0)