]> vault307.fbx.one Git - ir_decoder.git/commitdiff
ir decoder master
authorjimmy <jimipunk88@gmail.com>
Fri, 28 Jun 2024 01:18:33 +0000 (20:18 -0500)
committerjimmy <jimipunk88@gmail.com>
Fri, 28 Jun 2024 01:18:33 +0000 (20:18 -0500)
read_code.py [new file with mode: 0644]
validate_code.py [new file with mode: 0644]

diff --git a/read_code.py b/read_code.py
new file mode 100644 (file)
index 0000000..b87cf2a
--- /dev/null
@@ -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 (file)
index 0000000..61650a5
--- /dev/null
@@ -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