- if width > 4000: # 9ms leading mark for all valid data
- width = ticks_diff(self._times[2], self._times[1])
- if width > 3000: # 4.5ms space for normal data
- if self.edge < 68:
- # Haven't received the correct number of edges
- val = BADBLOCK
- else:
- # Time spaces only (marks are always 562.5µs)
- # Space is 1.6875ms (1) or 562.5µs (0)
- # Skip last bit which is always 1
- val = 0
- for edge in range(3, 68 - 2, 2):
- val >>= 1
- if ticks_diff(self._times[edge + 1], self._times[edge]) > 1120:
- val |= 0x80000000
- elif width > 1700: # 2.5ms space for a repeat code. Should have exactly 4 edges.
- val = REPEAT if self.edge == 4 else BADREP
- addr = 0
- if val >= 0: # validate. Byte layout of val ~cmd cmd ~addr addr
- addr = val & 0xff
- cmd = (val >> 16) & 0xff
- if addr == ((val >> 8) ^ 0xff) & 0xff: # 8 bit address OK
- val = cmd if cmd == (val >> 24) ^ 0xff else BADDATA
- self._addr = addr
+ if width < 4000: # 9ms leading mark for all valid data
+ raise RuntimeError(BADSTART)
+ width = ticks_diff(self._times[2], self._times[1])
+ if width > 3000: # 4.5ms space for normal data
+ if self.edge < 68: # Haven't received the correct number of edges
+ raise RuntimeError(BADBLOCK)
+ # Time spaces only (marks are always 562.5µs)
+ # Space is 1.6875ms (1) or 562.5µs (0)
+ # Skip last bit which is always 1
+ val = 0
+ for edge in range(3, 68 - 2, 2):
+ val >>= 1
+ if ticks_diff(self._times[edge + 1], self._times[edge]) > 1120:
+ val |= 0x80000000
+ elif width > 1700: # 2.5ms space for a repeat code. Should have exactly 4 edges.
+ raise RuntimeError(REPEAT if self.edge == 4 else BADREP) # Treat REPEAT as error.