forked from TillRepo/IrSender
bugfix
This commit is contained in:
+26
-18
@@ -3,13 +3,12 @@
|
||||
IR Capture Script
|
||||
- Führt mode2 --device /dev/lirc1 aus
|
||||
- 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
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
from pathlib import Path
|
||||
@@ -17,7 +16,7 @@ from pathlib import Path
|
||||
# === Anpassbare Pfade ===
|
||||
DEVICE = "/dev/lirc1"
|
||||
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"
|
||||
CARRIER = 38000
|
||||
REPEAT = 1
|
||||
@@ -51,22 +50,18 @@ def capture_mode2():
|
||||
if not line:
|
||||
continue
|
||||
|
||||
# mode2 Ausgabe: "pulse 1234", "space 5678" oder "timeout 21585"
|
||||
m = re.match(r"^(pulse|space|timeout)\s+(\d+)$", line, re.IGNORECASE)
|
||||
if not m:
|
||||
# z.B. "Using device: ..." ignorieren
|
||||
continue
|
||||
|
||||
kind, duration = m.group(1).lower(), int(m.group(2))
|
||||
|
||||
if kind == "timeout":
|
||||
# timeout → abschließender space (wie in deinem Original-Beispiel)
|
||||
signals.append({"type": "space", "duration": duration})
|
||||
print(f" space {duration} (timeout → Signalende)")
|
||||
print("\nTimeout erkannt – Signal aufgenommen.")
|
||||
break
|
||||
|
||||
# pulse → mark, space → space
|
||||
typ = "mark" if kind == "pulse" else "space"
|
||||
signals.append({"type": typ, "duration": duration})
|
||||
print(f" {typ:5} {duration}")
|
||||
@@ -83,14 +78,30 @@ def capture_mode2():
|
||||
return signals
|
||||
|
||||
def save_json(signals, filename: Path):
|
||||
"""Speichert das Signal im gewünschten Format."""
|
||||
data = {
|
||||
"carrier": CARRIER,
|
||||
"repeat": REPEAT,
|
||||
"signal": signals
|
||||
}
|
||||
"""
|
||||
Speichert das Signal im gewünschten Format:
|
||||
- Jedes { "type": "...", "duration": N } steht in EINER Zeile
|
||||
- Kein Zeilenumbruch zwischen type und duration
|
||||
"""
|
||||
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:
|
||||
json.dump(data, f, indent=2)
|
||||
f.write(content)
|
||||
|
||||
print(f"\nJSON gespeichert: {filename}")
|
||||
|
||||
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():
|
||||
print("=== IR Capture → JSON + Config ===\n")
|
||||
|
||||
# 1. Signal aufnehmen
|
||||
signals = capture_mode2()
|
||||
|
||||
if not signals:
|
||||
@@ -142,7 +152,6 @@ def main():
|
||||
|
||||
print(f"\n{len(signals)} Einträge aufgenommen.")
|
||||
|
||||
# 2. Dateiname vom Benutzer erfragen
|
||||
while True:
|
||||
name = input("Dateiname (ohne .json, z.B. hektagonYellow): ").strip()
|
||||
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"
|
||||
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()
|
||||
if add in ("j", "y", "ja", "yes"):
|
||||
cmd_name = input(f"Command-Name (Standard: {name}): ").strip() or name
|
||||
@@ -162,4 +170,4 @@ def main():
|
||||
print("\nFertig!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
Reference in New Issue
Block a user