Created
April 11, 2025 04:36
-
-
Save chayanforyou/a0d82ef2603f4a8d09e7ad514faccb4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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