Last active
April 23, 2017 09:24
-
-
Save abihf/85fc40c4c7340123dd812354c6f7cae6 to your computer and use it in GitHub Desktop.
Script to decode remote tv code
This file contains 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
#!/usr/bin/env python | |
# How to use: | |
# ensure lircd is stoped then run | |
# $ mode2 | python nec-decode.py | |
# | |
# Example output: | |
# ----------------- | |
# 00100000 11011111 - 0xfb04 | |
# 00100010 11011101 - 0xbb44 | |
# ----------------- | |
# 00100000 11011111 - 0xfb04 | |
# 01100000 10011111 - 0xf906 | |
# ----------------- | |
# 00100000 11011111 - 0xfb04 | |
# 11100000 00011111 - 0xf807 | |
import fileinput | |
import sys | |
def main(): | |
i = 0 | |
byte = 0 | |
mode = 'ready' | |
for line in fileinput.input(): | |
tokens = line.split() | |
if len(tokens) == 2: | |
t = tokens[0] | |
n = normalize(int(tokens[1])) | |
if t == 'pulse' and n == 9000: | |
mode = 'init' | |
elif mode == 'init' and t == 'space' and n == 4500: | |
i = 0 | |
byte = 0 | |
mode = 'active' | |
print('-----------------') | |
elif mode == 'active': | |
if t == 'space' and n == 0: | |
mode = 'ready' | |
if t == 'space' and n <= 1600: | |
bit = 1 if n == 1600 else 0 | |
sys.stdout.write(str(bit)) | |
byte = byte | (bit << i) | |
i += 1 | |
if i == 8: | |
sys.stdout.write(' ') | |
if i == 16: | |
sys.stdout.write(' - 0x%02x\n' % byte) | |
i = 0 | |
byte = 0 | |
else: | |
mode = 'ready' | |
else: | |
print('unknown input') | |
def normalize(n): | |
if n < 800: | |
return 500 | |
elif n < 1800: | |
return 1600 | |
elif n < 4700: | |
return 4500 | |
elif n < 9200: | |
return 9000 | |
else: | |
return 0 | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment