Skip to content

Instantly share code, notes, and snippets.

@bkbilly
Created June 28, 2024 13:43
Show Gist options
  • Save bkbilly/f200d1d78091ba527d3880c10118aac0 to your computer and use it in GitHub Desktop.
Save bkbilly/f200d1d78091ba527d3880c10118aac0 to your computer and use it in GitHub Desktop.
""" Decode IR signals from remote controls """
def decode_nec(ir_signal):
protocol_leading = 9000
protocol_leading_space = 4500
protocol_pulse = 562.5
protocol_pulse_high = 1687.5
width = 200
leading_pulse = ir_signal.pop(0)
if not protocol_leading - width < leading_pulse < protocol_leading + width:
print(f"leading_pulse: {leading_pulse}")
return None
leading_space = ir_signal.pop(0)
if not protocol_leading_space - width < leading_space < protocol_leading_space + width:
print(f"leading_space: {leading_space}")
return None
mybin = []
for num, asd in enumerate(ir_signal, 1):
if protocol_pulse - width < asd < protocol_pulse + width:
mybin.append("0")
elif protocol_pulse_high - width < asd < protocol_pulse_high + width:
mybin.append("1")
else:
print(f"{num}/{len(ir_signal)}: {asd}")
# return None
return "".join(mybin)
def decode_rc(ir_signal):
protocol_pulse = 889
width = 500
pop_items = 0
for signal in ir_signal:
if 4500 - width < signal < 4500 + width:
pop_items += 1
else:
break
for pop in range(pop_items):
ir_signal.pop(0)
tmp_signal = [0]
for pulse, space in zip(ir_signal[0::2], ir_signal[1::2]):
if protocol_pulse - width < pulse < protocol_pulse + width:
tmp_signal.append(1)
elif protocol_pulse * 2 - width < pulse < protocol_pulse * 2 + width:
tmp_signal.append(1)
tmp_signal.append(1)
else:
print(f"pulse: {pulse}")
return None
if protocol_pulse - width < space < protocol_pulse + width:
tmp_signal.append(0)
elif protocol_pulse * 2 - width < space < protocol_pulse * 2 + width:
tmp_signal.append(0)
tmp_signal.append(0)
else:
print(f"space: {space}")
break
mybin = []
for first, second in zip(tmp_signal[0::2], tmp_signal[1::2]):
if first == 0 and second == 1:
mybin.append("1")
elif first == 1 and second == 0:
mybin.append("0")
else:
print(f"can't decode: {first}, {second}")
# return None
mybin.append("2")
return "".join(mybin)
# 1100000101000
nec_signal = [8952, 4305, 1504, 1504, 718, 400, 718, 400, 718, 1504, 718, 1504, 718, 400, 718]
rc_signal = [900, 874, 1788, 874, 900, 874, 900, 874, 900, 874, 900, 1762, 1788, 1762, 1788, 874, 900, 874, 900, 874, 900]
print(decode_nec(nec_signal.copy()))
print(decode_rc(rc_signal.copy()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment