Created
May 10, 2026 14:19
-
-
Save peterc/7f618761f47b1669068ed4c93c9d0eb2 to your computer and use it in GitHub Desktop.
Control the pad LEDs for the Akai MPD218 over USB MIDI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """Reusable LED control for the Akai MPD218 over USB MIDI. | |
| from mpd218 import pad_on, pad_off, all_on, all_off | |
| pad_on(13) # light pad 13 | |
| pad_off(13) | |
| all_on(); all_off() | |
| Pad indices are 1..16 (bottom-left = 1, top-right = 16, matching the device's default | |
| note mapping of pads 1..16 -> MIDI notes 36..51 on channel 10). | |
| """ | |
| import atexit | |
| import mido | |
| PORT_NAME = 'MPD218 Port A' | |
| CHANNEL = 9 # MIDI channel 10 | |
| FIRST_NOTE = 36 # pad 1 | |
| NUM_PADS = 16 | |
| _out = None | |
| def _port(): | |
| global _out | |
| if _out is None: | |
| _out = mido.open_output(PORT_NAME) | |
| atexit.register(_out.close) | |
| return _out | |
| def _note(pad): | |
| if not 1 <= pad <= NUM_PADS: | |
| raise ValueError(f"pad must be 1..{NUM_PADS}, got {pad}") | |
| return FIRST_NOTE + pad - 1 | |
| def pad_on(pad): | |
| _port().send(mido.Message('note_on', channel=CHANNEL, note=_note(pad), velocity=127)) | |
| def pad_off(pad): | |
| _port().send(mido.Message('note_off', channel=CHANNEL, note=_note(pad), velocity=0)) | |
| def all_on(): | |
| p = _port() | |
| for pad in range(1, NUM_PADS + 1): | |
| p.send(mido.Message('note_on', channel=CHANNEL, note=_note(pad), velocity=127)) | |
| def all_off(): | |
| p = _port() | |
| p.send(mido.Message('control_change', channel=CHANNEL, control=123, value=0)) | |
| for pad in range(1, NUM_PADS + 1): | |
| p.send(mido.Message('note_off', channel=CHANNEL, note=_note(pad), velocity=0)) | |
| if __name__ == '__main__': | |
| import time | |
| print("Self-test: sweep, all-on, all-off.") | |
| for pad in range(1, NUM_PADS + 1): | |
| pad_on(pad); time.sleep(0.1); pad_off(pad) | |
| time.sleep(0.3) | |
| all_on(); time.sleep(0.8); all_off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment