Skip to content

Instantly share code, notes, and snippets.

@bkbilly
Created June 6, 2018 16:43
Show Gist options
  • Select an option

  • Save bkbilly/7953ee8c8a88e7e159fdfc8dcca99531 to your computer and use it in GitHub Desktop.

Select an option

Save bkbilly/7953ee8c8a88e7e159fdfc8dcca99531 to your computer and use it in GitHub Desktop.
Convert an IR signal to binary
#! /usr/bin/python3
import time
import subprocess
import re
START = 4000
DIFF = 500
name = input("Enter a name: ")
cmd = "mode2 -d /dev/lirc0"
p = subprocess.Popen("exec " + cmd, stdout=subprocess.PIPE, shell=True)
input("Press enter to stop")
p.kill()
lirc_out = p.stdout.readlines()
lirc_out = lirc_out[1:]
pulse_space = []
it = iter(lirc_out)
for pulse, space in zip(it, it):
pulse = int(re.findall(r"\d+", str(pulse))[0])
space = int(re.findall(r"\d+", str(space))[0])
pulse_space.append((pulse, space))
if (len(lirc_out) % 2) > 0:
pulse = lirc_out[-1]
pulse = int(re.findall(r"\d+", str(pulse))[0])
pulse_space.append((pulse, pulse+(DIFF * 2)))
binresult = ''
for pulse, space in pulse_space:
print(pulse, space)
if pulse > START and space > START:
binresult += ' 2'
elif abs(space - pulse) >= DIFF:
binresult += '1'
elif abs(space - pulse) < DIFF:
binresult += '0'
else:
binresult += 'W'
binresult = set(binresult.strip().split(' '))
for binary in binresult:
print('%s = %s' % (name, binary))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment