Skip to content

Instantly share code, notes, and snippets.

@chayanforyou
Created April 11, 2025 04:36
Show Gist options
  • Save chayanforyou/a0d82ef2603f4a8d09e7ad514faccb4d to your computer and use it in GitHub Desktop.
Save chayanforyou/a0d82ef2603f4a8d09e7ad514faccb4d to your computer and use it in GitHub Desktop.
def nec_generate(command_hex: str):
HEADER = ["0158", "00B0"] # 9ms + 4.5ms
BIT_0 = ["0017", "0017"]
BIT_1 = ["0017", "0041"]
STOP = ["0017"]
TAIL = ["05A5", "0158", "0056", "0017", "0E5B"]
# Pronto preamble
pronto = ["0000", "006C", "0022", "0002"]
pronto.extend(HEADER)
# Convert input like '0x19' to int
command = int(command_hex, 16)
address = 0x00
inv_address = 0xFF
inv_command = command ^ 0xFF
# Helper to convert a byte to IR bits
def encode_byte(byte_val):
bits = []
for i in range(8):
if (byte_val >> i) & 1:
bits.extend(BIT_1)
else:
bits.extend(BIT_0)
return bits
# Add encoded data
pronto += encode_byte(address)
pronto += encode_byte(inv_address)
pronto += encode_byte(command)
pronto += encode_byte(inv_command)
# Stop bit and tail
pronto += STOP
pronto += TAIL
# Format and return output
formatted = " ".join(pronto)
return formatted
# Example usage:
if __name__ == "__main__":
user_input = input("Enter NEC command (e.g., 0x19): ")
output = nec_generate(user_input)
print("\nGenerated Pronto Hex:")
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment