import re

with open('copy_screenlog_new.txt', 'r', errors='ignore') as f:
    lines = f.readlines()

with open('firmware.bin', 'wb') as out:
    for line in lines:
        if len(line) >= 8:
            prefix = line[:8]
            if re.match(r'^[0-9a-fA-F]{8}$', prefix):
                # Extract only the hex bytes between address and ASCII
                hex_part = line[10:58].strip()
                hex_bytes = hex_part.split()
                out.write(bytes(int(b, 16) for b in hex_bytes if re.match(r'^[0-9a-fA-F]{2}$', b)))

print("Done. firmware.bin created.")
