Skip to content

Instantly share code, notes, and snippets.

@peterjc
Last active December 26, 2015 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterjc/7127551 to your computer and use it in GitHub Desktop.
Save peterjc/7127551 to your computer and use it in GitHub Desktop.
Python scipt to decode Roomba IR codes via the Linux mode2 tool. See http://astrobeano.blogspot.co.uk/2013/10/roomba-620-infrared-signals.html
#!/usr/bin/env python
#Copyright 2013, Peter Cock. All rights reserved.
#Released under the MIT License.
"""Python scipt to decode Roomba IR codes via the Linux mode2 tool.
Tested under Python 2.7 and Python 3.3, example usage:
$ mode2 -d /dev/lirc0 | python decode_roomba_ir.py
10101100 - 172 - 0xAC
10101100 - 172 - 0xAC
10101100 - 172 - 0xAC
10101100 - 172 - 0xAC
^C
or,
$ mode2 -d /dev/lirc0 | python3 decode_roomba_ir.py
10101100 - 172 - 0xAC
10101100 - 172 - 0xAC
10101100 - 172 - 0xAC
10101100 - 172 - 0xAC
^C
http://astrobeano.blogspot.co.uk/2013/10/roomba-620-infrared-signals.html
"""
import sys
def expand(code):
return tuple(min(1, code & (2**(7-i))) for i in range(8))
mapping = dict((expand(code), code) for code in range(256))
assert len(mapping) == 256
code = []
try:
line = next(sys.stdin)
while line.startswith("space "):
line = next(sys.stdin)
while line:
assert line.startswith("pulse ")
on = int(line.split(None, 1)[1])
line = next(sys.stdin)
assert line.startswith("space ")
off = int(line.split(None, 1)[1])
if 700 < on < 1300 and 2700 < off:
code.append(0) # 1ms on, 3ms off
elif 2700 < on < 3300 and 700 < off:
code.append(1) # 3ms on, 1 ms off
else:
#print("? (%i, %i)" % (on, off))
code.append("?")
if 4000 < off:
#Long pause at end of signal
try:
value = mapping[tuple(code)]
print("%s - %i - 0x%X" % ("".join(str(x) for x in code), value, value))
except KeyError:
print("".join(str(x) for x in code))
code = []
line = next(sys.stdin)
except StopIteration:
#Finished
print("The end")
pass
except KeyboardInterrupt:
#Told to quit
print("Stopped")
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment