This commit is contained in:
2026-08-23 18:22:18 +00:00
parent 0e8847a134
commit a433fb2dfb
+26 -18
View File
@@ -3,13 +3,12 @@
IR Capture Script IR Capture Script
- Führt mode2 --device /dev/lirc1 aus - Führt mode2 --device /dev/lirc1 aus
- Wandelt pulse/space/timeout in mark/space JSON um - Wandelt pulse/space/timeout in mark/space JSON um
- Speichert die JSON-Datei - Speichert die JSON-Datei im gewünschten Format
- Fügt optional einen neuen Command in die Config ein - Fügt optional einen neuen Command in die Config ein
""" """
import subprocess import subprocess
import json import json
import os
import sys import sys
import re import re
from pathlib import Path from pathlib import Path
@@ -17,7 +16,7 @@ from pathlib import Path
# === Anpassbare Pfade === # === Anpassbare Pfade ===
DEVICE = "/dev/lirc1" DEVICE = "/dev/lirc1"
IR_SENDER_DIR = Path("/home/till/IrSender2") IR_SENDER_DIR = Path("/home/till/IrSender2")
CONFIG_FILE = IR_SENDER_DIR / "commands.json" # ggf. anpassen CONFIG_FILE = IR_SENDER_DIR / "config.json" # ggf. anpassen
IR_SEND_SCRIPT = IR_SENDER_DIR / "ir_send.py" IR_SEND_SCRIPT = IR_SENDER_DIR / "ir_send.py"
CARRIER = 38000 CARRIER = 38000
REPEAT = 1 REPEAT = 1
@@ -51,22 +50,18 @@ def capture_mode2():
if not line: if not line:
continue continue
# mode2 Ausgabe: "pulse 1234", "space 5678" oder "timeout 21585"
m = re.match(r"^(pulse|space|timeout)\s+(\d+)$", line, re.IGNORECASE) m = re.match(r"^(pulse|space|timeout)\s+(\d+)$", line, re.IGNORECASE)
if not m: if not m:
# z.B. "Using device: ..." ignorieren
continue continue
kind, duration = m.group(1).lower(), int(m.group(2)) kind, duration = m.group(1).lower(), int(m.group(2))
if kind == "timeout": if kind == "timeout":
# timeout → abschließender space (wie in deinem Original-Beispiel)
signals.append({"type": "space", "duration": duration}) signals.append({"type": "space", "duration": duration})
print(f" space {duration} (timeout → Signalende)") print(f" space {duration} (timeout → Signalende)")
print("\nTimeout erkannt Signal aufgenommen.") print("\nTimeout erkannt Signal aufgenommen.")
break break
# pulse → mark, space → space
typ = "mark" if kind == "pulse" else "space" typ = "mark" if kind == "pulse" else "space"
signals.append({"type": typ, "duration": duration}) signals.append({"type": typ, "duration": duration})
print(f" {typ:5} {duration}") print(f" {typ:5} {duration}")
@@ -83,14 +78,30 @@ def capture_mode2():
return signals return signals
def save_json(signals, filename: Path): def save_json(signals, filename: Path):
"""Speichert das Signal im gewünschten Format.""" """
data = { Speichert das Signal im gewünschten Format:
"carrier": CARRIER, - Jedes { "type": "...", "duration": N } steht in EINER Zeile
"repeat": REPEAT, - Kein Zeilenumbruch zwischen type und duration
"signal": signals """
} lines = []
lines.append('{')
lines.append(f' "carrier": {CARRIER},')
lines.append(f' "repeat": {REPEAT},')
lines.append(' "signal": [')
for i, s in enumerate(signals):
comma = "," if i < len(signals) - 1 else ""
# Wichtig: alles in einer Zeile
lines.append(f' {{ "type": "{s["type"]}", "duration": {s["duration"]} }}{comma}')
lines.append(' ]')
lines.append('}')
content = "\n".join(lines) + "\n"
with open(filename, "w", encoding="utf-8") as f: with open(filename, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2) f.write(content)
print(f"\nJSON gespeichert: {filename}") print(f"\nJSON gespeichert: {filename}")
def add_command_to_config(command_name: str, json_filename: str, web_title: str): def add_command_to_config(command_name: str, json_filename: str, web_title: str):
@@ -133,7 +144,6 @@ def add_command_to_config(command_name: str, json_filename: str, web_title: str)
def main(): def main():
print("=== IR Capture → JSON + Config ===\n") print("=== IR Capture → JSON + Config ===\n")
# 1. Signal aufnehmen
signals = capture_mode2() signals = capture_mode2()
if not signals: if not signals:
@@ -142,7 +152,6 @@ def main():
print(f"\n{len(signals)} Einträge aufgenommen.") print(f"\n{len(signals)} Einträge aufgenommen.")
# 2. Dateiname vom Benutzer erfragen
while True: while True:
name = input("Dateiname (ohne .json, z.B. hektagonYellow): ").strip() name = input("Dateiname (ohne .json, z.B. hektagonYellow): ").strip()
if name and re.match(r"^[a-zA-Z0-9_\-]+$", name): if name and re.match(r"^[a-zA-Z0-9_\-]+$", name):
@@ -152,7 +161,6 @@ def main():
json_path = IR_SENDER_DIR / f"{name}.json" json_path = IR_SENDER_DIR / f"{name}.json"
save_json(signals, json_path) save_json(signals, json_path)
# 3. Optional Command in Config eintragen
add = input("\nSoll ein neuer Command in die Config eingetragen werden? [j/N]: ").strip().lower() add = input("\nSoll ein neuer Command in die Config eingetragen werden? [j/N]: ").strip().lower()
if add in ("j", "y", "ja", "yes"): if add in ("j", "y", "ja", "yes"):
cmd_name = input(f"Command-Name (Standard: {name}): ").strip() or name cmd_name = input(f"Command-Name (Standard: {name}): ").strip() or name
@@ -162,4 +170,4 @@ def main():
print("\nFertig!") print("\nFertig!")
if __name__ == "__main__": if __name__ == "__main__":
main() main()