Light pad bank 2 from page 7, and add factory reset and a pad bank toggle to the editor.
Bank 2's lamp colours come from preset page 7, not page 3, so pad_colours.py writes there and the editor labels it Bank 2. The factory reset loads the image MidiSuite uploads for its own reset, and the bank toggle writes the global-block byte MidiSuite uses to switch banks. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
63b3129cca
commit
3992744a1d
9 files changed
+239
-113
No files matched your search
+82
-103
@@ -8,6 +8,10 @@ bytes in the preset, written over the Private USB port.
|
||||
.venv/bin/python scripts/pad_colours.py --xchc --bank 2
|
||||
.venv/bin/python scripts/pad_colours.py --dry-run
|
||||
.venv/bin/python scripts/pad_colours.py --clear
|
||||
.venv/bin/python scripts/pad_colours.py --identify
|
||||
|
||||
--identify lights every pad on a page in that page's colour, in rainbow
|
||||
order, in all eight presets, so the page on show can be read at a glance.
|
||||
|
||||
The write is checked by reading it back. It is sent to the running copy
|
||||
and to the stored preset, and it leaves notes, channels, and knobs as
|
||||
@@ -20,89 +24,32 @@ from __future__ import annotations
|
||||
import argparse
|
||||
import glob
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT))
|
||||
sys.path.insert(0, str(ROOT / "scripts"))
|
||||
|
||||
from patch_smc_pad import PAD_COLORS # noqa: E402
|
||||
from padeditor import preset # noqa: E402
|
||||
from padeditor.device import PadError, read_image, save_image, transact # noqa: E402
|
||||
from padeditor.device import private_port as _private_port # noqa: E402
|
||||
from padeditor.preset import scale # noqa: E402
|
||||
from padeditor.sysex import ( # noqa: E402
|
||||
RAM,
|
||||
decode,
|
||||
encode,
|
||||
payload_data,
|
||||
read_request,
|
||||
save_request,
|
||||
write_request,
|
||||
)
|
||||
|
||||
# Preset image, memory type 5. Each of these offsets is a note-36 pad,
|
||||
# and the next 15 records are notes 37–51. The running copy is at RAM.
|
||||
RECORD = 26
|
||||
RGB_AT = 4
|
||||
BANKS = (1044, 8122, 11661, 15200, 18739, 22278, 25817)
|
||||
RAM = 0x80000000
|
||||
FULL = 0xF0
|
||||
|
||||
|
||||
def encode(source: bytes) -> bytes:
|
||||
"""Pack a protocol message into the 7-bit SysEx body."""
|
||||
bit_num = 0
|
||||
accum = 0
|
||||
out = bytearray()
|
||||
for byte in source:
|
||||
accum = ((byte << (bit_num & 0x1F)) | accum) & 0xFFFFFFFF
|
||||
bit_num += 1
|
||||
while True:
|
||||
out.append(accum & 0x7F)
|
||||
accum = (accum >> 7) & 0xFFFFFFFF
|
||||
if bit_num < 7:
|
||||
break
|
||||
bit_num -= 7
|
||||
out.append(accum & 0x7F)
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def decode(source: bytes) -> bytes:
|
||||
bit_num = 0
|
||||
last_val = 0
|
||||
out = bytearray()
|
||||
for byte in source:
|
||||
accum = byte & (0xFFFFFFFF >> (32 - bit_num) if bit_num else 0)
|
||||
if bit_num > 0:
|
||||
out.append((last_val & 0x7F) | ((accum << (8 - bit_num)) & 0xFF))
|
||||
last_val = byte >> bit_num
|
||||
bit_num = (bit_num + 1) % 8
|
||||
if last_val > 0:
|
||||
out.append(last_val)
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def checksum(payload: bytes) -> int:
|
||||
total = 0
|
||||
for byte in payload:
|
||||
total = (total + byte) & 0xFF
|
||||
return (~total) & 0xFF
|
||||
|
||||
|
||||
def frame(command: int, payload: bytes) -> list[int]:
|
||||
data = bytearray((0x00, 0x59, command))
|
||||
data += len(payload).to_bytes(3, "little")
|
||||
data += payload
|
||||
data.append(checksum(payload))
|
||||
return [0xF0, *encode(bytes(data)), 0xF7]
|
||||
|
||||
|
||||
def read_request(address: int, size: int) -> list[int]:
|
||||
payload = bytes([5]) + address.to_bytes(4, "little") + size.to_bytes(3, "little")
|
||||
return frame(0x23, payload)
|
||||
|
||||
|
||||
def write_request(address: int, data: bytes) -> list[int]:
|
||||
payload = bytes([5]) + address.to_bytes(4, "little") + len(data).to_bytes(3, "little") + data
|
||||
return frame(0x22, payload)
|
||||
|
||||
|
||||
def save_request() -> list[int]:
|
||||
"""Commit the running preset. MidiSuite's Save is this empty write."""
|
||||
return write_request(0, b"")
|
||||
|
||||
|
||||
def scale(value: int) -> int:
|
||||
"""Map 0–255 onto the pad's 0–240 colour range."""
|
||||
return (value * FULL + 127) // 255
|
||||
|
||||
|
||||
def led_rgb(r: int, g: int, b: int, w: int) -> tuple[int, int, int]:
|
||||
@@ -124,6 +71,31 @@ XCHC_COLOURS = (
|
||||
)
|
||||
|
||||
|
||||
# One colour per pad page, page 1 first. Rainbow order, so the page
|
||||
# number can be counted from red.
|
||||
PAGE_COLOURS = (
|
||||
("Red", 255, 0, 0),
|
||||
("Orange", 255, 100, 0),
|
||||
("Yellow", 255, 255, 0),
|
||||
("Green", 0, 255, 0),
|
||||
("Cyan", 0, 255, 255),
|
||||
("Blue", 0, 0, 255),
|
||||
("Violet", 160, 0, 255),
|
||||
("White", 255, 255, 255),
|
||||
)
|
||||
|
||||
|
||||
def identify_image(image: bytes) -> bytes:
|
||||
"""The image with each pad page lit in its PAGE_COLOURS colour."""
|
||||
out = bytearray(image)
|
||||
for number in range(preset.PRESET_COUNT):
|
||||
for index in range(preset.PAD_COUNT):
|
||||
_name, red, green, blue = PAGE_COLOURS[index // preset.PAGE_LEN]
|
||||
at = preset.colour_offset(number, index)
|
||||
out[at:at + 3] = bytes((scale(red), scale(green), scale(blue)))
|
||||
return bytes(out)
|
||||
|
||||
|
||||
def scene_colours(clear: bool = False) -> list[tuple[str, bytes]]:
|
||||
colours = []
|
||||
for name, r, g, b, w in PAD_COLORS:
|
||||
@@ -145,11 +117,17 @@ def xchc_colours(clear: bool = False) -> list[tuple[str, bytes]]:
|
||||
return colours
|
||||
|
||||
|
||||
# Preset page whose records light each pad bank. Page 3 also sends notes
|
||||
# 52–67 but its colours are not shown; bank 2 takes them from page 7.
|
||||
BANK_PAGE = {1: 2, 2: 7}
|
||||
|
||||
|
||||
def bank_bases(bank: int) -> tuple[int, ...]:
|
||||
"""Preset addresses where this pad bank's note records start."""
|
||||
if bank < 1:
|
||||
raise ValueError(f"pad bank {bank} is not on this controller")
|
||||
skip = (bank - 1) * 16 * RECORD
|
||||
page = BANK_PAGE.get(bank, bank + 1)
|
||||
skip = (page - 2) * 16 * RECORD
|
||||
return tuple(base + skip for base in BANKS)
|
||||
|
||||
|
||||
@@ -178,33 +156,18 @@ def payload_data(decoded: bytes) -> bytes:
|
||||
|
||||
|
||||
def private_port(names: list[str]) -> str:
|
||||
for name in names:
|
||||
if "Private" in name:
|
||||
return name
|
||||
raise SystemExit(
|
||||
"SMC-PAD Private port not found. Ports:\n " + "\n ".join(names or ["(none)"])
|
||||
)
|
||||
try:
|
||||
return _private_port(names)
|
||||
except PadError as exc:
|
||||
raise SystemExit(str(exc)) from exc
|
||||
|
||||
|
||||
def _transact(midiout, midiin, message: list[int], timeout: float = 0.8) -> bytes:
|
||||
while True:
|
||||
item = midiin.get_message()
|
||||
if not item or not item[0]:
|
||||
break
|
||||
midiout.send_message(message)
|
||||
deadline = time.time() + timeout
|
||||
while time.time() < deadline:
|
||||
item = midiin.get_message()
|
||||
if not item or not item[0]:
|
||||
time.sleep(0.004)
|
||||
continue
|
||||
raw = bytes(item[0])
|
||||
if raw[:1] == b"\xf0" and raw[-1:] == b"\xf7":
|
||||
return decode(raw[1:-1])
|
||||
raise TimeoutError("the pad did not answer")
|
||||
|
||||
|
||||
def apply(clear: bool, bank: int = 1, colours: list[tuple[str, bytes]] | None = None) -> None:
|
||||
def apply(
|
||||
clear: bool,
|
||||
bank: int = 1,
|
||||
colours: list[tuple[str, bytes]] | None = None,
|
||||
any_note: bool = False,
|
||||
) -> None:
|
||||
for path in glob.glob(str(ROOT / ".venv" / "lib" / "python*" / "site-packages")):
|
||||
if path not in sys.path:
|
||||
sys.path.insert(0, path)
|
||||
@@ -221,9 +184,9 @@ def apply(clear: bool, bank: int = 1, colours: list[tuple[str, bytes]] | None =
|
||||
try:
|
||||
note = 36 + (bank - 1) * 16
|
||||
for base in bank_bases(bank):
|
||||
reply = _transact(midiout, midiin, read_request(base, 2))
|
||||
reply = transact(midiout, midiin, read_request(base, 2))
|
||||
mark = payload_data(reply)
|
||||
if mark[:2] != bytes((0x09, note)):
|
||||
if mark[0] != 0x09 or (not any_note and mark[1] != note):
|
||||
raise SystemExit(
|
||||
f"preset at {base} is {mark.hex()}, not the note-{note} pads; nothing was written"
|
||||
)
|
||||
@@ -231,16 +194,16 @@ def apply(clear: bool, bank: int = 1, colours: list[tuple[str, bytes]] | None =
|
||||
shown = set()
|
||||
for name, address, rgb in colour_writes(clear, bank, colours):
|
||||
for base in bases:
|
||||
_transact(midiout, midiin, write_request(base + address, rgb))
|
||||
transact(midiout, midiin, write_request(base + address, rgb))
|
||||
if name in shown:
|
||||
continue
|
||||
check = payload_data(_transact(midiout, midiin, read_request(address, 3)))
|
||||
check = payload_data(transact(midiout, midiin, read_request(address, 3)))
|
||||
if check != rgb:
|
||||
raise SystemExit(f"{name or 'pad'} read back {check.hex()}, expected {rgb.hex()}")
|
||||
shown.add(name)
|
||||
if name:
|
||||
print(f" {name:<8} {address:#06x} {rgb.hex()}", flush=True)
|
||||
reply = _transact(midiout, midiin, save_request())
|
||||
reply = transact(midiout, midiin, save_request())
|
||||
if reply[:3] != b"\x00\x59\x00" or reply[6:7] != b"\x00":
|
||||
raise SystemExit(f"the pad did not accept the save ({reply.hex()})")
|
||||
print("Saved on the pad.", flush=True)
|
||||
@@ -252,10 +215,26 @@ def apply(clear: bool, bank: int = 1, colours: list[tuple[str, bytes]] | None =
|
||||
def main(argv: list[str] | None = None) -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||
parser.add_argument("--bank", type=int, default=1, help="pad bank to write (2 is notes 52–67)")
|
||||
parser.add_argument("--page", type=int, help="write preset page 0–7 instead (page 2 is bank 1, 3 is bank 2; 7 repeats 52–67)")
|
||||
parser.add_argument("--xchc", action="store_true", help="use the solid colour scenes from shows/xchc.qxw")
|
||||
parser.add_argument("--clear", action="store_true", help="store black on every scene pad")
|
||||
parser.add_argument("--identify", action="store_true", help="light each pad page in its own colour")
|
||||
parser.add_argument("--dry-run", action="store_true", help="print the colours and do not open a port")
|
||||
args = parser.parse_args(argv)
|
||||
if args.page is not None:
|
||||
if not 2 <= args.page < preset.PAGE_COUNT:
|
||||
parser.error("--page must be 2–7")
|
||||
args.bank = args.page - 1
|
||||
if args.identify:
|
||||
for page, (name, red, green, blue) in enumerate(PAGE_COLOURS, 1):
|
||||
print(f"page {page} {name:<7} {bytes((scale(red), scale(green), scale(blue))).hex()}")
|
||||
if not args.dry_run:
|
||||
try:
|
||||
written = save_image(identify_image(read_image()))
|
||||
except PadError as exc:
|
||||
raise SystemExit(str(exc)) from exc
|
||||
print(f"Saved on the pad ({written} bytes changed).")
|
||||
return 0
|
||||
palette = xchc_colours(args.clear) if args.xchc else None
|
||||
if args.dry_run:
|
||||
seen = set()
|
||||
@@ -265,7 +244,7 @@ def main(argv: list[str] | None = None) -> int:
|
||||
seen.add(name)
|
||||
print(f"{name:<8} {address:#06x} {rgb.hex()}")
|
||||
return 0
|
||||
apply(args.clear, args.bank, palette)
|
||||
apply(args.clear, args.bank, palette, any_note=args.page is not None)
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user