Optimize
This commit is contained in:
+13
-17
@@ -10,7 +10,7 @@ Usage:
|
||||
python converter/convert_all.py
|
||||
(or via Makefile: make shows)
|
||||
|
||||
Show 0 is always 'blue_pulse' — the home/reset show.
|
||||
Show 0 is always 'blue_breath' — the home/reset show.
|
||||
All other shows are sorted alphabetically and follow after it.
|
||||
|
||||
SPDX-License-Identifier: BSD-2-Clause
|
||||
@@ -36,16 +36,6 @@ STEP_PATTERN = re.compile(r'^\s*#([0-9A-Fa-f]{6})\s*,\s*(\d+)')
|
||||
|
||||
# ---- Helpers -----------------------------------------------------------
|
||||
|
||||
def parse_mode(filepath: Path) -> str:
|
||||
"""Return the C mode constant for this show. Defaults to SHOW_LOOP."""
|
||||
with open(filepath) as f:
|
||||
for line in f:
|
||||
m = re.match(r'^\s*//\s*mode:\s*(\w+)', line, re.IGNORECASE)
|
||||
if m:
|
||||
return "SHOW_SINGLE" if m.group(1).lower() == "single" else "SHOW_LOOP"
|
||||
return "SHOW_LOOP"
|
||||
|
||||
|
||||
def filename_to_symbol(stem: str) -> str:
|
||||
"""Convert a filename stem to an uppercase C array symbol. e.g. 'example_fade' → 'SHOW_EXAMPLE_FADE'"""
|
||||
clean = re.sub(r'[^a-zA-Z0-9]', '_', stem)
|
||||
@@ -56,14 +46,21 @@ def hex_to_rgb(hex_str: str) -> tuple[int, int, int]:
|
||||
return int(hex_str[0:2], 16), int(hex_str[2:4], 16), int(hex_str[4:6], 16)
|
||||
|
||||
|
||||
def parse_show(filepath: Path) -> list[tuple[int, int, int, int]]:
|
||||
def parse_show_file(filepath: Path) -> tuple[list[tuple[int, int, int, int]], str]:
|
||||
"""
|
||||
Parse a .txt show file. Returns list of (r, g, b, duration_ms) tuples.
|
||||
Raises ValueError on malformed lines.
|
||||
Parse a .txt show file in one pass. Returns (steps, mode_constant).
|
||||
steps: list of (r, g, b, duration_ms) tuples
|
||||
mode_constant: 'SHOW_LOOP' or 'SHOW_SINGLE' (default SHOW_LOOP if not set)
|
||||
Raises ValueError on malformed lines or empty files.
|
||||
"""
|
||||
steps = []
|
||||
mode = "SHOW_LOOP"
|
||||
with open(filepath) as f:
|
||||
for lineno, raw_line in enumerate(f, 1):
|
||||
m = re.match(r'^\s*//\s*mode:\s*(\w+)', raw_line, re.IGNORECASE)
|
||||
if m:
|
||||
mode = "SHOW_SINGLE" if m.group(1).lower() == "single" else "SHOW_LOOP"
|
||||
continue
|
||||
line = raw_line.split("//")[0].strip()
|
||||
if not line:
|
||||
continue
|
||||
@@ -78,7 +75,7 @@ def parse_show(filepath: Path) -> list[tuple[int, int, int, int]]:
|
||||
steps.append((r, g, b, duration))
|
||||
if not steps:
|
||||
raise ValueError(f"{filepath.name}: file contains no steps.")
|
||||
return steps
|
||||
return steps, mode
|
||||
|
||||
|
||||
def render_show_header(steps: list, source_name: str, symbol: str) -> str:
|
||||
@@ -166,9 +163,8 @@ def main() -> None:
|
||||
for txt_path in ordered_files:
|
||||
stem = txt_path.stem
|
||||
symbol = filename_to_symbol(stem)
|
||||
mode = parse_mode(txt_path)
|
||||
try:
|
||||
steps = parse_show(txt_path)
|
||||
steps, mode = parse_show_file(txt_path)
|
||||
header = render_show_header(steps, txt_path.name, symbol)
|
||||
out = SKETCH_DIR / f"show_{stem}.h"
|
||||
out.write_text(header, encoding="utf-8")
|
||||
|
||||
Reference in New Issue
Block a user