Hatkat

nec - IR_REMOTE

Dec 29th, 2023
957
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.82 KB | None | 0 0
  1. # Author: Jonathan Martin OrdoΓ±ez
  2. # Guardar en la carpeta ir_rx como nec.py
  3.  
  4. from utime import ticks_us, ticks_diff
  5. from ir_rx import IR_RX
  6.  
  7. class NEC_ABC(IR_RX):
  8.     def __init__(self, pin, extended, samsung, callback, *args):
  9.         # Block lasts <= 80ms (extended mode) and has 68 edges
  10.         super().__init__(pin, 68, 80, callback, *args)
  11.         self._extended = extended
  12.         self._addr = 0
  13.         self._leader = 2500 if samsung else 4000  # 4.5ms for Samsung else 9ms
  14.  
  15.     def decode(self, _):
  16.         try:
  17.             if self.edge > 68:
  18.                 raise RuntimeError(self.OVERRUN)
  19.             width = ticks_diff(self._times[1], self._times[0])
  20.             if width < self._leader:  # 9ms leading mark for all valid data
  21.                 raise RuntimeError(self.BADSTART)
  22.             width = ticks_diff(self._times[2], self._times[1])
  23.             if width > 3000:  # 4.5ms space for normal data
  24.                 if self.edge < 68:  # Haven't received the correct number of edges
  25.                     raise RuntimeError(self.BADBLOCK)
  26.                 # Time spaces only (marks are always 562.5Β΅s)
  27.                 # Space is 1.6875ms (1) or 562.5Β΅s (0)
  28.                 # Skip last bit which is always 1
  29.                 val = 0
  30.                 for edge in range(3, 68 - 2, 2):
  31.                     val >>= 1
  32.                     if ticks_diff(self._times[edge + 1], self._times[edge]) > 1120:
  33.                         val |= 0x80000000
  34.             elif width > 1700: # 2.5ms space for a repeat code. Should have exactly 4 edges.
  35.                 raise RuntimeError(self.REPEAT if self.edge == 4 else self.BADREP)  # Treat REPEAT as error.
  36.             else:
  37.                 raise RuntimeError(self.BADSTART)
  38.             addr = val & 0xff  # 8 bit addr
  39.             cmd = (val >> 16) & 0xff
  40.             if cmd != (val >> 24) ^ 0xff:
  41.                 raise RuntimeError(self.BADDATA)
  42.             if addr != ((val >> 8) ^ 0xff) & 0xff:  # 8 bit addr doesn't match check
  43.                 if not self._extended:
  44.                     raise RuntimeError(self.BADADDR)
  45.                 addr |= val & 0xff00  # pass assumed 16 bit address to callback
  46.             self._addr = addr
  47.         except RuntimeError as e:
  48.             cmd = e.args[0]
  49.             addr = self._addr if cmd == self.REPEAT else 0  # REPEAT uses last address
  50.         # Set up for new data burst and run user callback
  51.         self.do_callback(cmd, addr, 0, self.REPEAT)
  52.  
  53. class NEC_8(NEC_ABC):
  54.     def __init__(self, pin, callback, *args):
  55.         super().__init__(pin, False, False, callback, *args)
  56.  
  57. class NEC_16(NEC_ABC):
  58.     def __init__(self, pin, callback, *args):
  59.         super().__init__(pin, True, False, callback, *args)
  60.  
  61. class SAMSUNG(NEC_ABC):
  62.     def __init__(self, pin, callback, *args):
  63.         super().__init__(pin, True, True, callback, *args)
  64.  
  65.  
Advertisement
Add Comment
Please, Sign In to add comment