From 76ba320a0013d51f44fd4a93d5b56ae9043a1ff0 Mon Sep 17 00:00:00 2001 From: jimmy Date: Thu, 27 Jun 2024 20:18:33 -0500 Subject: [PATCH] ir decoder --- read_code.py | 40 ++++++++++++++++++++++++++++++++++++++++ validate_code.py | 22 ++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 read_code.py create mode 100644 validate_code.py diff --git a/read_code.py b/read_code.py new file mode 100644 index 0000000..b87cf2a --- /dev/null +++ b/read_code.py @@ -0,0 +1,40 @@ +import utime + + +def read_code(pin): + raw = [] + + # Signal is inverted: + # 0 - high signal + # 1 - low signal + + # wait for the leading pulse + while pin.value() == 1: + pass + + # 9ms leading pulse + # 4.5ms space + utime.sleep_us(13500) + + # Sample signal every 562.5µs + # Time sensitive + for i in range(1000): + raw.append(pin.value()) + utime.sleep_us(56) + + code = "" + count = 0 + + for sample in raw: + if sample == 1: + # count low signal + count += 1 + else: + # ignore high signal + if count > 0: + # if low signal is longer than 562.5µs it 1 otherwise 0 + code += "1" if count > 10 else "0" + count = 0 + + # trim message transmission and repeat codes + return code[0:32] \ No newline at end of file diff --git a/validate_code.py b/validate_code.py new file mode 100644 index 0000000..61650a5 --- /dev/null +++ b/validate_code.py @@ -0,0 +1,22 @@ +class InvalidCodeException(Exception): + pass + + +def validate_code(code): + if len(code) < 32: + raise InvalidCodeException + + if len(code) > 32: + raise InvalidCodeException + + # check 8-bit device address + # following 8-bits have to be a logical inverse of the device address + for i in range(0, 8): + if code[i] == code[i + 8]: + raise InvalidCodeException + + # check 8-bit command + # following 8-bits have to be a logical inverse of the command + for i in range(16, 24): + if code[i] == code[i + 8]: + raise InvalidCodeException \ No newline at end of file -- 2.47.3